perf(torch): inline GLOBAL_STATE_TRACKER getattr in get_device()/distribution() hot paths#23188
perf(torch): inline GLOBAL_STATE_TRACKER getattr in get_device()/distribution() hot paths#23188pctablet505 wants to merge 1 commit into
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request optimizes attribute retrieval in the PyTorch backend and distribution library by directly accessing GLOBAL_STATE_TRACKER via getattr instead of using get_global_attribute. It also adds corresponding unit tests. The review feedback points out that unconditionally importing PyTorch in backend-agnostic tests will cause ImportError in non-PyTorch environments, and modifying global state without properly restoring it in finally blocks can lead to test flakiness.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #23188 +/- ##
==========================================
- Coverage 84.70% 84.06% -0.65%
==========================================
Files 465 465
Lines 69335 69335
Branches 11407 11407
==========================================
- Hits 58729 58283 -446
- Misses 7672 8123 +451
+ Partials 2934 2929 -5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
385882c to
f50cd53
Compare
f50cd53 to
aa61a10
Compare
6e133c9 to
0a6c2d1
Compare
…ribution() hot paths Part of keras-team#22561.
0a6c2d1 to
eb997bb
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request updates how global state attributes are retrieved in the PyTorch backend and distribution library by replacing calls to global_state.get_global_attribute with direct getattr lookups on global_state.GLOBAL_STATE_TRACKER. It also adds a unit test to verify that get_device() correctly reflects the active device_scope. There are no review comments, and I have no feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Closes #23273
Summary
Inlines
getattr(GLOBAL_STATE_TRACKER, name, None)at two hot-path call sites —get_device()in the PyTorch backend anddistribution()in the training loop — cutting theget_global_attributefunction-call overhead on every forward pass.Why it's safe
get_global_attributeis a one-liner wrapper; the inlined form uses the same attribute name, same default (None), sameGLOBAL_STATE_TRACKERobject, so behavior is unchanged. Public API (device_scope,set_distribution) untouched. Equivalence checked against master: forward outputs bit-identical in isolation; the one non-zero diff (1.67e-06) in the stacked run traces to float32 recompute-order noise from the dense/einsum reformulation already merged separately, not to this change —generate-32token ids match exactly in both states.Benchmark
This change is a small slice of total per-call dispatch overhead, so an isolated NOISE verdict is expected — its effect shows up in the cumulative stacked curve once combined with the rest of the series (1.21×/1.22× above). torch 2.13.0+cu130, RTX 4050 Laptop GPU.
Tests
core_test.pycoversget_device()default/scoped behavior;distribution_lib_test.py::test_scopecoversdistribution()default/set/restore.Series context
Part of the #22561 torch eager-overhead series. Merge order: #23182 → #23183 → #23184 → #23185 → #23186 → #23187 → #23188 (this PR) → #23189 → #23190. Independent follow-ups: #23198, #23199, #23200, #23201.
Contributor Agreement