nncf/torch/exporter.py
def _export_to_onnx(self, save_path: str, opset_version: int) -> None:
        """
        Exports the compressed model to the ONNX format.

        :param save_path: The path where the model will be saved.
        """
        original_device = get_model_device(self._model)
        model = self._model.eval().cpu()
        input_tensor_list = []
        for info in self._model.input_infos:
            single_batch_info = copy(info)
            input_shape = tuple([1] + list(info.shape)[1:])
            single_batch_info.shape = input_shape
            input_tensor_list.append(create_mock_tensor(single_batch_info, 'cpu'))

        original_forward = model.forward
        args = self._model_args[:-1]
        kwargs = self._model_args[-1]
        model.forward = partial(model.forward, *args, **kwargs)

        if self._input_names is not None:
            input_names = self._input_names
        else:
            input_names = generate_input_names_list(len(input_tensor_list))


        # pylint:disable=unexpected-keyword-arg
        with torch.no_grad():
            # Should call this, otherwise the operations executed during export will end up in the graph.
            model.disable_dynamic_graph_building()

            if self._output_names is not None:
                output_names = self._output_names
            else:
                # Will have to run a dummy forward call in order to determine the number of outputs.
                dummy_forward = create_dummy_forward_fn(self._model.input_infos)
                retval = dummy_forward(self._model)
                output_names = generate_output_names_list(count_tensors(retval))

            torch.onnx.export(model, tuple(input_tensor_list), save_path,
                              input_names=input_names,
                              output_names=output_names,
                              enable_onnx_checker=False,
                              opset_version=opset_version,
                              # Do not fuse Conv+BN in ONNX. May cause dropout elements to appear in ONNX.
                              training=True)
            model.enable_dynamic_graph_building()
        model.forward = original_forward
        model.to(original_device)

        Exports the compressed model to the ONNX format.

        :param save_path: The path where the model will be saved.
        
Exports the compressed model to the ONNX format.
Exports the compressed model to the ONNX format .
def _export_to_onnx(self, save_path: str, opset_version: int) -> None:
        original_device = get_model_device(self._model)
        model = self._model.eval().cpu()
        input_tensor_list = []
        for info in self._model.input_infos:
            single_batch_info = copy(info)
            input_shape = tuple([1] + list(info.shape)[1:])
            single_batch_info.shape = input_shape
            input_tensor_list.append(create_mock_tensor(single_batch_info, 'cpu'))
        original_forward = model.forward
        args = self._model_args[:-1]
        kwargs = self._model_args[-1]
        model.forward = partial(model.forward, *args, **kwargs)
        if self._input_names is not None:
            input_names = self._input_names
        else:
            input_names = generate_input_names_list(len(input_tensor_list))
        with torch.no_grad():
            model.disable_dynamic_graph_building()
            if self._output_names is not None:
                output_names = self._output_names
            else:
                dummy_forward = create_dummy_forward_fn(self._model.input_infos)
                retval = dummy_forward(self._model)
                output_names = generate_output_names_list(count_tensors(retval))
            torch.onnx.export(model, tuple(input_tensor_list), save_path,
                              input_names=input_names,
                              output_names=output_names,
                              enable_onnx_checker=False,
                              opset_version=opset_version,
                              training=True)
            model.enable_dynamic_graph_building()
        model.forward = original_forward
        model.to(original_device)
def _export_to_onnx ( self , save_path : str , opset_version : int ) -> None : original_device = get_model_device ( self . _model ) model = self . _model . eval ( ) . cpu ( ) input_tensor_list = [ ] for info in self . _model . input_infos : single_batch_info = copy ( info ) input_shape = tuple ( [ 1 ] + list ( info . shape ) [ 1 : ] ) single_batch_info . shape = input_shape input_tensor_list . append ( create_mock_tensor ( single_batch_info , 'cpu' ) ) original_forward = model . forward args = self . _model_args [ : - 1 ] kwargs = self . _model_args [ - 1 ] model . forward = partial ( model . forward , * args , ** kwargs ) if self . _input_names is not None : input_names = self . _input_names else : input_names = generate_input_names_list ( len ( input_tensor_list ) ) with torch . no_grad ( ) : model . disable_dynamic_graph_building ( ) if self . _output_names is not None : output_names = self . _output_names else : dummy_forward = create_dummy_forward_fn ( self . _model . input_infos ) retval = dummy_forward ( self . _model ) output_names = generate_output_names_list ( count_tensors ( retval ) ) torch . onnx . export ( model , tuple ( input_tensor_list ) , save_path , input_names = input_names , output_names = output_names , enable_onnx_checker = False , opset_version = opset_version , training = True ) model . enable_dynamic_graph_building ( ) model . forward = original_forward model . to ( original_device )
Exports the compressed model to the ONNX format.
Exports the compressed model to the ONNX format .
"""
        Exports the compressed model to the ONNX format.

        :param save_path: The path where the model will be saved.
        """ # pylint:disable=unexpected-keyword-arg # Should call this, otherwise the operations executed during export will end up in the graph. # Will have to run a dummy forward call in order to determine the number of outputs. # Do not fuse Conv+BN in ONNX. May cause dropout elements to appear in ONNX.
self
save_path
str
The path where the model will be saved.
The path where the model will be saved .
opset_version
int