for t in chain(self.module.parameters(), self.module.buffers()): if t.device != self.src_device_obj: raise RuntimeError("module must have its parameters and buffers " f"on device {self.src_device_obj} (device_ids[0]) but found one of " f"them on device: {t.device}")
inputs, module_kwargs = self.scatter(inputs, kwargs, self.device_ids) # 将数据分发到不同GPU # for forward function without any inputs, empty list and dict will be created # so the module can be executed on one device which is the first one in device_ids ifnot inputs andnot module_kwargs: inputs = ((),) module_kwargs = ({},)
defevaluate(): model.eval() acc_num = 0 with torch.inference_mode(): for batch in validloader: if torch.cuda.is_available(): batch = {k: v.cuda() for k, v in batch.items()} output = model(**batch) pred = torch.argmax(output.logits, dim=-1) acc_num += (pred.long() == batch["labels"].long()).float().sum() return acc_num / len(validset)
deftrain(epoch=3, log_step=100): global_step = 0 for ep inrange(epoch): model.train() start = time.time() for batch in trainloader: if torch.cuda.is_available(): batch = {k: v.cuda() for k, v in batch.items()} optimizer.zero_grad() output = model(**batch) loss = output.loss.mean() # 多卡时需要计算损失的均值 # loss = output.loss 单卡时损失只有一个 loss.backward() optimizer.step() if global_step % log_step == 0: print(f"ep: {ep}, global_step: {global_step}, loss: {loss.item()}") global_step += 1 acc = evaluate() print(f"ep: {ep}, acc: {acc}, time: {time.time() - start}")
train()
完成训练后就可以进行推理预测了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 模型预测
sen = "我觉得这家酒店不错,饭很好吃!" id2_label = {0: "差评!", 1: "好评!"} model.eval() with torch.inference_mode(): inputs = tokenizer(sen, return_tensors="pt") inputs = {k: v.cuda() for k, v in inputs.items()} logits = model(**inputs).logits pred = torch.argmax(logits, dim=-1) print(f"输入:{sen}\n模型预测结果:{id2_label.get(pred.item())}")