-
Notifications
You must be signed in to change notification settings - Fork 21.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[guards][cpp-guards] Optimize NN module getattr guards #124522
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/124522
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (1 Unrelated Failure)As of commit 4af936f with merge base 3b5f6b1 (): FLAKY - The following job failed but was likely due to flakiness present on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
ghstack-source-id: 4589e8da5eca9343d7da392652e04cf92edd084e Pull Request resolved: #124522
cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
ghstack-source-id: 867b3748f8bc63c966f084ea68a03b2e505f8619 Pull Request resolved: #124522
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 34000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
ghstack-source-id: 814798e76bdefd38ca37aa15ab565f464fee4cee Pull Request resolved: #124522
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 34000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 34000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 34000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 38000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 38000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
ghstack-source-id: 7b52ca98577226915b946408da6659061c99bc11 Pull Request resolved: #124522
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
ghstack-source-id: 864bac523df4e3c9148e907420beac49b1ad2c2c Pull Request resolved: #124522
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
ghstack-source-id: 52d5675380bbd6c72a217b3c1c1c3a72599dafee Pull Request resolved: #124522
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add some more tests for this
mgr, key, source_name, base_example_value, example_value, guard_manager_enum | ||
): | ||
if isinstance(mgr, DictGuardManager): | ||
# Case where the user code relies on key order, e.g., |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there tests covering this case? (Where we change the order)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There were none for NNModuleVariableTracker. There are for UnSpecializedNNModuleVariable.
Good catch. Added and made the fixes as well.
torch/_dynamo/guards.py
Outdated
for x in inspect.getmro(base_example_value.__class__): | ||
all_class_attribute_names.update(x.__dict__.keys()) | ||
|
||
if attr_name in all_class_attribute_names: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class attributes are shadowed by module dict attributes... I think the order of these are wrong. add some tests for shadowing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right .. Thanks for pointing this out, I will fix it. But there is a corner case with submodules, where class attr take precedence. This is the example
import torch
import torch._dynamo.testing
torch._dynamo.config.guard_nn_modules = True
class Mod(torch.nn.Module):
def __init__(self):
super().__init__()
self.a = 3
def forward(self,x, c=4):
return x * c
def linear(self, x):
return x
# b is class attribute and this is a bug that jansel pointed out
# mod.b overshadows Mod.b
def b(self, x):
assert False
return x
class MyMod(Mod):
def __init__(self):
super().__init__()
# IF THIS LINE ACTUALLY CHAGES LINEAR, IT WOULD FAIL WITH SHAPE MISMATCH ERROR
self.linear = torch.nn.Linear(11, 11)
self.a = 2
self.b = 2
def forward(self, x, c=None):
return self.linear(x) * self.a * self.b
mod = MyMod()
mod(torch.ones(3, 3))
# Instance attributes overwrite the instance attributes of the parent class.
assert mod.a == 2
# ANOMALY - mod.linear is not the parameter
assert mod.linear.__code__ is Mod.linear.__code__
# jansel's valid comment - module dict attr overshadow the class attrs
# anijain2305 - This does not cause a bug because today we fallback to getattr,
# but we are losing perf opportunity here.
assert mod.b == 2
cnts = torch._dynamo.testing.CompileCounter()
opt_mod = torch.compile(mod, backend=cnts)
opt_mod(torch.ones(3, 3))
opt_mod(torch.ones(3, 3))
assert cnts.frame_count == 1
The discrepancy is for mod.linear
. Parent class has a function named linear
. When the derived class instance calls self.linear, it calls the parent class function.
This is a special case. Apart from that everything else conforms to the general rule - "class attributes are shadowed by the mod dict attributes".
I also think this is a bug in __setattr__
of nn.Module. For example, I am unable to assign a self.param
if the parent class already has a function named param
. If you think, this is just a bug, I can send a PR for nn modules.
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng chauhang [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 awgu penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 chauhang d4l3k voznesenskym EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 awgu penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 chauhang d4l3k voznesenskym EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng [ghstack-poisoned]
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units. cc mrshenli pritamdamania87 zhaojuanmao satgera rohan-varma gqchen aazzolini osalpekar jiayisuse H-Huang kwen2501 awgu penguinwu fegin XilunWu wanchaol fduwjj wz337 tianyu-l wconstab yf225 chauhang d4l3k voznesenskym EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx chenyang78 kadeng [ghstack-poisoned]
@jansel just a gentle ping in case this slipped in your notifications. |
ghstack-source-id: 5af5a978ab8b672fcfe1ee0622f48ff9d376635f Pull Request resolved: pytorch/pytorch#124522
Stack from ghstack (oldest at bottom):
Improves the guard overhead of MobileBert model with nn module guards from 92000 units to 20000 units.
cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @rohan-varma @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang @d4l3k @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @chenyang78 @kadeng