Skip to content
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

Add Amsgrad #137

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
amp version
  • Loading branch information
tginart committed Sep 15, 2020
commit dcd68dd5d5bae699844523935143dbc0d0fdac2b
20 changes: 15 additions & 5 deletions dlrm_s_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@

from torch.optim.lr_scheduler import _LRScheduler

try:
from apex import amp
except ImportError:
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this example.")


exc = getattr(builtins, "IOError", "FileNotFoundError")

class LRPolicyScheduler(_LRScheduler):
Expand Down Expand Up @@ -781,9 +787,6 @@ def parallel_forward(self, dense_x, lS_o, lS_i):
if dlrm.ndevices > 1:
dlrm.emb_l = dlrm.create_emb(m_spa, ln_emb)

if args.use_half_precision:
dlrm.half()

# specify the loss function
if args.loss_function == "mse":
loss_fn = torch.nn.MSELoss(reduction="mean")
Expand All @@ -801,6 +804,11 @@ def parallel_forward(self, dense_x, lS_o, lS_i):
lr_scheduler = LRPolicyScheduler(optimizer, args.lr_num_warmup_steps, args.lr_decay_start_step,
args.lr_num_decay_steps)


if args.use_half_precision:
dlrm, optimizer = amp.initialize(dlrm, optimizer, opt_level='O3')


### main loop ###
def time_wrap(use_gpu):
if use_gpu:
Expand Down Expand Up @@ -929,7 +937,8 @@ def loss_fn_wrap(Z, T, use_gpu, device):
for j, (X, lS_o, lS_i, T) in enumerate(train_ld):

if args.use_half_precision:
X = X.half()
pass
#X = X.half()
#T = T.half()

if j < skip_upto_batch:
Expand Down Expand Up @@ -981,7 +990,8 @@ def loss_fn_wrap(Z, T, use_gpu, device):
# (where we do not accumulate gradients across mini-batches)
optimizer.zero_grad()
# backward pass
E.backward()
with amp.scale_loss(E, optimizer) as scaled_loss:
scaled_loss.backward()
# debug prints (check gradient norm)
# for l in mlp.layers:
# if hasattr(l, 'weight'):
Expand Down