executorch icon indicating copy to clipboard operation
executorch copied to clipboard

[Request Help] “torch._dynamo.exc.UserError"

Open liye0626 opened this issue 1 year ago • 14 comments

Background: I tried to integrate EAGLE-2 into ExecuTorch, but encountered some errors. Relate code:

            if i not in noleaf_index: # An error occurred at this branch
                cid = i
                depth = position_ids_list[i]
                for j in reversed(range(depth + 1)):
                    retrieve_indices[rid][j] = cid
                    cid = mask_index_list[cid - 1]
                rid += 1

Specifically, I got an error after adding a branch in model forward

  • code:
if A_Tensor: # In forward, a Boolean value or bool tensor calculated using real-time data
	...
  • error:

torch._dynamo.exc.UserError: Dynamic control flow is not supported at the moment. Please use torch.cond to explicitly capture the control flow. For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#cond-operands

I refer to the above page and add the module "CondBranchNestedFunction", encountered a new error

  • code:
from functorch.experimental.control_flow import cond
@torch.compile(dynamic=True, fullgraph=True) # dynamic setting True and False will both result in an error
class CondBranchNestedFunction(torch.nn.Module):
    @torch.compile(dynamic=True, fullgraph=True)
    def forward(self, tmpA, i):
        def true_fn(i):
            i+=1
            return None

        def false_fn(i):
            return None

        return cond(tmpA, true_fn, false_fn, [i])


self.condition_func = CondBranchNestedFunction()
self.condition_func = torch.compile(self.condition_func, mode='max-autotune')

tmpA = True # or bool Tensor.  In forward, a Boolean value or bool tensor calculated using real-time data
tmpB = torch.tensor(i)
self.condition_func(tmpA, tmpB)
  • error: "torch._dynamo.exc.UncapturedHigherOrderOpError: Cond doesn't work unless it is captured completely with torch.compile."

I have tried using torch.compile in some locations. But the error did not change. I would like to ask which direction I should go to solve the problem.

Specification are as follows:

My torch version:

torch                     2.6.0.dev20241224+cpu

torchaudio                2.6.0.dev20241224+cpu

torchvision               0.22.0.dev20241224+cpu

ExecuTorch commit: 86cb5d79e2044608fa1d04f4ba9836cab254b9ee

The changes to ExecuTorch's llama_transformer.py(link: llama_transformer.py ) are as follows:

Add the following code to line 25 (link:llama_transformer.py)

from functorch.experimental.control_flow import cond
@torch.compile(dynamic=True, fullgraph=True) # dynamic设为True和False均会出错
class CondBranchNestedFunction(torch.nn.Module):
    @torch.compile(dynamic=True, fullgraph=True)
    def forward(self, tmpA, i):
        def true_fn(i):
            i+=1
            return None

        def false_fn(i):
            return None

        return cond(tmpA, true_fn, false_fn, [i])

Add the following code to line 425 (link:llama_transformer.py)

        self.condition_func = CondBranchNestedFunction()
        self.condition_func = torch.compile(self.condition_func, mode='max-autotune')

