MCPcopy Create free account
hub / github.com/pytorch/pytorch / prepare

Method prepare

caffe2/python/onnx/backend.py:678–737  ·  view source on GitHub ↗

For Onnx Caffe2Backend, we require that init_graph don't initialize the actual input of the predict_graph, for example, if "img" is the input blob for the predict_net, we require that in init_graph and in initializer of the predict_graph, "img" is not initalized. We don't h

(cls, model, device='CPU', raw_values_dict=None, **kwargs)

Source from the content-addressed store, hash-verified

676
677 @classmethod
678 def prepare(cls, model, device='CPU', raw_values_dict=None, **kwargs):
679 '''
680 For Onnx Caffe2Backend, we require that init_graph don't initialize the actual input of the predict_graph,
681
682 for example, if "img" is the input blob for the predict_net, we require that in init_graph and in
683 initializer of the predict_graph, "img" is not initalized. We don't have a check for this, since
684 there is no way we can know which blob is the input of the predict_graph.
685 '''
686 if not kwargs.pop('no_check_UNSAFE', False):
687 super(Caffe2Backend, cls).prepare(model, device, **kwargs)
688 opset_version = None
689 for imp in model.opset_import:
690 if not imp.HasField("domain") or imp.domain == "":
691 opset_version = imp.version
692 if imp.version > cls._known_opset_version:
693 warnings.warn("This version of onnx-caffe2 targets ONNX operator set version {}, but the model we are trying to import uses version {}. We will try to import it anyway, but if the model uses operators which had BC-breaking changes in the intervening versions, import will fail.".format(cls._known_opset_version, imp.version))
694 else:
695 warnings.warn("Unrecognized operator set {}".format(imp.domain))
696 if opset_version is None:
697 if model.ir_version >= 0x00000003:
698 raise RuntimeError("Model with IR version >= 3 did not specify ONNX operator set version (onnx-caffe2 requires it)")
699 else:
700 opset_version = 1
701
702 # Prior to onnx version update to onnx-1.8.0, errors caused by failures in
703 # in the onnx shape inference call were being supressed. Hence a try-catch block
704 # is added around the infer_shapes call to avoid these failures and preserve status
705 try:
706 model = onnx.shape_inference.infer_shapes(model)
707 except RuntimeError:
708 warnings.warn("ShapeInferenceWarning: Inferred shape and existing shape differ in rank")
709
710 ws = Workspace()
711 device_option = get_device_option(Device(device))
712
713 init_net, predict_net = cls._onnx_model_to_caffe2_net(model, device, opset_version, False)
714
715 if raw_values_dict:
716 cls._external_value_resolution_pass(model, raw_values_dict)
717
718 # Directly load initializer data into blobs in workspace
719 cls._direct_initialize_parameters(
720 model.graph.initializer,
721 ws,
722 device_option,
723 )
724
725 initialized = {init.name for init in model.graph.initializer}
726
727 cls._direct_initialize_inputs(
728 model.graph.input,
729 initialized,
730 ws,
731 device_option,
732 )
733
734 uninitialized = [value_info.name for value_info in model.graph.input if value_info.name not in initialized]
735

Callers 11

onnx_verifyFunction · 0.45
sample.pyFile · 0.45
prepare_zip_archiveMethod · 0.45
test_relu_graphMethod · 0.45
test_initializerMethod · 0.45
_test_netMethod · 0.45
initMethod · 0.45

Calls 11

WorkspaceClass · 0.90
Caffe2RepClass · 0.90
get_device_optionFunction · 0.85
warnMethod · 0.80
DeviceClass · 0.50
popMethod · 0.45
formatMethod · 0.45

Tested by 8

test_relu_graphMethod · 0.36
test_initializerMethod · 0.36
_test_netMethod · 0.36
initMethod · 0.36