MCPcopy Create free account
hub / github.com/OpenBitSys/BitDistiller / train

Function train

train/train.py:262–389  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

260
261
262def train():
263 parser = transformers.HfArgumentParser((ModelArguments, DataArguments, TrainingArguments))
264 model_args, data_args, training_args = parser.parse_args_into_dataclasses()
265
266 random.seed(TrainingArguments.seed)
267 n_gpus = torch.cuda.device_count()
268 max_memory = f'80000MB'
269 max_memory = {i: max_memory for i in range(n_gpus)}
270 device_map = "auto"
271
272 if "34B" in model_args.model_name_or_path:
273 device_map = None
274
275 # if we are in a distributed setting, we need to set the device map and max memory per device
276 if os.environ.get('LOCAL_RANK') is not None:
277 local_rank = int(os.environ.get('LOCAL_RANK', '0'))
278 device_map = {'': local_rank}
279 max_memory = {'': max_memory[local_rank]}
280
281 print(f"loading {model_args.model_name_or_path} model")
282 model = transformers.AutoModelForCausalLM.from_pretrained(
283 model_args.model_name_or_path,
284 torch_dtype=torch.bfloat16,
285 device_map=device_map,
286 )
287
288 tokenizer = transformers.AutoTokenizer.from_pretrained(
289 model_args.model_name_or_path,
290 cache_dir=training_args.cache_dir,
291 model_max_length=training_args.model_max_length,
292 padding_side="right",
293 use_fast=False,
294 )
295
296 pad_status = True
297 if tokenizer.pad_token is None:
298 print("tokenizer has not padding token")
299 pad_status = False
300 smart_tokenizer_and_embedding_resize(
301 special_tokens_dict=dict(pad_token=DEFAULT_PAD_TOKEN),
302 tokenizer=tokenizer,
303 model=model,
304 )
305 if tokenizer.eos_token is None:
306 tokenizer.add_special_tokens(
307 {
308 "eos_token": DEFAULT_EOS_TOKEN,
309 "bos_token": DEFAULT_BOS_TOKEN,
310 "unk_token": DEFAULT_UNK_TOKEN,
311 }
312 )
313
314 data_module = make_supervised_data_module(tokenizer=tokenizer, data_args=data_args)
315
316 if training_args.quant_type is not None:
317 print("converting the model to qat, this may take a while...")
318 model, _ = convertModelToQuant(model, compute_dtype=torch.bfloat16, quant_type=training_args.quant_type, q_group_size=training_args.q_group_size)
319

Callers 1

train.pyFile · 0.85

Calls 8

convertModelToQuantFunction · 0.90
apply_clipFunction · 0.90
KDTrainerClass · 0.90
add_special_tokensMethod · 0.80
loadMethod · 0.80

Tested by

no test coverage detected