MCPcopy Create free account
hub / github.com/deepspeedai/DeepSpeed / _resolve_replacement_param_groups

Function _resolve_replacement_param_groups

deepspeed/runtime/engine.py:270–311  ·  view source on GitHub ↗

Pick the param group each replacement parameter belongs to. A replacement inherits the group its source parameters were in. A replacement none of whose sources are in any param group is left out entirely: the caller kept those parameters away from this optimizer on purpose, and adding t

(missing, replacement_sources, param_groups)

Source from the content-addressed store, hash-verified

268
269
270def _resolve_replacement_param_groups(missing, replacement_sources, param_groups):
271 """Pick the param group each replacement parameter belongs to.
272
273 A replacement inherits the group its source parameters were in. A replacement none of whose
274 sources are in any param group is left out entirely: the caller kept those parameters away
275 from this optimizer on purpose, and adding the replacement would start training something
276 they excluded.
277
278 Returns the placement, keyed by ``id()``, and the number of replacements left out.
279 """
280 group_of_source = {}
281 for group_index, group in enumerate(param_groups):
282 for param in group["params"]:
283 group_of_source[id(param)] = group_index
284
285 placement = {}
286 unoptimized = 0
287 for param in missing:
288 sources = replacement_sources.get(id(param), ())
289 source_groups = sorted({group_of_source[id(s)] for s in sources if id(s) in group_of_source})
290 unoptimized_sources = [s for s in sources if id(s) not in group_of_source]
291 if source_groups and unoptimized_sources:
292 raise RuntimeError("Module replacement (AutoEP) packed several source parameters into one tensor, but "
293 f"the client optimizer holds only {len(sources) - len(unoptimized_sources)} of "
294 f"{len(sources)} of them. The replacement is a single tensor, so the sources left out "
295 "cannot stay unoptimized -- optimizing it would train parameters the caller excluded, "
296 "and skipping it would stop training the ones they included. Give the optimizer all of "
297 'a layer\'s expert weights or none of them, declare the optimizer in the DeepSpeed '
298 'config ("optimizer": {...}) so it is built after replacement, or construct it after '
299 "deepspeed.initialize().")
300 if len(source_groups) > 1:
301 raise RuntimeError("Module replacement (AutoEP) built a single parameter from sources that the client "
302 f"optimizer had split across param groups {source_groups}, so it cannot be assigned "
303 "unambiguously. Put those source parameters in one param group, declare the optimizer "
304 'in the DeepSpeed config ("optimizer": {...}) so it is built after replacement, or '
305 "construct it after deepspeed.initialize().")
306 if source_groups:
307 placement[id(param)] = source_groups[0]
308 else:
309 unoptimized += 1
310
311 return placement, unoptimized
312
313
314def _remap_param_groups_after_module_replacement(param_groups, model, replacement_sources):

Calls 1

getMethod · 0.45

Tested by

no test coverage detected