Skip to content

Cleanup some amp related behaviour to better support different (non-cuda) devices #2308

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 6 commits into from
Oct 19, 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
9 changes: 1 addition & 8 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
except ImportError:
pass

has_native_amp = False
try:
if getattr(torch.cuda.amp, 'autocast') is not None:
has_native_amp = True
except AttributeError:
pass

try:
from deepspeed.profiling.flops_profiler import get_model_profile
has_deepspeed_profiling = True
Expand Down Expand Up @@ -242,7 +235,7 @@ def __init__(
self.amp_dtype, self.model_dtype, self.data_dtype = resolve_precision(precision)
self.channels_last = kwargs.pop('channels_last', False)
if self.amp_dtype is not None:
self.amp_autocast = partial(torch.cuda.amp.autocast, dtype=self.amp_dtype)
self.amp_autocast = partial(torch.amp.autocast, device_type=device, dtype=self.amp_dtype)
else:
self.amp_autocast = suppress

Expand Down
8 changes: 0 additions & 8 deletions inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@
except ImportError:
has_apex = False

has_native_amp = False
try:
if getattr(torch.cuda.amp, 'autocast') is not None:
has_native_amp = True
except AttributeError:
pass

try:
from functorch.compile import memory_efficient_fusion
has_functorch = True
Expand Down Expand Up @@ -170,7 +163,6 @@ def main():
# resolve AMP arguments based on PyTorch / Apex availability
amp_autocast = suppress
if args.amp:
assert has_native_amp, 'Please update PyTorch to a version with native AMP (or use APEX).'
assert args.amp_dtype in ('float16', 'bfloat16')
amp_dtype = torch.bfloat16 if args.amp_dtype == 'bfloat16' else torch.float16
amp_autocast = partial(torch.autocast, device_type=device.type, dtype=amp_dtype)
Expand Down
11 changes: 9 additions & 2 deletions timm/data/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ def __init__(
)
else:
self.random_erasing = None
self.is_cuda = torch.cuda.is_available() and device.type == 'cuda'
self.is_cuda = device.type == 'cuda' and torch.cuda.is_available()
self.is_npu = device.type == 'npu' and torch.npu.is_available()

def __iter__(self):
first = True
if self.is_cuda:
stream = torch.cuda.Stream()
stream_context = partial(torch.cuda.stream, stream=stream)
elif self.is_npu:
stream = torch.npu.Stream()
stream_context = partial(torch.npu.stream, stream=stream)
else:
stream = None
stream_context = suppress
Expand All @@ -139,7 +143,10 @@ def __iter__(self):
first = False

if stream is not None:
torch.cuda.current_stream().wait_stream(stream)
if self.is_cuda:
torch.cuda.current_stream().wait_stream(stream)
elif self.is_npu:
torch.npu.current_stream().wait_stream(stream)

input = next_input
target = next_target
Expand Down
36 changes: 30 additions & 6 deletions timm/layers/fast_norm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,30 @@
_USE_FAST_NORM = False # defaulting to False for now


def get_autocast_dtype(device: str = 'cuda'):
try:
return torch.get_autocast_dtype(device)
except (AttributeError, TypeError):
# dispatch to older device specific fns, only covering cuda/cpu devices here
if device == 'cpu':
return torch.get_autocast_cpu_dtype()
else:
assert device == 'cuda'
return torch.get_autocast_gpu_dtype()


def is_autocast_enabled(device: str = 'cuda'):
try:
return torch.is_autocast_enabled(device)
except TypeError:
# dispatch to older device specific fns, only covering cuda/cpu devices here
if device == 'cpu':
return torch.is_autocast_cpu_enabled()
else:
assert device == 'cuda'
return torch.is_autocast_enabled() # defaults cuda (only cuda on older pytorch)


def is_fast_norm():
return _USE_FAST_NORM

Expand All @@ -48,14 +72,14 @@ def fast_group_norm(
# currently cannot use is_autocast_enabled within torchscript
return F.group_norm(x, num_groups, weight, bias, eps)

if torch.is_autocast_enabled():
if is_autocast_enabled(x.device.type):
# normally native AMP casts GN inputs to float32
# here we use the low precision autocast dtype
# FIXME what to do re CPU autocast?
dt = torch.get_autocast_gpu_dtype()
dt = get_autocast_dtype(x.device.type)
x, weight, bias = x.to(dt), weight.to(dt), bias.to(dt) if bias is not None else None

with torch.cuda.amp.autocast(enabled=False):
with torch.amp.autocast(device_type=x.device.type, enabled=False):
return F.group_norm(x, num_groups, weight, bias, eps)


Expand All @@ -73,14 +97,14 @@ def fast_layer_norm(
if has_apex:
return fused_layer_norm_affine(x, weight, bias, normalized_shape, eps)

if torch.is_autocast_enabled():
if is_autocast_enabled(x.device.type):
# normally native AMP casts LN inputs to float32
# apex LN does not, this is behaving like Apex
dt = torch.get_autocast_gpu_dtype()
dt = get_autocast_dtype(x.device.type)
# FIXME what to do re CPU autocast?
x, weight, bias = x.to(dt), weight.to(dt), bias.to(dt) if bias is not None else None

with torch.cuda.amp.autocast(enabled=False):
with torch.amp.autocast(device_type=x.device.type, enabled=False):
return F.layer_norm(x, normalized_shape, weight, bias, eps)


Expand Down
7 changes: 5 additions & 2 deletions timm/utils/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ def load_state_dict(self, state_dict):
class NativeScaler:
state_dict_key = "amp_scaler"

def __init__(self):
self._scaler = torch.cuda.amp.GradScaler()
def __init__(self, device='cuda'):
try:
self._scaler = torch.amp.GradScaler(device=device)
except (AttributeError, TypeError) as e:
self._scaler = torch.cuda.amp.GradScaler()

def __call__(
self,
Expand Down
3 changes: 3 additions & 0 deletions timm/utils/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def init_distributed_device_so(
"xpu": "ccl",
"hpu": "hccl",
"cuda": "nccl",
"npu": "hccl",
}
dist_backend = dist_backends.get(device_type, 'gloo')
dist_url = dist_url or 'env://'
Expand Down Expand Up @@ -159,6 +160,8 @@ def init_distributed_device_so(

if device_type == 'cuda':
assert torch.cuda.is_available(), f'CUDA is not available but {device} was specified.'
if device_type == 'npu':
assert torch.npu.is_available(), f'Ascend NPU is not available but {device} was specified.'

if distributed and device != 'cpu':
# Ignore manually specified device index in distributed mode and
Expand Down
27 changes: 10 additions & 17 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
except ImportError:
has_apex = False

has_native_amp = False
try:
if getattr(torch.cuda.amp, 'autocast') is not None:
has_native_amp = True
except AttributeError:
pass

try:
import wandb
Expand Down Expand Up @@ -442,7 +436,6 @@ def main():
use_amp = 'apex'
assert args.amp_dtype == 'float16'
else:
assert has_native_amp, 'Please update PyTorch to a version with native AMP (or use APEX).'
use_amp = 'native'
assert args.amp_dtype in ('float16', 'bfloat16')
if args.amp_dtype == 'bfloat16':
Expand Down Expand Up @@ -572,15 +565,10 @@ def main():
if utils.is_primary(args):
_logger.info('Using NVIDIA APEX AMP. Training in mixed precision.')
elif use_amp == 'native':
try:
amp_autocast = partial(torch.autocast, device_type=device.type, dtype=amp_dtype)
except (AttributeError, TypeError):
# fallback to CUDA only AMP for PyTorch < 1.10
assert device.type == 'cuda'
amp_autocast = torch.cuda.amp.autocast
if device.type == 'cuda' and amp_dtype == torch.float16:
amp_autocast = partial(torch.autocast, device_type=device.type, dtype=amp_dtype)
if device.type in ('cuda',) and amp_dtype == torch.float16:
# loss scaler only used for float16 (half) dtype, bfloat16 does not need it
loss_scaler = NativeScaler()
loss_scaler = NativeScaler(device=device.type)
if utils.is_primary(args):
_logger.info('Using native Torch AMP. Training in mixed precision.')
else:
Expand Down Expand Up @@ -1054,8 +1042,11 @@ def _backward(_loss):
if model_ema is not None:
model_ema.update(model, step=num_updates)

if args.synchronize_step and device.type == 'cuda':
torch.cuda.synchronize()
if args.synchronize_step:
if device.type == 'cuda':
torch.cuda.synchronize()
elif device.type == 'npu':
torch.npu.synchronize()
time_now = time.time()
update_time_m.update(time.time() - update_start_time)
update_start_time = time_now
Expand Down Expand Up @@ -1155,6 +1146,8 @@ def validate(

if device.type == 'cuda':
torch.cuda.synchronize()
elif device.type == "npu":
torch.npu.synchronize()

losses_m.update(reduced_loss.item(), input.size(0))
top1_m.update(acc1.item(), output.size(0))
Expand Down
12 changes: 3 additions & 9 deletions validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@
except ImportError:
has_apex = False

has_native_amp = False
try:
if getattr(torch.cuda.amp, 'autocast') is not None:
has_native_amp = True
except AttributeError:
pass

try:
from functorch.compile import memory_efficient_fusion
has_functorch = True
Expand Down Expand Up @@ -183,7 +176,6 @@ def validate(args):
use_amp = 'apex'
_logger.info('Validating in mixed precision with NVIDIA APEX AMP.')
else:
assert has_native_amp, 'Please update PyTorch to a version with native AMP (or use APEX).'
assert args.amp_dtype in ('float16', 'bfloat16')
use_amp = 'native'
amp_dtype = torch.bfloat16 if args.amp_dtype == 'bfloat16' else torch.float16
Expand Down Expand Up @@ -395,8 +387,10 @@ def _try_run(args, initial_batch_size):
while batch_size:
args.batch_size = batch_size * args.num_gpu # multiply by num-gpu for DataParallel case
try:
if torch.cuda.is_available() and 'cuda' in args.device:
if 'cuda' in args.device and torch.cuda.is_available():
torch.cuda.empty_cache()
elif "npu" in args.device and torch.npu.is_available():
torch.npu.empty_cache()
results = validate(args)
return results
except RuntimeError as e:
Expand Down
Loading