caffe2/opt/converter.cc
/// \brief Ingest a caffe2 protobuf model and output an NNModule.
/// \param net The caffe2 protobuf NetDef
/// \param blobMap [optional][output] A pointer to a blobMap to be populated with all the output blobs of the NetDef by name->NodeRef
\brief Ingest a caffe2 protobuf model and output an NNModule.
\param net The caffe2 protobuf NetDef
\param blobMap [optional][output] A pointer to a blobMap to be populated with all the output blobs of the NetDef by name->NodeRef
\ brief Ingest a caffe2 protobuf model and output an NNModule . \ param net The caffe2 protobuf NetDef \ param blobMap [ optional ] [ output ] A pointer to a blobMap to be populated with all the output blobs of the NetDef by name - > NodeRef
repr::NNModule convertToNNModule(caffe2::NetDef &net, std::unordered_map<std::string, repr::NNGraph::NodeRef>* blobMapOut) {
  repr::NNGraph dfg;
  repr::NNCFGraph cfg;
  std::unordered_map<std::string, repr::NNGraph::NodeRef> blobMap;
  auto bbNode =
      cfg.createNode(util::make_unique<repr::BasicBlockType<repr::NNGraph>>());
  for (auto &op : *net.mutable_op()) {
    auto opNode = dfg.createNode(); 
    for (const auto &input : op.input()) {
      if (!blobMap.count(input)) {
        auto tensor = util::make_unique<repr::Tensor>(input);
        blobMap[input] =
            dfg.createNode(unique_dyn_cast<repr::NeuralNetData>(tensor));
      }
      auto tensorNode = blobMap[input];
      dfg.createEdge(tensorNode, opNode);
    }
    for (const auto &output : op.output()) {
      auto tensor = util::make_unique<repr::Tensor>(output);
      auto tensorNode =
          dfg.createNode(unique_dyn_cast<repr::NeuralNetData>(tensor));
      dfg.createEdge(opNode, tensorNode);
      blobMap[output] = tensorNode;
    }
    opNode->resetData(convertToNeuralNetOperator(op));
    auto currentBasicBlock = bbNode->mutableData()->get();
    currentBasicBlock->pushInstructionNode(opNode);
  }
  repr::NNModule module;
  module.dataFlow = std::move(dfg);
  module.controlFlow = std::move(cfg);
  if (blobMapOut) {
    *blobMapOut = blobMap;
  }
  return module;
}
repr :: NNModule convertToNNModule ( caffe2 :: NetDef & net , std :: unordered_map < std :: string , repr :: NNGraph :: NodeRef > * blobMapOut ) { repr :: NNGraph dfg ; repr :: NNCFGraph cfg ; std :: unordered_map < std :: string , repr :: NNGraph :: NodeRef > blobMap ; auto bbNode = cfg . createNode ( util :: make_unique < repr :: BasicBlockType < repr :: NNGraph > > ( ) ) ; for ( auto & op : * net . mutable_op ( ) ) { auto opNode = dfg . createNode ( ) ; for ( const auto & input : op . input ( ) ) { if ( ! blobMap . count ( input ) ) { auto tensor = util :: make_unique < repr :: Tensor > ( input ) ; blobMap [ input ] = dfg . createNode ( unique_dyn_cast < repr :: NeuralNetData > ( tensor ) ) ; } auto tensorNode = blobMap [ input ] ; dfg . createEdge ( tensorNode , opNode ) ; } for ( const auto & output : op . output ( ) ) { auto tensor = util :: make_unique < repr :: Tensor > ( output ) ; auto tensorNode = dfg . createNode ( unique_dyn_cast < repr :: NeuralNetData > ( tensor ) ) ; dfg . createEdge ( opNode , tensorNode ) ; blobMap [ output ] = tensorNode ; } opNode -> resetData ( convertToNeuralNetOperator ( op ) ) ; auto currentBasicBlock = bbNode -> mutableData ( ) -> get ( ) ; currentBasicBlock -> pushInstructionNode ( opNode ) ; } repr :: NNModule module ; module . dataFlow = std :: move ( dfg ) ; module . controlFlow = std :: move ( cfg ) ; if ( blobMapOut ) { * blobMapOut = blobMap ; } return module ; }
\brief Ingest a caffe2 protobuf model and output an NNModule.
\ brief Ingest a caffe2 protobuf model and output an NNModule .
/// \brief We keep track of the producer of the blob. /// Because Caffe2 Nets are really just ordered operations /// we can just keep track of the most recent producer of /// a blob and draw and edge from that to any consumer we /// come by. If a new operator produces the blob we simply /// replace it in this map. /// \brief For the construction of the control flow graph we keep track /// of a current basic block, which we split up as we come accross control /// flow operations such as if and while. // std::unique_ptr<repr::BasicBlockType<repr::NNGraph>> currentBasicBlock = // Create an empty node for the operator. // First calculate in-edges (data dependencies). // If we've never seen this tensor, make one. // Then save outputs into the blobMap for later consumption.