Skip to content

Refactor optimizer implementations and improve multi_tensor ops#36

Merged
lxd-cumt merged 2 commits into
flagos-ai:mainfrom
lihongyang1990:fix/refactor-optimizer-impl-and-multi-tensor
Mar 3, 2026
Merged

Refactor optimizer implementations and improve multi_tensor ops#36
lxd-cumt merged 2 commits into
flagos-ai:mainfrom
lihongyang1990:fix/refactor-optimizer-impl-and-multi-tensor

Conversation

@lihongyang1990

Copy link
Copy Markdown

Summary

Refactor and improve the FlagOS optimizer and multi_tensor implementations to better match CUDA behavior and improve code quality.

Changes

fused_adam.py (FlagOS backend)

  • Remove unused inv_scale and out_dtype parameters from multi_tensor_adam_fl
  • multi_tensor_adam_param_remainder_fl: rewrite FP32 master weight reconstruction using bit manipulation (int16 high/low bits), matching the CUDA implementation exactly

multi_tensor.py (FlagOS backend)

  • multi_tensor_l2_norm_fl: add proper type hints, noop_flag check, inf/nan detection, and replace raw ** / + operators with flag_gems.mul / flag_gems.add
  • multi_tensor_scale_fl: add type hints, noop_flag check, inf/nan detection, and replace src * scale with flag_gems.mul(src, scale)

optimizer.py (reference backend)

  • Update multi_tensor_l2norm_torch and multi_tensor_adam_torch to match new signatures and CUDA behavior (L2 vs AdamW mode split)
  • Rewrite multi_tensor_adam_param_remainder_torch with bit manipulation matching CUDA
  • Rename epsepsilon for consistency

optimizers/__init__.py

  • Export multi_tensor_scale and multi_tensor_l2norm

Misc

  • Fix missing newline at end of files

@lxd-cumt lxd-cumt self-requested a review February 12, 2026 08:58
@lxd-cumt

lxd-cumt commented Mar 2, 2026

Copy link
Copy Markdown
Collaborator

Please pull the latest main branch, and format code, and pass CICD tests

@CLAassistant

CLAassistant commented Mar 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@lihongyang1990 lihongyang1990 force-pushed the fix/refactor-optimizer-impl-and-multi-tensor branch 5 times, most recently from 27ba5b3 to 1ed2d2e Compare March 2, 2026 03:50
@lihongyang1990 lihongyang1990 force-pushed the fix/refactor-optimizer-impl-and-multi-tensor branch from 1ed2d2e to f0b706f Compare March 2, 2026 06:40

@lxd-cumt lxd-cumt 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.

LGTM.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Refactors the FlagOS and reference (PyTorch) implementations of multi-tensor optimizer utilities to better match CUDA semantics, including updated signatures/returns and bit-exact BF16+remainder FP32 reconstruction.

Changes:

  • Updated multi-tensor L2 norm and scale implementations (type hints, noop/inf-nan handling, CUDA-aligned return shape/signature).
  • Refactored Adam/AdamW logic to match CUDA’s mode semantics and rewrote BF16+remainder master-weight reconstruction using bit manipulation.
  • Exported multi_tensor_scale and multi_tensor_l2norm from transformer_engine/pytorch/optimizers.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
transformer_engine/pytorch/optimizers/init.py Exposes additional multi-tensor ops from transformer_engine_torch.
transformer_engine/plugin/core/backends/reference/impl/optimizer.py Aligns reference multi-tensor ops with CUDA behavior (signatures, mode semantics, bit reconstruction).
transformer_engine/plugin/core/backends/flagos/impl/multi_tensor.py Refines FlagOS multi-tensor L2 norm/scale behavior and typing, adds inf/nan detection.
transformer_engine/plugin/core/backends/flagos/impl/fused_adam.py Simplifies FlagOS Adam signatures and switches param-remainder master-weight reconstruction to bit manipulation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

chunk_size: Chunk size for processing (unused in PyTorch implementation)
noop_flag: If non-zero, skip computation
tensor_lists: [grads, params (bf16), exp_avgs (fp32), exp_avg_sqs (fp32), param_remainders (int16)]
tensor_lists: [grads, params (int16/bf16), exp_avgs (fp32), exp_avg_sqs (fp32), param_remainders (int16)]

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

The docstring says params (int16/bf16), but the implementation treats param as a BF16 tensor whose storage is bit-cast via param.view(torch.int16) and written back the same way. Advertising int16 here is likely misleading for callers (an actual int16 param tensor wouldn’t represent model weights correctly). Consider updating the docstring to state that params are BF16 (with int16 view used for bit manipulation).

Suggested change
tensor_lists: [grads, params (int16/bf16), exp_avgs (fp32), exp_avg_sqs (fp32), param_remainders (int16)]
tensor_lists: [grads, params (bf16, with int16 view used for bit manipulation), exp_avgs (fp32), exp_avg_sqs (fp32), param_remainders (int16)]

Copilot uses AI. Check for mistakes.
Comment on lines 112 to +116
g = tensor_lists[0][i]
p = tensor_lists[1][i] # BF16 parameter
p = tensor_lists[1][i] # int16 parameter (high 16 bits of FP32)
m = tensor_lists[2][i] # FP32 first moment
v = tensor_lists[3][i] # FP32 second moment
p_remainder = tensor_lists[4][i] # int16 remainder
p_remainder = tensor_lists[4][i] # int16 remainder (low 16 bits of FP32)

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

Inline comment says p is an int16 parameter, but p is later updated via flag_gems.copy_(p, new_p.view(torch.bfloat16)), implying p is actually a BF16 tensor whose underlying bits store the high 16 bits of the FP32 master weight. Please adjust the comment to avoid confusion about the expected dtype/storage of tensor_lists[1][i].

Copilot uses AI. Check for mistakes.
# See LICENSE for license information.

from typing import List, Union
from typing import List, Tuple, Union

Copilot AI Mar 2, 2026

Copy link

Choose a reason for hiding this comment

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

Union is imported but no longer used in this module after changing multi_tensor_l2norm_torch to always return a Tuple. Please remove the unused import to avoid lint/type-check noise.

Suggested change
from typing import List, Tuple, Union
from typing import List, Tuple

Copilot uses AI. Check for mistakes.
@lxd-cumt lxd-cumt merged commit 47e8ee7 into flagos-ai:main Mar 3, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants