MCPcopy Create free account
hub / github.com/zai-org/CodeGeeX / load_state_dict

Method load_state_dict

codegeex/megatron/model/language_model.py:214–270  ·  view source on GitHub ↗

Customized load.

(self, state_dict, strict=True)

Source from the content-addressed store, hash-verified

212 return state_dict_
213
214 def load_state_dict(self, state_dict, strict=True):
215 """Customized load."""
216
217 # Word embedding.
218 if self._word_embeddings_key in state_dict:
219 state_dict_ = state_dict[self._word_embeddings_key]
220 else:
221 # for backward compatibility.
222 state_dict_ = {}
223 for key in state_dict.keys():
224 if 'word_embeddings' in key:
225 state_dict_[key.split('word_embeddings.')[1]] \
226 = state_dict[key]
227 vocab_len = state_dict_['weight'].shape[0]
228 state_dict_["weight"] = state_dict_["weight"][:self.vocab_size // get_tensor_model_parallel_world_size()]
229 self.word_embeddings.load_state_dict(state_dict_, strict=strict)
230
231 # Position embedding.
232 if self._position_embeddings_key in state_dict:
233 state_dict_ = state_dict[self._position_embeddings_key]
234 else:
235 # for backward compatibility.
236 state_dict_ = {}
237 for key in state_dict.keys():
238 if 'position_embeddings' in key:
239 state_dict_[key.split('position_embeddings.')[1]] \
240 = state_dict[key]
241
242 pos_len = state_dict_['weight'].shape[0]
243 max_seq_len = self.max_sequence_length
244 if pos_len < max_seq_len:
245 print_rank_0(f"Position embedding padded {pos_len} -> {max_seq_len}.")
246 position_embeddings_padded = torch.nn.Embedding(
247 max_seq_len - pos_len, self.hidden_size).half()
248 self.init_method(position_embeddings_padded.weight)
249 state_dict_['weight'] = torch.cat([state_dict_['weight'], position_embeddings_padded.weight], dim=0)
250
251 # self.position_embeddings = self.position_embeddings.half()
252 self.position_embeddings.load_state_dict(state_dict_, strict=strict)
253
254 # Tokentype embedding.
255 if self.num_tokentypes > 0:
256 state_dict_ = {}
257 if self._tokentype_embeddings_key in state_dict:
258 state_dict_ = state_dict[self._tokentype_embeddings_key]
259 else:
260 # for backward compatibility.
261 for key in state_dict.keys():
262 if 'tokentype_embeddings' in key:
263 state_dict_[key.split('tokentype_embeddings.')[1]] \
264 = state_dict[key]
265 if len(state_dict_.keys()) > 0:
266 self.tokentype_embeddings.load_state_dict(state_dict_,
267 strict=strict)
268 else:
269 print('***WARNING*** expected tokentype embeddings in the '
270 'checkpoint but could not find it', flush=True)
271

Callers 2

load_state_dictMethod · 0.45
load_state_dictMethod · 0.45

Calls 2

print_rank_0Function · 0.90

Tested by

no test coverage detected