tensorflow/lite/toco/graph_transformations/convert_trivial_addn_to_add.cc
// This pass will convert an AddN operator with only 2 inputs into a regular Add
// operator, to which more optimizations may apply.
This pass will convert an AddN operator with only 2 inputs into a regular Add
operator, to which more optimizations may apply.
This pass will convert an AddN operator with only 2 inputs into a regular Add operator to which more optimizations may apply .
::tensorflow::Status ConvertTrivialAddNToAdd::Run(Model* model,
                                                  std::size_t op_index,
                                                  bool* modified) {
  *modified = false;
  auto addn_it = model->operators.begin() + op_index;
  if (addn_it->get()->type != OperatorType::kAddN) {
    return ::tensorflow::Status::OK();
  }
  AddNOperator* addn_op = static_cast<AddNOperator*>(addn_it->get());
  CHECK_GE(addn_op->inputs.size(), 2);
  CHECK_EQ(addn_op->outputs.size(), 1);
  if (addn_op->inputs.size() != 2) {
    return ::tensorflow::Status::OK();
  }
  auto* add_op = new AddOperator;
  add_op->inputs.push_back(addn_op->inputs[0]);
  add_op->inputs.push_back(addn_op->inputs[1]);
  add_op->outputs = addn_op->outputs;
  model->operators.emplace(addn_it, add_op);
  DeleteOpAndArrays(model, addn_op);
  *modified = true;
  return ::tensorflow::Status::OK();
}
:: tensorflow :: Status ConvertTrivialAddNToAdd :: Run ( Model * model , std :: size_t op_index , bool * modified ) { * modified = false ; auto addn_it = model -> operators . begin ( ) + op_index ; if ( addn_it -> get ( ) -> type != OperatorType :: kAddN ) { return :: tensorflow :: Status :: OK ( ) ; } AddNOperator * addn_op = static_cast < AddNOperator * > ( addn_it -> get ( ) ) ; CHECK_GE ( addn_op -> inputs . size ( ) , 2 ) ; CHECK_EQ ( addn_op -> outputs . size ( ) , 1 ) ; if ( addn_op -> inputs . size ( ) != 2 ) { return :: tensorflow :: Status :: OK ( ) ; } auto * add_op = new AddOperator ; add_op -> inputs . push_back ( addn_op -> inputs [ 0 ] ) ; add_op -> inputs . push_back ( addn_op -> inputs [ 1 ] ) ; add_op -> outputs = addn_op -> outputs ; model -> operators . emplace ( addn_it , add_op ) ; DeleteOpAndArrays ( model , addn_op ) ; * modified = true ; return :: tensorflow :: Status :: OK ( ) ; }
This pass will convert an AddN operator with only 2 inputs into a regular Add
operator, to which more optimizations may apply.
This pass will convert an AddN operator with only 2 inputs into a regular Add operator to which more optimizations may apply .
// We only reduce AddN with N=2 to a regular Add. // Copy inputs & outputs to regular Add. // Replace the AddN operator in the graph.