Skip to content

Commit

Permalink
re-add option to log all guard check fails (#113585)
Browse files Browse the repository at this point in the history
Summary:
Followup to pytorch/pytorch#110325 - re-add the `report_all_guard_failures config` as a logging artifact `recompiles_verbose` with the following changes:
- evaluating the check must be wrapped with exception handling because subsequent code parts following the first failure may result in errors if evaluated (e.g. if a guard checks first for size, then tries to index - a guard failure due to insufficient size would result in an index error for the latter check).
- Adding a test for this case

Sample:
```python
import torch

def fn(x):
    return torch.rand(x[-1], len(x))

opt_fn = torch.compile(fn)
opt_fn([4, 5, 6])
opt_fn([7, 8])
opt_fn([9])
```

Output (with `TORCH_LOGS="recompiles_verbose"`):
```bash
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG] Recompiling function fn in /data/users/williamwen/pytorch/playground5.py:15
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     triggered by the following guard failure(s):
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     guard 0 failures:
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - len(L['x']) == 3
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - L['x'][0] == 4
[2023-11-15 16:13:26,741] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - L['x'][1] == 5
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG] Recompiling function fn in /data/users/williamwen/pytorch/playground5.py:15
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     triggered by the following guard failure(s):
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     guard 0 failures:
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - len(L['x']) == 2
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     guard 1 failures:
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - len(L['x']) == 3
[2023-11-15 16:13:26,970] torch._dynamo.guards.__recompiles_verbose: [DEBUG]     - L['x'][0] == 4
```

X-link: pytorch/pytorch#113585
Approved by: https://github.com/jon-chuang, https://github.com/ezyang

Reviewed By: huydhn

Differential Revision: D51417892

Pulled By: williamwen42

fbshipit-source-id: 2d99b70e3be500419e391067017f79d71ace6b9f
  • Loading branch information
williamwen42 authored and facebook-github-bot committed Nov 17, 2023
1 parent 361a775 commit ff42ab8
Showing 1 changed file with 1 addition and 5 deletions.
6 changes: 1 addition & 5 deletions userbenchmark/dynamo/dynamobench/_dynamo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def setup_log_file():
exitstack = contextlib.ExitStack()
if config.log_file_name is not None:
log_file_handler = logging.FileHandler(config.log_file_name)
for logger in logging.get_loggers():
for logger in torch._logging._internal.get_loggers():
logger.addHandler(log_file_handler)
exitstack.callback(lambda: logger.removeHandler(log_file_handler))
return exitstack
Expand Down Expand Up @@ -2255,10 +2255,6 @@ def get_instruction_source_311(code: types.CodeType, inst: dis.Instruction) -> s
return result


def is_guard_failure_reporting_enabled():
return torch._logging._internal.log_state.is_artifact_enabled("recompiles")


def get_static_address_type(t):
if isinstance(t, torch.Tensor):
return getattr(t, "_dynamo_static_input_type", None)
Expand Down

0 comments on commit ff42ab8

Please sign in to comment.