Skip to content
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
3 changes: 2 additions & 1 deletion mindnlp/core/npu/amp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from .autocast_mode import autocast, custom_bwd, custom_fwd

from .grad_scaler import GradScaler

__all__ = [
"autocast",
"custom_bwd",
"custom_fwd",
"GradScaler"
]
38 changes: 38 additions & 0 deletions mindnlp/core/npu/amp/grad_scaler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing_extensions import deprecated

from mindnlp import core

# We need to keep this unused import for BC reasons
from ...amp.grad_scaler import OptState # noqa: F401


__all__ = ["GradScaler"]


class GradScaler(core.amp.GradScaler):
r"""
See :class:`torch.amp.GradScaler`.
``torch.npu.amp.GradScaler(args...)`` is deprecated. Please use ``torch.amp.GradScaler("npu", args...)`` instead.
"""

@deprecated(
"`torch.npu.amp.GradScaler(args...)` is deprecated. "
"Please use `torch.amp.GradScaler('npu', args...)` instead.",
category=FutureWarning,
)
def __init__(
self,
init_scale: float = 2.0**16,
growth_factor: float = 2.0,
backoff_factor: float = 0.5,
growth_interval: int = 2000,
enabled: bool = True,
) -> None:
super().__init__(
"npu",
init_scale=init_scale,
growth_factor=growth_factor,
backoff_factor=backoff_factor,
growth_interval=growth_interval,
enabled=enabled,
)
6 changes: 4 additions & 2 deletions mindnlp/core/ops/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,13 @@ def empty(*size, dtype=None, device=None, requires_grad=False, pin_memory=False,
device = 'CPU'
elif device.lower() == 'npu':
device = 'Ascend'
else:
elif device.lower() == 'cuda':
device = 'GPU'
else:
device = 'meta'

# To avoid the problem in irecv and recv of using empty.
if has_empty and use_pyboost():
if device != 'meta':
out = mindspore.mint.empty(size, dtype=dtype, device=device)
else:
out = CTensor(dtype=dtype, shape=size)
Expand Down
4 changes: 4 additions & 0 deletions mindnlp/utils/torch_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import importlib.machinery
from types import ModuleType

from mindnlp.core.configs import DEVICE_TARGET

TORCH_VERSION = '2.7.1+dev'

class RedirectFinder(importlib.abc.MetaPathFinder):
Expand All @@ -19,6 +21,8 @@ def find_spec(self, fullname, path, target=None):
if fullname == proxy_prefix or fullname.startswith(proxy_prefix + "."):
# 计算实际模块名
target_name = fullname.replace(proxy_prefix, target_prefix, 1)
if DEVICE_TARGET == 'Ascend':
target_name = target_name.replace('cuda', 'npu')
try:
importlib.import_module(target_name)
except Exception as e:
Expand Down
Loading