Repository navigation
[spmd_types] spmd infra - #3253
Conversation
|
|
||
| Populated once during ``ParallelDims.build_mesh()`` when | ||
| ``full_spmd_types=True``. Provides forward-time access to mesh axes | ||
| and process groups. |
There was a problem hiding this comment.
Is this because torchtitan doesn't have a concept of a global mesh? I think @tianyu-l has stated he's amenable to adding this idea to torchtitan? But what I hear from RL land is that it is annoying if the mesh is global as it makes it difficult to have multiple meshes coexisting together in weird RL setup. Why isn't spmd type's concept of current mesh enough?
There was a problem hiding this comment.
ParallelDims is required for Module. We cannot kill it without killing the existing workflow. We can switch to MeshAxes and ditch DeviceMesh once we kill the legacy path. But for now, we have to keep DeviceMesh alive.
There was a problem hiding this comment.
Concretely, I would like torchtitan to keep DeviceMesh. Perhaps I just don't understand the lifecycle of DeviceMesh in torchtitan today.
| # Value is either a DTensor Placement (Shard, Replicate, Partial) or an | ||
| # spmd_types type (R, I, V, P, S(dim)). parallelize() duck-types on the | ||
| # value to dispatch DTensor vs SPMD paths. | ||
| NamedPlacement = dict[MeshAxisName, Placement | spmd.PerMeshAxisSpmdType] |
There was a problem hiding this comment.
So, what exactly is the checkpointing plan? I know we talked about it on Friday and it has escaped my memory again. (I am wondering about it here because I'm not sure if this is going to work here, depending on where NamedPlacement is used in torchtitan?)
There was a problem hiding this comment.
NamedPlacement is used for TorchTitan config system only to support both DTensor and spmd_types. Shouldn't be a checkpointing issue.
There was a problem hiding this comment.
@ezyang One way is to let state_dict create a DTensor on the full storage mesh, so that DCP can communicate with. The pro is that it will work regardless of fully_shard applied or not, e.g. not applied in RL's generator.
There was a problem hiding this comment.
Ah yes, I remember this constraint. So if we have DTensor on all the at rest tensors (non compute), then post FSDP we have a DTensor that we need to turn into a plain tensor and put spmd types annotation on. So it's mostly about making sure we do the TP type translation correctly here (since DTensor doesn't specify if it was expecting to handle the TP all-reduce as part of the DP all-reduce or not).
Adding this to allow both spmd types specs, and DTensor-style placements. We're adding something like D107576760 in spmd_types, but in the meantime, because non-llama3 models are still in DTensor placement form, I think we have 2 options: 1) keep this as a hybrid spmd/DTensor container (e.g. moe_sharding.py calls `dense_activation_placement(tp=Replicate())`, and migrate to SpmdLayout once all models are off full_dtensor. 2) since we're adding a spmd->DTensor translation shim anyways, migrate all models from `Replicate/Shard/Partial` to `spmd.R/S/P` immediately, use SpmdLayout, and go deal with I/R distinctions later. This should be ok because non-llama3 models won't support spmd backend yet. Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #3278 * #3472 * #3468 * #3471 * #3253 * #3587 * #3586 * __->__ #3501
| # This means fully_shard() has no spmd annotations -> DTensor param translation, | ||
| # and we shouldn't be passing a DataParallelMeshDims object to FSDP. | ||
| return storage_mesh, None |
There was a problem hiding this comment.
oh, you mean both functions are short? Sure, collapsing sounds good.
| tuple(["pp"] + candidate_spmd_dense_axes), | ||
| (self.pp, batch, self.cp, self.tp), | ||
| ) | ||
| spmd_dense_mesh_for_fwdbwd = full_dense_mesh_for_fwdbwd["dp", "cp", "tp"] |
There was a problem hiding this comment.
shall we add a TODO:
after migration is done, we let spmd_types handle all the size 1 processing (maybe except for fully_shard call with DP dim info)
|
|
||
| self.forward = forward_with_redistribution | ||
|
|
||
| def _register_spmd_state( |
There was a problem hiding this comment.
| def _register_spmd_state( | |
| def _spmd_register_state( |
| device_mesh=resolved_mesh, | ||
| ) | ||
|
|
||
| def _apply_local_spmd( |
There was a problem hiding this comment.
| def _apply_local_spmd( | |
| def _spmd_apply_local_map( |
After migration is done, we can remove all spmd prefixes
| src_spmd_layout.per_axis_spmd_types(), | ||
| dst_spmd_layout.per_axis_spmd_types(), |
There was a problem hiding this comment.
this can allow subtle bugs?
e.g. what if src: (DP, CP), dst: (CP, DP)
may need some additional guardrail
There was a problem hiding this comment.
check the validation helper in the previous PR, that should handle it
There was a problem hiding this comment.
OK, cool. Maybe
_validate_spmd_redistributions -> spmd_validate_redistributions
since it's public
| out_src.per_axis_spmd_types(), | ||
| out_dst.per_axis_spmd_types(), |
| loss = self.loss_fn(pred, labels, global_valid_tokens) | ||
| del pred | ||
| loss.backward() | ||
| with spmd.no_typecheck(): |
There was a problem hiding this comment.
so without adding this no_typecheck context
the backward would also attempt to propagate spmd_types, intercepted by torch_function?
There was a problem hiding this comment.
adding a comment would be good to readers
Splitting up infra PR. This adds: - _shard_spmd_state: util for distributing param/buffer onto mesh in SpmdLayout format, supporting PartitionSpec shard order - redistribute_spmd_per_axis: module boundary, SpmdLayout-based redistribution handler - _validate_spmd_redistributions: helper for validating it resolves to just one spmd.redistribute call - that's all titan will support Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #3278 * #3472 * #3468 * #3471 * #3253 * __->__ #3587
| loss = self.loss_fn(pred, labels, global_valid_tokens) | ||
| del pred | ||
| loss.backward() | ||
| with spmd.no_typecheck(): |
There was a problem hiding this comment.
adding a comment would be good to readers
| return stack | ||
|
|
||
|
|
||
| def current_mesh() -> DeviceMesh | None: |
There was a problem hiding this comment.
| def current_mesh() -> DeviceMesh | None: | |
| def current_spmd_mesh() -> DeviceMesh | None: |
| _spmd_backend = spmd_backend | ||
|
|
||
|
|
||
| def _mesh_stack() -> list[DeviceMesh | None]: |
There was a problem hiding this comment.
| def _mesh_stack() -> list[DeviceMesh | None]: | |
| def _spmd_mesh_stack() -> list[DeviceMesh | None]: |
| return stack[-1] | ||
|
|
||
|
|
||
| def mesh_size(axis_name: str) -> int: |
There was a problem hiding this comment.
| def mesh_size(axis_name: str) -> int: | |
| def spmd_mesh_size(axis_name: str) -> int: |
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def preserve_buffers_spmd(model: nn.Module) -> Iterator[None]: |
There was a problem hiding this comment.
| def preserve_buffers_spmd(model: nn.Module) -> Iterator[None]: | |
| def spmd_preserve_buffers(model: nn.Module) -> Iterator[None]: |
There was a problem hiding this comment.
Can we put this into Module, as Module._preserve_buffer_spmd_types()
| layout.partition_spec, | ||
| ) | ||
|
|
||
| def _shard_states(self, parallel_dims: ParallelDims) -> None: |
There was a problem hiding this comment.
| def _shard_states(self, parallel_dims: ParallelDims) -> None: | |
| def _distribute_states(self, parallel_dims: ParallelDims) -> None: |
to not confuse with spmd.Shard
|
|
||
| self.forward = forward_with_redistribution | ||
|
|
||
| def _spmd_register_state( |
There was a problem hiding this comment.
| def _spmd_register_state( | |
| def _spmd_distribute_state( |
sorry for the back-n-forth
| return | ||
|
|
||
| saved = { | ||
| fqn: (dict(spmd.get_local_type(buf)), spmd.get_partition_spec(buf)) |
There was a problem hiding this comment.
could you use an SpmdLayout object here?
Adding this to allow both spmd types specs, and DTensor-style placements. We're adding something like D107576760 in spmd_types, but in the meantime, because non-llama3 models are still in DTensor placement form, I think we have 2 options: 1) keep this as a hybrid spmd/DTensor container (e.g. moe_sharding.py calls `dense_activation_placement(tp=Replicate())`, and migrate to SpmdLayout once all models are off full_dtensor. 2) since we're adding a spmd->DTensor translation shim anyways, migrate all models from `Replicate/Shard/Partial` to `spmd.R/S/P` immediately, use SpmdLayout, and go deal with I/R distinctions later. This should be ok because non-llama3 models won't support spmd backend yet. Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #3278 * #3472 * #3468 * #3471 * #3253 * #3587 * #3586 * __->__ #3501
Splitting up infra PR. This adds: - _shard_spmd_state: util for distributing param/buffer onto mesh in SpmdLayout format, supporting PartitionSpec shard order - redistribute_spmd_per_axis: module boundary, SpmdLayout-based redistribution handler - _validate_spmd_redistributions: helper for validating it resolves to just one spmd.redistribute call - that's all titan will support Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * #3278 * #3472 * #3468 * #3471 * #3253 * __->__ #3587
Splitting up infra PR. This adds: - _shard_spmd_state: util for distributing param/buffer onto mesh in SpmdLayout format, supporting PartitionSpec shard order - redistribute_spmd_per_axis: module boundary, SpmdLayout-based redistribution handler - _validate_spmd_redistributions: helper for validating it resolves to just one spmd.redistribute call - that's all titan will support Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.12.0) (oldest at bottom): * pytorch#3278 * pytorch#3472 * pytorch#3468 * pytorch#3471 * pytorch#3253 * __->__ pytorch#3587
Main infra PR for spmd_types + titan.
spmd_types.py:mesh_size(axis_name)helper that returns > 1 when mesh is set & axis is active. Some of this will be moved to spmd_types in near-term, see comments.parallel_dims.py:module.py: module.parallelize() paths for spmd backend: weight init, local SPMD drop in, input/output redistribution.trainer.py/utils.py: typechecking context, input annotation, spmd set_current_spmd_mesh, PP + typechecking raises a hard error.Stack from ghstack (oldest at bottom):