# RuntimeError: TorchScript supports tracing only for tensors. This function cannot be traced because it uses dynamic control flow (if/else or loops) that depends on tensor data.

- **ID:** `pytorch/torchscript-unsupported-dynamic-control-flow`
- **Domain:** pytorch
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

The model contains control flow (e.g., if x.sum() > 0) that depends on tensor values, which is not supported by torch.jit.trace; torch.jit.script is required instead.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pytorch>=1.8.0 | active | — | — |

## Workarounds

1. **Replace torch.jit.trace with torch.jit.script for the entire model or the specific module with control flow. Script supports dynamic control flow.** (95% success)
   ```
   Replace torch.jit.trace with torch.jit.script for the entire model or the specific module with control flow. Script supports dynamic control flow.
   ```
2. **If you must use tracing, refactor the control flow into a separate function and use torch.jit.script for that function, then call it from the traced module.** (85% success)
   ```
   If you must use tracing, refactor the control flow into a separate function and use torch.jit.script for that function, then call it from the traced module.
   ```

## Dead Ends

- **** — Tracing captures the operations executed for given inputs; it cannot handle branches not taken. (90% fail)
- **** — Ignoring the function means it won't be compiled, leading to missing operations in the exported graph. (70% fail)
- **** — Removing control flow changes model logic; it may work but is error-prone and not always feasible. (50% fail)