Add the following code to line 544(link:llama_transformer.py#L544)

        for i in range(10):
            tmpA = (i == logits).int().sum() == 0
            tmpB = torch.tensor(i)
            self.condition_func(tmpA, tmpB) 
python -m examples.models.llama.export_llama \
    --model "llama3_2" \
    --checkpoint $MODEL_PATH \
    --params $PARAM_PATH \
    -kv \
    -X \
    -d bf16 \
    --output_name=$OUTPUT_NAME \
    --use_sdpa_with_kv_cache \

Then the above error will occur

torch._dynamo.exc.UncapturedHigherOrderOpError: Cond doesn't work unless it is captured completely with torch.compile. Scroll up to find out what causes the graph break.

cc @JacobSzwejbka @angelayi @mergennachin @byjlw

liye0626 avatar Mar 03 '25 03:03 liye0626

@ydwu4 , could you help on this issue since it's related to torch.cond?

iseeyuan avatar Mar 03 '25 19:03 iseeyuan

Hi @liye0626 , i'm not sure about the purpose of the loop:

        for i in range(10):
            tmpA = (i == logits).int().sum() == 0
            tmpB = torch.tensor(i)
            self.condition_func(tmpA, tmpB) 

Is it that you want to count the number of "(i == logits).int().sum() == 0"? In this case, we could do

class Cond(torch.nn.Module):
    def forward(self, tmpA, i):
        def true_fn(i):
            return torch.ones([], dtype=int64)

        def false_fn(i):
            return torch.zeros([], dtype=int64)

        return cond(tmpA, true_fn, false_fn, [i])

Then sum the outputs over.

Cond is functional meaning that it shouldn't mutate the input and shouldn't have side effects. that's why the += 1 doesn't work.

ydwu4 avatar Mar 03 '25 19:03 ydwu4

@ydwu4

The purpose of this loop is just an example, the actual scenario is similar but more complex.

Thank you very much for your help. In "true_fn" or "false_fn", we cannot modify the input value, but we can use the input value for calculation and return the result in the form of tensor. Is my understanding correct?

liye0626 avatar Mar 04 '25 00:03 liye0626

return the result in the form of tensor

yeah!

ydwu4 avatar Mar 04 '25 00:03 ydwu4

@ydwu4 I added a loop in true_fn but I got the issue: torch._dynamo.exc.UncapturedHigherOrderOpError: Cond doesn't work unless it is captured completely with torch.compile. Scroll up to find out what causes the graph break.

Does “true_fn” not support "torch.arange"? Or is there something wrong with my usage for "torch.arange"?

Related code:

...
max_depth = max_depth.item()
torch._check_is_size(max_depth)
torch._check(max_depth < 10)
torch._check(max_depth > 0)

retrieve_indice = torch.full((1, max_depth),-1, dtype=torch.long)

cid = i

depth = position_ids_list[cid].item() + 1
torch._check_is_size(depth)
torch._check(depth < 10)
torch._check(depth > 0)


for j in torch.arange(depth):  #After adding this loop will result in an issue: torch._dynamo.exc.UncapturedHigherOrderOpError: Cond doesn't work unless it is captured completely with torch.compile. Scroll up to find out what causes the graph break.
    pass

liye0626 avatar Mar 04 '25 03:03 liye0626

can you paste the full error message? Maybe we should improve the error message and print the error directly instead of "Scroll up to find out what causes the graph break."

arrange is supported in cond's body_fn like

import torch
def g(x):
    def f(x):
        for i in torch.arange(10):
            x = x + i
        return x
    return torch.cond(x.sum() > 0, f, f, (x,))

out = torch.compile(g, fullgraph=True)(torch.randn(4, 4))
print(out)

works for me.

ydwu4 avatar Mar 04 '25 18:03 ydwu4

@ydwu4 Hello!

I guess the main difference between our code is that my variable ”depth“ is a dynamic value, while "10" is a static value. If there is no loop " for j in torch.arange(depth): ", there is no issue.

Full code:

@torch.compile(dynamic=True, fullgraph=True) 
class CondBranchNestedFunction(torch.nn.Module):
    @torch.compile(dynamic=True, fullgraph=True)
    def forward(self, tmpA, i, position_ids_list, retrieve_indices, mask_index_list, max_depth):
        def true_fn(i, position_ids_list, retrieve_indices, mask_index_list, max_depth):
            
            max_depth = max_depth.item()
            torch._check_is_size(max_depth)
            torch._check(max_depth < 10)
            torch._check(max_depth > 0)
        
            retrieve_indice = torch.full((1, max_depth),-1, dtype=torch.long)
            
            cid = i 
            # torch._check_is_size(cid)
            # torch._check(cid < 70)
            # torch._check(cid > 0)

            depth = position_ids_list[cid].item() + 1
            torch._check_is_size(depth)
            torch._check(depth < 10)
            torch._check(depth > 0)


            retrieve_indice[0][0] = cid

            for j in torch.arange(depth): # Error position
                pass 
                # retrieve_indice[0][j] = cid
            
            return retrieve_indice, torch.tensor(1, dtype=torch.int64)

Full log

W0305 00:11:12.279000 6201 torch/fx/experimental/symbolic_shapes.py:6354] [0/0_2] failed during evaluate_expr(u78 + 1, hint=None, size_oblivious=False, forcing_spec=False
E0305 00:11:12.279000 6201 torch/fx/experimental/recording.py:299] [0/0_2] failed while running evaluate_expr(*(u78 + 1, None), **{'fx_node': False})
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/tensor.py", line 1200, in evaluate_expr
    return guard_scalar(self.sym_num)
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/symbolic_shapes.py", line 1242, in guard_scalar
    return guard_int(a)
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/symbolic_shapes.py", line 1434, in guard_int
    return a.node.guard_int("", 0)  # NB: uses Python backtrace
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/sym_node.py", line 492, in guard_int
    r = self.shape_env.evaluate_expr(self.expr, self.hint, fx_node=self.fx_node)
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/recording.py", line 263, in wrapper
    return retlog(fn(*args, **kwargs))
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/symbolic_shapes.py", line 6350, in evaluate_expr
    return self._evaluate_expr(
  File "/usr/local/lib/python3.10/dist-packages/torch/fx/experimental/symbolic_shapes.py", line 6540, in _evaluate_expr
    raise self._make_data_dependent_error(
torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u78 + 1 (unhinted: u78 + 1).  (Size-like symbols: none)

Caused by: for j in torch.arange(depth): # Error position  # executorch/examples/models/llama/cnets.py:84 in true_fn (_dynamo/variables/tensor.py:1200 in evaluate_expr)
For more information, run with TORCH_LOGS="dynamic"
For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u78"
If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing

User Stack (most recent call last):
  (snipped, see stack below for prefix)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/external_utils.py", line 45, in inner
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/ea_model.py", line 109, in forward
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids, logits, hidden_state, sample_token = initialize_tree(
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/utils.py", line 250, in initialize_tree
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids = model.ea_layer.topK_genrate(hidden_states, input_ids, model.base_model.output,logits_processor)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 1039, in topK_genrate
    retrieve_indice, inc_rid = self.condition_func(tmpA, tmpB, position_ids_list, retrieve_indices, tmpC, max_depth)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 95, in forward
    return cond(tmpA, true_fn, false_fn, [i, position_ids_list, retrieve_indices, mask_index_list, max_depth])
  File "/usr/local/lib/python3.10/dist-packages/torch/_higher_order_ops/cond.py", line 144, in cond
    return cond_op(pred, true_fn, false_fn, operands)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 84, in true_fn
    for j in torch.arange(depth): # Error position

For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 55, in graph_break_as_hard_error
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 884, in call_function
    (true_r, true_treespec, true_graph, true_lifted_freevars) = speculate_branch(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 868, in speculate_branch
    ) = speculate_subgraph(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 693, in speculate_subgraph
    raise ex
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 558, in speculate_subgraph
    output = f.call_function(tx, args, sub_kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1679, in GET_ITER
    self.call_function(BuiltinVariable(iter), [self.pop()], {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/builtin.py", line 1003, in call_function
    return handler(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/builtin.py", line 842, in builtin_dispatch
    rv = handler(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/builtin.py", line 771, in call_self_handler
    result = self_handler(tx, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/builtin.py", line 1316, in call_iter
    ret = self._call_iter_tuple_list(tx, obj, *args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/builtin.py", line 1297, in _call_iter_tuple_list
    list(obj.unpack_var_sequence(tx)),
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/tensor.py", line 507, in unpack_var_sequence
    length = dyn_length.evaluate_expr(tx.output)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/tensor.py", line 1202, in evaluate_expr
    raise UserError(  # noqa: B904
torch._dynamo.exc.UserError: Consider annotating your code using torch._check*(). Could not extract specialized integer from data-dependent expression u78 + 1 (unhinted: u78 + 1).  (Size-like symbols: none)

Caused by: for j in torch.arange(depth): # Error position  # executorch/examples/models/llama/cnets.py:84 in true_fn (_dynamo/variables/tensor.py:1200 in evaluate_expr)
For more information, run with TORCH_LOGS="dynamic"
For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u78"
If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing

User Stack (most recent call last):
  (snipped, see stack below for prefix)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/external_utils.py", line 45, in inner
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/ea_model.py", line 109, in forward
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids, logits, hidden_state, sample_token = initialize_tree(
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/utils.py", line 250, in initialize_tree
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids = model.ea_layer.topK_genrate(hidden_states, input_ids, model.base_model.output,logits_processor)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 1039, in topK_genrate
    retrieve_indice, inc_rid = self.condition_func(tmpA, tmpB, position_ids_list, retrieve_indices, tmpC, max_depth)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 95, in forward
    return cond(tmpA, true_fn, false_fn, [i, position_ids_list, retrieve_indices, mask_index_list, max_depth])
  File "/usr/local/lib/python3.10/dist-packages/torch/_higher_order_ops/cond.py", line 144, in cond
    return cond_op(pred, true_fn, false_fn, operands)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 84, in true_fn
    for j in torch.arange(depth): # Error position

For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1
For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#constrain-as-size-example

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/user/workspace/project/2025/executorch/examples/models/llama/export_llama.py", line 32, in <module>
    main()  # pragma: no cover
  File "/home/user/workspace/project/2025/executorch/examples/models/llama/export_llama.py", line 28, in main
    export_llama(args)
  File "/home/user/workspace/project/2025/executorch/examples/models/llama/export_llama_lib.py", line 520, in export_llama
    builder = _export_llama(args)
  File "/home/user/workspace/project/2025/executorch/examples/models/llama/export_llama_lib.py", line 655, in _export_llama
    builder_exported = _prepare_for_llama_export(args).export()
  File "/usr/local/lib/python3.10/dist-packages/executorch/extension/llm/export/builder.py", line 201, in export
    exported_module = export_for_training(
  File "/usr/local/lib/python3.10/dist-packages/torch/export/__init__.py", line 168, in export_for_training
    return _export_for_training(
  File "/usr/local/lib/python3.10/dist-packages/torch/export/_trace.py", line 1031, in wrapper
    raise e
  File "/usr/local/lib/python3.10/dist-packages/torch/export/_trace.py", line 1004, in wrapper
    ep = fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/export/exported_program.py", line 128, in wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/export/_trace.py", line 1825, in _export_for_training
    export_artifact = export_func(  # type: ignore[operator]
  File "/usr/local/lib/python3.10/dist-packages/torch/export/_trace.py", line 1283, in _strict_export_lower_to_aten_ir
    gm_torch_level = _export_to_torch_ir(
  File "/usr/local/lib/python3.10/dist-packages/torch/export/_trace.py", line 667, in _export_to_torch_ir
    gm_torch_level, _ = torch._dynamo.export(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py", line 1579, in inner
    result_traced = opt_f(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1739, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/eval_frame.py", line 576, in _fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 1404, in __call__
    return self._torchdynamo_orig_callable(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 565, in __call__
    return _compile(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 1005, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 733, in compile_inner
    return _compile_inner(code, one_graph, hooks, transform)
  File "/usr/local/lib/python3.10/dist-packages/torch/_utils_internal.py", line 95, in wrapper_function
    return function(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 768, in _compile_inner
    out_code = transform_code_object(code, transform)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/bytecode_transformation.py", line 1402, in transform_code_object
    transformations(instructions, code_options)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 236, in _fn
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/convert_frame.py", line 680, in transform
    tracer.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 2906, in run
    super().run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1763, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/nn_module.py", line 443, in call_function
    return tx.inline_user_function_return(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1763, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 437, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1763, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1685, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1685, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 437, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1763, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1685, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/lazy.py", line 171, in realize_and_forward
    return getattr(self.realize(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/nn_module.py", line 443, in call_function
    return tx.inline_user_function_return(
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1763, in CALL_FUNCTION_EX
    self.call_function(fn, argsvars.items, kwargsvars)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 437, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1685, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 319, in call_function
    return super().call_function(tx, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/functions.py", line 120, in call_function
    return tx.inline_user_function_return(self, [*self.self_args(), *args], kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 927, in inline_user_function_return
    return InliningInstructionTranslator.inline_call(self, fn, args, kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3111, in inline_call
    return tracer.inline_call_()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 3248, in inline_call_
    self.run()
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1076, in run
    while self.step():
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 986, in step
    self.dispatch_table[inst.opcode](self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 683, in wrapper
    return inner_fn(self, inst)
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 1685, in CALL_FUNCTION
    self.call_function(fn, args, {})
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/symbolic_convert.py", line 921, in call_function
    self.push(fn.call_function(self, args, kwargs))  # type: ignore[arg-type]
  File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/variables/higher_order_ops.py", line 58, in graph_break_as_hard_error
    raise UncapturedHigherOrderOpError(reason + msg) from e
torch._dynamo.exc.UncapturedHigherOrderOpError: Cond doesn't work unless it is captured completely with torch.compile. Scroll up to find out what causes the graph break.

from user code:
   File "/usr/local/lib/python3.10/dist-packages/torch/_dynamo/external_utils.py", line 45, in inner
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/ea_model.py", line 109, in forward
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids, logits, hidden_state, sample_token = initialize_tree(
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/utils.py", line 250, in initialize_tree
    draft_tokens, retrieve_indices,tree_mask,tree_position_ids = model.ea_layer.topK_genrate(hidden_states, input_ids, model.base_model.output,logits_processor)
  File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 1039, in topK_genrate
    retrieve_indice, inc_rid = self.condition_func(tmpA, tmpB, position_ids_list, retrieve_indices, tmpC, max_depth)
  File "/usr/local/lib/python3.10/dist-packages/torch/nn/modules/module.py", line 1750, in _call_impl
    return forward_call(*args, **kwargs)
  File "/usr/local/lib/python3.10/dist-packages/executorch/examples/models/llama/cnets.py", line 95, in forward
    return cond(tmpA, true_fn, false_fn, [i, position_ids_list, retrieve_indices, mask_index_list, max_depth])
  File "/usr/local/lib/python3.10/dist-packages/torch/_higher_order_ops/cond.py", line 144, in cond
    return cond_op(pred, true_fn, false_fn, operands)

Set TORCH_LOGS="+dynamo" and TORCHDYNAMO_VERBOSE=1 for more information


liye0626 avatar Mar 05 '25 00:03 liye0626

yeah, i think you're dealing with a dynamic loop case. The real error message is here

torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not extract specialized integer from data-dependent expression u78 + 1 (unhinted: u78 + 1).  (Size-like symbols: none)

Essentially, what it means is that depth is created out of some tensor value i.e. depth = position_ids_list[cid].item() + 1, dynamo is unable to unroll the loop according depth because the value of position_ids_list[cid].item() changes from iteration to iteration because it depends on the actual data.

There are two ways of supporting a dynamic loop:

  1. If the number of loop is truly data-dependent( i.e. it based on value inside tensor), can you try re-writing the loop into a while_loop? It's another functional control flow operator like cond.
  2. Somehow re-write your code in a batch manner so that we don't do fine-grained .item() then for loop.

ydwu4 avatar Mar 05 '25 00:03 ydwu4

Thanks for your help. Due to the complex code logic, the first way is more suitable for me.

I don't seem to find any information about while_loop on the exportdb page: https://pytorch.org/docs/stable/generated/exportdb/index.html

If we could add the while_loop case to this page, it will be of great help to those who encounter similar problems later.

liye0626 avatar Mar 05 '25 00:03 liye0626

Thanks for your help. Due to the complex code logic, the first way is more suitable for me.

I don't seem to find any information about while_loop on the exportdb page: https://pytorch.org/docs/stable/generated/exportdb/index.html

If we could add the while_loop case to this page, it will be of great help to those who encounter similar problems later.

@tarun292 FYI

byjlw avatar Mar 05 '25 17:03 byjlw

Thanks for your help! I have solved my issues by while_loop

liye0626 avatar Mar 06 '25 02:03 liye0626

@liye0626 are you also looking to run this model on device with the while loop? Currently although export supports while, we don't support it yet in the rest of the ExecuTorch stack.

tarun292 avatar Mar 06 '25 16:03 tarun292

@tarun292 Thanks for your reminder. Currently I have only successfully exported it, but I have not yet run it on the device.

liye0626 avatar Mar 07 '25 00:03 liye0626

Hi @liye0626, are you still facing this issue?

nil-is-all avatar Dec 19 '25 22:12 nil-is-all