8000 Fix SGD to support bf16 by amkatrutsa · Pull Request #97 · nntile/nntile · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix SGD to support bf16 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions wrappers/python/examples/gpt2_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,19 @@ def check_grads(model_torch, nntile_model):
requires_grad=False).contiguous())
torch_input.append(minibatch_input)
torch_output.append(minibatch_output)
optim = Adam(model_torch.parameters(), lr=args.lr)
loss_func = nn.CrossEntropyLoss(reduction="mean")
if args.optimizer == "adam":
optimizer = Adam(model_torch.parameters(), args.lr)
elif args.optimizer == "sgd":
optimizer = SGD(model_torch.parameters(), args.lr)
elif args.optimizer == "adamw":
optimizer = AdamW(model_torch.parameters(), args.lr)
else:
raise ValueError
# Warmup training
for i in range(args.torch_nepochs_warmup):
for j in range(num_train_batches):
optim.zero_grad()
optimizer.zero_grad()
loss = torch.zeros(1, dtype=torch_dtype, device=args.torch_device)
for k in range(num_minibatch):
train_input = torch_input[j][k].to(args.torch_device)
Expand All @@ -657,14 +664,14 @@ def check_grads(model_torch, nntile_model):
loss_local.backward()
loss += loss_local
print("loss={}".format(loss.item()), flush=True)
optim.step()
optimizer.step()
# Actual training
if args.torch_device.startswith("cuda"):
torch.cuda.synchronize()
time0 = time.time()
for i in range(args.torch_nepochs):
for j in range(num_train_batches):
optim.zero_grad()
optimizer.zero_grad()
loss = torch.zeros(1, dtype=torch_dtype, device=args.torch_device)
for k in range(num_minibatch):
train_input = torch_input[j][k].to(args.torch_device)
Expand All @@ -677,7 +684,7 @@ def check_grads(model_torch, nntile_model):
print("Batch={}/{} Epoch={}/{} Loss={}".format(j+1, num_train_batches,
i+1, args.torch_nepochs,
loss.item()), flush=True)
optim.step()
optimizer.step()
if args.torch_device.startswith("cuda"):
torch.cuda.synchronize()
time1 = time.time() - time0
Expand Down
9 changes: 4 additions & 5 deletions wrappers/python/nntile/optimizer/sgd.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,16 @@ def unregister(self):
def step(self):
for i, p in enumerate(self.params):
if self.weight_decay != 0.:
nntile.tensor.axpy_async(self.weight_decay, p.value, p.grad)
nntile.tensor.add_async(self.weight_decay, p.value, 1., p.grad)

if self.momentum > 0:
if self.num_iter == 0:
nntile.tensor.copy_async(p.grad, self.states[i])
else:
nntile.tensor.scal_inplace_async(self.momentum, self.states[i])
nntile.tensor.axpy_async(1 - self.damping, p.grad, self.states[i])
nntile.tensor.add_async(1 - self.damping, p.grad, self.momentum, self.states[i])
if self.nesterov:
nntile.tensor.axpy_async(self.momentum, self.states[i], p.grad)
nntile.tensor.add_async(self.momentum, self.states[i], 1., p.grad)
else:
nntile.tensor.copy_async(self.states[i], p.grad)
nntile.tensor.axpy_async(-self.lr, p.grad, p.value)
nntile.tensor.add_async(-self.lr, p.grad, 1., p.value)
self.num_iter += 1
Loading
0