(original_model, fsdp_config, ignored_modules=[])
| 82 | |
| 83 | |
| 84 | def fsdp_wrapper(original_model, fsdp_config, ignored_modules=[]): |
| 85 | |
| 86 | device_id = dist.get_rank() % torch.cuda.device_count() |
| 87 | target_device = torch.device(f"cuda:{device_id}") # Use same device as FSDP |
| 88 | # Explicitly move ignored modules to the target device |
| 89 | for module in ignored_modules: |
| 90 | module.to(target_device) |
| 91 | |
| 92 | if fsdp_config.sharding_strategy == 'HYBRID_SHARD': |
| 93 | device_mesh = init_device_mesh( |
| 94 | "cuda", |
| 95 | mesh_shape=(fsdp_config.num_replicate, fsdp_config.num_shard), |
| 96 | mesh_dim_names=("replicate", "shard") |
| 97 | ) |
| 98 | print("device_mesh shape:", device_mesh.mesh.shape) |
| 99 | else: |
| 100 | device_mesh = None |
| 101 | |
| 102 | |
| 103 | return FSDP( |
| 104 | original_model, |
| 105 | auto_wrap_policy=functools.partial( |
| 106 | transformer_auto_wrap_policy, |
| 107 | transformer_layer_cls={ |
| 108 | Qwen2VLDecoderLayer, |
| 109 | Qwen2VLMoEDecoderLayer, |
| 110 | Qwen2VLMoTDecoderLayer, |
| 111 | Dinov2WithRegistersLayer, |
| 112 | Dinov2WithRegistersModel, |
| 113 | Dinov2WithRegistersEmbeddings, |
| 114 | DINOv3ViTLayer, DINOv3ViTEmbeddings,DINOv3ViTRopePositionEmbedding, DINOv3ViTModel, |
| 115 | MLPconnector, |
| 116 | PatchEmbed, VisionRotaryEmbedding, Qwen2VLVisionBlock, PatchMerger, |
| 117 | Qwen2VisionTransformerPretrainedModel, |
| 118 | }, |
| 119 | ), |
| 120 | ignored_modules=ignored_modules, |
| 121 | mixed_precision=MixedPrecision( |
| 122 | param_dtype=torch.bfloat16, |
| 123 | reduce_dtype=torch.bfloat16, |
| 124 | buffer_dtype=torch.bfloat16, |
| 125 | ), |
| 126 | device_id=device_id, |
| 127 | sharding_strategy=ShardingStrategy[fsdp_config.sharding_strategy], |
| 128 | backward_prefetch=BackwardPrefetch[fsdp_config.backward_prefetch], |
| 129 | cpu_offload=CPUOffload(offload_params=fsdp_config.cpu_offload), |
| 130 | device_mesh=device_mesh, |
| 131 | ) |
| 132 | |
| 133 | |
| 134 | class FSDPCheckpoint: |
no test coverage detected