180 Commits

Author SHA1 Message Date
a2f04e9841 Force multi-line messages to still get log format prefix (#104932)
This makes it easier to exclude multi-line messages using single line
grepping.  If your screen is wide enough this should not be a big
problem.

Example of what it looks like:

```
[2023-07-10 20:11:30,529] torch._dynamo.convert_frame.__guards: [DEBUG] GUARDS:
[2023-07-10 20:11:30,529] torch._dynamo.convert_frame.__guards: [DEBUG]   hasattr(L['x'], '_dynamo_dynamic_indices') == False
[2023-07-10 20:11:30,529] torch._dynamo.convert_frame.__guards: [DEBUG]   ___is_grad_enabled()
[2023-07-10 20:11:30,529] torch._dynamo.convert_frame.__guards: [DEBUG]   not ___are_deterministic_algorithms_enabled()
[2023-07-10 20:11:30,529] torch._dynamo.convert_frame.__guards: [DEBUG]   utils_device.CURRENT_DEVICE == None
```

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104932
Approved by: https://github.com/mlazos, https://github.com/albanD
2023-07-11 20:00:52 +00:00
515e3f2bb9 Add [rankN]: to log messages when distributed is initialized (#104929)
Doing it in the formatter is kind of naughty but I stared a while at logging.setLogRecordFactory for a bit, and then decided it was a bit too global for a library to use well.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104929
Approved by: https://github.com/mlazos, https://github.com/Skylion007
2023-07-11 20:00:52 +00:00
dbb69f78fe Add assert + test for artifact log booleans (#104907)
Fixes https://github.com/pytorch/pytorch/issues/104885

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104907
Approved by: https://github.com/ezyang
2023-07-11 01:59:23 +00:00
8c13e96be2 [dynamo] add logging artifact for traced graph tensor sizes (#104672)
Log tensor size information with the `graph_sizes` logging artifact, as part of the model x-ray feature requests. Typically can be combined with `graph_code`.

Sample:
```python
import torch

def fn(a, b, c, d):
    return (a + b) @ (c + d)

opt_fn = torch.compile(fn, backend="eager", dynamic=False)
opt_fn(torch.randn(10, 20), torch.randn(1, 20), torch.randn(20, 15), torch.randn(1, 15))
opt_fn(torch.randn(5, 2), torch.randn(1, 2), torch.randn(2, 4), torch.randn(1, 4))
```

Output:
```shell
$ TORCH_LOGS="graph_sizes,graph_code" python playground8.py
[2023-07-06 01:42:39,093] torch._dynamo.output_graph.__graph_code: [DEBUG] TRACED GRAPH
 ===== __compiled_fn_0 =====
 <eval_with_key>.0 class GraphModule(torch.nn.Module):
    def forward(self, L_a_ : torch.Tensor, L_b_ : torch.Tensor, L_c_ : torch.Tensor, L_d_ : torch.Tensor):
        l_a_ = L_a_
        l_b_ = L_b_
        l_c_ = L_c_
        l_d_ = L_d_

        # File: playground8.py:66, code: return (a + b) @ (c + d)
        add = l_a_ + l_b_;  l_a_ = l_b_ = None
        add_1 = l_c_ + l_d_;  l_c_ = l_d_ = None
        matmul = add @ add_1;  add = add_1 = None
        return (matmul,)

[2023-07-06 01:42:39,093] torch._dynamo.output_graph.__graph_sizes: [DEBUG] TRACED GRAPH TENSOR SIZES
===== __compiled_fn_0 =====
l_a_: (10, 20)
l_b_: (1, 20)
l_c_: (20, 15)
l_d_: (1, 15)
add: (10, 20)
add_1: (20, 15)
matmul: (10, 15)

[2023-07-06 01:42:39,198] torch._dynamo.output_graph.__graph_code: [DEBUG] TRACED GRAPH
 ===== __compiled_fn_1 =====
 <eval_with_key>.1 class GraphModule(torch.nn.Module):
    def forward(self, s0 : torch.SymInt, s1 : torch.SymInt, L_a_ : torch.Tensor, L_b_ : torch.Tensor, s4 : torch.SymInt, L_c_ : torch.Tensor, L_d_ : torch.Tensor):
        l_a_ = L_a_
        l_b_ = L_b_
        l_c_ = L_c_
        l_d_ = L_d_

        # File: playground8.py:66, code: return (a + b) @ (c + d)
        add = l_a_ + l_b_;  l_a_ = l_b_ = None
        add_1 = l_c_ + l_d_;  l_c_ = l_d_ = None
        matmul = add @ add_1;  add = add_1 = None
        return (matmul,)

[2023-07-06 01:42:39,198] torch._dynamo.output_graph.__graph_sizes: [DEBUG] TRACED GRAPH TENSOR SIZES
===== __compiled_fn_1 =====
l_a_: (s0, s1)
l_a_ (concrete): (5, 2)
l_b_: (1, s1)
l_b_ (concrete): (1, 2)
l_c_: (s1, s4)
l_c_ (concrete): (2, 4)
l_d_: (1, s4)
l_d_ (concrete): (1, 4)
add: (s0, s1)
add (concrete): (5, 2)
add_1: (s1, s4)
add_1 (concrete): (2, 4)
matmul: (s0, s4)
matmul (concrete): (5, 4)
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104672
Approved by: https://github.com/ezyang
2023-07-06 21:44:05 +00:00
c7c9aa797f [dynamo] New logging artifacts for source code attribution (#104013)
Prototype for the feature request:
>When working on a codebase that is unfamiliar to you, it can be helpful to single step through all of the code to see what is getting executed, what conditional branches are taken, and where indirect function jumps go. Model x-ray uses dynamo to give you a single step log of every source code line that does something relevant (i.e., a Tensor operation)

Dynamo logs to the ~`starts_line`~ `trace_source` logging artifact at the start of tracing new bytecode with a new line. It logs the line of source code associated with that bytecode.

~~Dynamo logs to the `graph_source` logging when a FX GraphModule is constructed. For each node in the graph, it logs the location of the original source code associated with that node.~~

Development notes: https://docs.google.com/document/d/1LjFeHzCgDDt535QUq5HydcQs56d7jWl5RvW8TLZN19g/edit?usp=sharing

Since the draft, we removed the `graph_source` logging artifact since printing the code of `GraphModule`s already displays the original source.

Sample:

```python
import torch
from functorch.experimental.control_flow import cond

def true_fn(x):
    return x * 2

def false_fn(x):
    return x * 3

def f_cond(pred, x):
    return cond(pred, true_fn, false_fn, [x])

def f_outer(pred, x):
    y = f_cond(pred, x)
    if x.sum() > 0:
        x = x * 2
    else:
        x = x * 3
    return x, y

opt_f_cond = torch.compile(f_outer, backend="eager")
opt_f_cond(torch.tensor(True), torch.randn(3, 3))
```

Logs:
```shell
$ TORCH_LOGS="trace_source" python playground8.py
TRACE starts_line f_outer playground8.py:54
    def f_outer(pred, x):
TRACE starts_line f_outer playground8.py:55
        y = f_cond(pred, x)
TRACE starts_line f_cond playground8.py:51 (inline depth: 1)
    def f_cond(pred, x):
TRACE starts_line f_cond playground8.py:52 (inline depth: 1)
        return cond(pred, true_fn, false_fn, [x])
TRACE starts_line true_fn playground8.py:45 (inline depth: 2)
    def true_fn(x):
TRACE starts_line true_fn playground8.py:46 (inline depth: 2)
        return x * 2
TRACE starts_line false_fn playground8.py:48 (inline depth: 2)
    def false_fn(x):
TRACE starts_line false_fn playground8.py:49 (inline depth: 2)
        return x * 3
TRACE starts_line f_outer playground8.py:56
        if x.sum() > 0:
TRACE starts_line <resume in f_outer> playground8.py:56
        if x.sum() > 0:
TRACE starts_line <resume in f_outer> playground8.py:57
            x = x * 2
TRACE starts_line <resume in f_outer> playground8.py:60
        return x, y
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/104013
Approved by: https://github.com/ezyang
2023-07-06 21:43:55 +00:00
d901dd94cb [logging] add custom format option to logging artifacts (#104443)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/104443
Approved by: https://github.com/mlazos
2023-06-30 19:54:14 +00:00
6c6c897d6b Add graph break logging option instead of config flag (#103202)
Make graph break logging a logging option vs a config setting

Pull Request resolved: https://github.com/pytorch/pytorch/pull/103202
Approved by: https://github.com/yanboliang, https://github.com/anijain2305
2023-06-12 19:52:31 +00:00
a8549357d2 Add distributed category to TORCH_LOGS (#103351)
Fix use of torch distributed testing assertLogs
Pull Request resolved: https://github.com/pytorch/pytorch/pull/103351
Approved by: https://github.com/wanchaol
2023-06-10 02:21:36 +00:00
90110b0e4f Revert "Add distributed category to TORCH_LOGS (#103287)"
This reverts commit 0b252aebb2e46c0dc5585ec6d296832a308f563b.

Reverted https://github.com/pytorch/pytorch/pull/103287 on behalf of https://github.com/ZainRizvi due to Breaks trunk ([comment](https://github.com/pytorch/pytorch/pull/103287#issuecomment-1585161976))
2023-06-09 21:51:25 +00:00
0b252aebb2 Add distributed category to TORCH_LOGS (#103287)
This lets users run `TORCH_LOGS="+distributed" python myscript.py` and enable additional logging output for the distributed module.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/103287
Approved by: https://github.com/ezyang
2023-06-09 19:25:07 +00:00
5ee46afc05 perf hint logging in inductor (#102250)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/102250
Approved by: https://github.com/Skylion007, https://github.com/shunting314, https://github.com/jansel
2023-05-27 03:43:30 +00:00
3318a832b3 Tighten FakeTensor reentrancy asserts, add debugging (#102091)
When investigating failures in https://github.com/pytorch/pytorch/pull/100017 I realized that we were reentering FakeTensorMode even though there was already one on the stack. Although we have attempted assert for these cases in the past, e.g., as in https://github.com/pytorch/pytorch/pull/97186 it seems that the existing protections were insufficient.

In this particular case, the reapplication of FakeTensorMode was due to an interaction with NotImplemented multiple dispatch handling. If proxy tensor mode detects an unrecognized tensor type (this includes FakeTensor, if it is not tracked with a proxy), it will return NotImplemented to give this tensor a chance to unpack itself into proxyable operation. However, this is never the right thing for FakeTensor, where no unpacking is possible. However, today, FakeTensor attempts to reapply the FakeTensorMode, resulting in FakeTensorMode being twice on the stack.

This PR does a number of things:

* It adds an assert in `FakeTensorMode.__torch_dispatch__` that you must not already have this mode on the stack, this is ALWAYS an error
* It modifies `FakeTensor.__torch_dispatch__` to return `NotImplemented` if the mode is already active. This prevents us from readding the mode on the stack
* It adds a new logging artifact `not_implemented` which you can use to get debug logs about all of the times a `__torch_dispatch__` handler returned NotImplemented and why it did so. Your subclass has to manually opt into this logging, but I inserted the necessary logs for ProxyTensorMode and FakeTensor(Mode)
* `with fake_mode` now no-ops if the fake mode is already on the stack, which is what users want anyway
* I am BREAKING pre-autograd tracing, because it is currently doing something weird with the original C++ mode stack. Brian is going to follow up with a fix next week.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/102091
Approved by: https://github.com/thiagocrepaldi, https://github.com/eellison, https://github.com/wanchaol, https://github.com/bdhirsh
2023-05-24 05:37:51 +00:00
e7a6818e97 Register top level logger for torch (#102090)
This enables use of artifact logging in modules that aren't under
the modules that were specified here.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/102090
Approved by: https://github.com/Skylion007, https://github.com/mlazos
2023-05-23 21:24:21 +00:00
dfe484a3b3 [BE]: Bugfix functorch and some generic typing improvements (#101337)
Fixes some typing bugs found with newer versions of mypy

Pull Request resolved: https://github.com/pytorch/pytorch/pull/101337
Approved by: https://github.com/ezyang
2023-05-14 14:20:56 +00:00
f6c2859ee3 Print the path to the code with TORCH_LOGS=output_code (#99038)
This is quite useful to play around with the code once it's been generated.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/99038
Approved by: https://github.com/mlazos
2023-05-13 16:20:57 +00:00
f73973d789 Expose function to retrieve list of registered loggers (#100776)
Fixes #ISSUE_NUMBER

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100776
Approved by: https://github.com/ezyang
2023-05-06 04:22:28 +00:00
850556ed6e Add "all" option to logging (#100664)
Adds the long-promised "all" option to logging.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100664
Approved by: https://github.com/lezcano
2023-05-06 01:11:18 +00:00
c525440ba3 Logging documentation updates (#100595)
Updated the logging.rst with info about the env var.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100595
Approved by: https://github.com/msaroufim, https://github.com/lezcano
2023-05-04 21:54:02 +00:00
e0bf51d3bf [dynamo] Add ddp_graphs artifact (#100021)
I want to be able to decouple DDP graph printing from the rest of
dynamo DEBUG-level logging, since frequently these logs are particularly
enlightening.

Differential Revision: [D45290919](https://our.internmc.facebook.com/intern/diff/D45290919/)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100021
Approved by: https://github.com/wconstab, https://github.com/mlazos
2023-04-27 03:53:23 +00:00
6789342a56 [dynamo] Make bytecode logging off-by-default (#100093)
A big model (like Meta's production models) can dump 100s of MBs of
bytecode, making the logs virtually unusable.  Let's only turn these on if
they're explicitly requested.

Differential Revision: [D45314055](https://our.internmc.facebook.com/intern/diff/D45314055/)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100093
Approved by: https://github.com/mlazos
2023-04-26 23:06:22 +00:00
1e8cf6ad7f Add documentation for torch._logging.set_logs (#99219)
Part of #98871

Pull Request resolved: https://github.com/pytorch/pytorch/pull/99219
Approved by: https://github.com/mlazos, https://github.com/lezcano
2023-04-24 08:06:57 +00:00
6df87b2e9b Rename sym_shapes logger to dynamic (#99335)
This matches the logging with the user facing UX dynamic=True,
rather than a new abbreviation that shows up no where else
in the codebase.

Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/99335
Approved by: https://github.com/Skylion007, https://github.com/mlazos, https://github.com/voznesenskym
2023-04-18 00:45:39 +00:00
a6a90eaf28 Remove unnecessary check when logging artifacts (#99260)
Removes a check which would sometimes allow `off_by_default` artifacts to be logged if logged at a higher level.

This change will only allow artifact messages to be displayed if the artifact is enabled, regardless of level.

closes #99144
Pull Request resolved: https://github.com/pytorch/pytorch/pull/99260
Approved by: https://github.com/lezcano
2023-04-17 20:47:25 +00:00
3a400a5adc Enable passing a dict of module names: log level to set_logs python api (#98989)
Adds "module" kwarg to set_logs to allow a user to pass a dict of module qualified names to log level to the API.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/98989
Approved by: https://github.com/ezyang
2023-04-13 09:42:32 +00:00
f55e72c0f6 Add option to log recomps (#98564)
Adds an option to TORCH_LOGS to log recompilations

Pull Request resolved: https://github.com/pytorch/pytorch/pull/98564
Approved by: https://github.com/ezyang, https://github.com/voznesenskym
2023-04-07 17:30:27 +00:00
c37ab85d96 Improve TORCH_LOGS settings error msg (#97264)
Lists registered loggable entities if an invalid settings string is passed via TORCH_LOGS
[before](https://gist.github.com/mlazos/91fcbc3d577f874bcb3daea44f8b41f2)
[after](https://gist.github.com/mlazos/815ea9e76aca665602228f960e0eb0d6)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/97264
Approved by: https://github.com/ezyang, https://github.com/jansel
2023-03-22 13:26:53 +00:00
e49b4d3827 Changed logging in aotautograd a little (#97289)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/97289
Approved by: https://github.com/mlazos
2023-03-22 09:33:30 +00:00
ec3894ec0a Fix typo in settings regex logging (#97245)
Signed-off-by: Edward Z. Yang <ezyang@meta.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/97245
Approved by: https://github.com/jansel, https://github.com/mlazos
2023-03-21 19:02:04 +00:00
5d3c347bf6 Make split reduction warning only emit once (#97112)
Signed-off-by: Edward Z. Yang <ezyang@meta.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/97112
Approved by: https://github.com/Skylion007
2023-03-21 14:57:31 +00:00
a1c46e5f8f component-level configurable logging for dynamo, inductor, aot (#94858)
Summary:

Adds NNC-like logging that is configured through an env var `TORCH_COMPILE_LOGS`
Examples:
`TORCH_LOGS="dynamo,guards" python script.py` - prints dynamo logs at level INFO with guards of all functions that are compiled

`TORCH_LOGS="+dynamo,guards,graph" python script.py` - prints dynamo logs at level DEBUG with guards and graphs (in tabular) format of all graphs that are compiled

[More examples with full output](https://gist.github.com/mlazos/b17f474457308ce15e88c91721ac1cce)

Implementation:
The implementation parses the log settings from the environment, finds any components (aot, dynamo, inductor) or other loggable objects (guards, graph, etc.) and generates a log_state object. This object contains all of the enabled artifacts, and a qualified log name -> level mapping. _init_logs then adds handlers to the highest level logs (the registered logs), and sets any artifact loggers to level DEBUG if the artifact is enabled.

Note: set_logs is an alternative for manipulating the log_state, but if the environment contains TORCH_LOGS, the environment settings will be prioritized.

Adding a new log:
To add a new log, a dev should add their log name to torch._logging._registrations (there are examples there already).

Adding a new artifact:
To add a new artifact, a dev should add their artifact name to torch._logging._registrations as well.
Additionally, wherever the artifact is logged, `torch._logging.getArtifactLogger(__name__, <artifact_name>)` should be used instead of the standard logging implementation.

[design doc](https://docs.google.com/document/d/1ZRfTWKa8eaPq1AxaiHrq4ASTPouzzlPiuquSBEJYwS8/edit#)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/94858
Approved by: https://github.com/ezyang
2023-03-18 04:17:31 +00:00