Skip to content

perf(torch): inline GLOBAL_STATE_TRACKER getattr in get_device()/distribution() hot paths#23188

Draft
pctablet505 wants to merge 1 commit into
keras-team:masterfrom
pctablet505:perf-P3-global-state
Draft

perf(torch): inline GLOBAL_STATE_TRACKER getattr in get_device()/distribution() hot paths#23188
pctablet505 wants to merge 1 commit into
keras-team:masterfrom
pctablet505:perf-P3-global-state

Conversation

@pctablet505

@pctablet505 pctablet505 commented Jun 27, 2026

Copy link
Copy Markdown
Collaborator

Closes #23273

Summary

Inlines getattr(GLOBAL_STATE_TRACKER, name, None) at two hot-path call sites — get_device() in the PyTorch backend and distribution() in the training loop — cutting the get_global_attribute function-call overhead on every forward pass.

Why it's safe

get_global_attribute is a one-liner wrapper; the inlined form uses the same attribute name, same default (None), same GLOBAL_STATE_TRACKER object, 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-32 token ids match exactly in both states.

Benchmark

PR #23188: master vs PR alone vs PR+parents, Keras torch GPU eager

workload master this PR alone + parents (#23182#23187)
CNN forward 1.21 ms 1.25 ms (~noise) 1.11 ms (~noise)
LLM forward 3.43 ms 3.52 ms (~noise) 2.82 ms (1.21×)
LLM generate-32 111.6 ms 112.7 ms (~noise) 90.3 ms (1.22×)

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.py covers get_device() default/scoped behavior; distribution_lib_test.py::test_scope covers distribution() 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

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

@google-cla

google-cla Bot commented Jun 27, 2026

Copy link
Copy Markdown

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread keras/src/backend/common/global_state_test.py Outdated
Comment thread keras/src/backend/common/global_state_test.py Outdated
Comment thread keras/src/backend/common/global_state_test.py Outdated
Comment thread keras/src/backend/common/global_state_test.py Outdated
@codecov-commenter

codecov-commenter commented Jun 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 84.06%. Comparing base (da5a4cb) to head (eb997bb).

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     
Flag Coverage Δ
keras 83.88% <100.00%> (-0.63%) ⬇️
keras-cpu 83.88% <100.00%> (ø)
keras-gpu ?
keras-jax 57.87% <100.00%> (-0.23%) ⬇️
keras-numpy 53.66% <100.00%> (ø)
keras-openvino 59.51% <100.00%> (ø)
keras-tensorflow 59.51% <100.00%> (-0.30%) ⬇️
keras-torch 58.63% <100.00%> (-0.40%) ⬇️
keras-tpu ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pctablet505
pctablet505 force-pushed the perf-P3-global-state branch 2 times, most recently from 385882c to f50cd53 Compare June 27, 2026 15:17
@pctablet505
pctablet505 force-pushed the perf-P3-global-state branch from f50cd53 to aa61a10 Compare June 30, 2026 17:11
@pctablet505
pctablet505 force-pushed the perf-P3-global-state branch 3 times, most recently from 6e133c9 to 0a6c2d1 Compare July 1, 2026 16:37
@pctablet505 pctablet505 closed this Jul 3, 2026
@pctablet505 pctablet505 reopened this Jul 3, 2026
@pctablet505

Copy link
Copy Markdown
Collaborator Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_device/distribution hit threading.local global state on every call

3 participants