1 #include "caffe2/opt/backend_transformer_base.h" 2 #include "caffe2/onnx/onnx_exporter.h" 3 #include "caffe2/utils/proto_utils.h" 8 void annotateOpIndex(NetDef* net) {
10 for (
auto& op : *(net->mutable_op())) {
11 AddArgument(kNetPos, i++, &op);
16 std::string BackendTransformerBase::getModelId(
const NetDef& net) {
17 static std::atomic<size_t> seq_id{0};
19 for (
const auto& arg : net.arg()) {
20 if (arg.name() == kModelId) {
23 }
else if (arg.has_i()) {
24 model_id = c10::to_string(arg.i());
30 if (model_id.empty()) {
31 model_id =
"unnamed_" + c10::to_string(seq_id++);
36 TensorProto BackendTransformerBase::wrapShapeInfoIntoTensorProto(
37 const std::string& name,
38 const ShapeInfo& shape_info)
const {
41 t.set_data_type(shape_info.shape.data_type());
42 for (
const auto i : shape_info.shape.dims()) {
48 std::unordered_map<std::string, TensorShape>
49 BackendTransformerBase::ssaRewriteAndMapNames(
52 const std::unordered_map<std::string, TensorShape>& input_shape_hints) {
53 input_mapping_ = onnx::SsaRewrite(
nullptr, pred_net);
55 annotateOpIndex(pred_net);
60 std::vector<std::string> exclude_mapping;
61 for (
const auto kv : input_mapping_) {
62 if (!ws->HasBlob(kv.second)) {
63 exclude_mapping.emplace_back(kv.first);
66 for (
const auto& i : exclude_mapping) {
67 input_mapping_.erase(i);
70 std::unordered_map<std::string, TensorShape> shape_hints_mapped;
71 for (
const auto& kv : input_shape_hints) {
72 shape_hints_mapped.emplace(kv.first, kv.second);
74 return shape_hints_mapped;
77 ShapeInfoMap BackendTransformerBase::inferShapes(
80 const std::unordered_map<std::string, TensorShape>& shape_hints_mapped,
81 const BoundShapeSpec& spec) {
82 ShapeInfoMap shape_map;
84 const std::vector<std::string> ws_blobs = ws->Blobs();
85 for (
const auto& s : ws_blobs) {
86 auto shape_info = getShapeInfoFromBlob(ws->GetBlob(s));
87 if (shape_info.dim_type != ShapeInfo::DimType::UNKNOWN) {
88 shape_map[s] = shape_info;
95 for (
const auto& kv : shape_hints_mapped) {
97 std::piecewise_construct,
98 std::forward_as_tuple(kv.first),
99 std::forward_as_tuple(ShapeInfo::DimType::BATCH, kv.second));
101 BoundShapeInferencer eng(spec);
102 eng.InferBoundShapeAndType(*pred_net, shape_map);
103 const auto& out_map = eng.shape_info();
105 for (
const auto& kv : out_map) {
107 std::piecewise_construct,
108 std::forward_as_tuple(kv.first),
109 std::forward_as_tuple(kv.second.dim_type, kv.second.shape));
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...