Repository: pytorch
Issue: Torch.onnx.dynamo_export stuck at reshape
Labels: module: onnx,triaged,module: dynamo,oncall: export

## Issue description

I am trying out dynamo_export to convert my pytorch model to onnx and it seemed to be stuck at reshaping a tensor of all things.

## Code example

The model code can be found [here](https://github.com/andresprados/SPIGA/blob/main/spiga/models/spiga.py), and the portion that caused the error is in the trace stack below:
<details>
  <summary>Click to expand</summary>
  
```
/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py:130: UserWarning: torch.onnx.dynamo_export only implements opset version 18 for now. If you need to use a different opset version, please register them with register_custom_op.
warnings.warn(
/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/nn/functional.py:4358: UserWarning: Default grid_sample and affine_grid behavior has changed to align_corners=False since 1.3.0. Please specify align_corners=True if the old behavior is desired. See the documentation of grid_sample for details.
warnings.warn(
/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/nn/functional.py:4296: UserWarning: Default grid_sample and affine_grid behavior has changed to align_corners=False since 1.3.0. Please specify align_corners=True if the old behavior is desired. See the documentation of grid_sample for details.
warnings.warn(
Traceback (most recent call last):
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1433, in run_node
return getattr(args[0], node.target)(*args[1:], **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/utils/_stats.py”, line 20, in wrapper
return fn(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py”, line 1304, in torch_dispatch
return self.dispatch(func, types, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py”, line 1510, in dispatch
return decomposition_table[func](*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_refs/init.py”, line 4450, in view
return _reshape_view_helper(a, shape, allow_copy=False)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_refs/init.py”, line 3583, in reshape_view_helper
shape = utils.infer_size(shape, a.numel())
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/prims_common/init.py”, line 824, in infer_size
numel == newsize or (dim is not None and newsize > 0 and numel % newsize == 0),
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/init.py”, line 361, in bool
return self.node.bool()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 1036, in bool
return self.guard_bool(“”, 0)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 1006, in guard_bool
r = self.shape_env.evaluate_expr(self.expr, self.hint, fx_node=self.fx_node)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/recording.py”, line 246, in wrapper
return event.run(self)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/recording.py”, line 151, in run
return self.f(args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 3834, in evaluate_expr
concrete_val = self.size_hint(orig_expr)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 3633, in size_hint
raise self._make_data_dependent_error(result_expr, expr)
torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: It appears that you’re trying to get a value out of symbolic int/float whose value is data-dependent (and thus we do not know the true value.) The expression we were trying to evaluate is Eq(2i0, 98) (unhinted: Eq(2i0, 98)). Scroll up to see where each of these data-dependent accesses originally occurred.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1352, in get_fake_value
return wrap_fake_exception(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 921, in wrap_fake_exception
return fn()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1353, in
lambda: run_node(tx.output, node, args, kwargs, nnmodule)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1452, in run_node
raise RuntimeError(fn_str + str(e)).with_traceback(e.traceback) from e
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1433, in run_node
return getattr(args[0], node.target)(*args[1:], **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/utils/_stats.py”, line 20, in wrapper
return fn(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py”, line 1304, in torch_dispatch
return self.dispatch(func, types, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_subclasses/fake_tensor.py”, line 1510, in dispatch
return decomposition_table[func](args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_refs/init.py”, line 4450, in view
return _reshape_view_helper(a, shape, allow_copy=False)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_refs/init.py”, line 3583, in reshape_view_helper
shape = utils.infer_size(shape, a.numel())
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/prims_common/init.py”, line 824, in infer_size
numel == newsize or (dim is not None and newsize > 0 and numel % newsize == 0),
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/init.py”, line 361, in bool
return self.node.bool()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 1036, in bool
return self.guard_bool(“”, 0)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 1006, in guard_bool
r = self.shape_env.evaluate_expr(self.expr, self.hint, fx_node=self.fx_node)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/recording.py”, line 246, in wrapper
return event.run(self)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/recording.py”, line 151, in run
return self.f(args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 3834, in evaluate_expr
concrete_val = self.size_hint(orig_expr)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py”, line 3633, in size_hint
raise self._make_data_dependent_error(result_expr, expr)
RuntimeError: Failed running call_method reshape((FakeTensor(…, device=‘cuda:0’, size=(1, i0, 2), grad_fn=), 1, 98, -1), **{}):
It appears that you’re trying to get a value out of symbolic int/float whose value is data-dependent (and thus we do not know the true value.) The expression we were trying to evaluate is Eq(2i0, 98) (unhinted: Eq(2i0, 98)). Scroll up to see where each of these data-dependent accesses originally occurred.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py”, line 1195, in dynamo_export
).export()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py”, line 941, in export
graph_module = self.options.fx_tracer.generate_fx(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/fx/dynamo_graph_extractor.py”, line 199, in generate_fx
graph_module, graph_guard = torch._dynamo.export(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py”, line 1216, in inner
result_traced = opt_f(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py”, line 406, in _fn
return fn(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/fx/dynamo_graph_extractor.py”, line 154, in wrapped
return output_adapter.apply(model_func(*args, **kwargs))
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/eval_frame.py”, line 554, in catch_errors
return callback(frame, cache_entry, hooks, frame_state)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py”, line 140, in _fn
return fn(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py”, line 380, in _convert_frame_assert
return _compile(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py”, line 559, in _compile
guarded_code = compile_inner(code, one_graph, hooks, transform)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 190, in time_wrapper
r = func(*args, **kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py”, line 481, in compile_inner
out_code = transform_code_object(code, transform)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/bytecode_transformation.py”, line 1028, in transform_code_object
transformations(instructions, code_options)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/convert_frame.py”, line 451, in transform
tracer.run()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 2094, in run
super().run()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 739, in run
and self.step()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 702, in step
getattr(self, inst.opname)(inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 403, in wrapper
return inner_fn(self, inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 1135, in CALL_FUNCTION
self.call_function(fn, args, {})
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 576, in call_function
self.push(fn.call_function(self, args, kwargs))
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 307, in call_function
return super().call_function(tx, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 261, in call_function
return super().call_function(tx, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 90, in call_function
return tx.inline_user_function_return(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 612, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/dynamo/symbolic_convert.py”, line 2221, in inline_call
return cls.inline_call(parent, func, args, kwargs)
File "/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/dynamo/symbolic_convert.py", line 2343, in inline_call
tracer.run()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 739, in run
and self.step()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 702, in step
getattr(self, inst.opname)(inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 403, in wrapper
return inner_fn(self, inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 1135, in CALL_FUNCTION
self.call_function(fn, args, {})
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 576, in call_function
self.push(fn.call_function(self, args, kwargs))
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 307, in call_function
return super().call_function(tx, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 261, in call_function
return super().call_function(tx, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/functions.py”, line 90, in call_function
return tx.inline_user_function_return(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 612, in inline_user_function_return
result = InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/dynamo/symbolic_convert.py”, line 2221, in inline_call
return cls.inline_call(parent, func, args, kwargs)
File "/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/dynamo/symbolic_convert.py", line 2343, in inline_call
tracer.run()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 739, in run
and self.step()
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 702, in step
getattr(self, inst.opname)(inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 403, in wrapper
return inner_fn(self, inst)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 1135, in CALL_FUNCTION
self.call_function(fn, args, {})
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/symbolic_convert.py”, line 576, in call_function
self.push(fn.call_function(self, args, kwargs))
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/misc.py”, line 617, in call_function
return self.obj.call_method(tx, self.name, args, kwargs).add_options(self)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/tensor.py”, line 693, in call_method
return wrap_fx_proxy(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/builder.py”, line 1289, in wrap_fx_proxy
return wrap_fx_proxy_cls(
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/variables/builder.py”, line 1376, in wrap_fx_proxy_cls
example_value = get_fake_value(proxy.node, tx)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/_dynamo/utils.py”, line 1381, in get_fake_value
raise UserError(
torch._dynamo.exc.UserError: Tried to use data-dependent value in the subsequent computation. This can happen when we encounter unbounded dynamic value that is unknown during tracing time.You will need to explicitly give hint to the compiler. Please take a look at constrain_as_value OR constrain_as_size APIs

from user code:
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/models/spiga.py”, line 82, in forward
embedded_ft = self.extract_embedded(pts_proj, visual_field, step)
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/models/spiga.py”, line 124, in extract_embedded
shape_ft = self.calculate_distances(pts_proj)
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/models/spiga.py”, line 163, in calculate_distances
dist_wo_self = dist[:, self.diagonal_mask, :].reshape(B, L, -1)

dist: tensor([[[-0.0072, -0.0387],
         [-0.0144, -0.0772],
         [-0.0311, -0.1161],
         ...,
         [ 0.1014, -0.2628],
         [ 0.1312, -0.2631],
         [ 0.2310, -0.0109]]], device='cuda:0', grad_fn=<IndexBackward0>)
shape: (1, 98)
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “/home/evas/Downloads/SPIGA/SPIGA/app_CH.py”, line 74, in
tracked_obj = processor.process_frame(frame, [faces[0]])
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/demo/analyze/extract/spiga_processor.py”, line 35, in process_frame
features = self.processor.inference(frame, bboxes)
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/inference/framework.py”, line 70, in inference
outputs = self.net_forward(batch_crops)
File “/home/evas/Downloads/SPIGA/SPIGA/spiga/inference/framework.py”, line 98, in net_forward
torch.onnx.dynamo_export(self.model, inputs).save(“wflw.onnx”)
File “/home/evas/anaconda3/envs/CH_test/lib/python3.10/site-packages/torch/onnx/_internal/exporter.py”, line 1206, in dynamo_export
raise OnnxExporterError(
torch.onnx.OnnxExporterError: Failed to export the model to ONNX. Generating SARIF report at ‘report_dynamo_export.sarif’. SARIF is a standard format for the output of static analysis tools. SARIF logs can be loaded in VS Code SARIF viewer extension, or SARIF web viewer ([Sarif Viewer](https://microsoft.github.io/sarif-web-component/)). Please report a bug on PyTorch Github: [Issues · pytorch/pytorch · GitHub](https://github.com/pytorch/pytorch/issues)
> 
```

</details>  
  
OS: Ubuntu 20.04
| package  | Version |
| ------------- | ------------- |
| torch | 2.1.0.dev20230830+cu118  |
| torchaudio  | 2.1.0.dev20230830+cu118  |
|torchmetrics              |0.11.4|
|torchvision               |0.16.0.dev20230830+cu118        |
|onnx                      |1.14.0                   |
|onnxruntime-gpu           |1.15.1            |
|onnxscript-preview        |0.1.0.dev20230905          |



cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @Xia-Weiwen @wenzhe-nrv @jiayisunx @chenyang78 @aakhundov @kadeng @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan