mirror of
https://github.com/huggingface/pytorch-image-models.git
synced 2025-06-03 15:01:08 +08:00
* update * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * merge with poolformer, initial version * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Revert "Update metaformers.py" This reverts commit 2916f37f8d862cd4fcbc86c858b23894daec3339. * Revert "Update metaformers.py" This reverts commit 1d882eb494caf5b5a3a1e9c7442d883e1707998e. * Revert "Update metaformers.py" This reverts commit 2209d0830eed9b87149df4a1f25f65da46d47f4b. * Revert "Update metaformers.py" This reverts commit 32bede4e279ee1027a63d5b55591153e53c209ec. * Revert "Update metaformers.py" This reverts commit 4ed934e00068be576881a87d5218b355ca01a6be. * Revert "Update metaformers.py" This reverts commit 3f0b07536764c47af42290a218a15097b5cec1a0. * Revert "Update metaformers.py" This reverts commit 2fef9006d72824789b733dd8dbc20b21b79add0b. * Update metaformers.py * Update metaformers.py * rename model * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Stem/Downsample rework * Update metaformers.py * try NHWC * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Squashed commit of the following: commit b7696a30a772dbbb2e00d81e7096c24dac97df73 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Fri Feb 10 01:46:44 2023 -0800 Update metaformers.py commit 41fe5c36263b40a6cd7caddb85b10c5d82d48023 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Fri Feb 10 01:03:47 2023 -0800 Update metaformers.py commit a3aee37c35985c01ca07902d860f809b648c612c Author: Fredo Guan <fredo.guan@hotmail.com> Date: Fri Feb 10 00:32:04 2023 -0800 Update metaformers.py commit f938beb81b4f46851d6d6f04ae7a9a74871ee40d Author: Fredo Guan <fredo.guan@hotmail.com> Date: Fri Feb 10 00:24:58 2023 -0800 Update metaformers.py commit 10bde717e51c95cdf20135c8bba77a7a1b00d78c Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sun Feb 5 02:11:28 2023 -0800 Update metaformers.py commit 39274bd45e78b8ead0509367f800121f0d7c25f4 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sun Feb 5 02:06:58 2023 -0800 Update metaformers.py commit a2329ab8ec00d0ebc00979690293c3887cc44a4c Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sun Feb 5 02:03:34 2023 -0800 Update metaformers.py commit 53b8ce5b8a6b6d828de61788bcc2e6043ebb3081 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sun Feb 5 02:02:37 2023 -0800 Update metaformers.py commit ab6225b9414f534815958036f6d5a392038d7ab2 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sun Feb 5 01:04:55 2023 -0800 try NHWC commit 02fcc30eaa67a3c92cae56f3062b2542c32c9283 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sat Feb 4 23:47:06 2023 -0800 Update metaformers.py commit 366aae93047934bd3d7d37a077e713d424fa429c Author: Fredo Guan <fredo.guan@hotmail.com> Date: Sat Feb 4 23:37:30 2023 -0800 Stem/Downsample rework commit 26a8e481a5cb2a32004a796bc25c6800cd2fb7b7 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Wed Feb 1 07:42:07 2023 -0800 Update metaformers.py commit a913f5d4384aa4b2f62fdab46254ae3772df00ee Author: Fredo Guan <fredo.guan@hotmail.com> Date: Wed Feb 1 07:41:24 2023 -0800 Update metaformers.py * Update metaformers.py * Update metaformers.py * channels first for whole network * Channels first * Update metaformers.py * Use buffer for randformer * Update metaformers.py * Remove einsum * don't test randformer for feature extraction * arbitrary input sizes for randformer * Squashed commit of the following: commit 6c089ca4325ab10942fe56e0999dcc1a11e1d2f0 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 02:11:17 2023 -0800 Update metaformers.py commit 521528a900e49ef8f462f5ccd795efb3a5d14214 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 02:06:08 2023 -0800 Update metaformers.py commit 3827eec7963698ff727fbb13ace53594ceb374d5 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 02:03:08 2023 -0800 Update metaformers.py commit ac1c6fea8adcd846e031ea0f5fa81ffe63d3c4bb Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 02:01:04 2023 -0800 Update metaformers.py commit 26f3d343cdc46183543f83482187f669f3181ddf Merge: d577129 f736730 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 01:57:29 2023 -0800 Merge branch 'metaformer_workspace' of https://github.com/fffffgggg54/pytorch-image-models into metaformer_workspace commit d577129aaa23fb348a8bb93bcd17cf1d5a4e8ff8 Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 01:57:20 2023 -0800 Update metaformers.py commit f7367304e8f3b7a9a7f16e0a032bb72546afcc2a Author: Fredo Guan <fredo.guan@hotmail.com> Date: Mon Mar 6 01:56:11 2023 -0800 Metaformer baselines for vision (#12) * formatting, cleanup, fix dropout * fix regression, pass kwargs * fix poolformerv1 weights, formatting * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * Update metaformers.py * some cleanup * SDPA from ViT, fix imports * Update metaformers.py * fix head reset * fast norm bias patch for metaformers * Metaformer refactor, remove rand/ident models, fix issues, remove old poolformer * Switch to hub weights --------- Co-authored-by: Fredo Guan <fredo.guan@hotmail.com>
127 lines
3.9 KiB
Python
127 lines
3.9 KiB
Python
""" 'Fast' Normalization Functions
|
|
|
|
For GroupNorm and LayerNorm these functions bypass typical AMP upcast to float32.
|
|
|
|
Additionally, for LayerNorm, the APEX fused LN is used if available (which also does not upcast)
|
|
|
|
Hacked together by / Copyright 2022 Ross Wightman
|
|
"""
|
|
from typing import List, Optional
|
|
|
|
import torch
|
|
from torch.nn import functional as F
|
|
|
|
try:
|
|
from apex.normalization.fused_layer_norm import fused_layer_norm_affine
|
|
has_apex = True
|
|
except ImportError:
|
|
has_apex = False
|
|
|
|
try:
|
|
from apex.normalization.fused_layer_norm import fused_rms_norm_affine, fused_rms_norm
|
|
has_apex_rmsnorm = True
|
|
except ImportError:
|
|
has_apex_rmsnorm = False
|
|
|
|
|
|
# fast (ie lower precision LN) can be disabled with this flag if issues crop up
|
|
_USE_FAST_NORM = False # defaulting to False for now
|
|
|
|
|
|
def is_fast_norm():
|
|
return _USE_FAST_NORM
|
|
|
|
|
|
def set_fast_norm(enable=True):
|
|
global _USE_FAST_NORM
|
|
_USE_FAST_NORM = enable
|
|
|
|
|
|
def fast_group_norm(
|
|
x: torch.Tensor,
|
|
num_groups: int,
|
|
weight: Optional[torch.Tensor] = None,
|
|
bias: Optional[torch.Tensor] = None,
|
|
eps: float = 1e-5
|
|
) -> torch.Tensor:
|
|
if torch.jit.is_scripting():
|
|
# currently cannot use is_autocast_enabled within torchscript
|
|
return F.group_norm(x, num_groups, weight, bias, eps)
|
|
|
|
if torch.is_autocast_enabled():
|
|
# 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()
|
|
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):
|
|
return F.group_norm(x, num_groups, weight, bias, eps)
|
|
|
|
|
|
def fast_layer_norm(
|
|
x: torch.Tensor,
|
|
normalized_shape: List[int],
|
|
weight: Optional[torch.Tensor] = None,
|
|
bias: Optional[torch.Tensor] = None,
|
|
eps: float = 1e-5
|
|
) -> torch.Tensor:
|
|
if torch.jit.is_scripting():
|
|
# currently cannot use is_autocast_enabled within torchscript
|
|
return F.layer_norm(x, normalized_shape, weight, bias, eps)
|
|
|
|
if has_apex:
|
|
return fused_layer_norm_affine(x, weight, bias, normalized_shape, eps)
|
|
|
|
if torch.is_autocast_enabled():
|
|
# normally native AMP casts LN inputs to float32
|
|
# apex LN does not, this is behaving like Apex
|
|
dt = torch.get_autocast_gpu_dtype()
|
|
# 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):
|
|
return F.layer_norm(x, normalized_shape, weight, bias, eps)
|
|
|
|
|
|
def rms_norm(
|
|
x: torch.Tensor,
|
|
normalized_shape: List[int],
|
|
weight: Optional[torch.Tensor] = None,
|
|
eps: float = 1e-5,
|
|
):
|
|
norm_ndim = len(normalized_shape)
|
|
if torch.jit.is_scripting():
|
|
# ndim = len(x.shape)
|
|
# dims = list(range(ndim - norm_ndim, ndim)) # this doesn't work on pytorch <= 1.13.x
|
|
# NOTE -ve dims cause torchscript to crash in some cases, out of options to work around
|
|
assert norm_ndim == 1
|
|
v = torch.var(x, dim=-1).unsqueeze(-1) # ts crashes with -ve dim + keepdim=True
|
|
else:
|
|
dims = tuple(range(-1, -norm_ndim - 1, -1))
|
|
v = torch.var(x, dim=dims, keepdim=True)
|
|
x = x * torch.rsqrt(v + eps)
|
|
if weight is not None:
|
|
x = x * weight
|
|
return x
|
|
|
|
|
|
def fast_rms_norm(
|
|
x: torch.Tensor,
|
|
normalized_shape: List[int],
|
|
weight: Optional[torch.Tensor] = None,
|
|
eps: float = 1e-5,
|
|
) -> torch.Tensor:
|
|
if torch.jit.is_scripting():
|
|
# this must be by itself, cannot merge with has_apex_rmsnorm
|
|
return rms_norm(x, normalized_shape, weight, eps)
|
|
|
|
if has_apex_rmsnorm:
|
|
if weight is None:
|
|
return fused_rms_norm(x, normalized_shape, eps)
|
|
else:
|
|
return fused_rms_norm_affine(x, weight, normalized_shape, eps)
|
|
|
|
# fallback
|
|
return rms_norm(x, normalized_shape, weight, eps)
|