refactor(cp): sharder ownership - #3187
Conversation
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
57214b9 to
3197656
Compare
|
/ok to test 3197656 |
| self._padding_token_id = resolved._padding_token_id | ||
|
|
||
| @classmethod | ||
| def _from_strategy( |
There was a problem hiding this comment.
Thanks @akoumpa . We have a init and also a _from_strategy and then bind. I'm wondering whats the use case/if necessary?
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
| num_chunks=_num_chunks_value, | ||
| num_chunks=self.pp.pp_batch_size // self.pp.pp_microbatch_size if self.pp_enabled else 1, | ||
| ) | ||
| train_ctx, batch = cp_sharder.shard(batch) |
There was a problem hiding this comment.
Not super sure about using init sharder+sharder.shard(). The sharder init becomes a bit confusing. Could you please explain your consideration for replacing the function call? many thanks
There was a problem hiding this comment.
Thanks @HuiyingLi, the goal was to make ContextParallelSharder the single owner of CP strategy and mesh state, rather than keeping a procedural wrapper around it.
The object-based API has two advantages:
- The sharder can be cached on the trainer and reused, avoiding repeated strategy and mesh resolution. Only the batch-dependent
ShardLayoutis refreshed by eachshard()call. - The sharder owns the device mesh and related options, so
shard(),shard_token_tensor(), andgather_token_tensor()do not need the mesh and other runtime arguments passed on every call.
The separate .shard(batch) operation is intentional because that is where the batch is mutated and any data-dependent layout is computed and retained for subsequent token-aligned sharding or gathering.
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
|
/ok to test 47925da |
Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com>
|
/ok to test 6ccbc76 |
* refactor(distributed): simplify CP sharder API Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * refactor(distributed): group context parallel modules Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * refactor(cp): simplify THD recipe policy Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * refactor(cp): construct strategy sharders directly Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * refactor(cp): remove legacy sharder construction Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> * test(cp): update VLM sharder mocks Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> --------- Signed-off-by: Alexandros Koumparoulis <akoumparouli@nvidia.com> Signed-off-by: Alexandros Koumparoulis <153118171+akoumpa@users.noreply.github.com>
What does this PR do?
Simplifies the context-parallel forward API introduced by #2937 and makes
ContextParallelSharderthe single recipe-facing owner of CP batch preparation and token layout.The sharder now:
use_te,magi, or backend arguments;batch["qkv_format"];prepare_model_inputs_for_cpsharder when one is available;shard(batch)and returns(train_ctx, sharded_batch);nemo_automodel.components.distributed.context_parallel, with onlyContextParallelSharderexported at the package level.API
There is intentionally no separate
.context()call.shard(batch)performs the batch transformation, captures the resulting token layout, and returns the context factory required by the model forward.shard_token_tensor(...)andgather_token_tensor(...)use the mesh and layout already bound to the sharder. This keeps auxiliary token tensors aligned with round-robin SDPA, contiguous model-owned layouts, packed THD partitions, and Magi dispatch permutations without adding them to the model batch.A
DTensoris not returned because standardShard(dim)placements cannot represent all supported CP layouts, including head/tail ordering, packed THD partitions, Magi permutations, and model-owned re-padding.Changelog
prepare_cp_forwardwrapper.ContextParallelismShardertoContextParallelSharder.components/distributed/context_parallel/.Validation
138 passedacross the focused CP sharder, CP utility, differential coverage, input-embedding, and Mamba CP unit tests.142 passed, 1 skippedacross Magi and model-owned CP tests.Local recipe collection is currently blocked before reaching this code by the environment's incompatible
mlflowand systemcachetoolsinstallations.Additional Information
Related to #2861 and #2937.