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

Function _resolve_replacement_param_groups

deepspeed/runtime/engine.py:269–310  ·  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

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

Calls 1

getMethod · 0.45

Tested by

no test coverage detected