Skip to content

refactor: remove redundant apply_optimizer_in_backward in sharding.py - #330

Merged
shijieliu merged 3 commits into
NVIDIA:mainfrom
ShaobinChen-AH:fix-issue-322
Mar 23, 2026
Merged

shijieliu merged 3 commits into
NVIDIA:mainfrom
ShaobinChen-AH:fix-issue-322

Conversation

@ShaobinChen-AH

@ShaobinChen-AH ShaobinChen-AH commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Description

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

Summary

Remove the redundant apply_optimizer_in_backward call in sharding.py.

ContextAs discussed in #322, both apply_optimizer_in_backward and fused_params are used to add optimizer for embeddings. However, fused_params already has the capability to handle optimizer parameters and absorbs the params that apply_optimizer_in_backward provides.

Since apply_optimizer_in_backward has higher priority than fused_params but serves the same purpose, it is redundant in this context.

Changes- Remove apply_optimizer_in_backward call from examples/commons/distributed/sharding.py (lines 236-272)- Keep fused_params as the sole optimizer configuration method

Testing- [ ] Verified no syntax errors- [ ] Ran related tests (if applicable)

Closes #322

CI

@greptile-apps

greptile-apps Bot commented Mar 19, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR removes the redundant apply_optimizer_in_backward call (and the associated sparse_optimizer_factory_and_class helper function) from examples/commons/distributed/sharding.py. As described in issue #322, both apply_optimizer_in_backward and fused_params were independently configuring the sparse optimizer for embedding parameters, making the former redundant. The fused_params dict is already passed to DynamicEmbeddingBagCollectionSharder and DynamicEmbeddingCollectionSharder, which embed optimizer settings (type, learning rate, betas, eps, etc.) directly into the FBGEMM fused kernel — and into DataParallelEmbeddingCollection for data-parallel tables. Since apply_optimizer_in_backward had higher priority over fused_params but served the same purpose, removing it allows fused_params to be the single source of truth for sparse optimizer configuration.

Key changes:

  • Removed sparse_optimizer_factory_and_class helper that built the (optimizer_cls, kwargs) tuple for the now-deleted apply_optimizer_in_backward call.
  • Removed the pre-DMP parameter loop that called apply_optimizer_in_backward on each embedding parameter.
  • Cleaned up unused imports: Type from typing, _apply_optimizer_in_backward, and Optimizer from torch.optim.optimizer.
  • The existing assert len(non_fused_sparse_params) == 0 sanity check remains intact, ensuring all sparse parameters are covered by the fused path.

Confidence Score: 5/5

  • This PR is safe to merge — it is a clean, well-scoped removal of a code path that was confirmed redundant.
  • The change is narrowly scoped to removing one function and one loop that duplicated what fused_params already does. The existing assert len(non_fused_sparse_params) == 0 guard still runs after the refactoring and will catch any regression where a sharded embedding param is not covered by the fused optimizer path. Imports are cleaned up correctly. No new logic is introduced.
  • No files require special attention.

Important Files Changed

Filename Overview
examples/commons/distributed/sharding.py Removes the redundant apply_optimizer_in_backward call and its associated helper sparse_optimizer_factory_and_class. The fused_params dict passed to the sharders already handles sparse optimizer configuration, so removing the duplicated path is correct and clean.

Sequence Diagram

sequenceDiagram
    participant Caller as make_optimizer_and_shard
    participant DMP as apply_dmp
    participant Sharder as DynamicEmb Sharders
    participant DMP_Module as DistributedModelParallel

    Caller->>DMP: call apply_dmp(model, ...)

    Note over DMP: Build fused_params dict<br/>(optimizer, lr, beta1, beta2, eps, ...)

    DMP->>DMP: Enumerate TORCHREC_TYPES modules<br/>Collect eb_configs

    Note over DMP: ❌ REMOVED (was here):<br/>apply_optimizer_in_backward(sparse_opt_cls, [param], ...)<br/>for each param in TORCHREC_TYPES modules

    DMP->>Sharder: Create DynamicEmbeddingBagCollectionSharder(fused_params=fused_params)
    DMP->>Sharder: Create DynamicEmbeddingCollectionSharder(fused_params=fused_params)

    DMP->>DMP_Module: DistributedModelParallel(module, sharders, plan, ...)
    Note over Sharder,DMP_Module: fused_params is the SOLE optimizer config path.<br/>Sharders embed optimizer settings into FBGEMM kernels.

    DMP->>DMP: Assert non_fused_sparse_params == 0<br/>(sanity check — fused path covers all params)

    DMP-->>Caller: return sharded model
Loading

Last reviewed commit: "refactor: remove red..."

@shijieliu

Copy link
Copy Markdown
Collaborator

@JacoCheung could you help review? If it's safe you can merge it.

@JacoCheung
JacoCheung self-requested a review March 20, 2026 05:43
@JacoCheung

Copy link
Copy Markdown
Collaborator

Added CI. Let's merge if ci passes. @shijieliu

@JacoCheung
JacoCheung requested a review from jiashuy March 20, 2026 05:59
@JacoCheung

Copy link
Copy Markdown
Collaborator

Could you help re-format the code? @gameofdimension .

@ShaobinChen-AH

Copy link
Copy Markdown
Contributor Author

Could you help re-format the code? @gameofdimension .

I notice the issue author hasn't responded yet. I'd be happy to re-format
the code myself to keep this PR moving forward.

Could you let me know which formatter or style guide I should follow?
I can apply the formatting and push the changes right away.

@JacoCheung

JacoCheung commented Mar 23, 2026 •

Copy link
Copy Markdown
Collaborator

Hi, I'll merge it first and format it later.

Thanks for your contribution. @gameofdimension @ShaobinChen-AH

@shijieliu
shijieliu merged commit 0f4e759 into NVIDIA:main Mar 23, 2026
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.

[QST] On adding an optimizer for sparse feature embeddings

4 participants