Revise OperandUsage and related methods (#4408)
author오형석/On-Device Lab(SR)/Staff Engineer/삼성전자 <hseok82.oh@samsung.com>
Thu, 14 Feb 2019 05:11:29 +0000 (14:11 +0900)
committer박세희/On-Device Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Thu, 14 Feb 2019 05:11:29 +0000 (14:11 +0900)
Rename OperandUsage as Usage
Remove setters for each enum value and use one setter
Rename getter and setter for usage

Signed-off-by: Hyeongseok Oh <hseok82.oh@samsung.com>
runtimes/neurun/src/compiler/ConstantInitializer.cc
runtimes/neurun/src/compiler/SubTensorAnalyzer.cc
runtimes/neurun/src/dumper/dot/DotDumper.cc
runtimes/neurun/src/frontend/model.cc
runtimes/neurun/src/graph/pass/PermutationInsertionPass.cc
runtimes/neurun/src/linear/Linear.cc
runtimes/neurun/src/model/operand/Object.cc
runtimes/neurun/src/model/operand/Object.h
runtimes/neurun/test/graph/operand/UseDef.cc
runtimes/neurun/test/graph/verifier/Verifier.cc

index 57f62d3..356c7b5 100644 (file)
@@ -134,7 +134,7 @@ void ConstantInitializer::operator()()
     const auto &model_obj = _graph.operands().at(index);
 
     // For only CONSTANTS
-    if (model_obj.getUsage() != neurun::model::operand::OperandUsage::CONSTANT)
+    if (model_obj.usage() != neurun::model::operand::Usage::CONSTANT)
       return;
 
     auto type = model_obj.typeInfo().type();
index 949eeb9..4fd3e24 100644 (file)
@@ -38,7 +38,7 @@ void SubTensorAnalyzer::visit(const model::operation::ConcatNode &node)
   auto axis_index = node.param().axis_index;
 
   // To prepare concat elimination, axis should be constant
-  if (_ctx.at(axis_index).getUsage() != model::operand::OperandUsage::CONSTANT)
+  if (_ctx.at(axis_index).usage() != model::operand::Usage::CONSTANT)
   {
     VERBOSE(SUBTENSOR) << "Cannot handle non-constant axis" << std::endl;
     return;
index 0bb8412..cb64f39 100644 (file)
@@ -56,17 +56,17 @@ void DotDumper::dumpIfNeeded(const std::string &tag)
 
   operands.iterate([&](const model::operand::Index &index, const model::operand::Object &object) {
     bool showing_cond = false;
-    auto usage = object.getUsage();
+    auto usage = object.usage();
     if (_option == OPTIONS::SHOW_CONSTANTS)
     {
       showing_cond = object.usageIsDefined();
     }
     else
     {
-      showing_cond = (usage == model::operand::OperandUsage::MODEL_INPUT) ||
-                     (usage == model::operand::OperandUsage::OPERATION_OUTPUT);
+      showing_cond = (usage == model::operand::Usage::MODEL_INPUT) ||
+                     (usage == model::operand::Usage::OPERATION_OUTPUT);
     }
-    if (usage != model::operand::OperandUsage::OPERATION_OUTPUT)
+    if (usage != model::operand::Usage::OPERATION_OUTPUT)
     {
       showing_cond = showing_cond && (object.getUses().size() > 0);
     }
index 4c30979..5bbe59c 100644 (file)
@@ -147,7 +147,7 @@ int ANeuralNetworksModel_setOperandValue(ANeuralNetworksModel *model, int32_t in
   {
     return ANEURALNETWORKS_BAD_DATA;
   }
-  if (!obj.setAsConstant())
+  if (!obj.usage(neurun::model::operand::Usage::CONSTANT))
   {
     return ANEURALNETWORKS_BAD_DATA;
   }
@@ -211,7 +211,7 @@ int ANeuralNetworksModel_setOperandValueFromMemory(ANeuralNetworksModel *model,
   {
     return ANEURALNETWORKS_BAD_DATA;
   }
-  if (!obj.setAsConstant())
+  if (!obj.usage(neurun::model::operand::Usage::CONSTANT))
   {
     return ANEURALNETWORKS_BAD_DATA;
   }
@@ -252,7 +252,7 @@ int ANeuralNetworksModel_addOperation(ANeuralNetworksModel *model,
     const ::neurun::model::operand::Index ind{outputs[i]};
     auto &obj = model->deref().operands().at(ind);
 
-    if (!obj.setAsOperationOutput())
+    if (!obj.usage(neurun::model::operand::Usage::OPERATION_OUTPUT))
     {
       return ANEURALNETWORKS_BAD_DATA;
     }
@@ -404,7 +404,7 @@ int ANeuralNetworksModel_addOperationEx(ANeuralNetworksModel *model,
     const ::neurun::model::operand::Index ind{outputs[i]};
     auto &obj = model->deref().operands().at(ind);
 
-    if (!obj.setAsOperationOutput())
+    if (!obj.usage(neurun::model::operand::Usage::OPERATION_OUTPUT))
     {
       return ANEURALNETWORKS_BAD_DATA;
     }
@@ -459,7 +459,7 @@ int ANeuralNetworksModel_identifyInputsAndOutputs(ANeuralNetworksModel *model, u
     model->deref().addInput(ind);
 
     auto &obj = model->deref().operands().at(ind);
-    if (!obj.setAsModelInput())
+    if (!obj.usage(neurun::model::operand::Usage::MODEL_INPUT))
     {
       return ANEURALNETWORKS_BAD_DATA;
     }
index e4632e1..bcdb570 100644 (file)
@@ -124,14 +124,14 @@ PermutationInsertionPass::insertPermute(const model::operand::Index &operand_ind
   // Generate output operand and permute operation
   auto out_operand_index = _graph.addOperand(operand.shape(), operand.typeInfo());
   auto &out_operand = _graph.operands().at(out_operand_index);
-  out_operand.setAsOperationOutput();
+  out_operand.usage(model::operand::Usage::OPERATION_OUTPUT);
   // change model output if operand_index is model output index
   auto &model_outputs = _graph.getOutputs();
   if (model_outputs.contains(operand_index))
   {
     model_outputs.replace(operand_index, out_operand_index);
   }
-  out_operand.setAsOperationOutput();
+  out_operand.usage(model::operand::Usage::OPERATION_OUTPUT);
   auto out_operand_li =
       nnfw::cpp14::make_unique<operand::LowerInfo>(operand::asShape4D(operand.shape()));
   out_operand_li->addDefBackend(backend);
index 3446a4a..a672d5d 100644 (file)
@@ -89,7 +89,7 @@ backend::TensorBuilderSet Linear::planTensors()
 
         // If a tensor is a constant, increase the use of the tensor.
         // It makes the tensor not be dealloced.
-        if (obj.getUsage() == model::operand::OperandUsage::CONSTANT)
+        if (obj.usage() == model::operand::Usage::CONSTANT)
         {
           constants.push_back(ind);
           uses_map[ind]++;
index d7d9d0e..10a0128 100644 (file)
@@ -40,7 +40,7 @@ size_t Object::operandSize(void) const
   return element_size * elements;
 }
 
-bool Object::setUsage(const OperandUsage usage)
+bool Object::usage(const Usage usage)
 {
   if (usageIsDefined() && (_usage != usage))
   {
@@ -55,7 +55,7 @@ bool Object::setUsage(const OperandUsage usage)
 
 void Object::appendUse(const ::neurun::model::operation::Index &idx)
 {
-  assert(_usage != OperandUsage::NOT_DEFINED);
+  assert(_usage != Usage::NOT_DEFINED);
   assert(!_uses.contains(idx));
 
   _uses.append(idx);
@@ -63,7 +63,7 @@ void Object::appendUse(const ::neurun::model::operation::Index &idx)
 
 void Object::removeUse(const ::neurun::model::operation::Index &idx)
 {
-  assert(_usage != OperandUsage::NOT_DEFINED);
+  assert(_usage != Usage::NOT_DEFINED);
   assert(_uses.contains(idx));
 
   _uses.remove(idx);
@@ -71,7 +71,7 @@ void Object::removeUse(const ::neurun::model::operation::Index &idx)
 
 void Object::appendDef(const ::neurun::model::operation::Index &idx)
 {
-  assert(_usage != OperandUsage::NOT_DEFINED && _usage != OperandUsage::CONSTANT);
+  assert(_usage != Usage::NOT_DEFINED && _usage != Usage::CONSTANT);
   assert(_def.size() == 0);
 
   _def.append(idx);
@@ -79,7 +79,7 @@ void Object::appendDef(const ::neurun::model::operation::Index &idx)
 
 void Object::removeDef(const ::neurun::model::operation::Index &idx)
 {
-  assert(_usage != OperandUsage::NOT_DEFINED);
+  assert(_usage != Usage::NOT_DEFINED);
   assert(_def.contains(idx));
 
   _def.remove(idx);
index 1842041..9ebf037 100644 (file)
@@ -36,7 +36,7 @@ namespace operand
 {
 
 // Operand usage should be exact one of these
-enum class OperandUsage
+enum class Usage
 {
   NOT_DEFINED,
   MODEL_INPUT,
@@ -48,7 +48,7 @@ class Object
 {
 public:
   explicit Object(const Shape &shape, const TypeInfo &type)
-      : _shape{shape}, _type{type}, _usage{OperandUsage::NOT_DEFINED}
+      : _shape{shape}, _type{type}, _usage{Usage::NOT_DEFINED}
   {
     // DO NOTHING
   }
@@ -57,12 +57,10 @@ public:
   const Shape &shape(void) const { return _shape; }
   const TypeInfo &typeInfo(void) const { return _type; }
   size_t operandSize(void) const;
-  bool setAsConstant() { return setUsage(OperandUsage::CONSTANT); }
-  bool setAsModelInput() { return setUsage(OperandUsage::MODEL_INPUT); }
-  bool setAsOperationOutput() { return setUsage(OperandUsage::OPERATION_OUTPUT); }
-  bool usageIsDefined(void) const { return _usage != OperandUsage::NOT_DEFINED; }
-  bool isModelInput(void) const { return _usage == OperandUsage::MODEL_INPUT; }
-  OperandUsage getUsage() const { return _usage; }
+  bool usageIsDefined(void) const { return _usage != Usage::NOT_DEFINED; }
+  bool isModelInput(void) const { return _usage == Usage::MODEL_INPUT; }
+  Usage usage() const { return _usage; }
+  bool usage(Usage usage);
 
   const operation::IndexList &getUses() const { return _uses; }
   const operation::IndexList &getDef() const { return _def; }
@@ -71,9 +69,6 @@ public:
   void appendDef(const operation::Index &idx);
   void removeDef(const operation::Index &idx);
 
-private:
-  bool setUsage(OperandUsage usage);
-
 public:
   void data(std::unique_ptr<Data> &&data) { _data = std::move(data); }
   const Data &data(void) const { return *_data; }
@@ -115,7 +110,7 @@ private:
   const Shape _shape;
   const TypeInfo _type;
   std::unique_ptr<Data> _data;
-  OperandUsage _usage;
+  Usage _usage;
 
   operation::IndexList _uses;
   operation::IndexList _def; // size is 0 (constant) or 1 (from def operation)
index 7f68b13..fd451c1 100644 (file)
@@ -45,19 +45,19 @@ TEST(graph_operand_usedef, usedef_test)
   auto output_operand = graph.addOperand(shape, type);
 
   graph.addInput(input_operand);
-  graph.operands().at(input_operand).setAsModelInput();
+  graph.operands().at(input_operand).usage(neurun::model::operand::Usage::MODEL_INPUT);
   graph.addOutput(output_operand);
-  graph.operands().at(output_operand).setAsOperationOutput();
+  graph.operands().at(output_operand).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
 
   // MockNode1
   auto operand_index1 = graph.addOperand(shape, type);
-  graph.operands().at(operand_index1).setAsOperationOutput();
+  graph.operands().at(operand_index1).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
   auto mocknode_index1 = graph.addOperation(
       nnfw::cpp14::make_unique<MockNode>(IndexSet{input_operand}, IndexSet{operand_index1}));
 
   // MockNode2
   auto operand_index2 = graph.addOperand(shape, type);
-  graph.operands().at(operand_index2).setAsOperationOutput();
+  graph.operands().at(operand_index2).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
   auto mocknode_index2 = graph.addOperation(
       nnfw::cpp14::make_unique<MockNode>(IndexSet{input_operand}, IndexSet{operand_index2}));
 
index b736ad2..b4e15a5 100644 (file)
@@ -39,9 +39,9 @@ TEST(Verifier, dag_checker)
   auto operand2 = graph.addOperand(shape, type);
 
   graph.addInput(operand1);
-  graph.operands().at(operand1).setAsModelInput();
+  graph.operands().at(operand1).usage(neurun::model::operand::Usage::MODEL_INPUT);
   graph.addOutput(operand2);
-  graph.operands().at(operand2).setAsOperationOutput();
+  graph.operands().at(operand2).usage(neurun::model::operand::Usage::OPERATION_OUTPUT);
 
   graph.addOperation(nnfw::cpp14::make_unique<MockNode>(IndexSet{operand1}, IndexSet{operand2}));