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

Function module_inject

deepspeed/module_inject/inject.py:11–71  ·  view source on GitHub ↗
(layer_obj, model, config, micro_batch_size, max_seq_length, seed, preln, fp16=True)

Source from the content-addressed store, hash-verified

9
10
11def module_inject(layer_obj, model, config, micro_batch_size, max_seq_length, seed, preln, fp16=True):
12 for name, child in model.named_children():
13 if isinstance(child, layer_obj):
14 print('REPLACING BertLayer')
15
16 cuda_config = DeepSpeedTransformerConfig(batch_size=micro_batch_size,
17 max_seq_length=max_seq_length,
18 hidden_size=config.hidden_size,
19 heads=config.num_attention_heads,
20 attn_dropout_ratio=config.attention_probs_dropout_prob,
21 hidden_dropout_ratio=config.hidden_dropout_prob,
22 num_hidden_layers=config.num_hidden_layers,
23 initializer_range=config.initializer_range,
24 seed=seed,
25 fp16=fp16,
26 pre_layer_norm=preln)
27
28 new_module = DeepSpeedTransformerLayer(cuda_config)
29
30 # copy relevant state from child -> new module
31 qw = child.attention.self.query.weight
32 qb = child.attention.self.query.bias
33 kw = child.attention.self.key.weight
34 kb = child.attention.self.key.bias
35 vw = child.attention.self.value.weight
36 vb = child.attention.self.value.bias
37
38 qkvw = torch.cat((qw, kw, vw), 0)
39 qkvb = torch.cat((qb, kb, vb), 0)
40
41 new_module.attn_qkvw.data = qkvw
42 new_module.attn_qkvb.data = qkvb
43 new_module.attn_ow.data = child.attention.output.dense.weight
44 new_module.attn_ob.data = child.attention.output.dense.bias
45 if preln:
46 attention_layerNorm = child.PostAttentionLayerNorm
47 else:
48 attention_layerNorm = child.attention.output.LayerNorm
49 new_module.attn_nw.data = attention_layerNorm.weight
50 new_module.attn_nb.data = attention_layerNorm.bias
51 if preln:
52 intermediate_FF = child.intermediate.dense_act
53 else:
54 intermediate_FF = child.intermediate.dense
55 new_module.inter_w.data = intermediate_FF.weight
56 new_module.inter_b.data = intermediate_FF.bias
57 new_module.output_w.data = child.output.dense.weight
58 new_module.output_b.data = child.output.dense.bias
59 if preln:
60 transformer_LayerNorm = child.PreAttentionLayerNorm
61 else:
62 transformer_LayerNorm = child.output.LayerNorm
63 new_module.norm_w.data = transformer_LayerNorm.weight
64 new_module.norm_b.data = transformer_LayerNorm.bias
65
66 setattr(model, name, copy.deepcopy(new_module))
67
68 else:

Callers 1

test_hiFunction · 0.85

Tested by 1

test_hiFunction · 0.68