[layer] Verify positional encoding layer with fp16
authorskykongkong8 <ss.kong@samsung.com>
Fri, 18 Aug 2023 05:37:34 +0000 (14:37 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Tue, 22 Aug 2023 08:03:04 +0000 (17:03 +0900)
- added tensor_type getting code into layer
- added test case in positional encoding layer unittest

**Self evaluation:**
1. Build test:     [X]Passed [ ]Failed [ ]Skipped
2. Run test:     [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: skykongkong8 <ss.kong@samsung.com>
nntrainer/layers/positional_encoding_layer.cpp
test/unittest/layers/unittest_layers_positional_encoding.cpp

index a5db15d..6295bba 100644 (file)
@@ -27,8 +27,7 @@ enum PositionalEncodingParams {
 };
 
 PositionalEncodingLayer::PositionalEncodingLayer() :
-  isPEcalculated(false),
-  positional_encoding_props(props::MaxTimestep()) {
+  isPEcalculated(false), positional_encoding_props(props::MaxTimestep()) {
   weight_idx.fill(std::numeric_limits<unsigned>::max());
 }
 
@@ -43,7 +42,9 @@ void PositionalEncodingLayer::finalize(InitLayerContext &context) {
 
   unsigned int model_dim = input_dims[SINGLE_INOUT_IDX].width();
 
-  ml::train::TensorDim pe_dim({max_token_size, model_dim});
+  ml::train::TensorDim pe_dim(
+    {max_token_size, model_dim},
+    {context.getFormat(), context.getWeightDataType()});
   weight_idx[PositionalEncodingParams::positional_encoding] =
     context.requestTensor(pe_dim, "positional_encoding",
                           nntrainer::Tensor::Initializer::NONE, false,
@@ -59,7 +60,8 @@ void PositionalEncodingLayer::forwarding(RunLayerContext &context,
     weight_idx[PositionalEncodingParams::positional_encoding]);
 
   TensorDim input_dim = input.getDim();
-  TensorDim pe_partial_dim({input_dim.height(), input_dim.width()});
+  TensorDim pe_partial_dim({input_dim.height(), input_dim.width()},
+                           context.getTensor(0).getTensorType());
   nntrainer::Tensor pe_partial = pe.getSharedDataTensor(pe_partial_dim, 0);
 
   if (!isPEcalculated) {
index 95c7660..039b0c3 100644 (file)
@@ -38,3 +38,18 @@ auto positional_encoding = LayerGoldenTestParamType(
 INSTANTIATE_TEST_CASE_P(PositionalEncoding, LayerGoldenTest,
                         ::testing::Values(positional_encoding_partial,
                                           positional_encoding));
+
+auto positional_encoding_partial_fp16fp16 = LayerGoldenTestParamType(
+  nntrainer::createLayer<nntrainer::PositionalEncodingLayer>,
+  {"max_timestep=10"}, "3:1:7:6",
+  "positional_encoding_partial_fp16fp16.nnlayergolden",
+  LayerGoldenTestParamOptions::DEFAULT, "nchw", "fp16", "fp16");
+
+auto positional_encoding_fp16fp16 = LayerGoldenTestParamType(
+  nntrainer::createLayer<nntrainer::PositionalEncodingLayer>,
+  {"max_timestep=10"}, "3:1:10:6", "positional_encoding_fp16fp16.nnlayergolden",
+  LayerGoldenTestParamOptions::DEFAULT, "nchw", "fp16", "fp16");
+
+INSTANTIATE_TEST_CASE_P(PositionalEncoding16, LayerGoldenTest,
+                        ::testing::Values(positional_encoding_partial_fp16fp16,
+                                          positional_encoding_fp16fp16));