From 0971637c115d2a41aff08d75deca02751a24f709 Mon Sep 17 00:00:00 2001 From: Alexander Novikov <79649566+novikov-alexander@users.noreply.github.com> Date: Tue, 22 Jul 2025 16:32:45 +0000 Subject: [PATCH] Fix torch.tensor warning in ONNX symbolic_opset10 export (#158835) Fix PyTorch tensor copying warning in ONNX export ## Problem PyTorch ONNX exporter was generating a warning about incorrect tensor copying method: ``` UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor). ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/158835 Approved by: https://github.com/justinchuby --- torch/onnx/symbolic_opset10.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/torch/onnx/symbolic_opset10.py b/torch/onnx/symbolic_opset10.py index 469d7a80f77..0b8e2478ce3 100644 --- a/torch/onnx/symbolic_opset10.py +++ b/torch/onnx/symbolic_opset10.py @@ -513,7 +513,9 @@ def _slice( if is_none_value(list_or_value) and default_value is not None: list_or_value = [default_value] - if isinstance(list_or_value, (list, torch.Tensor)): + if isinstance(list_or_value, torch.Tensor): + return g.op("Constant", value_t=list_or_value.clone().detach()) + elif isinstance(list_or_value, list): return g.op("Constant", value_t=torch.tensor(list_or_value)) rank = symbolic_helper._get_tensor_rank(list_or_value)