MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / zero3_state_dict

Function zero3_state_dict

SwissArmyTransformer/sat/training/model_io.py:407–442  ·  view source on GitHub ↗

r"""Return a dictionary containing references to the whole state of a already paritioned module.

(module, *args, destination=None, prefix='', keep_vars=False)

Source from the content-addressed store, hash-verified

405 destination[extra_state_key] = module.get_extra_state()
406
407def zero3_state_dict(module, *args, destination=None, prefix='', keep_vars=False):
408 r"""Return a dictionary containing references to the whole state of a already paritioned module.
409 """
410 # TODO: Remove `args` and the parsing logic when BC allows.
411 if len(args) > 0:
412 if destination is None:
413 destination = args[0]
414 if len(args) > 1 and prefix == '':
415 prefix = args[1]
416 if len(args) > 2 and keep_vars is False:
417 keep_vars = args[2]
418 # DeprecationWarning is ignored by default
419 warnings.warn(
420 "Positional args are being deprecated, use kwargs instead. Refer to "
421 "https://pytorch.org/docs/master/generated/torch.nn.Module.html#torch.nn.Module.state_dict"
422 " for details.")
423
424 if destination is None:
425 destination = OrderedDict()
426 destination._metadata = OrderedDict()
427
428 local_metadata = dict(version=module._version)
429 if hasattr(destination, "_metadata"):
430 destination._metadata[prefix[:-1]] = local_metadata
431
432 for hook in module._state_dict_pre_hooks.values():
433 hook(module, prefix, keep_vars)
434 zero3_save_to_state_dict(module, destination, prefix, keep_vars)
435 for name, module in module._modules.items():
436 if module is not None:
437 zero3_state_dict(module, destination=destination, prefix=prefix + name + '.', keep_vars=keep_vars)
438 for hook in module._state_dict_hooks.values():
439 hook_result = hook(module, destination, prefix, local_metadata)
440 if hook_result is not None:
441 destination = hook_result
442 return destination
443
444def _zero3_load_from_state_dict(self, state_dict, prefix, local_metadata, strict,
445 missing_keys, unexpected_keys, error_msgs):

Callers 1

Calls 1

zero3_save_to_state_dictFunction · 0.85

Tested by

no test coverage detected