Skip to content

Publicize optim functional APIs under torch.optim.functional - #194986

Closed
janeyx99 wants to merge 7 commits into
gh/janeyx99/413/basefrom
gh/janeyx99/413/head
Closed

janeyx99 wants to merge 7 commits into
gh/janeyx99/413/basefrom
gh/janeyx99/413/head

Conversation

@janeyx99

@janeyx99 janeyx99 commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

This PR, in short, does:

  • expose and publicize functional APIs (they already existed)
  • document this more uniformly
  • add big examples in optim.md for using them (and also step hooks), notably a bf16 adamw example.

Stack from ghstack (oldest at bottom):

[ghstack-poisoned]
@janeyx99
janeyx99 requested a review from albanD as a code owner August 27, 2026 04:55
@pytorch-bot

pytorch-bot Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194986

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 957e463 with merge base 2b3ecd9 (image):
💚 Looks good so far! There are no failures yet. 💚

GREEN LIGHT - PR requires human review:

This PR creates a brand-new permanent public API surface —
torch/optim/functional.py exporting 14 functions via __all__, plus "functional"
added to torch.optim.__all__ in torch/optim/__init__.py — yet the diff touches
no test file, so nothing verifies that the new namespace imports or that each
_wrap_functional wrapper forwards to the right implementation (e.g.
sparse_adam's list[int] state_steps contract vs. the singleton-tensor contract
of the other 13). It also makes project-level commitments in
docs/source/optim.md that a human should ratify: declaring which optimizers do
and do not expose a functional API ("LBFGS does not currently expose a
functional API"), and promoting the previously undocumented mixed-precision
fused CUDA path to officially supported guidance with an external attribution
("As seen in DeepSeek-V3"). The code itself looks correct and additive — the
FP32-params/BF16-moments claim matches validate_mixed_precision_dtypes in
aten/src/ATen/native/cuda/fused_adam_utils.cuh, and the underscore-aliased
imports in functional.py correctly avoid test_public_bindings failures — but a
permanent public API expansion plus policy-bearing docs with zero test coverage
warrants human review rather than auto-land.

reason: insufficient_tests
Reviewed commit: 957e463

Inference job

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@janeyx99 janeyx99 changed the title Expose, make uniform, and document functional APIs Expose, make uniform, and document optim functional APIs Aug 27, 2026
Comment thread docs/source/optim.aliases.md Resolved
[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026
Comment thread docs/source/optim.md Outdated
if not active:
return

with torch.no_grad():

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: you can use the decorator to save an indentation

Comment thread docs/source/optim.md Outdated

parameter = torch.nn.Parameter(torch.tensor(2.0))
optimizer = torch.optim.SGD([parameter], lr=0.01)
events = []

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Anything else more meaningful we can use here? Maybe just using profiler API to add a custom range around the optimizer?

Comment thread torch/optim/__init__.py Outdated
from torch.optim._adafactor import Adafactor as Adafactor
from torch.optim._muon import Muon as Muon
from torch.optim._adafactor import Adafactor as Adafactor, adafactor as adafactor
from torch.optim._muon import Muon as Muon, muon as muon

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not a fan of this and it makes things inconsistent with the rest...
I would slightly prefer adding a new torch.optim.muon module that re-exposes the content of the _muon part.
But no very strong opinion, so if you prefer this, that sounds ok as well

Comment thread torch/optim/sparse_adam.py Outdated


__all__ = ["SparseAdam"]
__all__ = ["SparseAdam", "sparse_adam"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I would expect this to fix public API binding error that would require removing from the allowlist 🤔

[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026
@janeyx99 janeyx99 changed the title Expose, make uniform, and document optim functional APIs Publicize optim functional APIs under torch.optim.functional Aug 27, 2026
[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026
[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026
[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026
[ghstack-poisoned]
janeyx99 added a commit that referenced this pull request Aug 27, 2026

@albanD albanD left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@janeyx99

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Aug 27, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: docs-build / linux-docs / build-docs-python-false

Details for Dev Infra team Raised by workflow job

@janeyx99

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

VaggelisGian added a commit to VaggelisGian/pytorch that referenced this pull request Aug 29, 2026
Three documentation errors, each verified against the implementation:

Muon: the math block documented the momentum recurrence as
B_t = mu*B_{t-1} + g_t and the nesterov mix as g_t + mu*B_t, but the
implementation uses buf.lerp_(grad, 1 - momentum) and
grad.lerp(buf, momentum), i.e. both carry a (1-mu) coefficient on the
fresh gradient that the formulas omitted. Also drop a stray character
after a \rule command that typeset a literal 's' in the rendered
formula.

Adafactor: weight_decay was documented as defaulting to 1e-2 while the
signature and every code path use 0.

LBFGS: removed the stale claim that parameters must be real; complex
parameters have been supported since view_as_real flattening was added
and are covered by test_complex_2d in test/test_optim.py.

A fourth item, the stale torch.optim.RMSProp cross-reference in the
functional rmsprop docstring, was fixed upstream by the functional API
publicization in pytorch#194986, which removed that docstring; the rebased
branch no longer touches torch/optim/rmsprop.py.

Test Plan:
python -m py_compile on the three edited files: clean
python -m ruff check on the three edited files: no new findings
Numerical check of the corrected recurrence against lerp semantics for
mu in {0, 0.5, 0.95, 1.0}: matches within float32 rounding
LBFGS with complex parameters converged to the optimum on installed
torch binaries; docs build untouched since no Example blocks changed
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…ch#194986)

This PR, in short, does:
- expose and publicize functional APIs (they already existed)
- document this more uniformly
- add big examples in optim.md for using them (and also step hooks), notably a bf16 adamw example.

Pull Request resolved: pytorch#194986
Approved by: https://github.com/albanD
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…ch#194986)

This PR, in short, does:
- expose and publicize functional APIs (they already existed)
- document this more uniformly
- add big examples in optim.md for using them (and also step hooks), notably a bf16 adamw example.

Pull Request resolved: pytorch#194986
Approved by: https://github.com/albanD
@github-actions
github-actions Bot deleted the gh/janeyx99/413/head branch November 10, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged release notes: optim

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants