1 #include "caffe2/opt/distributed.h" 2 #include "caffe2/opt/converter.h" 9 getOrAddCaffe2Annotation(n);
10 auto op = nn::get<NeuralNetOperator>(n);
12 CAFFE_ENFORCE(c2Annot,
"getOrAddCaffe2Annotation failed!");
13 c2Annot->setDeviceOption(d);
17 std::map<std::string, caffe2::DeviceOption> blobMap,
20 std::unordered_set<std::string> seen;
22 auto declareNodes = nn::filter<Declare>(*nn);
24 for (
auto& declareNode : declareNodes) {
25 auto inputNode = nn::getOutputs(declareNode).at(0);
26 auto input = nn::get<nom::repr::Tensor>(inputNode);
28 if (!blobMap.count(input->getName())) {
33 !seen.count(input->getName()),
34 "Ambiguous name->deviceOption map. Please do this manually.");
36 seen.insert(input->getName());
37 setDeviceOption(declareNode, blobMap[input->getName()]);
40 auto exportNodes = nn::filter<Export>(*nn);
42 for (
auto& exportNode : exportNodes) {
43 auto outputNode = nn::getInputs(exportNode).at(0);
44 auto output = nn::get<nom::repr::Tensor>(outputNode);
46 if (!blobMap.count(output->getName())) {
51 !seen.count(output->getName()),
52 "Ambiguous name->deviceOption map. Please do this manually.");
54 seen.insert(output->getName());
55 setDeviceOption(exportNode, blobMap[output->getName()]);
58 if (seen.size() != blobMap.size()) {
59 std::ostringstream os;
60 for (
const auto& kv : blobMap) {
61 if (!(seen.count(kv.first))) {
62 os <<
"\"" << kv.first <<
"\" ";
66 seen.size() == blobMap.size(),
67 "Unused names in the blob map: ",
73 for (
auto& input : nn->inputs) {
75 nn->dataFlow.
createNode(nom::util::make_unique<Declare>());
79 for (
auto& output : nn->outputs) {
80 auto exportNode = nn->dataFlow.
createNode(nom::util::make_unique<Export>());
89 auto declareNodes = nn::filter<Declare>(*nn);
90 for (
auto& declareNode : declareNodes) {
91 auto input = nn::getOutputs(declareNode).at(0);
92 nn->inputs.insert(input);
93 nn->dataFlow.deleteNode(declareNode);
95 auto exportNodes = nn::filter<Export>(*nn);
96 for (
auto& exportNode : exportNodes) {
97 auto output = nn::getInputs(exportNode).at(0);
98 nn->outputs.insert(output);
99 nn->dataFlow.deleteNode(exportNode);
105 std::map<std::string, caffe2::DeviceOption> blobMap) {
107 injectDataEdgeIndicators(&nn);
NodeRef createNode(T &&data)
Creates a node and retains ownership of it.
void addBlobDeviceOptions(std::map< std::string, caffe2::DeviceOption > blobMap, NNModule *nn)
Helpers for the convertToNNModule for use if you already have an NNModule.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
repr::NNModule convertToNNModule(const caffe2::NetDef &net, bool strict, std::vector< repr::NNGraph::NodeRef > *opNodeVec)
Ingest a caffe2 protobuf model and output an NNModule.
EdgeRef createEdge(NodeRef tail, NodeRef head, U...data)
Creates a directed edge and retains ownership of it.