Skip to content

Sync 11 diffs to facebookresearch/optimizers - #280

Closed
wz337 wants to merge 1 commit into
facebookresearch:mainfrom
wz337:export-D126126133
Closed

wz337 wants to merge 1 commit into
facebookresearch:mainfrom
wz337:export-D126126133

Conversation

@wz337

@wz337 wz337 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor

Summary:
Syncs the following changes from the internal dev tree to facebookresearch/optimizers via scripts/oss_sync.sh. Everything under ai_codesign/optimizers/ is script output, with two hand edits:

  • The new CHANGELOG.md entry: the generated bullets were rewritten into the release notes below.

  • scripts/.last_oss_sync_rev: the script set it to the hash of the draft fix commit below this diff (D126125211). That hash changes on amend and on land, so the next sync could never find it. It is set instead to the last landed synced commit (D125057665). As a result, the next sync will list D126125211 again once it has landed, and that one test-only bullet should be deleted then.

  • [shampoo] pack_upper_triangular and unpack_upper_triangular, used when inverse factor matrices are stored packed, now use a single index gather instead of zero-filling a d×d matrix and doing two scatters. Results are bit-identical, and the per-step cost of packing drops. Both functions now raise ValueError on malformed input: a non-square matrix passed to pack, or a packed tensor whose length is not d*(d+1)/2 passed to unpack. In eager mode, the cached index maps grow from d*(d+1) to d*d + d*(d+1)/2 int32 elements per distinct block dimension per device. Under torch.compile nothing is cached.

  • [shampoo] Add DistributedShampoo.free_dist_buffer(), allocate_dist_buffer() and the dist_buffer_allocated property. They release and restore the all-gather communication buffer that the DDP, HSDP and HybridShard distributors otherwise hold for the whole run, so its memory can be used outside the optimizer step. Opt-in: if free_dist_buffer() is never called, the buffer stays resident as before. Both calls are rank-local and do not synchronize ranks. step(), and train() / eval() when they communicate, raise RuntimeError on a rank whose buffer is still freed; other ranks may then block in a collective until timeout. The check runs before any optimizer state or parameter changes, so restoring the buffer allows a clean retry. Views over the buffer stay valid across free and restore.

  • [shampoo] Add use_eager_init_step to DistributedShampoo (default False). With a ShampooPT2CompileConfig set, the first optimizer step runs eagerly before switching to the compiled step, for callers that use the first step to initialize lazy optimizer state. A gradient-selector change during that eager step no longer triggers the PT2 recompilation warning.

  • [shampoo] HybridShardDistributedConfig.communication_dtype is now honored for gradient communication when param_assignment_strategy is FSDPParamAssignmentStrategy.REPLICATE or ROUND_ROBIN. A dtype narrower than the parameter dtype (e.g. torch.bfloat16 for fp32 parameters) halves the bytes sent but quantizes the gradients. Parameter communication always stays at the parameter dtype. An equal-width dtype is honored. A wider dtype is ignored rather than applied, so the default torch.float32 leaves fp32 runs unchanged and never upcasts bf16 parameters. With bf16 parameters, that default now logs a warning at optimizer construction.

  • [shampoo] Stop allocating each Kronecker factor matrix twice when building preconditioner state. One extra copy of the factor matrices is no longer held for the life of the job (no numerical change).

  • [shampoo] After an optimizer checkpoint is loaded, the lossless FSDP2/HSDP2 distributors now mark their gathered full parameters stale and rebuild them from the live model at the next optimizer step, rather than inside the load hook. Model and optimizer state can therefore be restored in either order. Restoring a lossless-distributor checkpoint across train/eval modes with GPA or Schedule-Free iterate averaging now raises RuntimeError. Same-mode restores are unaffected.

  • [shampoo] Reuse the supplied process group in DDPDistributor when num_trainers_per_group equals its size, rather than creating an equivalent subgroup and an extra NCCL communicator. Smaller groups are still partitioned as before.

  • [shampoo] Fix a hang with HybridShardDistributedConfig when a rank is assigned no parameter blocks (e.g. with round-robin assignment). Optimizer-state device meshes are now created on every rank while process groups are being set up, instead of lazily by block-owning ranks only. Later optimizer instances reuse the same meshes and process groups.

  • [gpa] Add -> None return annotations to GPAAdamW.__init__, train and eval (no behavior change).

  • [shampoo] Test-only type-checking fixes: add type-checker suppression comments in test utilities, and fix mypy errors in two test files (no behavior change).

Internal revisions: D126125211, D125057665, D117586164, D123045794, D122710197, D121495947, D121698623, D121272407, D120951040, D120749948, D118385572

D124277336 is listed by the script but has no bullet: it only removed a # pyre-unsafe header, which the sync transform strips anyway, so it changes nothing in the mirror.

Differential Revision: D126126133

