MCPcopy Create free account
hub / github.com/NVIDIA/FasterTransformer / train

Function train

examples/pytorch/vit/ViT-quantization/main.py:196–325  ·  view source on GitHub ↗
(args, config)

Source from the content-addressed store, hash-verified

194 logger.info(f'Model is saved to {output_model_path}')
195
196def train(args, config):
197 num_classes = 1000
198 model_config = CONFIGS[args.model_type]
199 model = VisionTransformerINT8(model_config, args.img_size, zero_head=False, num_classes=num_classes)
200 model_ckpt = torch.load(args.pretrained_dir, map_location="cpu")
201 model.load_state_dict(model_ckpt["model"] if "model" in model_ckpt else model_ckpt, strict=False)
202 model.cuda()
203 model.train()
204
205 teacher = None
206 dis_loss = None
207 if args.distill:
208 teacher = VisionTransformerINT8(model_config, args.img_size, zero_head=False, num_classes=num_classes)
209 teacher.load_from(np.load(args.teacher))
210 dis_loss = Knowledge_Distillation_Loss(scale=args.distillation_loss_scale).cuda()
211 teacher.cuda()
212 teacher.eval()
213 quant_utils.set_quantizer_by_name(teacher, [''], _disabled=True)
214
215 """ Train the model """
216 if args.local_rank in [-1, 0]:
217 os.makedirs(args.output_dir, exist_ok=True)
218
219 args.train_batch_size = args.train_batch_size // args.gradient_accumulation_steps
220
221 # Prepare dataset
222 # train_loader, test_loader = get_loader(args)
223 dataset_train, dataset_val, train_loader, test_loader = build_loader(config, args)
224
225 # Prepare optimizer and scheduler
226 optimizer = torch.optim.SGD(model.parameters(),
227 lr=args.qat_lr,
228 momentum=0.9,
229 weight_decay=args.weight_decay)
230
231 print('args.qat_lr: %.6f' % (args.qat_lr))
232 print('optimizer.lr: %.6f' % optimizer.state_dict()['param_groups'][0]['lr'])
233 t_total = args.num_steps
234 # if args.decay_type == "cosine":
235 # scheduler = WarmupCosineSchedule(optimizer, warmup_steps=args.warmup_steps, t_total=t_total)
236 # else:
237 # scheduler = WarmupLinearSchedule(optimizer, warmup_steps=args.warmup_steps, t_total=t_total)
238
239
240 print('查看optimizer.param_groups结构:')
241 i_list=[i for i in optimizer.param_groups[0].keys()]
242 print(i_list)
243 if args.fp16:
244 model, optimizer = amp.initialize(models=model,
245 optimizers=optimizer,
246 opt_level=args.fp16_opt_level)
247 amp._amp_state.loss_scalers[0]._loss_scale = 2**20
248
249 # Distributed training
250 if args.local_rank != -1:
251 model = DDP(model, message_size=250000000, gradient_predivide_factor=get_world_size())
252
253 # Train!

Callers 1

mainFunction · 0.70

Calls 15

load_fromMethod · 0.95
updateMethod · 0.95
resetMethod · 0.95
build_loaderFunction · 0.90
set_seedFunction · 0.70
AverageMeterClass · 0.70
validFunction · 0.70
save_modelFunction · 0.70
get_world_sizeFunction · 0.50
loadMethod · 0.45

Tested by

no test coverage detected