Repository: Project-MONAI/MONAI
File: monai/networks/utils.py (Python)

Code before:
    """
    model.eval()
    with torch.no_grad():
        torch_versioned_kwargs = {}
        if use_trace:
            # let torch.onnx.export to trace the model.
            mode_to_export = model
            torch_versioned_kwargs = kwargs
        else:
            if not pytorch_after(1, 10):
                if "example_outputs" not in kwargs:
                    # https://github.com/pytorch/pytorch/blob/release/1.9/torch/onnx/__init__.py#L182
                    raise TypeError(
                        "example_outputs is required in scripting mode before PyTorch 1.10."
                        "Please provide example outputs or use trace mode to export onnx model."
                    )
                torch_versioned_kwargs["example_outputs"] = kwargs["example_outputs"]
                del kwargs["example_outputs"]
            mode_to_export = torch.jit.script(model, **kwargs)

        if torch.is_tensor(inputs) or isinstance(inputs, dict):
            onnx_inputs = (inputs,)
        else:
            onnx_inputs = tuple(inputs)
        if filename is None:
            temp_file = tempfile.NamedTemporaryFile(delete=False)
            f = f"{temp_file.name}/model.onnx"
        else:
            f = filename

        torch.onnx.export(
            mode_to_export,
            onnx_inputs,
            f=f,
            input_names=input_names,
            output_names=output_names,
            dynamic_axes=dynamic_axes,
            opset_version=opset_version,
            do_constant_folding=do_constant_folding,
            **torch_versioned_kwargs,
        )
        onnx_model = onnx.load(f)
        temp_file.close()
        os.remove(temp_file.name)

    if do_constant_folding and polygraphy_imported:
        from polygraphy.backend.onnx.loader import fold_constants

        fold_constants(onnx_model, size_threshold=constant_size_threshold)

    if verify:

Review comment (suggestion):
Hi @borisfom, based on your comments here: https://github.com/Project-MONAI/MONAI/issues/8149#issuecomment-2433177783
I try to create a temp file when filename is None, could you please take a look at whether the change here make senses? Thanks.

Code after:
    model.eval()
    with torch.no_grad():
        torch_versioned_kwargs = {}
        if use_trace:
            # let torch.onnx.export to trace the model.
            mode_to_export = model
            torch_versioned_kwargs = kwargs
        else:
            if not pytorch_after(1, 10):
                if "example_outputs" not in kwargs:
                    # https://github.com/pytorch/pytorch/blob/release/1.9/torch/onnx/__init__.py#L182
                    raise TypeError(
                        "example_outputs is required in scripting mode before PyTorch 1.10."
                        "Please provide example outputs or use trace mode to export onnx model."
                    )
                torch_versioned_kwargs["example_outputs"] = kwargs["example_outputs"]
                del kwargs["example_outputs"]
            mode_to_export = torch.jit.script(model, **kwargs)

        if torch.is_tensor(inputs) or isinstance(inputs, dict):
            onnx_inputs = (inputs,)
        else:
            onnx_inputs = tuple(inputs)
        temp_file = None
        if filename is None:
            temp_file = tempfile.NamedTemporaryFile()
            f = temp_file.name
        else:
            f = filename

        torch.onnx.export(
            mode_to_export,
            onnx_inputs,
            f=f,
            input_names=input_names,
            output_names=output_names,
            dynamic_axes=dynamic_axes,
            opset_version=opset_version,
            do_constant_folding=do_constant_folding,
            **torch_versioned_kwargs,
        )
        onnx_model = onnx.load(f)

    if do_constant_folding and polygraphy_imported:
        from polygraphy.backend.onnx.loader import fold_constants

        fold_constants(onnx_model, size_threshold=constant_size_threshold)

    if verify:
        if device is None:
            device = torch.device("cuda" if torch.cuda.is_available() else "cpu")