Summary:
Syncs the following changes from the internal dev tree to facebookresearch/optimizers via `scripts/oss_sync.sh`. Everything under `ai_codesign/optimizers/` is script output, with two hand edits:
- The new `CHANGELOG.md` entry: the generated bullets were rewritten into the release notes below.
- `scripts/.last_oss_sync_rev`: the script set it to the hash of the draft fix commit below this diff (D126125211). That hash changes on amend and on land, so the next sync could never find it. It is set instead to the last landed synced commit (D125057665). As a result, the next sync will list D126125211 again once it has landed, and that one test-only bullet should be deleted then.

- [shampoo] `pack_upper_triangular` and `unpack_upper_triangular`, used when inverse factor matrices are stored packed, now use a single index gather instead of zero-filling a `d×d` matrix and doing two scatters. Results are bit-identical, and the per-step cost of packing drops. Both functions now raise `ValueError` on malformed input: a non-square matrix passed to pack, or a packed tensor whose length is not `d*(d+1)/2` passed to unpack. In eager mode, the cached index maps grow from `d*(d+1)` to `d*d + d*(d+1)/2` int32 elements per distinct block dimension per device. Under `torch.compile` nothing is cached.
- [shampoo] Add `DistributedShampoo.free_dist_buffer()`, `allocate_dist_buffer()` and the `dist_buffer_allocated` property. They release and restore the all-gather communication buffer that the DDP, HSDP and HybridShard distributors otherwise hold for the whole run, so its memory can be used outside the optimizer step. Opt-in: if `free_dist_buffer()` is never called, the buffer stays resident as before. Both calls are rank-local and do not synchronize ranks. `step()`, and `train()` / `eval()` when they communicate, raise `RuntimeError` on a rank whose buffer is still freed; other ranks may then block in a collective until timeout. The check runs before any optimizer state or parameter changes, so restoring the buffer allows a clean retry. Views over the buffer stay valid across free and restore.
- [shampoo] Add `use_eager_init_step` to `DistributedShampoo` (default `False`). With a `ShampooPT2CompileConfig` set, the first optimizer step runs eagerly before switching to the compiled step, for callers that use the first step to initialize lazy optimizer state. A gradient-selector change during that eager step no longer triggers the PT2 recompilation warning.
- [shampoo] `HybridShardDistributedConfig.communication_dtype` is now honored for gradient communication when `param_assignment_strategy` is `FSDPParamAssignmentStrategy.REPLICATE` or `ROUND_ROBIN`. A dtype narrower than the parameter dtype (e.g. `torch.bfloat16` for fp32 parameters) halves the bytes sent but quantizes the gradients. Parameter communication always stays at the parameter dtype. An equal-width dtype is honored. A wider dtype is ignored rather than applied, so the default `torch.float32` leaves fp32 runs unchanged and never upcasts bf16 parameters. With bf16 parameters, that default now logs a warning at optimizer construction.
- [shampoo] Stop allocating each Kronecker factor matrix twice when building preconditioner state. One extra copy of the factor matrices is no longer held for the life of the job (no numerical change).
- [shampoo] After an optimizer checkpoint is loaded, the lossless FSDP2/HSDP2 distributors now mark their gathered full parameters stale and rebuild them from the live model at the next optimizer step, rather than inside the load hook. Model and optimizer state can therefore be restored in either order. Restoring a lossless-distributor checkpoint across train/eval modes with GPA or Schedule-Free iterate averaging now raises `RuntimeError`. Same-mode restores are unaffected.
- [shampoo] Reuse the supplied process group in `DDPDistributor` when `num_trainers_per_group` equals its size, rather than creating an equivalent subgroup and an extra NCCL communicator. Smaller groups are still partitioned as before.
- [shampoo] Fix a hang with `HybridShardDistributedConfig` when a rank is assigned no parameter blocks (e.g. with round-robin assignment). Optimizer-state device meshes are now created on every rank while process groups are being set up, instead of lazily by block-owning ranks only. Later optimizer instances reuse the same meshes and process groups.
- [gpa] Add `-> None` return annotations to `GPAAdamW.__init__`, `train` and `eval` (no behavior change).
- [shampoo] Test-only type-checking fixes: add type-checker suppression comments in test utilities, and fix mypy errors in two test files (no behavior change).

Internal revisions: D126125211, D125057665, D117586164, D123045794, D122710197, D121495947, D121698623, D121272407, D120951040, D120749948, D118385572

D124277336 is listed by the script but has no bullet: it only removed a `# pyre-unsafe` header, which the sync transform strips anyway, so it changes nothing in the mirror.

Differential Revision: D126126133
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 19, 2026
@meta-codesync

meta-codesync Bot commented Oct 19, 2026

Copy link
Copy Markdown

@wz337 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D126126133.

@meta-codesync

meta-codesync Bot commented Oct 21, 2026

Copy link
Copy Markdown

This pull request has been merged in 0ac3bd7.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant