MCPcopy Create free account
hub / github.com/AIS-SNU/Smart-Infinity / load_module_recursive

Function load_module_recursive

deepspeed/module_inject/load_checkpoint.py:205–250  ·  view source on GitHub ↗
(module, prefix='', level=0)

Source from the content-addressed store, hash-verified

203 all_ds_ids = {}
204
205 def load_module_recursive(module, prefix='', level=0):
206 for name, child in module.named_children():
207 if child.__class__ in layer_policies:
208 checking_key = prefix + name + '.'
209 if not any(checking_key in item for item in sd[0].keys()):
210 if hasattr(child, 'weight') and \
211 (hasattr(child.weight, 'ds_id') and \
212 child.weight.ds_id in all_ds_ids):
213 prefix1 = all_ds_ids[child.weight.ds_id]
214 if child.__class__ is nn.Linear:
215 child = LinearLayer(weight=all_ds_ids[child.weight.ds_id])
216 setattr(module, name, child)
217 continue
218 child_params = list(child.parameters())
219 if len(child_params) > 0 and (child_params[0].numel() == 0 or child_params[0].is_meta):
220 if child.weight.is_meta:
221 ds_shape = child.weight.shape
222 else:
223 ds_shape = child.weight.ds_shape
224 if child.__class__ is nn.LayerNorm:
225 child = Normalize(dim=ds_shape[-1], dtype=child.weight.dtype, eps=child.eps)
226 setattr(module, name, child)
227 elif child.__class__ is nn.Linear:
228 child = LinearLayer(weight_shape=child.weight.shape, bias=child.bias)
229 setattr(module, name, child)
230 elif child.__class__ is OPTLearnedPositionalEmbedding:
231 child = OPTEmbedding(weight_shape=ds_shape)
232 setattr(module, name, child)
233 elif child.__class__ is LlamaRMSNorm:
234 child = RMSNormalize(dim=ds_shape[-1], dtype=child.weight.dtype, eps=child.variance_epsilon)
235 setattr(module, name, child)
236 else:
237 ds_id = None
238 if hasattr(child.weight, 'ds_id'):
239 ds_id = child.weight.ds_id
240 child = EmbeddingLayer(weight_shape=ds_shape, dtype=child.weight.dtype)
241 if ds_id is not None:
242 all_ds_ids[ds_id] = child.weight
243 setattr(module, name, child)
244 layer_policies[child.__class__](child, prefix + name + '.')
245 else:
246 load_module_recursive(
247 child,
248 prefix if (level == 0 and ckpt_type == 'pp') and skip_level_0_prefix else \
249 prefix + name + '.',
250 level + 1)
251
252 load_module_recursive(r_module)
253

Callers 3

load_module_recursiveMethod · 0.85

Calls 6

LinearLayerClass · 0.85
NormalizeClass · 0.85
OPTEmbeddingClass · 0.85
RMSNormalizeClass · 0.85
EmbeddingLayerClass · 0.85
numelMethod · 0.80

Tested by

no test coverage detected