mirror of
https://github.com/hpcaitech/ColossalAI.git
synced 2025-09-17 23:18:36 +00:00
[npu] change device to accelerator api (#5239)
* update accelerator * fix timer * fix amp * update * fix * update bug * add error raise * fix autocast * fix set device * remove doc accelerator * update doc * update doc * update doc * use nullcontext * update cpu * update null context * change time limit for example * udpate * update * update * update * [npu] polish accelerator code --------- Co-authored-by: Xuanlei Zhao <xuanlei.zhao@gmail.com> Co-authored-by: zxl <43881818+oahzxl@users.noreply.github.com>
This commit is contained in:
@@ -6,8 +6,8 @@ import weakref
|
||||
import torch
|
||||
from torch.utils.checkpoint import check_backward_validity, detach_variable
|
||||
|
||||
from colossalai.accelerator import get_accelerator
|
||||
from colossalai.legacy.context.random import get_current_mode, get_states, set_mode, set_seed_states, sync_states
|
||||
from colossalai.utils.device import autocast, get_current_device
|
||||
|
||||
|
||||
def copy_to_device(obj, device):
|
||||
@@ -33,7 +33,7 @@ class CheckpointFunction(torch.autograd.Function):
|
||||
check_backward_validity(args)
|
||||
ctx.run_function = run_function
|
||||
ctx.activation_offload = activation_offload
|
||||
ctx.device = get_current_device()
|
||||
ctx.device = get_accelerator().get_current_device()
|
||||
|
||||
# preserve rng states
|
||||
ctx.fwd_cpu_rng_state = torch.get_rng_state()
|
||||
@@ -110,7 +110,7 @@ class CheckpointFunction(torch.autograd.Function):
|
||||
inputs[idx] = tensors[i]
|
||||
detached_inputs = detach_variable(tuple(inputs))
|
||||
if ctx.had_autocast_in_fwd:
|
||||
with torch.enable_grad(), autocast():
|
||||
with torch.enable_grad(), get_accelerator().autocast()():
|
||||
outputs = ctx.run_function(*detached_inputs)
|
||||
else:
|
||||
with torch.enable_grad():
|
||||
@@ -226,7 +226,7 @@ def _checkpoint_without_reentrant(function, activation_offload=False, *args):
|
||||
|
||||
# rerun forward, the inner_pack will store all the activations in storage
|
||||
if has_autocast_in_fwd:
|
||||
with torch.enable_grad(), autocast(), torch.autograd.graph.saved_tensors_hooks(
|
||||
with torch.enable_grad(), get_accelerator().autocast()(), torch.autograd.graph.saved_tensors_hooks(
|
||||
inner_pack, inner_unpack
|
||||
):
|
||||
_unused = function(*args)
|
||||
@@ -245,7 +245,7 @@ def _checkpoint_without_reentrant(function, activation_offload=False, *args):
|
||||
|
||||
# get device if we need to offload the activation
|
||||
if activation_offload:
|
||||
device = get_current_device()
|
||||
device = get_accelerator().get_current_device()
|
||||
|
||||
# run function with pack and unpack as saved_tensors_hooks
|
||||
with torch.autograd.graph.saved_tensors_hooks(pack, unpack):
|
||||
|
@@ -6,9 +6,9 @@ import torch
|
||||
import torch.distributed as dist
|
||||
from packaging import version
|
||||
|
||||
from colossalai.accelerator import get_accelerator
|
||||
from colossalai.legacy.core import global_context as gpc
|
||||
from colossalai.logging import get_dist_logger
|
||||
from colossalai.utils import get_current_device
|
||||
|
||||
_GLOBAL_CUDA_MEM_FRACTION = 1.0
|
||||
_GLOBAL_CPU_MEM_CAPACITY = -1
|
||||
@@ -112,7 +112,10 @@ def colo_device_memory_capacity(device: torch.device) -> int:
|
||||
# In the context of 1-CPU-N-GPU, the memory capacity of the current process is 1/N overall CPU memory.
|
||||
return colo_get_cpu_memory_capacity() / gpc.num_processes_on_current_node
|
||||
if device.type == "cuda":
|
||||
return torch.cuda.get_device_properties(get_current_device()).total_memory * _GLOBAL_CUDA_MEM_FRACTION
|
||||
return (
|
||||
torch.cuda.get_device_properties(get_accelerator().get_current_device()).total_memory
|
||||
* _GLOBAL_CUDA_MEM_FRACTION
|
||||
)
|
||||
|
||||
|
||||
def colo_device_memory_used(device: torch.device) -> int:
|
||||
@@ -153,7 +156,7 @@ def colo_set_process_memory_fraction(ratio: float) -> None:
|
||||
return
|
||||
global _GLOBAL_CUDA_MEM_FRACTION
|
||||
_GLOBAL_CUDA_MEM_FRACTION = ratio
|
||||
torch.cuda.set_per_process_memory_fraction(_GLOBAL_CUDA_MEM_FRACTION, get_current_device())
|
||||
torch.cuda.set_per_process_memory_fraction(_GLOBAL_CUDA_MEM_FRACTION, get_accelerator().get_current_device())
|
||||
|
||||
|
||||
def colo_set_cpu_memory_capacity(size: int) -> None:
|
||||
|
@@ -8,7 +8,7 @@ import torch.distributed as dist
|
||||
from torch.autograd.profiler import profile
|
||||
from torch.distributed import ReduceOp
|
||||
|
||||
from colossalai.utils import get_current_device
|
||||
from colossalai.accelerator import get_accelerator
|
||||
|
||||
from .prof_utils import BaseProfiler, _format_bandwidth, _format_memory, _format_time
|
||||
|
||||
@@ -177,7 +177,7 @@ class CommProfiler(BaseProfiler):
|
||||
|
||||
assert current_comm_event is not None, "dist op has not been found"
|
||||
|
||||
buffer = torch.tensor([current_comm_event.self_cuda_time], device=get_current_device())
|
||||
buffer = torch.tensor([current_comm_event.self_cuda_time], device=get_accelerator().get_current_device())
|
||||
torch_all_reduce(buffer, op=ReduceOp.MIN, group=group)
|
||||
current_comm_event.self_cuda_time = buffer.item()
|
||||
|
||||
|
Reference in New Issue
Block a user