[Feature] Support for Ascend NPU backend (#3853)
Signed-off-by: Song Zhang <gepin.zs@antgroup.com> Co-authored-by: 22dimensions <waitingwind@foxmail.com>
This commit is contained in:
@@ -145,6 +145,10 @@ def is_xpu() -> bool:
|
||||
return hasattr(torch, "xpu") and torch.xpu.is_available()
|
||||
|
||||
|
||||
def is_npu() -> bool:
|
||||
return hasattr(torch, "npu") and torch.npu.is_available()
|
||||
|
||||
|
||||
def is_flashinfer_available():
|
||||
"""
|
||||
Check whether flashinfer is available.
|
||||
@@ -328,6 +332,16 @@ def get_available_gpu_memory(device, gpu_id, distributed=False, empty_cache=True
|
||||
elif device == "cpu":
|
||||
# TODO: rename the variables in the current function to be not GPU specific
|
||||
free_gpu_memory = psutil.virtual_memory().available
|
||||
elif device == "npu":
|
||||
num_gpus = torch.npu.device_count()
|
||||
assert gpu_id < num_gpus
|
||||
|
||||
if torch.npu.current_device() != gpu_id:
|
||||
print(
|
||||
f"WARNING: current device is not {gpu_id}, but {torch.npu.current_device()}, ",
|
||||
"which may cause useless memory allocation for torch NPU context.",
|
||||
)
|
||||
free_gpu_memory, total_gpu_memory = torch.npu.mem_get_info()
|
||||
|
||||
if distributed:
|
||||
tensor = torch.tensor(free_gpu_memory, dtype=torch.float32).to(
|
||||
@@ -1348,6 +1362,9 @@ def get_device_name(device_id: int = 0) -> str:
|
||||
if hasattr(torch, "hpu") and torch.hpu.is_available():
|
||||
return torch.hpu.get_device_name(device_id)
|
||||
|
||||
if hasattr(torch, "npu") and torch.npu.is_available():
|
||||
return torch.npu.get_device_name(device_id)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def is_habana_available() -> bool:
|
||||
@@ -1444,6 +1461,13 @@ def get_compiler_backend() -> str:
|
||||
if hasattr(torch, "hpu") and torch.hpu.is_available():
|
||||
return "hpu_backend"
|
||||
|
||||
if hasattr(torch, "npu") and torch.npu.is_available():
|
||||
import torchair
|
||||
|
||||
config = torchair.CompilerConfig()
|
||||
npu_backend = torchair.get_npu_backend(compiler_config=config)
|
||||
return npu_backend
|
||||
|
||||
return "inductor"
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user