MCPcopy Create free account
hub / github.com/ZinYY/TreeLoRA / CL_Base_Model

Class CL_Base_Model

model/base_model.py:22–127  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class CL_Base_Model:
23 def __init__(self,
24 model,
25 tokenizer,
26 optimizer,
27 train_task_list,
28 eval_task_list,
29 test_task_list,
30 args):
31 self.model = model
32 self.tokenizer = tokenizer
33 self.optimizer = optimizer
34 self.train_task_list = train_task_list
35 self.eval_task_list = eval_task_list
36 self.test_task_list = test_task_list
37 self.args = args
38
39
40 def perplexity_evaluation(self, eval_dataloader, device):
41 self.model.eval()
42 losses = 0
43 for step, batch in enumerate(eval_dataloader):
44 # implementation, batch = {k: v.to(device) for k, v in batch.items()}
45 del batch['sources']
46 batch = to_device(batch, device)
47 with torch.no_grad():
48 outputs = self.model(**batch, use_cache=False)
49 loss = outputs.loss
50 losses += loss.float()
51 losses = losses / (step + 1)
52 try:
53 perplexity = torch.exp(losses)
54 except OverflowError:
55 perplexity = float("inf")
56 try:
57 perplexity = get_all_reduce_mean(perplexity).item()
58 except:
59 pass
60 return perplexity
61
62
63 def train_one_task(self, task, i_task, epochs):
64 if self.args.local_rank == -1:
65 device = torch.device("cuda")
66 else:
67 torch.cuda.set_device(self.args.local_rank)
68 device = torch.device("cuda", self.args.local_rank)
69
70 #### TRAIN ####
71 train_dataloader = self.train_task_list[task]
72 eval_dataloader = self.eval_task_list[task]
73 total_steps = epochs * len(train_dataloader)
74 progress_bar = tqdm(total=total_steps, leave=True, disable=(self.args.global_rank != 0))
75 for epoch in range(epochs):
76 print_rank_0(
77 f"Beginning of Epoch {epoch+1}/{epochs}, Total Micro Batches {len(train_dataloader)}",
78 self.args.global_rank)
79 self.model.train()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected