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

[#168 fix] add context manager to fake ScalingTensor/ScalingParameter's __class__ as torch.Tensor #169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def train(args, model, device, train_loader, optimizer, epoch):
output = model(data)
loss = F.nll_loss(output, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
with msamp.common.tensor.tensor.pretend_scaling_is_torch(): scaler.step(optimizer)
scaler.update()
if batch_idx % args.log_interval == 0:
print(
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist_ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def train(args, model, device, train_loader, optimizer, epoch):
output = model(data)
loss = F.nll_loss(output, target)
scaler.scale(loss).backward()
scaler.step(optimizer)
with msamp.common.tensor.tensor.pretend_scaling_is_torch(): scaler.step(optimizer)
scaler.update()
if dist.get_rank() == 0:
if batch_idx % args.log_interval == 0:
Expand Down
10 changes: 10 additions & 0 deletions msamp/common/tensor/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""MS-AMP tensor module."""

from contextlib import contextmanager
import torch
import torch.nn.functional as F
from msamp.common.tensor import ScalingMeta
Expand All @@ -12,7 +13,16 @@
from msamp.common.utils import TransformerEngineWrapper


should_pretend_to_be_tt = False
@contextmanager
def pretend_scaling_is_torch():
global should_pretend_to_be_tt
should_pretend_to_be_tt = True
yield
should_pretend_to_be_tt = False
class ScalingTensor:
@property
def __class__(self): return torch.Tensor if should_pretend_to_be_tt else ScalingTensor
"""Customized tensor with scaling."""
class UniqueDtypeDecorator:
"""A decorator class to check whether dtype is supported and parameters are uniqie."""
Expand Down
4 changes: 4 additions & 0 deletions msamp/nn/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

"""MS-AMP parameter module."""

import torch
from msamp.common.tensor import ScalingTensor
import msamp.common.tensor.tensor as tensor_py


class ScalingParameter(ScalingTensor):
"""Parameter class for ScalingTensor."""
@property
def __class__(self): return torch.Tensor if tensor_py.should_pretend_to_be_tt else ScalingParameter
def __init__(self, tensor, requires_grad=True):
"""Constructor.

Expand Down