Caffe2 - C++ API
A deep learning, cross platform ML framework
converter.h
1 #ifndef CAFFE2_OPT_CONVERTER_H
2 #define CAFFE2_OPT_CONVERTER_H
3 
4 #include "caffe2/core/common.h"
5 #include "caffe2/core/logging.h"
6 #include "caffe2/opt/annotations.h"
7 #include "caffe2/proto/caffe2_pb.h"
8 #include "nomnigraph/Graph/Graph.h"
9 #include "nomnigraph/Representations/ControlFlow.h"
10 #include "nomnigraph/Representations/NeuralNet.h"
11 
12 #include <unordered_map>
13 
14 namespace caffe2 {
15 
16 CAFFE2_API void injectDataEdgeIndicators(caffe2::NetDef* net);
17 CAFFE2_API void removeDataEdgeIndicators(caffe2::NetDef* net);
18 
19 // Default conversion to a NNModule
20 // Optionally strict -- which checks for various input and output conditions.
21 // Optionally this function will update a vector that maps operators in the
22 // netdef positionally to NodeRefs in the resultant NNModule.
24  const caffe2::NetDef& net,
25  bool strict = false,
26  std::vector<nom::repr::NNGraph::NodeRef>* = nullptr);
27 CAFFE2_API caffe2::NetDef convertToCaffe2Proto(nom::repr::NNModule&);
28 
29 // Pass in an oldNet to copy all the attributes of that network.
30 // Be warned that transformations that modify the graph's inputs or outputs
31 // are not reflected in changes to external_input or external_output.
32 CAFFE2_API caffe2::NetDef convertToCaffe2Proto(nom::repr::NNModule&, const caffe2::NetDef& oldNet);
33 
34 // Use these functions instead of the registry directly.
35 CAFFE2_API std::unique_ptr<nom::repr::NeuralNetOperator> convertToNeuralNetOperator(
36  const caffe2::OperatorDef& op);
37 
38 CAFFE2_API caffe2::OperatorDef convertToOperatorDef(
39  const nom::repr::NNGraph::NodeRef& instrNode);
40 
41 // If the annotation doesn't exist, attempt to add it
42 CAFFE2_API Caffe2Annotation* getOrAddCaffe2Annotation(
43  nom::repr::NNGraph::NodeRef& instrNode);
44 
45 class CAFFE2_API Converter {
46  public:
47  explicit Converter() = default;
48  virtual std::unique_ptr<nom::repr::NeuralNetOperator>
49  convertToNeuralNetOperator(const OperatorDef&) = 0;
50  virtual OperatorDef convertToOperatorDef(const nom::repr::NeuralNetOperator*);
51  static std::map<std::string, caffe2::Argument> getArgumentsFromOperator(
52  caffe2::OperatorDef op);
53 
54  virtual ~Converter() {}
55 };
56 
57 C10_DECLARE_REGISTRY(ConverterRegistry, Converter);
58 #define REGISTER_CONVERTER(name, cls) \
59  C10_REGISTER_CLASS(ConverterRegistry, name, cls)
60 
61 #define TRIVIAL_CONVERTER(opName) \
62  class opName##Converter : public Converter { \
63  std::unique_ptr<nom::repr::NeuralNetOperator> convertToNeuralNetOperator( \
64  const OperatorDef& op) override { \
65  return nom::util::make_unique<nom::repr::opName>(); \
66  } \
67  virtual ~opName##Converter() {} \
68  };
69 
70 } // namespace caffe2
71 
72 
73 #endif // CAFFE2_OPT_CONVERTER_H
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
repr::NNModule convertToNNModule(const caffe2::NetDef &net, bool strict, std::vector< repr::NNGraph::NodeRef > *opNodeVec)
Ingest a caffe2 protobuf model and output an NNModule.
Definition: converter.cc:301