MCPcopy
hub / github.com/huggingface/transformers / propagate_frozenset

Function propagate_frozenset

src/transformers/utils/import_utils.py:2934–2981  ·  view source on GitHub ↗
(unordered_import_structure)

Source from the content-addressed store, hash-verified

2932 """
2933
2934 def propagate_frozenset(unordered_import_structure):
2935 frozenset_first_import_structure = {}
2936 for _key, _value in unordered_import_structure.items():
2937 # If the value is not a dict but a string, no need for custom manipulation
2938 if not isinstance(_value, dict):
2939 frozenset_first_import_structure[_key] = _value
2940
2941 elif any(isinstance(v, frozenset) for v in _value):
2942 for k, v in _value.items():
2943 if isinstance(k, frozenset):
2944 # Here we want to switch around _key and k to propagate k upstream if it is a frozenset
2945 if k not in frozenset_first_import_structure:
2946 frozenset_first_import_structure[k] = {}
2947 if _key not in frozenset_first_import_structure[k]:
2948 frozenset_first_import_structure[k][_key] = {}
2949
2950 frozenset_first_import_structure[k][_key].update(v)
2951
2952 else:
2953 # If k is not a frozenset, it means that the dictionary is not "level": some keys (top-level)
2954 # are frozensets, whereas some are not -> frozenset keys are at an unknown depth-level of the
2955 # dictionary.
2956 #
2957 # We recursively propagate the frozenset for this specific dictionary so that the frozensets
2958 # are at the top-level when we handle them.
2959 propagated_frozenset = propagate_frozenset({k: v})
2960 for r_k, r_v in propagated_frozenset.items():
2961 if isinstance(_key, frozenset):
2962 if r_k not in frozenset_first_import_structure:
2963 frozenset_first_import_structure[r_k] = {}
2964 if _key not in frozenset_first_import_structure[r_k]:
2965 frozenset_first_import_structure[r_k][_key] = {}
2966
2967 # _key is a frozenset -> we switch around the r_k and _key
2968 frozenset_first_import_structure[r_k][_key].update(r_v)
2969 else:
2970 if _key not in frozenset_first_import_structure:
2971 frozenset_first_import_structure[_key] = {}
2972 if r_k not in frozenset_first_import_structure[_key]:
2973 frozenset_first_import_structure[_key][r_k] = {}
2974
2975 # _key is not a frozenset -> we keep the order of r_k and _key
2976 frozenset_first_import_structure[_key][r_k].update(r_v)
2977
2978 else:
2979 frozenset_first_import_structure[_key] = propagate_frozenset(_value)
2980
2981 return frozenset_first_import_structure
2982
2983 def flatten_dict(_dict, previous_key=None):
2984 items = []

Callers 1

spread_import_structureFunction · 0.85

Calls 2

itemsMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected