[neurun] Remove unnecessary map in base_loader (#8955)
authorSergei Barannikov/AI Tools Lab /SRR/Engineer/Samsung Electronics <s.barannikov@samsung.com>
Fri, 15 Nov 2019 13:10:25 +0000 (16:10 +0300)
committerAlexander Efimov/AI Tools Lab /SRR/Engineer/Samsung Electronics <a.efimov@samsung.com>
Fri, 15 Nov 2019 13:10:25 +0000 (16:10 +0300)
Set Operand data right when creating it from Tensor, `_tensor_to_operand` map is not needed.

Signed-off-by: Sergei Barannikov <s.barannikov@samsung.com>
runtime/neurun/frontend/base_loader/base_loader.h

index e25b209..f474e85 100644 (file)
@@ -67,8 +67,6 @@ protected:
   model::Activation convertActivation(ActivationFunctionType type);
   model::DataType tensorTypeToDataType(TensorType type);
 
-  // Load data from buffer to tensor on index
-  void loadConstantTensor(const Buffer *buffer, const uint32_t &index);
   // Create operands form tflite::Tensor
   void loadOperand(const Tensor *tensor);
   void loadOperationIO(const Operator *op, model::OperandIndexSequence &inputs,
@@ -123,8 +121,6 @@ protected:
   std::vector<char> _buffer;
   // Reference on loadable Graph
   graph::Graph &_graph;
-  // Mapping from tensor index to Graph OperandIndex
-  std::map<uint32_t, model::OperandIndex> _tensor_to_operand;
   // Mapping from operator pointer to subgraph pointer
   std::map<const Operator *, const SubGraph *> _op_to_subgraph;
   const Model *_model;
@@ -212,9 +208,15 @@ void BaseLoader<LoaderDomain, SpecificLoader>::loadOperand(const Tensor *tensor)
   model::TypeInfo type_info(data_type);
   // Create operand
   const auto &operand_index = _graph.addOperand(shape, type_info);
-  // Buffer index
-  if (tensor->buffer() != 0)
-    _tensor_to_operand[tensor->buffer()] = operand_index;
+
+  // Constant tensors are indicated by non-empty data.
+  const auto *data = _model->buffers()->Get(tensor->buffer())->data();
+  if (data != nullptr)
+  {
+    auto ptr = nnfw::cpp14::make_unique<model::CachedData>(data->data(), data->size());
+    _graph.setOperandValue(operand_index, std::move(ptr));
+  }
+
   // Name unused
   // auto name = tensor->name();
   // Quantization
@@ -989,20 +991,6 @@ void BaseLoader<LoaderDomain, SpecificLoader>::loadOperation(const Operator *op)
 }
 
 template <typename LoaderDomain, typename SpecificLoader>
-void BaseLoader<LoaderDomain, SpecificLoader>::loadConstantTensor(const Buffer *buffer,
-                                                                  const uint32_t &index)
-{
-  const auto *data = buffer->data();
-  if (data != nullptr)
-  {
-    auto ptr = nnfw::cpp14::make_unique<model::CachedData>(data->data(), data->size());
-    const auto &operand_index = _tensor_to_operand[index];
-    auto &operand = _graph.operands().at(operand_index);
-    operand.data(std::move(ptr));
-  }
-}
-
-template <typename LoaderDomain, typename SpecificLoader>
 void BaseLoader<LoaderDomain, SpecificLoader>::loadModel()
 {
   static_cast<SpecificLoader *>(this)->verify();
@@ -1011,7 +999,6 @@ void BaseLoader<LoaderDomain, SpecificLoader>::loadModel()
   // const auto version = _model->version();
   // Description unused
   // const auto *description = _model->description();
-  const auto *buffers = _model->buffers();
   // Metabuffer unsued
   // const auto *metadata_buffer = _model->metadata_buffer();
   // Load subgraphs and mapping from op to subgraph
@@ -1025,11 +1012,6 @@ void BaseLoader<LoaderDomain, SpecificLoader>::loadModel()
 
     static_cast<SpecificLoader *>(this)->loadSubgraph(subgraph);
   }
-  // Load buffers with constant tensors
-  for (uint32_t ind = 0; ind < buffers->size(); ind++)
-  {
-    loadConstantTensor(buffers->Get(ind), ind);
-  }
 
   _graph.finishBuilding();
 }