MCPcopy Index your code
hub / github.com/huggingface/diffusers / _custom_mesh_worker

Function _custom_mesh_worker

tests/models/testing_utils/parallelism.py:171–227  ·  view source on GitHub ↗

Worker function for context parallel testing with a user-provided custom DeviceMesh.

(
    rank,
    world_size,
    master_port,
    model_class,
    init_dict,
    cp_dict,
    mesh_shape,
    mesh_dim_names,
    inputs_dict,
    return_dict,
)

Source from the content-addressed store, hash-verified

169
170
171def _custom_mesh_worker(
172 rank,
173 world_size,
174 master_port,
175 model_class,
176 init_dict,
177 cp_dict,
178 mesh_shape,
179 mesh_dim_names,
180 inputs_dict,
181 return_dict,
182):
183 """Worker function for context parallel testing with a user-provided custom DeviceMesh."""
184 try:
185 os.environ["MASTER_ADDR"] = "localhost"
186 os.environ["MASTER_PORT"] = str(master_port)
187 os.environ["RANK"] = str(rank)
188 os.environ["WORLD_SIZE"] = str(world_size)
189
190 # Get device configuration
191 device_config = DEVICE_CONFIG.get(torch_device, DEVICE_CONFIG["cuda"])
192 backend = device_config["backend"]
193 device_module = device_config["module"]
194
195 dist.init_process_group(backend=backend, rank=rank, world_size=world_size)
196
197 # Set device for this process
198 device_module.set_device(rank)
199 device = torch.device(f"{torch_device}:{rank}")
200
201 model = model_class(**init_dict)
202 model.to(device)
203 model.eval()
204
205 inputs_on_device = {k: v.to(device) if isinstance(v, torch.Tensor) else v for k, v in inputs_dict.items()}
206
207 # DeviceMesh must be created after init_process_group, inside each worker process.
208 mesh = torch.distributed.device_mesh.init_device_mesh(
209 torch_device, mesh_shape=mesh_shape, mesh_dim_names=mesh_dim_names
210 )
211 cp_config = ContextParallelConfig(**cp_dict, mesh=mesh)
212 model.enable_parallelism(config=cp_config)
213
214 with torch.no_grad():
215 output = model(**inputs_on_device, return_dict=False)[0]
216
217 if rank == 0:
218 return_dict["status"] = "success"
219 return_dict["output_shape"] = list(output.shape)
220
221 except Exception as e:
222 if rank == 0:
223 return_dict["status"] = "error"
224 return_dict["error"] = str(e)
225 finally:
226 if dist.is_initialized():
227 dist.destroy_process_group()
228

Callers

nothing calls this directly

Calls 5

enable_parallelismMethod · 0.80
getMethod · 0.45
deviceMethod · 0.45
toMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…