[moco/tf] support bias in ShapeInferenceData (#3913)
author박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Fri, 21 Jun 2019 07:41:48 +0000 (16:41 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 21 Jun 2019 07:41:48 +0000 (16:41 +0900)
This will introduce bias attribute accessor in ShapeInferenceData

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
contrib/moco/lib/frontend/tf/src/Annotations/ShapeInferenceData.cpp
contrib/moco/lib/frontend/tf/src/Annotations/ShapeInferenceData.h
contrib/moco/lib/frontend/tf/src/Annotations/ShapeInferenceData.test.cpp

index 04b5f5a..6f73bd2 100644 (file)
@@ -75,6 +75,18 @@ loco::FilterShape ShapeInferenceData::filter_shape(void) const
   return shape;
 }
 
+loco_tobe::BiasShape ShapeInferenceData::bias_shape(void) const
+{
+  assert(_domain == loco::Domain::Bias);
+
+  loco_tobe::BiasShape shape;
+
+  // Note: this may change when loco::BiasShape becomes available
+  shape = dim(0);
+
+  return shape;
+}
+
 void ShapeInferenceData::feature_shape(const loco::FeatureShape &shape)
 {
   _domain = loco::Domain::Feature;
@@ -97,5 +109,14 @@ void ShapeInferenceData::filter_shape(const loco::FilterShape &shape)
   dim(3) = shape.depth();
 }
 
+void ShapeInferenceData::bias_shape(const loco_tobe::BiasShape &shape)
+{
+  _domain = loco::Domain::Bias;
+
+  // Note: this may change when loco::BiasShape becomes available
+  rank(1);
+  dim(0) = shape;
+}
+
 } // namespace tf
 } // namespace moco
index 2d111e1..831e645 100644 (file)
 
 #include <cassert>
 
+namespace loco_tobe
+{
+
+// Temporary BiasShape that loco will provide in near future
+using BiasShape = loco::Dimension;
+
+} // namespace loco_tobe
+
 namespace moco
 {
 namespace tf
@@ -43,9 +51,11 @@ public:
   loco::TensorShape tensor_shape(void) const;
   loco::FeatureShape feature_shape(void) const;
   loco::FilterShape filter_shape(void) const;
+  loco_tobe::BiasShape bias_shape(void) const;
 
   void feature_shape(const loco::FeatureShape &shape);
   void filter_shape(const loco::FilterShape &shape);
+  void bias_shape(const loco_tobe::BiasShape &shape);
 
 private:
   // TODO set default as Unknown, setting Tensor is to minimize change
index 1935b0a..89f9b85 100644 (file)
@@ -97,3 +97,38 @@ TEST(TensorFlowFrontend, shapeinferencedata_filter_set)
   ASSERT_EQ(shapedata.dim(2), 3);
   ASSERT_EQ(shapedata.dim(3), 4);
 }
+
+TEST(TensorFlowFrontend, shapeinferencedata_bias_set)
+{
+  // Note: this may change when loco::BiasShape becomes available
+
+  loco_tobe::BiasShape bias;
+
+  bias = 3;
+
+  moco::tf::ShapeInferenceData shapedata;
+
+  shapedata.bias_shape(bias);
+
+  ASSERT_EQ(shapedata.rank(), 1);
+  ASSERT_EQ(shapedata.dim(0), 3);
+}
+
+TEST(TensorFlowFrontend, shapeinferencedata_bias_get)
+{
+  // Note: this may change when loco::BiasShape becomes available
+
+  // This test is to check bias is stored in first dimension
+  // for normal case, we shouldn't access directly for bias
+  moco::tf::ShapeInferenceData shapedata;
+  loco_tobe::BiasShape dummy;
+
+  shapedata.bias_shape(dummy); // this is to set domain to bias
+
+  shapedata.rank(1);
+  shapedata.dim(0) = 3;
+
+  loco_tobe::BiasShape bias = shapedata.bias_shape();
+
+  ASSERT_EQ(bias, 3);
+}