MCPcopy Create free account
hub / github.com/modelscope/modelscope / compare_cfg_and_optimizers

Function compare_cfg_and_optimizers

modelscope/utils/regress_test_utils.py:724–787  ·  view source on GitHub ↗
(baseline_json,
                               cfg_json,
                               compare_fn=None,
                               **kwargs)

Source from the content-addressed store, hash-verified

722
723
724def compare_cfg_and_optimizers(baseline_json,
725 cfg_json,
726 compare_fn=None,
727 **kwargs):
728 if compare_fn is None:
729
730 def compare_fn(*args, **kwargs):
731 return None
732
733 optimizer1, lr_scheduler1, cfg1, state1 = baseline_json[
734 'optimizer'], baseline_json['lr_scheduler'], baseline_json[
735 'cfg'], baseline_json['state']
736 optimizer2, lr_scheduler2, cfg2, state2 = cfg_json['optimizer'], cfg_json[
737 'lr_scheduler'], cfg_json['cfg'], baseline_json['state']
738
739 match = True
740 res = compare_fn(optimizer1, optimizer2, None, 'optimizer')
741 if res is not None:
742 print(f'optimizer compared with user compare_fn with result:{res}\n')
743 match = match and res
744 else:
745 if optimizer1['type'] != optimizer2['type']:
746 print(
747 f"Optimizer type not equal:{optimizer1['type']} and {optimizer2['type']}"
748 )
749 match = compare_arguments_nested(
750 'unmatched optimizer defaults', optimizer1['defaults'],
751 optimizer2['defaults'], **kwargs) and match
752 match = compare_arguments_nested(
753 'unmatched optimizer state_dict', optimizer1['state_dict'],
754 optimizer2['state_dict'], **kwargs) and match
755
756 res = compare_fn(lr_scheduler1, lr_scheduler2, None, 'lr_scheduler')
757 if res is not None:
758 print(
759 f'lr_scheduler compared with user compare_fn with result:{res}\n')
760 match = match and res
761 else:
762 if lr_scheduler1['type'] != lr_scheduler2['type']:
763 print(
764 f"Optimizer type not equal:{lr_scheduler1['type']} and {lr_scheduler2['type']}"
765 )
766 match = compare_arguments_nested(
767 'unmatched lr_scheduler state_dict', lr_scheduler1['state_dict'],
768 lr_scheduler2['state_dict'], **kwargs) and match
769
770 res = compare_fn(cfg1, cfg2, None, 'cfg')
771 if res is not None:
772 print(f'cfg compared with user compare_fn with result:{res}\n')
773 match = match and res
774 else:
775 match = compare_arguments_nested(
776 'unmatched cfg', arg1=cfg1, arg2=cfg2, **kwargs) and match
777
778 res = compare_fn(state1, state2, None, 'state')
779 if res is not None:
780 print(
781 f'random state compared with user compare_fn with result:{res}\n')

Callers 1

monitor_module_trainMethod · 0.85

Calls 3

compare_fnFunction · 0.85
printFunction · 0.85
compare_arguments_nestedFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…