Repository: pytorch
Issue: Could not guard on data-dependent expression
Labels: module: nn,triaged,oncall: pt2,module: dynamic shapes,oncall: export

@anijain2305  I tried this call (see complete relevant code below)
```
pre_autograd_aten_dialect = torch.export.export(model, args=(x , x_dict, device), strict=True)
```
removing the ```device``` parameter as I do not use it (one of the changes @angelayi had me make a few months ago).

Execution failed again. The error is very different (at least to my uneducated eye). 

You can find the relevant portion of the code, and the complete error log below.

If you have time, please take a look and let me know what you think.

Thanks

CODE
=====
```
        x, y, x_dict = send_to_device(input_data, device, config)

        pytree.register_pytree_node(edict,
                                    flatten_fn=_dict_flatten,
                                    unflatten_fn=_dict_unflatten,
                                    serialized_type_name="EasyDict",
                                    flatten_with_keys_fn=_dict_flatten_with_keys
                                    )

        pre_autograd_aten_dialect = torch.export.export(model, args=(x, x_dict), strict=False)

        aten_dialect: ExportedProgram = export(pre_autograd_aten_dialect, (x, x_dict), strict=False)
        edge_program: EdgeProgramManager = to_edge(aten_dialect)
        to_be_lowered_module = edge_program.exported_program()

        from executorch.exir.backend.backend_api import LoweredBackendModule, to_backend

        lowered_module = edge_program.to_backend(XnnpackPartitioner())

        print(" - train_minimum - Lowering the Whole Module - lowered_module - ", lowered_module)

        save_path = save_path = "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPrediction/loweredModels/tpt_delegate.pte"
        with open(save_path, "wb") as f:
            f.write(lowered_module.to_executorch().buffer)
  
```

ERROR LOG
=========
```
I0827 16:24:51.507875 140126416844096 torch/fx/experimental/symbolic_shapes.py:3317] create_unbacked_symbool u0 [0, 1] (_subclasses/fake_impls.py:381 in local_scalar_dense)
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] failed while running evaluate_expr(*(Eq(u0, 1), None), **{'fx_node': None})
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] Traceback (most recent call last):
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]   File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/experimental/recording.py", line 245, in wrapper
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]     return fn(*args, **kwargs)
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]   File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 5205, in evaluate_expr
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]     raise self._make_data_dependent_error(
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not guard on data-dependent expression Eq(u0, 1) (unhinted: Eq(u0, 1)).  (Size-like symbols: none)
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] 
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] Potential framework code culprit (scroll up for full backtrace):
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]   File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/transformer.py", line 971, in _detect_is_causal_mask
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281]     make_causal = bool((mask == causal_comparison).all())
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] 
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] For more information, run with TORCH_LOGS="dynamic"
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u0"
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] 
E0827 16:24:51.554366 140126416844096 torch/fx/experimental/recording.py:281] For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
Traceback (most recent call last):
  File "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/main.py", line 68, in <module>
    res_single = single_run(train_loader, val_loader, test_loader, config, device, log_dir)
  File "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/main.py", line 23, in single_run
    model, perf = get_trainedNets(config, model, train_loader, val_loader, device, log_dir)
  File "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/utils/utils.py", line 47, in get_trainedNets
    best_model, performance = trainNet(config, model, train_loader, val_loader, device, log_dir=log_dir)
  File "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/utils/train.py", line 418, in trainNet
    pre_autograd_aten_dialect = torch.export.export(model, args=(x, x_dict), strict=False)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/__init__.py", line 174, in export
    return _export(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 945, in wrapper
    raise e
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 928, in wrapper
    ep = fn(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/exported_program.py", line 89, in wrapper
    return fn(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 1455, in _export
    aten_export_artifact = export_func(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 1317, in _non_strict_export
    aten_export_artifact = _export_to_aten_ir(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 583, in _export_to_aten_ir
    gm, graph_signature = transform(aot_export_module)(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 1268, in _aot_export_non_strict
    gm, sig = aot_export(wrapped_mod, args, kwargs=kwargs, **flags)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1131, in aot_export_module
    fx_g, metadata, in_spec, out_spec = _aot_export_function(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 1350, in _aot_export_function
    fx_g, meta = create_aot_dispatcher_function(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_dynamo/utils.py", line 231, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/aot_autograd.py", line 562, in create_aot_dispatcher_function
    fw_metadata = run_functionalized_fw_and_collect_metadata(
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/collect_metadata_analysis.py", line 163, in inner
    flat_f_outs = f(*flat_f_args)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/utils.py", line 178, in flat_fn
    tree_out = fn(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/_functorch/_aot_autograd/traced_function_transforms.py", line 748, in functional_call
    out = mod(*args[params_len:], **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/export/_trace.py", line 1255, in forward
    tree_out = self._export_root(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/adonnini1/Development/ContextQSourceCode/NeuralNetworks/LocationPredictionContextQ/models/MHSA.py", line 48, in forward
    out = self.encoder(emb, mask=src_mask, src_key_padding_mask=src_padding_mask)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/transformer.py", line 413, in forward
    is_causal = _detect_is_causal_mask(mask, is_causal, seq_len)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/transformer.py", line 971, in _detect_is_causal_mask
    make_causal = bool((mask == causal_comparison).all())
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/experimental/sym_node.py", line 414, in guard_bool
    r = self.shape_env.evaluate_expr(self.expr, self.hint, fx_node=self.fx_node)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/experimental/recording.py", line 245, in wrapper
    return fn(*args, **kwargs)
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/fx/experimental/symbolic_shapes.py", line 5205, in evaluate_expr
    raise self._make_data_dependent_error(
torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not guard on data-dependent expression Eq(u0, 1) (unhinted: Eq(u0, 1)).  (Size-like symbols: none)

Potential framework code culprit (scroll up for full backtrace):
  File "/home/adonnini1/anaconda3/envs/executorch/lib/python3.10/site-packages/torch/nn/modules/transformer.py", line 971, in _detect_is_causal_mask
    make_causal = bool((mask == causal_comparison).all())

```

_Originally posted by @adonnini in https://github.com/pytorch/pytorch/issues/120219#issuecomment-2313454912_
            

cc @albanD @mruberry @jbschlosser @walterddr @mikaylagawarecki @chauhang @penguinwu @ezyang @bobrenjc93 @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4 @bhosmer @cpuhrsch @erichan1 @drisspg