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

Function student_initialization

deepspeed/compression/compress.py:192–239  ·  view source on GitHub ↗

Given a student model and a teacher model, select the Args: student_model (`torch.nn.Module`) The model we will update weight teacher_model (`torch.nn.Module`) The model guide the student to learn deepspeed_config (`DeepSpeedConfig`)

(student_model, teacher_model, deepspeed_config)

Source from the content-addressed store, hash-verified

190
191
192def student_initialization(student_model, teacher_model, deepspeed_config):
193 '''
194 Given a student model and a teacher model, select the
195 Args:
196 student_model (`torch.nn.Module`)
197 The model we will update weight
198 teacher_model (`torch.nn.Module`)
199 The model guide the student to learn
200 deepspeed_config (`DeepSpeedConfig`)
201 The path of ds_config
202 '''
203 config = get_compression_config(check_deepspeed_config(deepspeed_config))
204 compress_methods = config[LAYER_REDUCTION]
205
206 module_name_prefix = compress_methods[MODULE_NAME_PREFIX]
207 teacher_layer = compress_methods[TEACHER_LAYER]
208 student_layer = [i for i in range(len(teacher_layer))]
209 other_module_name = compress_methods[OTHER_MODULE_NAME]
210 '''
211 name_prefix (`str`)
212 The prefix name before the layer #.
213 Example 1: bert.encoder.layer, for BERT_base model's prefix name
214 Example 2: transformer.h, for GPT-2 hugging face prefix name
215 teacher_layer (`list of integers`)
216 The layer of teacher will be used for student's reinitialization
217 Example 1: [1,3,5,7,9], means we want to matches the 2nd/4th/6th/8th/10th layer of teacher to the first 5 layers of student
218 student_layer (`list` or None)
219 The layer of student need to be re-initialized
220 Example 1: None, means we want to reinitialize all the layers
221 Example 1: [0,1,2,3,4], means we want to reinitialize the first 5 layers
222 other_module_name (`list of string`)
223 The modules will be used for student's reinitialization
224 Example 1: ['bert.pooler', 'bert.embeddings', 'classifier'], means we want to apply the weight in teacher's embedding/pooler/classier module to the student
225 Example 2: ['transformer.w', 'transformer.ln_f', 'lm_head'], means we want to apply the weight in teacher's embedding layers module to the student
226 Note that teacher_layer should matches student layer
227 '''
228 assert len(student_layer) == len(teacher_layer)
229 for s_name, t_name in zip(student_layer, teacher_layer):
230 s_module = recursive_getattr(student_model, module_name_prefix + '.' + str(s_name))
231 t_module = recursive_getattr(teacher_model, module_name_prefix + '.' + str(t_name))
232 for s_param, t_param in zip(s_module.parameters(), t_module.parameters()):
233 s_param.data.copy_(t_param.data)
234 for name in other_module_name:
235 s_module = recursive_getattr(student_model, name)
236 t_module = recursive_getattr(teacher_model, name)
237 print(name)
238 for s_param, t_param in zip(s_module.parameters(), t_module.parameters()):
239 s_param.data.copy_(t_param.data)

Callers 1

init_compressionFunction · 0.85

Calls 3

get_compression_configFunction · 0.85
check_deepspeed_configFunction · 0.85
recursive_getattrFunction · 0.85

Tested by

no test coverage detected