|
| 1 | +from typing import Tuple |
| 2 | +from torch.distributed.tensor.parallel import ( |
| 3 | + ColwiseParallel, |
| 4 | + parallelize_module, |
| 5 | + PrepareModuleInput, |
| 6 | + RowwiseParallel, |
| 7 | + SequenceParallel, |
| 8 | +) |
| 9 | + |
| 10 | +from distributed.parallel_config import ParallelConfig |
| 11 | + |
| 12 | + |
| 13 | +def get_tp_parallel_strategy( |
| 14 | + config: ParallelConfig, |
| 15 | +) -> Tuple[RowwiseParallel, ColwiseParallel, PrepareModuleInput]: |
| 16 | + """Get the parallel strategy for the transformer model. |
| 17 | +
|
| 18 | + This function handles the special case of using float8 with tensor parallelism. |
| 19 | + """ |
| 20 | + if config.fp8_linear == "dynamic": |
| 21 | + from float8_experimental.float8_tensor_parallel import ( |
| 22 | + Float8ColwiseParallel, |
| 23 | + Float8RowwiseParallel, |
| 24 | + PrepareFloat8ModuleInput, |
| 25 | + ) |
| 26 | + |
| 27 | + return Float8RowwiseParallel, Float8ColwiseParallel, PrepareFloat8ModuleInput |
| 28 | + return RowwiseParallel, ColwiseParallel, PrepareModuleInput |
| 29 | + |
| 30 | + |
| 31 | +def apply_tp(model, world_mesh, parallel_dims, config: ParallelConfig): |
| 32 | + """ |
| 33 | + Apply tensor parallelism. |
| 34 | + """ |
| 35 | + |
| 36 | + tp_mesh = world_mesh["tp"] |
| 37 | + ( |
| 38 | + row_parallel_strategy, |
| 39 | + col_parallel_strategy, |
| 40 | + prepare_module_input, |
| 41 | + ) = get_tp_parallel_strategy(config) |
| 42 | + loss_parallel = parallel_dims.loss_parallel_enabled |
| 43 | + |
| 44 | + # 1. Parallelize the first embedding and the last linear proj layer |
| 45 | + # 2. Parallelize the root norm layer over the sequence dim |
| 46 | + # 3. Shard the first transformer block's inputs |
| 47 | + model = parallelize_module( |
| 48 | + model, |
| 49 | + tp_mesh, |
| 50 | + { |
| 51 | + "tok_embeddings": RowwiseParallel( |
| 52 | + input_layouts=Replicate(), |
| 53 | + output_layouts=Shard(1), |
| 54 | + ), |
| 55 | + "output": col_parallel_strategy( |
| 56 | + input_layouts=Shard(1), |
| 57 | + output_layouts=Shard(-1) if loss_parallel else Replicate(), |
| 58 | + use_local_output=not loss_parallel, |
| 59 | + ), |
| 60 | + "norm": SequenceParallel(), |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + # Apply tensor + sequence parallelism to every transformer block |
| 65 | + for layer_id, transformer_block in model.layers.items(): |
| 66 | + layer_plan = { |
| 67 | + "attention": prepare_module_input( |
| 68 | + input_layouts=(Shard(1), None), |
| 69 | + desired_input_layouts=(Replicate(), None), |
| 70 | + ), |
| 71 | + "attention.wq": col_parallel_strategy(), |
| 72 | + "attention.wk": col_parallel_strategy(), |
| 73 | + "attention.wv": col_parallel_strategy(), |
| 74 | + "attention.wo": row_parallel_strategy(output_layouts=Shard(1)), |
| 75 | + "attention_norm": SequenceParallel(), |
| 76 | + "feed_forward": prepare_module_input( |
| 77 | + input_layouts=(Shard(1),), |
| 78 | + desired_input_layouts=(Replicate(),), |
| 79 | + ), |
| 80 | + "feed_forward.w1": col_parallel_strategy(), |
| 81 | + "feed_forward.w2": row_parallel_strategy(output_layouts=Shard(1)), |
| 82 | + "feed_forward.w3": col_parallel_strategy(), |
| 83 | + "ffn_norm": SequenceParallel(), |
| 84 | + } |
| 85 | + |
| 86 | + # Adjust attention module to use the local number of heads |
| 87 | + attn_layer = transformer_block.attention |
| 88 | + attn_layer.n_heads = attn_layer.n_heads // tp_mesh.size() |
| 89 | + attn_layer.n_kv_heads = attn_layer.n_kv_heads // tp_mesh.size() |
| 90 | + |
| 91 | + parallelize_module( |
| 92 | + module=transformer_block, |
| 93 | + device_mesh=tp_mesh, |
| 94 | + parallelize_plan=layer_plan, |
| 95 | + ) |
| 96 | + |
| 97 | + logger.info("Applied Tensor Parallelism to the model") |
| 98 | + return model |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +def parallelize_llama(model, world_mesh, parallel_dims, config: ParallelConfig): |
| 104 | + """ |
| 105 | + Apply tensor parallelism, activation checkpointing, torch.compile, and data |
| 106 | + parallelism to the model. |
| 107 | +
|
| 108 | + NOTE: The passed-in model preferably should be on meta device. Otherwise, |
| 109 | + the model must fit on GPU or CPU memory. |
| 110 | + """ |
| 111 | + |
| 112 | + if parallel_dims.tp_enabled: |
| 113 | + model = apply_tp(model, world_mesh, parallel_dims, job_config) |
| 114 | + |
| 115 | + # only enable TP for now. |
| 116 | + # if job_config.training.compile: |
| 117 | + # model = apply_compile(model, job_config) |
| 118 | + |
| 119 | + return model |
0 commit comments