MCPcopy Create free account
hub / github.com/TorchSSL/TorchSSL / warmup

Method warmup

models/freematch/freematch.py:83–175  ·  view source on GitHub ↗
(self, args, logger=None)

Source from the content-addressed store, hash-verified

81 self.scheduler = scheduler
82
83 def warmup(self, args, logger=None):
84 ngpus_per_node = torch.cuda.device_count()
85
86 self.model.train()
87
88 # for gpu profiling
89 start_batch = torch.cuda.Event(enable_timing=True)
90 end_batch = torch.cuda.Event(enable_timing=True)
91 start_run = torch.cuda.Event(enable_timing=True)
92 end_run = torch.cuda.Event(enable_timing=True)
93
94 warmup_it = 0
95 start_batch.record()
96
97 scaler = GradScaler()
98 amp_cm = autocast if args.amp else contextlib.nullcontext
99
100 for _, x_lb, y_lb in self.loader_dict['train_lb']:
101
102 # prevent the training iterations exceed args.num_train_iter
103 if warmup_it > 2048:
104 break
105
106 end_batch.record()
107 torch.cuda.synchronize()
108 start_run.record()
109
110 x_lb = x_lb.cuda(args.gpu)
111 y_lb = y_lb.cuda(args.gpu)
112
113 num_lb = x_lb.shape[0]
114
115 # inference and calculate sup/unsup losses
116 with amp_cm():
117
118 logits_x_lb = self.model(x_lb)
119 sup_loss = ce_loss(logits_x_lb, y_lb, use_hard_labels=True, reduction='mean')
120
121 total_loss = sup_loss
122
123 # parameter updates
124 if args.amp:
125 scaler.scale(total_loss).backward()
126 if (args.clip > 0):
127 torch.nn.utils.clip_grad_norm_(self.model.parameters(), args.clip)
128 scaler.step(self.optimizer)
129 scaler.update()
130 else:
131 total_loss.backward()
132 if (args.clip > 0):
133 torch.nn.utils.clip_grad_norm_(self.model.parameters(), args.clip)
134 self.optimizer.step()
135
136 self.model.zero_grad()
137
138 end_run.record()
139 torch.cuda.synchronize()
140

Callers 2

main_workerFunction · 0.45
main_workerFunction · 0.45

Calls 4

ce_lossFunction · 0.90
stepMethod · 0.80
trainMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected