Skip to content

Commit

Permalink
[dynamo] Turn on guard_nn_modules (pytorch#125202)
Browse files Browse the repository at this point in the history
Turning on guard_nn_modules adds large number of guards, so we are bound to take a perf hit. But the perf hit is small. These are the numbers

![image](https://github.com/pytorch/pytorch/assets/13822661/c8793906-c8c7-432b-9af4-4594713067be)

First we observe that compared to Python guards, C++ guards give around 6x speedup. This reduces the total time spent in guards. This is shown in the last column (cpp_guards/inductor_optimized_latency). The worst model is around 1.61%, with most of the models below 1%. I think this is good enough signal to turn the config on.

One might also wonder how much guard slowdown occurs with `guard_nn_modules=True`. This is the table
![image](https://github.com/pytorch/pytorch/assets/13822661/932a885b-1c03-424b-8405-5bc8fd35dd39)

For most models, the guard overhead with nn module guards is under 2x. There are a few outliers, where the slowdown is really high and for those models we spend 1%-2% time in C++ guards as shown in first table.

Pull Request resolved: pytorch#125202
Approved by: https://github.com/ezyang
  • Loading branch information
anijain2305 authored and tinglvv committed May 14, 2024
1 parent 8b25673 commit 462cef4
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions torch/_dynamo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

import torch


def is_fbcode():
return not hasattr(torch.version, "git_version")


# to configure logging for dynamo, aot, and inductor
# use the following API in the torch._logging module
# torch._logging.set_logs(dynamo=<level>, aot=<level>, inductor<level>)
Expand Down Expand Up @@ -87,7 +92,7 @@
allow_ignore_mark_dynamic = False

# Set this to False to assume nn.Modules() contents are immutable (similar assumption as freezing)
guard_nn_modules = False
guard_nn_modules = False if is_fbcode() else True

# Uses CPython internal dictionary tags to detect mutation. There is some
# overlap between guard_nn_modules_using_dict_tags and guard_nn_modules flag.
Expand Down Expand Up @@ -349,10 +354,6 @@ def _get_optimize_ddp_mode():
)


def is_fbcode():
return not hasattr(torch.version, "git_version")


def default_debug_dir_root():
# [@compile_ignored: debug]
DEBUG_DIR_VAR_NAME = "TORCH_COMPILE_DEBUG_DIR"
Expand Down

0 comments on commit 462cef4

Please sign in to comment.