已合并
[master][bugfix]cann and pta header mixing bulid bugfix #44990
Dring创建于 8月20日
[master][bugfix]cann and pta header mixing bulid bugfix #44990
已合并
Dring创建于 8月20日
共 82 个文件变更+1465-1596
@@ -242,8 +242,6 @@ def copy_hpp():
242 "torch_npu/csrc/inductor/**/*.h",242 "torch_npu/csrc/inductor/**/*.h",
243 "torch_npu/csrc/distributed/*.h",243 "torch_npu/csrc/distributed/*.h",
244 "torch_npu/csrc/distributed/*.hpp",244 "torch_npu/csrc/distributed/*.hpp",
245- "third_party/acl/inc/*/*.h",
246- "third_party/acl/inc/*/*/*.h",
247 "third_party/hccl/inc/*/*.h",245 "third_party/hccl/inc/*/*.h",
248 ]246 ]
249 glob_header_files = []247 glob_header_files = []
@@ -257,6 +255,39 @@ def copy_hpp():
257 os.makedirs(os.path.dirname(dst), exist_ok=True)255 os.makedirs(os.path.dirname(dst), exist_ok=True)
258 ret.append((src, dst))256 ret.append((src, dst))
259 257 
258+ acl_include_root = os.path.join(BASE_DIR, "third_party", "acl", "inc")
259+ acl_header_files = glob.glob(
260+ os.path.join(acl_include_root, "**", "*.h"),
261+ recursive=True,
262+ )
263+ for src in acl_header_files:
264+ relative_header = os.path.relpath(src, acl_include_root)
265+ dst = os.path.join(
266+ BASE_DIR,
267+ "libtorch_npu/include",
268+ relative_header,
269+ )
270+ os.makedirs(os.path.dirname(dst), exist_ok=True)
271+ ret.append((src, dst))
272+ 
273+ # Preserve legacy include paths with forwarding headers, not duplicate ACL headers.
274+ compatibility_src = os.path.join(
275+ BASE_DIR,
276+ "build/acl_compat_headers",
277+ relative_header,
278+ )
279+ os.makedirs(os.path.dirname(compatibility_src), exist_ok=True)
280+ compatibility_include = relative_header.replace(os.sep, "/")
281+ with open(compatibility_src, "w", encoding="utf-8", newline="\n") as compatibility_header:
282+ compatibility_header.write(f"#pragma once\n#include <{compatibility_include}>\n")
283+ compatibility_dst = os.path.join(
284+ BASE_DIR,
285+ "libtorch_npu/include/third_party/acl/inc",
286+ relative_header,
287+ )
288+ os.makedirs(os.path.dirname(compatibility_dst), exist_ok=True)
289+ ret.append((compatibility_src, compatibility_dst))
290+ 
260 return ret291 return ret
261 ret = get_src_py_and_dst()292 ret = get_src_py_and_dst()
262 for src, dst in ret:293 for src, dst in ret:
@@ -61,7 +61,7 @@ def fetch_acl_headers():
61 try:61 try:
62 import torch_npu62 import torch_npu
63 installed_acl = Path(63 installed_acl = Path(
64- torch_npu.__file__).resolve().parent / 'include' / 'third_party' / 'acl' / 'inc' / 'acl'64+ torch_npu.__file__).resolve().parent / 'include' / 'acl'
65 if installed_acl.is_dir():65 if installed_acl.is_dir():
66 acl_dest.mkdir(parents=True, exist_ok=True)66 acl_dest.mkdir(parents=True, exist_ok=True)
67 shutil.copytree(str(installed_acl), str(acl_dest), dirs_exist_ok=True)67 shutil.copytree(str(installed_acl), str(acl_dest), dirs_exist_ok=True)
Msetup.py+33-2
@@ -477,9 +477,7 @@ def get_src_py_and_dst():
477 "torch_npu/csrc/*/*/*.h",477 "torch_npu/csrc/*/*/*.h",
478 "torch_npu/csrc/*/*/*/*.h",478 "torch_npu/csrc/*/*/*/*.h",
479 "torch_npu/csrc/*/*/*/*/*.h",479 "torch_npu/csrc/*/*/*/*/*.h",
480- "third_party/acl/inc/*/*.h",
481 "third_party/hccl/inc/*/*.h",480 "third_party/hccl/inc/*/*.h",
482- "third_party/acl/inc/*/*/*.h",
483 "torch_npu/csrc/distributed/HCCLUtils.hpp",481 "torch_npu/csrc/distributed/HCCLUtils.hpp",
484 "torch_npu/csrc/distributed/ProcessGroupHCCL.hpp"482 "torch_npu/csrc/distributed/ProcessGroupHCCL.hpp"
485 ]483 ]
@@ -495,6 +493,39 @@ def get_src_py_and_dst():
495 os.makedirs(os.path.dirname(dst), exist_ok=True)493 os.makedirs(os.path.dirname(dst), exist_ok=True)
496 ret.append((src, dst))494 ret.append((src, dst))
497 495 
496+ acl_include_root = os.path.join(BASE_DIR, "third_party", "acl", "inc")
497+ acl_header_files = glob.glob(
498+ os.path.join(acl_include_root, "**", "*.h"),
499+ recursive=True,
500+ )
501+ for src in acl_header_files:
502+ relative_header = os.path.relpath(src, acl_include_root)
503+ dst = os.path.join(
504+ BASE_DIR,
505+ "build/packages/torch_npu/include",
506+ relative_header,
507+ )
508+ os.makedirs(os.path.dirname(dst), exist_ok=True)
509+ ret.append((src, dst))
510+ 
511+ # Preserve legacy include paths with forwarding headers, not duplicate ACL headers.
512+ compatibility_src = os.path.join(
513+ BASE_DIR,
514+ "build/acl_compat_headers",
515+ relative_header,
516+ )
517+ os.makedirs(os.path.dirname(compatibility_src), exist_ok=True)
518+ compatibility_include = relative_header.replace(os.sep, "/")
519+ with open(compatibility_src, "w", encoding="utf-8", newline="\n") as compatibility_header:
520+ compatibility_header.write(f"#pragma once\n#include <{compatibility_include}>\n")
521+ compatibility_dst = os.path.join(
522+ BASE_DIR,
523+ "build/packages/torch_npu/include/third_party/acl/inc",
524+ relative_header,
525+ )
526+ os.makedirs(os.path.dirname(compatibility_dst), exist_ok=True)
527+ ret.append((compatibility_src, compatibility_dst))
528+ 
498 torch_header_files = [529 torch_header_files = [
499 "*/*.h",530 "*/*.h",
500 "*/*/*.h",531 "*/*/*.h",
@@ -1,5 +1,4 @@
1import os1import os
2-import sys
3import shutil2import shutil
4import subprocess3import subprocess
5import ctypes4import ctypes
@@ -21,7 +20,12 @@ def create_build_path(build_directory):
21 20 
22 21 
23def build_stub(base_dir):22def build_stub(base_dir):
24- build_stub_cmd = ["sh", os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh')]23+ build_stub_cmd = [
24+ "sh",
25+ os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh'),
26+ # Build the stub and Extension against the same installed header root.
27+ os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'),
28+ ]
25 if subprocess.call(build_stub_cmd) != 0:29 if subprocess.call(build_stub_cmd) != 0:
26 raise RuntimeError('Failed to build stub: {}'.format(build_stub_cmd))30 raise RuntimeError('Failed to build stub: {}'.format(build_stub_cmd))
27 31 
@@ -47,7 +51,6 @@ class TestPluggableAllocator(TestCase):
47 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")51 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")
48 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]52 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]
49 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'))53 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'))
50- extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include', 'third_party', 'acl', 'inc'))
51 54 
52 cls.module = torch.utils.cpp_extension.load(55 cls.module = torch.utils.cpp_extension.load(
53 name="pluggable_allocator_extensions",56 name="pluggable_allocator_extensions",
@@ -77,7 +80,7 @@ class TestPluggableAllocator(TestCase):
77 def test_set_get_device_stats_fn(self):80 def test_set_get_device_stats_fn(self):
78 os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')81 os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
79 myallocator = ctypes.CDLL(os_path)82 myallocator = ctypes.CDLL(os_path)
80- get_device_stats_fn = ctypes.cast(getattr(myallocator, "my_get_device_stats"), ctypes.c_void_p).value83+ get_device_stats_fn = ctypes.cast(myallocator.my_get_device_stats, ctypes.c_void_p).value
81 84 
82 TestPluggableAllocator.new_alloc.allocator().set_get_device_stats_fn(get_device_stats_fn)85 TestPluggableAllocator.new_alloc.allocator().set_get_device_stats_fn(get_device_stats_fn)
83 self.assertEqual(torch.npu.memory_stats_as_nested_dict()["num_alloc_retries"], 0)86 self.assertEqual(torch.npu.memory_stats_as_nested_dict()["num_alloc_retries"], 0)
@@ -85,7 +88,7 @@ class TestPluggableAllocator(TestCase):
85 def test_set_reset_peak_status_fn(self):88 def test_set_reset_peak_status_fn(self):
86 os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')89 os_path = os.path.join(TestPluggableAllocator.build_directory, 'pluggable_allocator_extensions.so')
87 myallocator = ctypes.CDLL(os_path)90 myallocator = ctypes.CDLL(os_path)
88- reset_peak_status_fn = ctypes.cast(getattr(myallocator, "my_reset_peak_status"), ctypes.c_void_p).value91+ reset_peak_status_fn = ctypes.cast(myallocator.my_reset_peak_status, ctypes.c_void_p).value
89 92 
90 TestPluggableAllocator.new_alloc.allocator().set_reset_peak_status_fn(reset_peak_status_fn)93 TestPluggableAllocator.new_alloc.allocator().set_reset_peak_status_fn(reset_peak_status_fn)
91 torch.npu.reset_peak_memory_stats()94 torch.npu.reset_peak_memory_stats()
@@ -3,8 +3,8 @@
3#include <iostream>3#include <iostream>
4#include <torch/extension.h>4#include <torch/extension.h>
5 5 
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7-#include "third_party/acl/inc/acl/acl_rt.h"7+#include <acl/acl_rt.h>
8#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"8#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"
9 9 
10extern "C" {10extern "C" {
@@ -3,16 +3,14 @@ import time
3import unittest3import unittest
4import warnings4import warnings
5import torch5import torch
6-import torch.distributed as dist
7import torch.distributed.autograd as dist_autograd6import torch.distributed.autograd as dist_autograd
8import torch.distributed.rpc as rpc7import torch.distributed.rpc as rpc
9from torch import multiprocessing as mp8from torch import multiprocessing as mp
10-from torch import nn, Tensor9+from torch import nn
11from torch.distributed.nn.api.remote_module import RemoteModule10from torch.distributed.nn.api.remote_module import RemoteModule
12-from torch.distributed.rpc import WorkerInfo, PyRRef11+from torch.distributed.rpc import WorkerInfo
13from torch._C import _get_privateuse1_backend_name12from torch._C import _get_privateuse1_backend_name
14 13 
15-import torch_npu
16from torch_npu.distributed.rpc.options import NPUTensorPipeRpcBackendOptions14from torch_npu.distributed.rpc.options import NPUTensorPipeRpcBackendOptions
17from torch_npu.testing.common_distributed import skipIfUnsupportMultiNPU15from torch_npu.testing.common_distributed import skipIfUnsupportMultiNPU
18from torch_npu.testing.testcase import TestCase, run_tests16from torch_npu.testing.testcase import TestCase, run_tests
@@ -335,7 +333,7 @@ class TestRpc(TestCase):
335 333 
336 @skipIfUnsupportMultiNPU(2)334 @skipIfUnsupportMultiNPU(2)
337 def test_async_call_for_cpu(self):335 def test_async_call_for_cpu(self):
338- inputs = [torch.rand(1024, 1024).cpu(), torch.rand(1024, 1024, 1024).cpu()]336+ inputs = [torch.rand(1024, 1024).cpu(), torch.rand(1024, 1024, 4).cpu()]
339 self._test_multiprocess(TestRpc._test_async_call_for_cpu, inputs, self.world_size_2p)337 self._test_multiprocess(TestRpc._test_async_call_for_cpu, inputs, self.world_size_2p)
340 338 
341 @skipIfUnsupportMultiNPU(2)339 @skipIfUnsupportMultiNPU(2)
@@ -23,7 +23,11 @@ def create_build_path(build_directory):
23 23 
24 24 
25def build_stub(base_dir):25def build_stub(base_dir):
26- build_stub_cmd = ["sh", os.path.join(base_dir, "third_party/acl/libs/build_stub.sh")]26+ build_stub_cmd = [
27+ "sh",
28+ os.path.join(base_dir, "third_party/acl/libs/build_stub.sh"),
29+ os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"),
30+ ]
27 if subprocess.call(build_stub_cmd) != 0:31 if subprocess.call(build_stub_cmd) != 0:
28 raise RuntimeError(f"Failed to build stub: {build_stub_cmd}")32 raise RuntimeError(f"Failed to build stub: {build_stub_cmd}")
29 33 
@@ -41,10 +45,7 @@ class TestAllocatorTraceTracker(TestCase):
41 45 
42 cann_lib_path = os.path.join(REPO_ROOT, "third_party", "acl", "libs")46 cann_lib_path = os.path.join(REPO_ROOT, "third_party", "acl", "libs")
43 torch_npu_lib_path = os.path.join(PYTORCH_NPU_INSTALL_PATH, "lib")47 torch_npu_lib_path = os.path.join(PYTORCH_NPU_INSTALL_PATH, "lib")
44- extra_include_paths = [48+ extra_include_paths = [os.path.join(PYTORCH_NPU_INSTALL_PATH, "include")]
45- os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"),
46- os.path.join(PYTORCH_NPU_INSTALL_PATH, "include", "third_party", "acl", "inc"),
47- ]
48 extra_ldflags = [49 extra_ldflags = [
49 f"-L{cann_lib_path}",50 f"-L{cann_lib_path}",
50 "-lascendcl",51 "-lascendcl",
@@ -22,7 +22,11 @@ def create_build_path(build_directory):
22 22 
23 23 
24def build_stub(base_dir):24def build_stub(base_dir):
25- build_stub_cmd = ["sh", os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh')]25+ build_stub_cmd = [
26+ "sh",
27+ os.path.join(base_dir, 'third_party/acl/libs/build_stub.sh'),
28+ os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'),
29+ ]
26 if subprocess.call(build_stub_cmd) != 0:30 if subprocess.call(build_stub_cmd) != 0:
27 raise RuntimeError('Failed to build stub: {}'.format(build_stub_cmd))31 raise RuntimeError('Failed to build stub: {}'.format(build_stub_cmd))
28 32 
@@ -69,7 +73,6 @@ def get_pluggable_allocator():
69 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")73 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")
70 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]74 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]
71 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'))75 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include'))
72- extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include', 'third_party', 'acl', 'inc'))
73 module = torch.utils.cpp_extension.load(76 module = torch.utils.cpp_extension.load(
74 name="pluggable_allocator_extensions",77 name="pluggable_allocator_extensions",
75 sources=[78 sources=[
@@ -41,6 +41,7 @@ def build_stub(base_dir):
41 build_stub_cmd = [41 build_stub_cmd = [
42 "sh",42 "sh",
43 os.path.join(base_dir, "third_party/acl/libs/build_stub.sh"),43 os.path.join(base_dir, "third_party/acl/libs/build_stub.sh"),
44+ os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"),
44 ]45 ]
45 if subprocess.call(build_stub_cmd) != 0:46 if subprocess.call(build_stub_cmd) != 0:
46 raise RuntimeError(f"Failed to build stub: {build_stub_cmd}")47 raise RuntimeError(f"Failed to build stub: {build_stub_cmd}")
@@ -79,7 +80,6 @@ class TestSanitizerPluggableAllocator(TestCase):
79 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")80 extra_ldflags.append(f"-L{PYTORCH_INSTALL_PATH}")
80 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]81 extra_include_paths = [os.path.join(TEST_DIR, "cpp_extensions")]
81 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"))82 extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"))
82- extra_include_paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, 'include', 'third_party', 'acl', 'inc'))
83 83 
84 cls.module = torch.utils.cpp_extension.load(84 cls.module = torch.utils.cpp_extension.load(
85 name="sanitizer_pluggable_allocator_extensions",85 name="sanitizer_pluggable_allocator_extensions",
@@ -9,6 +9,8 @@ from torch_npu.testing.common_utils import create_common_tensor, check_operators
9 9 
10os.environ["COMBINED_ENABLE"] = "1" # Open combined-view cases optimization10os.environ["COMBINED_ENABLE"] = "1" # Open combined-view cases optimization
11 11 
12+SKIP_REASON = "Temporarily skipped; see https://gitcode.com/Ascend/pytorch/issues/4356"
13+ 
12# Optimized view Ops contains Transpose, permute, narrow, strideslice, select, unfold14# Optimized view Ops contains Transpose, permute, narrow, strideslice, select, unfold
13 15 
14# The test case is a continuous optimization test case for aclop16# The test case is a continuous optimization test case for aclop
@@ -19,6 +21,7 @@ os.environ["COMBINED_ENABLE"] = "1" # Open combined-view cases optimization
19 21 
20 22 
21class SingleViewCopyToContiguous(TestCase):23class SingleViewCopyToContiguous(TestCase):
24+ @unittest.skip(SKIP_REASON)
22 def test_view_copy(self, device="npu"):25 def test_view_copy(self, device="npu"):
23 dtype_list1 = [np.float16, np.float32]26 dtype_list1 = [np.float16, np.float32]
24 format_list1 = [0, 3, 29]27 format_list1 = [0, 3, 29]
@@ -66,6 +69,7 @@ class SingleViewCopyToContiguous(TestCase):
66 cpu_out2 = cpu_input.view(1, 6, cpu_input.size(2) * cpu_input.size(3), 1).clone()69 cpu_out2 = cpu_input.view(1, 6, cpu_input.size(2) * cpu_input.size(3), 1).clone()
67 self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy())70 self.assertRtolEqual(npu_out2.to("cpu").numpy(), cpu_out2.numpy())
68 71 
72+ @unittest.skip(SKIP_REASON)
69 def test_unsqueeze_copy(self, device="npu"):73 def test_unsqueeze_copy(self, device="npu"):
70 dtype_list2 = [np.float16, np.float32]74 dtype_list2 = [np.float16, np.float32]
71 format_list2 = [2, 3, 29]75 format_list2 = [2, 3, 29]
@@ -89,15 +93,16 @@ class SingleViewCopyToContiguous(TestCase):
89 npu_out = npu_input.unsqueeze(i).clone()93 npu_out = npu_input.unsqueeze(i).clone()
90 if match_case1 or match_case2:94 if match_case1 or match_case2:
91 self.assertEqual(check_operators_in_prof(['contiguous_d_Reshape'], prof) or95 self.assertEqual(check_operators_in_prof(['contiguous_d_Reshape'], prof) or
92- check_operators_in_prof(['aclnnInplaceCopy'], prof),96+ check_operators_in_prof(['aclnnInplaceCopy'], prof),
93 True, message="contiguous_d_Reshape or aclnnInplaceCopy is not called!")97 True, message="contiguous_d_Reshape or aclnnInplaceCopy is not called!")
94 else:98 else:
95 self.assertEqual(check_operators_in_prof(['d2dCopyAsync'], prof) or99 self.assertEqual(check_operators_in_prof(['d2dCopyAsync'], prof) or
96- check_operators_in_prof(['aclnnInplaceCopy'], prof),100+ check_operators_in_prof(['aclnnInplaceCopy'], prof),
97 True, message="d2dCopyAsync or aclnnInplaceCopy is not called!")101 True, message="d2dCopyAsync or aclnnInplaceCopy is not called!")
98 cpu_out = cpu_input.unsqueeze(i).clone()102 cpu_out = cpu_input.unsqueeze(i).clone()
99 self.assertRtolEqual(npu_out.to("cpu").numpy(), cpu_out.numpy())103 self.assertRtolEqual(npu_out.to("cpu").numpy(), cpu_out.numpy())
100 104 
105+ @unittest.skip(SKIP_REASON)
101 def test_flatten_copy(self, device="npu"):106 def test_flatten_copy(self, device="npu"):
102 dtype_list3 = [np.float16, np.float32]107 dtype_list3 = [np.float16, np.float32]
103 format_list3 = [0, 3, 29]108 format_list3 = [0, 3, 29]
@@ -127,6 +132,7 @@ class SingleViewCopyToContiguous(TestCase):
127 cpu_out = torch.flatten(cpu_input, 0, 1).clone()132 cpu_out = torch.flatten(cpu_input, 0, 1).clone()
128 self.assertRtolEqual(npu_out.to("cpu").numpy(), cpu_out.numpy())133 self.assertRtolEqual(npu_out.to("cpu").numpy(), cpu_out.numpy())
129 134 
135+ @unittest.skip(SKIP_REASON)
130 def test_narrow_at_first_axis_copy(self, device="npu"):136 def test_narrow_at_first_axis_copy(self, device="npu"):
131 # this case: slice at the first dim, tensor with offset remains contiguous137 # this case: slice at the first dim, tensor with offset remains contiguous
132 dtype_list4 = [np.float16, np.float32]138 dtype_list4 = [np.float16, np.float32]
@@ -1,20 +1,23 @@
1#!/bin/bash1#!/bin/bash
2 2 
3+set -e
4+ 
3CDIR="$(cd "$(dirname "$0")" ; pwd -P)"5CDIR="$(cd "$(dirname "$0")" ; pwd -P)"
4 6 
5cd ${CDIR}7cd ${CDIR}
6 8 
7gcc -fPIC -shared -o libhccl.so -I./ hccl.cpp9gcc -fPIC -shared -o libhccl.so -I./ hccl.cpp
8 10 
9-gcc -fPIC -shared -o libascendcl.so -I../inc acl.cpp11+ACL_INCLUDE_DIR="${1:-../inc}"
10 12 
11-gcc -fPIC -shared -o libacl_op_compiler.so -I../inc acl_op_compiler.cpp13+gcc -fPIC -shared -o libascendcl.so -I"${ACL_INCLUDE_DIR}" acl.cpp
12 14 
13-gcc -fPIC -shared -o libge_runner.so -I../inc ge_runner.cpp ge_api.cpp15+gcc -fPIC -shared -o libacl_op_compiler.so -I"${ACL_INCLUDE_DIR}" acl_op_compiler.cpp
14 16 
15-gcc -fPIC -shared -o libgraph.so -I../inc graph.cpp operator_factory.cpp operator.cpp tensor.cpp17+gcc -fPIC -shared -o libge_runner.so -I"${ACL_INCLUDE_DIR}" ge_runner.cpp ge_api.cpp
16 18 
17-gcc -fPIC -shared -o libacl_tdt_channel.so -I../inc acl_tdt.cpp19+gcc -fPIC -shared -o libgraph.so -I"${ACL_INCLUDE_DIR}" graph.cpp operator_factory.cpp operator.cpp tensor.cpp
18 20 
19-gcc -fPIC -shared -o libascend_ml.so -I../inc aml_fwk_detect.cpp21+gcc -fPIC -shared -o libacl_tdt_channel.so -I"${ACL_INCLUDE_DIR}" acl_tdt.cpp
20 22 
23+gcc -fPIC -shared -o libascend_ml.so -I"${ACL_INCLUDE_DIR}" aml_fwk_detect.cpp
@@ -12,7 +12,7 @@
12#define HCCL_H_12#define HCCL_H_
13 13 
14#include "third_party/hccl/inc/hccl/hccl_types.h"14#include "third_party/hccl/inc/hccl/hccl_types.h"
15-#include "third_party/acl/inc/acl/acl.h"15+#include <acl/acl.h>
16 16 
17#ifdef __cplusplus17#ifdef __cplusplus
18extern "C" {18extern "C" {
@@ -85,13 +85,11 @@ def _build_npu_ext(obj_name: str, src_path, src_dir) -> str:
85 85 
86 torch_npu_dir = torch_npu_root / "include"86 torch_npu_dir = torch_npu_root / "include"
87 torch_npu_lib_dir = torch_npu_root / "lib"87 torch_npu_lib_dir = torch_npu_root / "lib"
88- acl_inc_dir = torch_npu_dir / "third_party" / "acl" / "inc"
89 88 
90 cc_cmd += [89 cc_cmd += [
91 f"-I{torch_npu_dir}",90 f"-I{torch_npu_dir}",
92 f"-I{cpp_common_dir}",91 f"-I{cpp_common_dir}",
93 f"-L{torch_npu_lib_dir}",92 f"-L{torch_npu_lib_dir}",
94- f"-I{acl_inc_dir}",
95 "-ltorch_npu",93 "-ltorch_npu",
96 "-Wl,-rpath",94 "-Wl,-rpath",
97 "-std=c++17",95 "-std=c++17",
@@ -43,7 +43,7 @@ def include_paths(npu: bool = False) -> List[str]:
43 os.path.join(lib_include, 'TH'),43 os.path.join(lib_include, 'TH'),
44 os.path.join(lib_include, 'THC')44 os.path.join(lib_include, 'THC')
45 ]45 ]
46- include_path = os.path.join(PYTORCH_NPU_INSTALL_PATH, "include", "third_party", "acl", "inc")46+ include_path = os.path.join(PYTORCH_NPU_INSTALL_PATH, "include")
47 paths.extend([include_path])47 paths.extend([include_path])
48 if npu:48 if npu:
49 ASCEND_HOME = get_ascend_home()49 ASCEND_HOME = get_ascend_home()
@@ -55,7 +55,6 @@ def include_paths(npu: bool = False) -> List[str]:
55 os.path.join(ASCEND_HOME, "include/experiment/msprof"),55 os.path.join(ASCEND_HOME, "include/experiment/msprof"),
56 ])56 ])
57 57 
58- paths.append(os.path.join(PYTORCH_NPU_INSTALL_PATH, "include"))
59 return paths58 return paths
60 59 
61 60 
@@ -8,7 +8,7 @@
8#include "torch_npu/csrc/aten/NPUNativeFunctions.h"8#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
9#include "torch_npu/csrc/core/NPUBridge.h"9#include "torch_npu/csrc/core/NPUBridge.h"
10#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"10#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
11-#include "third_party/acl/inc/acl/acl.h"11+#include <acl/acl.h>
12 12 
13namespace at_npu {13namespace at_npu {
14namespace native {14namespace native {
@@ -3,7 +3,7 @@
3 3 
4#include <ATen/ATen.h>4#include <ATen/ATen.h>
5 5 
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7 7 
8namespace at_npu {8namespace at_npu {
9namespace native {9namespace native {
@@ -2,8 +2,8 @@
2#include <ATen/NativeFunctions.h>2#include <ATen/NativeFunctions.h>
3#include <ATen/Dispatch_v2.h>3#include <ATen/Dispatch_v2.h>
4 4 
5-#include "third_party/acl/inc/acl/acl_base.h"5+#include <acl/acl_base.h>
6-#include "third_party/acl/inc/acl/acl_rt.h"6+#include <acl/acl_rt.h>
7#include "torch_npu/csrc/core/npu/NPUStream.h"7#include "torch_npu/csrc/core/npu/NPUStream.h"
8#include "torch_npu/csrc/framework/utils/CalcuOpUtil.h"8#include "torch_npu/csrc/framework/utils/CalcuOpUtil.h"
9#include "torch_npu/csrc/aten/NPUNativeFunctions.h"9#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
@@ -13,8 +13,7 @@ namespace native {
13 13 
14c10::Scalar NPUNativeFunctions::_local_scalar_dense(const at::Tensor& self) {14c10::Scalar NPUNativeFunctions::_local_scalar_dense(const at::Tensor& self) {
15 c10::Scalar r;15 c10::Scalar r;
16- TORCH_CHECK(16+ TORCH_CHECK(self.numel() > 0, "_local_scalar_dense(): Empty tensor not supported");
17- self.numel() > 0, "_local_scalar_dense(): Empty tensor not supported");
18 AT_DISPATCH_V2(17 AT_DISPATCH_V2(
19 self.scalar_type(),18 self.scalar_type(),
20 "_local_scalar_dense_npu",19 "_local_scalar_dense_npu",
@@ -22,15 +21,12 @@ c10::Scalar NPUNativeFunctions::_local_scalar_dense(const at::Tensor& self) {
22 scalar_t value = 0;21 scalar_t value = 0;
23 c10_npu::NPUStream copy_stream = c10_npu::getCurrentNPUStream();22 c10_npu::NPUStream copy_stream = c10_npu::getCurrentNPUStream();
24 // Synchronous copy after stream synchronization23 // Synchronous copy after stream synchronization
25- NPU_CHECK_ERROR(24+ NPU_CHECK_ERROR(c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream));
26- c10_npu::acl::AclrtSynchronizeStreamWithTimeout(copy_stream));
27 25 
28 NPU_CHECK_ERROR(CalcuOpUtil::AclrtMemcpyWithModeSwitch(26 NPU_CHECK_ERROR(CalcuOpUtil::AclrtMemcpyWithModeSwitch(
29 &value,27 &value,
30 sizeof(scalar_t),28 sizeof(scalar_t),
31- std::make_pair(29+ std::make_pair(self.storage().unsafeGetStorageImpl(), self.storage_offset() * self.itemsize()),
32- self.storage().unsafeGetStorageImpl(),
33- self.storage_offset() * self.itemsize()),
34 sizeof(scalar_t),30 sizeof(scalar_t),
35 ACL_MEMCPY_DEVICE_TO_HOST));31 ACL_MEMCPY_DEVICE_TO_HOST));
36 r = c10::Scalar(value);32 r = c10::Scalar(value);
@@ -22,7 +22,7 @@
22#include "torch_npu/csrc/aten/common/ResizeNpu.h"22#include "torch_npu/csrc/aten/common/ResizeNpu.h"
23#include "torch_npu/csrc/framework/StorageDescHelper.h"23#include "torch_npu/csrc/framework/StorageDescHelper.h"
24#include "torch_npu/csrc/core/npu/NPUGuard.h"24#include "torch_npu/csrc/core/npu/NPUGuard.h"
25-#include "third_party/acl/inc/acl/acl_base.h"25+#include <acl/acl_base.h>
26#include "op_plugin/OpInterface.h"26#include "op_plugin/OpInterface.h"
27 27 
28namespace {28namespace {
@@ -3,7 +3,7 @@
3#include "torch_npu/csrc/core/npu/NPUGuard.h"3#include "torch_npu/csrc/core/npu/NPUGuard.h"
4#include "torch_npu/csrc/core/NPUSerialization.h"4#include "torch_npu/csrc/core/NPUSerialization.h"
5#include "torch_npu/csrc/framework/FormatHelper.h"5#include "torch_npu/csrc/framework/FormatHelper.h"
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7#include "torch_npu/csrc/framework/StorageDescHelper.h"7#include "torch_npu/csrc/framework/StorageDescHelper.h"
8 8 
9namespace torch_npu {9namespace torch_npu {
@@ -8,8 +8,8 @@
8#include <c10/util/typeid.h>8#include <c10/util/typeid.h>
9#include <c10/util/order_preserving_flat_hash_map.h>9#include <c10/util/order_preserving_flat_hash_map.h>
10 10 
11-#include "third_party/acl/inc/acl/acl_rt.h"11+#include <acl/acl_rt.h>
12-#include "third_party/acl/inc/acl/acl_base.h"12+#include <acl/acl_base.h>
13 13 
14namespace torch_npu {14namespace torch_npu {
15 15 
@@ -5,7 +5,7 @@
5 5 
6#include "torch_npu/csrc/framework/StorageDescHelper.h"6#include "torch_npu/csrc/framework/StorageDescHelper.h"
7#include "torch_npu/csrc/core/NPUTensorImpl.h"7#include "torch_npu/csrc/core/NPUTensorImpl.h"
8-#include "third_party/acl/inc/acl/acl_rt.h"8+#include <acl/acl_rt.h>
9#include "torch_npu/csrc/core/NPUStorageImpl.h"9#include "torch_npu/csrc/core/NPUStorageImpl.h"
10 10 
11namespace torch_npu {11namespace torch_npu {
@@ -6,8 +6,8 @@
6#include "torch_npu/csrc/core/npu/NPUMacros.h"6#include "torch_npu/csrc/core/npu/NPUMacros.h"
7#include "torch_npu/csrc/core/npu/NPUStream.h"7#include "torch_npu/csrc/core/npu/NPUStream.h"
8#include "torch_npu/csrc/core/npu/NPUException.h"8#include "torch_npu/csrc/core/npu/NPUException.h"
9-#include <third_party/acl/inc/acl/acl.h>9+#include <acl/acl.h>
10-#include <third_party/acl/inc/acl/acl_rt.h>10+#include <acl/acl_rt.h>
11 11 
12#include <c10/core/DeviceGuard.h>12#include <c10/core/DeviceGuard.h>
13#include <ATen/DeviceGuard.h>13#include <ATen/DeviceGuard.h>
@@ -9,7 +9,7 @@
9#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"9#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"
10#include "torch_npu/csrc/core/npu/NPUException.h"10#include "torch_npu/csrc/core/npu/NPUException.h"
11#include "torch_npu/csrc/core/npu/interface/AclInterface.h"11#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
12-#include "third_party/acl/inc/acl/acl.h"12+#include <acl/acl.h>
13 13 
14constexpr size_t kVersionIndex1 = 1;14constexpr size_t kVersionIndex1 = 1;
15constexpr size_t kVersionIndex2 = 2;15constexpr size_t kVersionIndex2 = 2;
@@ -9,7 +9,7 @@
9#include <c10/util/Deprecated.h>9#include <c10/util/Deprecated.h>
10#include <c10/util/env.h>10#include <c10/util/env.h>
11 11 
12-#include "third_party/acl/inc/acl/acl_base.h"12+#include <acl/acl_base.h>
13#include "torch_npu/csrc/core/npu/GetCANNInfo.h"13#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
14#include "torch_npu/csrc/core/npu/NPUException.h"14#include "torch_npu/csrc/core/npu/NPUException.h"
15#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"15#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"
@@ -18,8 +18,8 @@
18#include <c10/util/llvmMathExtras.h>18#include <c10/util/llvmMathExtras.h>
19#include <c10/util/ScopeExit.h>19#include <c10/util/ScopeExit.h>
20 20 
21-#include "third_party/acl/inc/acl/acl_base.h"21+#include <acl/acl_base.h>
22-#include "third_party/acl/inc/acl/acl_rt.h"22+#include <acl/acl_rt.h>
23#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"23#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
24#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"24#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"
25#include "torch_npu/csrc/core/npu/NPUAllocatorConfig.h"25#include "torch_npu/csrc/core/npu/NPUAllocatorConfig.h"
@@ -4,7 +4,7 @@
4#include "torch_npu/csrc/core/npu/NPUMacros.h"4#include "torch_npu/csrc/core/npu/NPUMacros.h"
5#include "torch_npu/csrc/core/npu/NPUGuard.h"5#include "torch_npu/csrc/core/npu/NPUGuard.h"
6#include "torch_npu/csrc/core/npu/NPUFunctions.h"6#include "torch_npu/csrc/core/npu/NPUFunctions.h"
7-#include "third_party/acl/inc/acl/acl.h"7+#include <acl/acl.h>
8#include <cstdint>8#include <cstdint>
9#include <utility>9#include <utility>
10 10 
@@ -5,7 +5,7 @@
5#include <unordered_set>5#include <unordered_set>
6#include <c10/core/thread_pool.h>6#include <c10/core/thread_pool.h>
7#include <c10/util/flat_hash_map.h>7#include <c10/util/flat_hash_map.h>
8-#include <third_party/acl/inc/acl/acl.h>8+#include <acl/acl.h>
9 9 
10#include "torch_npu/csrc/core/npu/NPUException.h"10#include "torch_npu/csrc/core/npu/NPUException.h"
11 11 
@@ -15,7 +15,7 @@
15#include <regex>15#include <regex>
16#include <c10/macros/Macros.h>16#include <c10/macros/Macros.h>
17#include <c10/util/Exception.h>17#include <c10/util/Exception.h>
18-#include <third_party/acl/inc/acl/acl_base.h>18+#include <acl/acl_base.h>
19#include "torch_npu/csrc/core/npu/NPUMacros.h"19#include "torch_npu/csrc/core/npu/NPUMacros.h"
20#include "torch_npu/csrc/core/npu/interface/AclInterface.h"20#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
21#include "torch_npu/csrc/core/npu/NPUErrorCodes.h"21#include "torch_npu/csrc/core/npu/NPUErrorCodes.h"
@@ -11,7 +11,7 @@
11#include "torch_npu/csrc/core/npu/register/OptionsManager.h"11#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
12#include "torch_npu/csrc/core/npu/GetCANNInfo.h"12#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
13#include "torch_npu/csrc/framework/interface/EnvVariables.h"13#include "torch_npu/csrc/framework/interface/EnvVariables.h"
14-#include "third_party/acl/inc/acl/acl_rt.h"14+#include <acl/acl_rt.h>
15#ifndef BUILD_LIBTORCH15#ifndef BUILD_LIBTORCH
16#include "torch_npu/csrc/sanitizer/NPUTrace.h"16#include "torch_npu/csrc/sanitizer/NPUTrace.h"
17#endif17#endif
@@ -44,9 +44,7 @@ struct DeterministicLevel3VersionCheck {
44 std::vector<DeterministicVersionFailure> failures;44 std::vector<DeterministicVersionFailure> failures;
45};45};
46 46 
47-bool IsModuleVersionGt(47+bool IsModuleVersionGt(const std::string& module, const std::string& required_version) {
48- const std::string& module,
49- const std::string& required_version) {
50 return IsGteCANNVersion(required_version, module);48 return IsGteCANNVersion(required_version, module);
51}49}
52 50 
@@ -73,8 +71,7 @@ bool CheckVersionGroup(
73DeterministicLevel3VersionCheck CheckDeterministicLevel3Version() {71DeterministicLevel3VersionCheck CheckDeterministicLevel3Version() {
74 DeterministicLevel3VersionCheck check;72 DeterministicLevel3VersionCheck check;
75 std::vector<DeterministicVersionFailure> runtime_failures;73 std::vector<DeterministicVersionFailure> runtime_failures;
76- const bool runtime_supported = CheckVersionGroup(74+ const bool runtime_supported = CheckVersionGroup({{"RUNTIME", kLevel3MinRuntimeVersion}}, runtime_failures);
77- {{"RUNTIME", kLevel3MinRuntimeVersion}}, runtime_failures);
78 75 
79 std::vector<DeterministicVersionFailure> legacy_pkg_failures;76 std::vector<DeterministicVersionFailure> legacy_pkg_failures;
80 const bool legacy_pkg_supported = CheckVersionGroup(77 const bool legacy_pkg_supported = CheckVersionGroup(
@@ -95,30 +92,21 @@ DeterministicLevel3VersionCheck CheckDeterministicLevel3Version() {
95 },92 },
96 split_pkg_failures);93 split_pkg_failures);
97 94 
98- check.supported =95+ check.supported = runtime_supported && (legacy_pkg_supported || split_pkg_supported);
99- runtime_supported && (legacy_pkg_supported || split_pkg_supported);
100 if (check.supported) {96 if (check.supported) {
101 return check;97 return check;
102 }98 }
103 99 
104- check.failures.insert(100+ check.failures.insert(check.failures.end(), runtime_failures.begin(), runtime_failures.end());
105- check.failures.end(), runtime_failures.begin(), runtime_failures.end());
106 if (!legacy_pkg_supported && !split_pkg_supported) {101 if (!legacy_pkg_supported && !split_pkg_supported) {
107- check.failures.insert(102+ check.failures.insert(check.failures.end(), legacy_pkg_failures.begin(), legacy_pkg_failures.end());
108- check.failures.end(),103+ check.failures.insert(check.failures.end(), split_pkg_failures.begin(), split_pkg_failures.end());
109- legacy_pkg_failures.begin(),
110- legacy_pkg_failures.end());
111- check.failures.insert(
112- check.failures.end(),
113- split_pkg_failures.begin(),
114- split_pkg_failures.end());
115 }104 }
116 return check;105 return check;
117}106}
118 107 
119const DeterministicLevel3VersionCheck& GetDeterministicLevel3VersionCheck() {108const DeterministicLevel3VersionCheck& GetDeterministicLevel3VersionCheck() {
120- static const DeterministicLevel3VersionCheck check =109+ static const DeterministicLevel3VersionCheck check = CheckDeterministicLevel3Version();
121- CheckDeterministicLevel3Version();
122 return check;110 return check;
123}111}
124 112 
@@ -130,10 +118,9 @@ void ThrowLevel3UnsupportedError() {
130 if (!check.failures.empty()) {118 if (!check.failures.empty()) {
131 oss << " Unsatisfied versions:";119 oss << " Unsatisfied versions:";
132 for (const auto& failure : check.failures) {120 for (const auto& failure : check.failures) {
133- oss << " " << failure.module << "(current="121+ oss << " " << failure.module
134- << (failure.current_version.empty() ? "unavailable"122+ << "(current=" << (failure.current_version.empty() ? "unavailable" : failure.current_version) << ", required>"
135- : failure.current_version)123+ << failure.required_version << ");";
136- << ", required>" << failure.required_version << ");";
137 }124 }
138 }125 }
139 TORCH_CHECK(false, oss.str(), PTA_ERROR(ErrCode::VALUE));126 TORCH_CHECK(false, oss.str(), PTA_ERROR(ErrCode::VALUE));
@@ -179,8 +166,7 @@ bool hasPrimaryContext(c10::DeviceIndex device_index) {
179 device_index,166 device_index,
180 PTA_ERROR(ErrCode::VALUE));167 PTA_ERROR(ErrCode::VALUE));
181 int32_t ctx_is_active = 0;168 int32_t ctx_is_active = 0;
182- NPU_CHECK_ERROR_WITHOUT_UCE(169+ NPU_CHECK_ERROR_WITHOUT_UCE(acl::AclrtGetPrimaryCtxState(device_index, nullptr, &ctx_is_active));
183- acl::AclrtGetPrimaryCtxState(device_index, nullptr, &ctx_is_active));
184 return ctx_is_active == 1;170 return ctx_is_active == 1;
185}171}
186 172 
@@ -248,8 +234,7 @@ aclError GetDeviceWithoutSet(int32_t* device) {
248}234}
249 235 
250aclError SetDevice(c10::DeviceIndex device) {236aclError SetDevice(c10::DeviceIndex device) {
251- TORCH_CHECK(237+ TORCH_CHECK(device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
252- device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
253 targetDeviceIndex = -1;238 targetDeviceIndex = -1;
254 if (local_device == device) {239 if (local_device == device) {
255 return ACL_ERROR_NONE;240 return ACL_ERROR_NONE;
@@ -264,8 +249,7 @@ aclError SetDevice(c10::DeviceIndex device) {
264 local_device = device;249 local_device = device;
265 std::lock_guard<std::recursive_mutex> lock(mtx);250 std::lock_guard<std::recursive_mutex> lock(mtx);
266 if (used_devices.find(local_device) == used_devices.end()) {251 if (used_devices.find(local_device) == used_devices.end()) {
267- NPU_CHECK_ERROR_WITHOUT_UCE(252+ NPU_CHECK_ERROR_WITHOUT_UCE(aclrtGetCurrentContext(&used_devices[local_device]));
268- aclrtGetCurrentContext(&used_devices[local_device]));
269 }253 }
270 }254 }
271 return err;255 return err;
@@ -275,9 +259,7 @@ aclError MaybeSetDevice(c10::DeviceIndex device) {
275 if (isDeviceCtxActive(device)) {259 if (isDeviceCtxActive(device)) {
276 NPU_CHECK_ERROR_WITHOUT_UCE(SetDevice(device));260 NPU_CHECK_ERROR_WITHOUT_UCE(SetDevice(device));
277 } else {261 } else {
278- ASCEND_LOGI(262+ ASCEND_LOGI("MaybeSetDevice: NPU device %d has not been initialized! We will set targetDeviceIndex.", device);
279- "MaybeSetDevice: NPU device %d has not been initialized! We will set targetDeviceIndex.",
280- device);
281 targetDeviceIndex = device;263 targetDeviceIndex = device;
282 }264 }
283 return ACL_ERROR_NONE;265 return ACL_ERROR_NONE;
@@ -334,8 +316,7 @@ aclError SynchronizeUsedDevices() {
334 return acl_ret;316 return acl_ret;
335 }317 }
336#ifndef BUILD_LIBTORCH318#ifndef BUILD_LIBTORCH
337- const c10_npu::impl::PyCallbackTrigger* trigger =319+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
338- c10_npu::impl::NPUTrace::getTrace();
339 if (C10_UNLIKELY(trigger)) {320 if (C10_UNLIKELY(trigger)) {
340 trigger->traceNpuDeviceSynchronization();321 trigger->traceNpuDeviceSynchronization();
341 }322 }
@@ -348,8 +329,7 @@ aclError SynchronizeUsedDevices() {
348aclrtContext GetDeviceContext(int32_t device) {329aclrtContext GetDeviceContext(int32_t device) {
349 std::lock_guard<std::recursive_mutex> lock(mtx);330 std::lock_guard<std::recursive_mutex> lock(mtx);
350 if (used_devices.find(device) == used_devices.end()) {331 if (used_devices.find(device) == used_devices.end()) {
351- ASCEND_LOGE(332+ ASCEND_LOGE("NPU device %d has not been initialized! Can not get context", device);
352- "NPU device %d has not been initialized! Can not get context", device);
353 return nullptr;333 return nullptr;
354 }334 }
355 return used_devices[device];335 return used_devices[device];
@@ -396,11 +376,9 @@ void set_device(c10::DeviceIndex device) {
396}376}
397 377 
398void device_synchronize() {378void device_synchronize() {
399- NPU_CHECK_ERROR_WITHOUT_UCE(379+ NPU_CHECK_ERROR_WITHOUT_UCE(c10_npu::acl::AclrtSynchronizeDeviceWithTimeout());
400- c10_npu::acl::AclrtSynchronizeDeviceWithTimeout());
401#ifndef BUILD_LIBTORCH380#ifndef BUILD_LIBTORCH
402- const c10_npu::impl::PyCallbackTrigger* trigger =381+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
403- c10_npu::impl::NPUTrace::getTrace();
404 if (C10_UNLIKELY(trigger)) {382 if (C10_UNLIKELY(trigger)) {
405 trigger->traceNpuDeviceSynchronization();383 trigger->traceNpuDeviceSynchronization();
406 }384 }
@@ -423,9 +401,7 @@ int MaybeExchangeDevice(int to_device) {
423 if (isDeviceCtxActive(to_device)) {401 if (isDeviceCtxActive(to_device)) {
424 NPU_CHECK_ERROR_WITHOUT_UCE(SetDevice(to_device));402 NPU_CHECK_ERROR_WITHOUT_UCE(SetDevice(to_device));
425 } else {403 } else {
426- ASCEND_LOGI(404+ ASCEND_LOGI("NPU device %d has not been initialized! We will set targetDeviceIndex.", to_device);
427- "NPU device %d has not been initialized! We will set targetDeviceIndex.",
428- to_device);
429 targetDeviceIndex = to_device;405 targetDeviceIndex = to_device;
430 }406 }
431 return cur_device;407 return cur_device;
@@ -471,8 +447,7 @@ void LazySetDevice(c10::DeviceIndex device) {
471 local_device = device;447 local_device = device;
472 std::lock_guard<std::recursive_mutex> lock(mtx);448 std::lock_guard<std::recursive_mutex> lock(mtx);
473 if (used_devices.find(local_device) == used_devices.end()) {449 if (used_devices.find(local_device) == used_devices.end()) {
474- NPU_CHECK_ERROR_WITHOUT_UCE(450+ NPU_CHECK_ERROR_WITHOUT_UCE(aclrtGetCurrentContext(&used_devices[local_device]));
475- aclrtGetCurrentContext(&used_devices[local_device]));
476 }451 }
477 }452 }
478 NPU_CHECK_ERROR_WITHOUT_UCE(err);453 NPU_CHECK_ERROR_WITHOUT_UCE(err);
@@ -481,21 +456,18 @@ void LazySetDevice(c10::DeviceIndex device) {
481 456 
482void warn_or_error_on_sync() {457void warn_or_error_on_sync() {
483 if (warning_state().get_sync_debug_mode() == SyncDebugMode::L_ERROR) {458 if (warning_state().get_sync_debug_mode() == SyncDebugMode::L_ERROR) {
484- TORCH_CHECK(459+ TORCH_CHECK(false, "called a synchronizing NPU operation", PTA_ERROR(ErrCode::ACL));
485- false, "called a synchronizing NPU operation", PTA_ERROR(ErrCode::ACL));
486 } else if (warning_state().get_sync_debug_mode() == SyncDebugMode::L_WARN) {460 } else if (warning_state().get_sync_debug_mode() == SyncDebugMode::L_WARN) {
487 TORCH_NPU_WARN("called a synchronizing NPU operation");461 TORCH_NPU_WARN("called a synchronizing NPU operation");
488 }462 }
489}463}
490 464 
491void stream_synchronize(aclrtStream stream) {465void stream_synchronize(aclrtStream stream) {
492- if (C10_UNLIKELY(466+ if (C10_UNLIKELY(warning_state().get_sync_debug_mode() != SyncDebugMode::L_DISABLED)) {
493- warning_state().get_sync_debug_mode() != SyncDebugMode::L_DISABLED)) {
494 warn_or_error_on_sync();467 warn_or_error_on_sync();
495 }468 }
496#ifndef BUILD_LIBTORCH469#ifndef BUILD_LIBTORCH
497- const c10_npu::impl::PyCallbackTrigger* trigger =470+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
498- c10_npu::impl::NPUTrace::getTrace();
499 if (C10_UNLIKELY(trigger)) {471 if (C10_UNLIKELY(trigger)) {
500 trigger->traceNpuStreamSynchronization(reinterpret_cast<uintptr_t>(stream));472 trigger->traceNpuStreamSynchronization(reinterpret_cast<uintptr_t>(stream));
501 }473 }
@@ -506,16 +478,10 @@ void stream_synchronize(aclrtStream stream) {
506aclError SetDeviceResLimit(int32_t device, int32_t type, uint32_t value) {478aclError SetDeviceResLimit(int32_t device, int32_t type, uint32_t value) {
507 std::lock_guard<std::recursive_mutex> lock(mtx);479 std::lock_guard<std::recursive_mutex> lock(mtx);
508 if (used_devices.find(device) == used_devices.end()) {480 if (used_devices.find(device) == used_devices.end()) {
509- TORCH_CHECK(481+ TORCH_CHECK(false, "NPU device ", device, " has not been initialized! Can not get device resource limit");
510- false,
511- "NPU device ",
512- device,
513- " has not been initialized! Can not get device resource limit");
514 }482 }
515- TORCH_CHECK(483+ TORCH_CHECK(device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
516- device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));484+ c10_npu::acl::aclrtDevResLimitType restype = static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
517- c10_npu::acl::aclrtDevResLimitType restype =
518- static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
519 aclError err = c10_npu::acl::AclrtSetDeviceResLimit(device, restype, value);485 aclError err = c10_npu::acl::AclrtSetDeviceResLimit(device, restype, value);
520 NPU_CHECK_ERROR(err);486 NPU_CHECK_ERROR(err);
521 return err;487 return err;
@@ -524,43 +490,29 @@ aclError SetDeviceResLimit(int32_t device, int32_t type, uint32_t value) {
524uint32_t GetDeviceResLimit(int32_t device, int32_t type) {490uint32_t GetDeviceResLimit(int32_t device, int32_t type) {
525 std::lock_guard<std::recursive_mutex> lock(mtx);491 std::lock_guard<std::recursive_mutex> lock(mtx);
526 if (used_devices.find(device) == used_devices.end()) {492 if (used_devices.find(device) == used_devices.end()) {
527- TORCH_CHECK(493+ TORCH_CHECK(false, "NPU device ", device, " has not been initialized! Can not get device resource limit");
528- false,
529- "NPU device ",
530- device,
531- " has not been initialized! Can not get device resource limit");
532 }494 }
533- TORCH_CHECK(495+ TORCH_CHECK(device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
534- device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));496+ c10_npu::acl::aclrtDevResLimitType restype = static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
535- c10_npu::acl::aclrtDevResLimitType restype =
536- static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
537 uint32_t value;497 uint32_t value;
538- NPU_CHECK_ERROR(498+ NPU_CHECK_ERROR(c10_npu::acl::AclrtGetDeviceResLimit(device, restype, &value));
539- c10_npu::acl::AclrtGetDeviceResLimit(device, restype, &value));
540 return value;499 return value;
541}500}
542 501 
543aclError ResetDeviceResLimit(int32_t device) {502aclError ResetDeviceResLimit(int32_t device) {
544 std::lock_guard<std::recursive_mutex> lock(mtx);503 std::lock_guard<std::recursive_mutex> lock(mtx);
545 if (used_devices.find(device) == used_devices.end()) {504 if (used_devices.find(device) == used_devices.end()) {
546- TORCH_CHECK(505+ TORCH_CHECK(false, "NPU device ", device, " has not been initialized! Can not reset device resource limit");
547- false,
548- "NPU device ",
549- device,
550- " has not been initialized! Can not reset device resource limit");
551 }506 }
552- TORCH_CHECK(507+ TORCH_CHECK(device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
553- device >= 0, "device id must be positive!", PTA_ERROR(ErrCode::VALUE));
554 aclError err = c10_npu::acl::AclrtResetDeviceResLimit(device);508 aclError err = c10_npu::acl::AclrtResetDeviceResLimit(device);
555 NPU_CHECK_ERROR(err);509 NPU_CHECK_ERROR(err);
556 return err;510 return err;
557}511}
558 512 
559aclError SetStreamResLimit(NPUStream npu_stream, int32_t type, uint32_t value) {513aclError SetStreamResLimit(NPUStream npu_stream, int32_t type, uint32_t value) {
560- c10_npu::acl::aclrtDevResLimitType restype =514+ c10_npu::acl::aclrtDevResLimitType restype = static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
561- static_cast<c10_npu::acl::aclrtDevResLimitType>(type);515+ aclError err = c10_npu::acl::AclrtSetStreamResLimit(npu_stream.stream(), restype, value);
562- aclError err =
563- c10_npu::acl::AclrtSetStreamResLimit(npu_stream.stream(), restype, value);
564 enable_core_control.store(true, std::memory_order_relaxed);516 enable_core_control.store(true, std::memory_order_relaxed);
565 NPU_CHECK_ERROR(err);517 NPU_CHECK_ERROR(err);
566 return err;518 return err;
@@ -573,11 +525,9 @@ aclError ResetStreamResLimit(NPUStream npu_stream) {
573}525}
574 526 
575uint32_t GetStreamResLimit(NPUStream npu_stream, int32_t type) {527uint32_t GetStreamResLimit(NPUStream npu_stream, int32_t type) {
576- c10_npu::acl::aclrtDevResLimitType restype =528+ c10_npu::acl::aclrtDevResLimitType restype = static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
577- static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
578 uint32_t value;529 uint32_t value;
579- NPU_CHECK_ERROR(c10_npu::acl::AclrtGetStreamResLimit(530+ NPU_CHECK_ERROR(c10_npu::acl::AclrtGetStreamResLimit(npu_stream.stream(false), restype, &value));
580- npu_stream.stream(false), restype, &value));
581 return value;531 return value;
582}532}
583 533 
@@ -594,8 +544,7 @@ aclError UnuseStreamResInCurrentThread(aclrtStream stream) {
594}544}
595 545 
596uint32_t GetResInCurrentThread(int32_t type) {546uint32_t GetResInCurrentThread(int32_t type) {
597- c10_npu::acl::aclrtDevResLimitType restype =547+ c10_npu::acl::aclrtDevResLimitType restype = static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
598- static_cast<c10_npu::acl::aclrtDevResLimitType>(type);
599 uint32_t value;548 uint32_t value;
600 NPU_CHECK_ERROR(c10_npu::acl::AclrtGetResInCurrentThread(restype, &value));549 NPU_CHECK_ERROR(c10_npu::acl::AclrtGetResInCurrentThread(restype, &value));
601 return value;550 return value;
@@ -620,13 +569,11 @@ uint32_t GetDeterministicLevel() {
620 569 
621DeterministicSnapshot CaptureDeterministicSnapshot() {570DeterministicSnapshot CaptureDeterministicSnapshot() {
622 DeterministicSnapshot snapshot;571 DeterministicSnapshot snapshot;
623- snapshot.deterministic_algorithms_enabled =572+ snapshot.deterministic_algorithms_enabled = at::globalContext().deterministicAlgorithms();
624- at::globalContext().deterministicAlgorithms();
625 snapshot.requested_level = GetDeterministicLevel();573 snapshot.requested_level = GetDeterministicLevel();
626 snapshot.effective_level = 0;574 snapshot.effective_level = 0;
627 if (snapshot.deterministic_algorithms_enabled) {575 if (snapshot.deterministic_algorithms_enabled) {
628- snapshot.effective_level =576+ snapshot.effective_level = snapshot.requested_level == 0 ? 1 : snapshot.requested_level;
629- snapshot.requested_level == 0 ? 1 : snapshot.requested_level;
630 }577 }
631 snapshot.backend = GetDeterministicBackend();578 snapshot.backend = GetDeterministicBackend();
632 return snapshot;579 return snapshot;
@@ -637,8 +584,7 @@ uint32_t GetEffectiveDeterministicLevel() {
637}584}
638 585 
639DeterministicBackend GetDeterministicBackend() {586DeterministicBackend GetDeterministicBackend() {
640- return IsSupportDeterministicLevel3() ? DeterministicBackend::V2587+ return IsSupportDeterministicLevel3() ? DeterministicBackend::V2 : DeterministicBackend::Legacy;
641- : DeterministicBackend::Legacy;
642}588}
643 589 
644bool IsSupportDeterministicLevel3() {590bool IsSupportDeterministicLevel3() {
@@ -16,7 +16,7 @@
16#include "torch_npu/csrc/core/npu/npu_log.h"16#include "torch_npu/csrc/core/npu/npu_log.h"
17#include "torch_npu/csrc/core/npu/NPUMacros.h"17#include "torch_npu/csrc/core/npu/NPUMacros.h"
18#include "torch_npu/csrc/core/npu/NPUStream.h"18#include "torch_npu/csrc/core/npu/NPUStream.h"
19-#include <third_party/acl/inc/acl/acl.h>19+#include <acl/acl.h>
20 20 
21namespace c10_npu {21namespace c10_npu {
22 22 
@@ -7,7 +7,7 @@
7#include "torch_npu/csrc/core/npu/NPUStreamUtils.h"7#include "torch_npu/csrc/core/npu/NPUStreamUtils.h"
8#include "torch_npu/csrc/aten/NPUGeneratorImpl.h"8#include "torch_npu/csrc/aten/NPUGeneratorImpl.h"
9#include "torch_npu/csrc/core/npu/register/OptionRegister.h"9#include "torch_npu/csrc/core/npu/register/OptionRegister.h"
10-#include "third_party/acl/inc/acl/error_codes/rt_error_codes.h"10+#include <acl/error_codes/rt_error_codes.h>
11#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"11#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"
12#include "torch_npu/csrc/core/npu/GetCANNInfo.h"12#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
13 13 
@@ -9,9 +9,9 @@
9#include <functional>9#include <functional>
10#include <limits>10#include <limits>
11 11 
12-#include "third_party/acl/inc/acl/acl_base.h"12+#include <acl/acl_base.h>
13-#include "third_party/acl/inc/acl/acl_rt.h"13+#include <acl/acl_rt.h>
14-#include "third_party/acl/inc/acl/super_kernel.h"14+#include <acl/super_kernel.h>
15#include "torch_npu/csrc/core/npu/interface/AclInterface.h"15#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
16#include "torch_npu/csrc/core/npu/interface/SkInterface.h"16#include "torch_npu/csrc/core/npu/interface/SkInterface.h"
17#include "torch_npu/csrc/core/npu/NPUGraphsUtils.h"17#include "torch_npu/csrc/core/npu/NPUGraphsUtils.h"
@@ -1,7 +1,7 @@
1#include <vector>1#include <vector>
2#include <c10/util/Exception.h>2#include <c10/util/Exception.h>
3#include <c10/util/irange.h>3#include <c10/util/irange.h>
4-#include <third_party/acl/inc/acl/acl_rt.h>4+#include <acl/acl_rt.h>
5#include "torch_npu/csrc/core/npu/NpuVariables.h"5#include "torch_npu/csrc/core/npu/NpuVariables.h"
6#include "torch_npu/csrc/core/npu/NPUPeerToPeerAccess.h"6#include "torch_npu/csrc/core/npu/NPUPeerToPeerAccess.h"
7#include "torch_npu/csrc/core/npu/NPUGuard.h"7#include "torch_npu/csrc/core/npu/NPUGuard.h"
@@ -20,7 +20,7 @@
20#include <sstream>20#include <sstream>
21#include <sys/time.h>21#include <sys/time.h>
22#include <sys/eventfd.h>22#include <sys/eventfd.h>
23-#include <third_party/acl/inc/acl/acl_rt.h>23+#include <acl/acl_rt.h>
24 24 
25namespace c10_npu {25namespace c10_npu {
26struct timeval delay = {0, 1};26struct timeval delay = {0, 1};
@@ -7,7 +7,7 @@
7 7 
8#include <c10/core/Device.h>8#include <c10/core/Device.h>
9#include "torch_npu/csrc/logging/LogContext.h"9#include "torch_npu/csrc/logging/LogContext.h"
10-#include <third_party/acl/inc/acl/acl_op.h>10+#include <acl/acl_op.h>
11 11 
12namespace c10_npu {12namespace c10_npu {
13 13 
@@ -20,7 +20,7 @@
20#include "torch_npu/csrc/core/npu/NPUStreamUtils.h"20#include "torch_npu/csrc/core/npu/NPUStreamUtils.h"
21#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"21#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"
22#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"22#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
23-#include "third_party/acl/inc/acl/acl_rt.h"23+#include <acl/acl_rt.h>
24#ifndef BUILD_LIBTORCH24#ifndef BUILD_LIBTORCH
25#include "torch_npu/csrc/sanitizer/NPUTrace.h"25#include "torch_npu/csrc/sanitizer/NPUTrace.h"
26#endif26#endif
@@ -10,8 +10,8 @@
10#include "torch_npu/csrc/core/npu/NPUException.h"10#include "torch_npu/csrc/core/npu/NPUException.h"
11#include "torch_npu/csrc/core/npu/NPUQueue.h"11#include "torch_npu/csrc/core/npu/NPUQueue.h"
12#include "torch_npu/csrc/core/npu/npu_log.h"12#include "torch_npu/csrc/core/npu/npu_log.h"
13-#include "third_party/acl/inc/acl/acl.h"13+#include <acl/acl.h>
14-#include "third_party/acl/inc/acl/acl_op.h"14+#include <acl/acl_op.h>
15#include "torch_npu/csrc/aten/NPUNativeFunctions.h"15#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
16 16 
17namespace c10_npu {17namespace c10_npu {
@@ -1,8 +1,8 @@
1#include <unistd.h>1#include <unistd.h>
2#include <c10/util/flat_hash_map.h>2#include <c10/util/flat_hash_map.h>
3 3 
4-#include "third_party/acl/inc/acl/acl_base.h"4+#include <acl/acl_base.h>
5-#include "third_party/acl/inc/acl/acl_rt.h"5+#include <acl/acl_rt.h>
6#include "torch_npu/csrc/core/npu/NPUFunctions.h"6#include "torch_npu/csrc/core/npu/NPUFunctions.h"
7#include "torch_npu/csrc/core/npu/NpuVariables.h"7#include "torch_npu/csrc/core/npu/NpuVariables.h"
8#include "torch_npu/csrc/core/npu/NPUSwappedMemoryAllocator.h"8#include "torch_npu/csrc/core/npu/NPUSwappedMemoryAllocator.h"
@@ -4,8 +4,8 @@
4#include <c10/util/flat_hash_map.h>4#include <c10/util/flat_hash_map.h>
5#include <c10/util/irange.h>5#include <c10/util/irange.h>
6 6 
7-#include "third_party/acl/inc/acl/acl_base.h"7+#include <acl/acl_base.h>
8-#include "third_party/acl/inc/acl/acl_rt.h"8+#include <acl/acl_rt.h>
9#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"9#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
10#include "torch_npu/csrc/core/npu/register/OptionsManager.h"10#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
11#include "torch_npu/csrc/core/npu/NPUFunctions.h"11#include "torch_npu/csrc/core/npu/NPUFunctions.h"
@@ -22,8 +22,7 @@ namespace at_npu {
22namespace native {22namespace native {
23 23 
24at::Tensor allocate_workspace(uint64_t workspace_size, aclrtStream stream) {24at::Tensor allocate_workspace(uint64_t workspace_size, aclrtStream stream) {
25- return at_npu::native::OpPreparation::unsafe_empty_workspace(25+ return at_npu::native::OpPreparation::unsafe_empty_workspace(workspace_size, stream);
26- workspace_size, stream);
27}26}
28 27 
29} // namespace native28} // namespace native
@@ -63,8 +62,7 @@ class DeviceWorkspaceAllocator {
63 context_recorder_.store(nullptr);62 context_recorder_.store(nullptr);
64 }63 }
65 64 
66- std::shared_ptr<c10::GatheredContext> maybeGatherContext(65+ std::shared_ptr<c10::GatheredContext> maybeGatherContext(RecordContext level) {
67- RecordContext level) {
68 // Memory snapshots have deadlock issues in some scenarios, no longer66 // Memory snapshots have deadlock issues in some scenarios, no longer
69 // capture python stacks.67 // capture python stacks.
70 return nullptr;68 return nullptr;
@@ -85,37 +83,28 @@ class DeviceWorkspaceAllocator {
85 WorkspaceBlock* block = blocks[stream];83 WorkspaceBlock* block = blocks[stream];
86 if (block->size < alloc_size) {84 if (block->size < alloc_size) {
87 if (block->data_ptr != nullptr) {85 if (block->data_ptr != nullptr) {
88- ASCEND_LOGI(86+ ASCEND_LOGI("NPUWorkspaceAllocator free by aclrtFree: size=%zu", block->size);
89- "NPUWorkspaceAllocator free by aclrtFree: size=%zu", block->size);
90 NPU_CHECK_ERROR(c10_npu::acl::AclrtSynchronizeDeviceWithTimeout());87 NPU_CHECK_ERROR(c10_npu::acl::AclrtSynchronizeDeviceWithTimeout());
91 NPU_CHECK_ERROR(aclrtFree(block->data_ptr));88 NPU_CHECK_ERROR(aclrtFree(block->data_ptr));
92 update_stat(stats.reserved_bytes, -block->size);89 update_stat(stats.reserved_bytes, -block->size);
93#ifndef BUILD_LIBTORCH90#ifndef BUILD_LIBTORCH
94 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {91 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {
95- mstxDomainHandle_t workspaceDomain =92+ mstxDomainHandle_t workspaceDomain = torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(
96- torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(93+ torch_npu::profiler::DOMAIN_WORKSPACE.c_str());
97- torch_npu::profiler::DOMAIN_WORKSPACE.c_str());94+ mstxMemVirtualRangeDesc_t desc{device, block->data_ptr, stats.reserved_bytes.current};
98- mstxMemVirtualRangeDesc_t desc{95+ torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(workspaceDomain, &desc);
99- device, block->data_ptr, stats.reserved_bytes.current};
100- torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(
101- workspaceDomain, &desc);
102 }96 }
103 record_mem_size_decrement(block->size);97 record_mem_size_decrement(block->size);
104- const c10_npu::impl::PyCallbackTrigger* trigger =98+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
105- c10_npu::impl::NPUTrace::getTrace();
106 if (C10_UNLIKELY(trigger)) {99 if (C10_UNLIKELY(trigger)) {
107- trigger->traceNpuMemoryDeallocation(100+ trigger->traceNpuMemoryDeallocation(reinterpret_cast<uintptr_t>(block->data_ptr));
108- reinterpret_cast<uintptr_t>(block->data_ptr));
109 }101 }
110 torch_npu::profiler::reportMemoryDataToNpuProfiler(102 torch_npu::profiler::reportMemoryDataToNpuProfiler(
111 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),103 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),
112 device,104 device,
113- static_cast<uint8_t>(105+ static_cast<uint8_t>(torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),
114- torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),106+ static_cast<uint8_t>(torch_npu::profiler::MemoryDataType::MEMORY_FREE),
115- static_cast<uint8_t>(107+ static_cast<uint8_t>(torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
116- torch_npu::profiler::MemoryDataType::MEMORY_FREE),
117- static_cast<uint8_t>(
118- torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
119 reinterpret_cast<int64_t>(block->data_ptr),108 reinterpret_cast<int64_t>(block->data_ptr),
120 -block->size,109 -block->size,
121 stats.allocated_bytes.current,110 stats.allocated_bytes.current,
@@ -126,8 +115,7 @@ class DeviceWorkspaceAllocator {
126 block->data_ptr = nullptr;115 block->data_ptr = nullptr;
127 }116 }
128 117 
129- block->size =118+ block->size = kRoundLarge * ((alloc_size + kRoundLarge - 1) / kRoundLarge);
130- kRoundLarge * ((alloc_size + kRoundLarge - 1) / kRoundLarge);
131 119 
132 TORCH_CHECK(120 TORCH_CHECK(
133 alloc_size <= block->size,121 alloc_size <= block->size,
@@ -139,39 +127,29 @@ class DeviceWorkspaceAllocator {
139 PTA_ERROR(ErrCode::MEMORY));127 PTA_ERROR(ErrCode::MEMORY));
140 128 
141 aclError err = c10_npu::acl::AclrtMallocAlign32(129 aclError err = c10_npu::acl::AclrtMallocAlign32(
142- &block->data_ptr,130+ &block->data_ptr, block->size, aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_ONLY);
143- block->size,
144- aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_ONLY);
145 if (err != ACL_ERROR_NONE) {131 if (err != ACL_ERROR_NONE) {
146 return nullptr;132 return nullptr;
147 }133 }
148 block->context_when_allocated = std::move(context);134 block->context_when_allocated = std::move(context);
149 block->requested_size = static_cast<int64_t>(size);135 block->requested_size = static_cast<int64_t>(size);
150 136 
151- ASCEND_LOGD(137+ ASCEND_LOGD("NPUWorkspaceAllocator malloc by AclrtMallocAlign32: size=%zu", block->size);
152- "NPUWorkspaceAllocator malloc by AclrtMallocAlign32: size=%zu",
153- block->size);
154 update_stat(stats.reserved_bytes, block->size);138 update_stat(stats.reserved_bytes, block->size);
155#ifndef BUILD_LIBTORCH139#ifndef BUILD_LIBTORCH
156 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {140 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {
157- mstxDomainHandle_t workspaceDomain =141+ mstxDomainHandle_t workspaceDomain = torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(
158- torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(142+ torch_npu::profiler::DOMAIN_WORKSPACE.c_str());
159- torch_npu::profiler::DOMAIN_WORKSPACE.c_str());143+ mstxMemVirtualRangeDesc_t desc{device, block->data_ptr, stats.reserved_bytes.current};
160- mstxMemVirtualRangeDesc_t desc{144+ torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(workspaceDomain, &desc);
161- device, block->data_ptr, stats.reserved_bytes.current};
162- torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(
163- workspaceDomain, &desc);
164 }145 }
165 record_mem_size_increment(block->size);146 record_mem_size_increment(block->size);
166 torch_npu::profiler::reportMemoryDataToNpuProfiler(147 torch_npu::profiler::reportMemoryDataToNpuProfiler(
167 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),148 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),
168 device,149 device,
169- static_cast<uint8_t>(150+ static_cast<uint8_t>(torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),
170- torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),151+ static_cast<uint8_t>(torch_npu::profiler::MemoryDataType::MEMORY_MALLOC),
171- static_cast<uint8_t>(152+ static_cast<uint8_t>(torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
172- torch_npu::profiler::MemoryDataType::MEMORY_MALLOC),
173- static_cast<uint8_t>(
174- torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
175 reinterpret_cast<int64_t>(block->data_ptr),153 reinterpret_cast<int64_t>(block->data_ptr),
176 block->size,154 block->size,
177 stats.allocated_bytes.current,155 stats.allocated_bytes.current,
@@ -180,11 +158,9 @@ class DeviceWorkspaceAllocator {
180 stream});158 stream});
181 this->last_block = block;159 this->last_block = block;
182 this->last_stream = stream;160 this->last_stream = stream;
183- const c10_npu::impl::PyCallbackTrigger* trigger =161+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
184- c10_npu::impl::NPUTrace::getTrace();
185 if (C10_UNLIKELY(trigger)) {162 if (C10_UNLIKELY(trigger)) {
186- trigger->traceNpuMemoryAllocation(163+ trigger->traceNpuMemoryAllocation(reinterpret_cast<uintptr_t>(block->data_ptr));
187- reinterpret_cast<uintptr_t>(block->data_ptr));
188 }164 }
189#endif165#endif
190 }166 }
@@ -194,22 +170,16 @@ class DeviceWorkspaceAllocator {
194#ifndef BUILD_LIBTORCH170#ifndef BUILD_LIBTORCH
195 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {171 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {
196 mstxDomainHandle_t workspaceDomain =172 mstxDomainHandle_t workspaceDomain =
197- torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(173+ torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(torch_npu::profiler::DOMAIN_WORKSPACE.c_str());
198- torch_npu::profiler::DOMAIN_WORKSPACE.c_str());174+ mstxMemVirtualRangeDesc_t desc{device, block->data_ptr, stats.allocated_bytes.current};
199- mstxMemVirtualRangeDesc_t desc{175+ torch_npu::profiler::MstxMgr::GetInstance()->memRegionsRegister(workspaceDomain, &desc);
200- device, block->data_ptr, stats.allocated_bytes.current};
201- torch_npu::profiler::MstxMgr::GetInstance()->memRegionsRegister(
202- workspaceDomain, &desc);
203 }176 }
204 torch_npu::profiler::reportMemoryDataToNpuProfiler(177 torch_npu::profiler::reportMemoryDataToNpuProfiler(
205 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),178 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),
206 device,179 device,
207- static_cast<uint8_t>(180+ static_cast<uint8_t>(torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),
208- torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),181+ static_cast<uint8_t>(torch_npu::profiler::MemoryDataType::MEMORY_MALLOC),
209- static_cast<uint8_t>(182+ static_cast<uint8_t>(torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
210- torch_npu::profiler::MemoryDataType::MEMORY_MALLOC),
211- static_cast<uint8_t>(
212- torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
213 reinterpret_cast<int64_t>(block->data_ptr),183 reinterpret_cast<int64_t>(block->data_ptr),
214 block->size,184 block->size,
215 stats.allocated_bytes.current,185 stats.allocated_bytes.current,
@@ -228,21 +198,16 @@ class DeviceWorkspaceAllocator {
228#ifndef BUILD_LIBTORCH198#ifndef BUILD_LIBTORCH
229 if (this->last_block && this->last_block->data_ptr && this->last_stream) {199 if (this->last_block && this->last_block->data_ptr && this->last_stream) {
230 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {200 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {
231- mstxDomainHandle_t workspaceDomain =201+ mstxDomainHandle_t workspaceDomain = torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(
232- torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(202+ torch_npu::profiler::DOMAIN_WORKSPACE.c_str());
233- torch_npu::profiler::DOMAIN_WORKSPACE.c_str());203+ torch_npu::profiler::MstxMgr::GetInstance()->memRegionsUnregister(workspaceDomain, this->last_block->data_ptr);
234- torch_npu::profiler::MstxMgr::GetInstance()->memRegionsUnregister(
235- workspaceDomain, this->last_block->data_ptr);
236 }204 }
237 torch_npu::profiler::reportMemoryDataToNpuProfiler(205 torch_npu::profiler::reportMemoryDataToNpuProfiler(
238 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),206 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),
239 device,207 device,
240- static_cast<uint8_t>(208+ static_cast<uint8_t>(torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),
241- torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),209+ static_cast<uint8_t>(torch_npu::profiler::MemoryDataType::MEMORY_FREE),
242- static_cast<uint8_t>(210+ static_cast<uint8_t>(torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
243- torch_npu::profiler::MemoryDataType::MEMORY_FREE),
244- static_cast<uint8_t>(
245- torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
246 reinterpret_cast<int64_t>(this->last_block->data_ptr),211 reinterpret_cast<int64_t>(this->last_block->data_ptr),
247 -allocated_size,212 -allocated_size,
248 stats.allocated_bytes.current,213 stats.allocated_bytes.current,
@@ -255,46 +220,32 @@ class DeviceWorkspaceAllocator {
255 220 
256 // return to the system allocator221 // return to the system allocator
257 void empty_cache(bool check_error) {222 void empty_cache(bool check_error) {
258- ASCEND_LOGD(223+ ASCEND_LOGD("NPUWorkspaceAllocator begin empty cache with check_error = %d", check_error);
259- "NPUWorkspaceAllocator begin empty cache with check_error = %d",
260- check_error);
261 224 
262 std::lock_guard<std::recursive_mutex> lock(mutex);225 std::lock_guard<std::recursive_mutex> lock(mutex);
263 for (const auto& block_pair : blocks) {226 for (const auto& block_pair : blocks) {
264 if (block_pair.second->data_ptr != nullptr) {227 if (block_pair.second->data_ptr != nullptr) {
265- ASCEND_LOGI(228+ ASCEND_LOGI("NPUWorkspaceAllocator free by aclrtFree: size=%zu", block_pair.second->size);
266- "NPUWorkspaceAllocator free by aclrtFree: size=%zu",
267- block_pair.second->size);
268 NPU_CHECK_ERROR(aclrtFree(block_pair.second->data_ptr));229 NPU_CHECK_ERROR(aclrtFree(block_pair.second->data_ptr));
269 update_stat(stats.reserved_bytes, -block_pair.second->size);230 update_stat(stats.reserved_bytes, -block_pair.second->size);
270#ifndef BUILD_LIBTORCH231#ifndef BUILD_LIBTORCH
271 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {232 if (torch_npu::profiler::MstxMgr::GetInstance()->isMsleaksEnable()) {
272- mstxDomainHandle_t workspaceDomain =233+ mstxDomainHandle_t workspaceDomain = torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(
273- torch_npu::profiler::MstxMgr::GetInstance()->createLeaksDomain(234+ torch_npu::profiler::DOMAIN_WORKSPACE.c_str());
274- torch_npu::profiler::DOMAIN_WORKSPACE.c_str());235+ mstxMemVirtualRangeDesc_t desc{device, block_pair.second->data_ptr, stats.reserved_bytes.current};
275- mstxMemVirtualRangeDesc_t desc{236+ torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(workspaceDomain, &desc);
276- device,
277- block_pair.second->data_ptr,
278- stats.reserved_bytes.current};
279- torch_npu::profiler::MstxMgr::GetInstance()->memHeapRegister(
280- workspaceDomain, &desc);
281 }237 }
282 record_mem_size_decrement(block_pair.second->size);238 record_mem_size_decrement(block_pair.second->size);
283- const c10_npu::impl::PyCallbackTrigger* trigger =239+ const c10_npu::impl::PyCallbackTrigger* trigger = c10_npu::impl::NPUTrace::getTrace();
284- c10_npu::impl::NPUTrace::getTrace();
285 if (C10_UNLIKELY(trigger)) {240 if (C10_UNLIKELY(trigger)) {
286- trigger->traceNpuMemoryDeallocation(241+ trigger->traceNpuMemoryDeallocation(reinterpret_cast<uintptr_t>(block_pair.second->data_ptr));
287- reinterpret_cast<uintptr_t>(block_pair.second->data_ptr));
288 }242 }
289 torch_npu::profiler::reportMemoryDataToNpuProfiler(243 torch_npu::profiler::reportMemoryDataToNpuProfiler(
290 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),244 {static_cast<int8_t>(c10::DeviceType::PrivateUse1),
291 device,245 device,
292- static_cast<uint8_t>(246+ static_cast<uint8_t>(torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),
293- torch_npu::profiler::MemoryComponentType::WORKSPACE_ALLOCATOR),247+ static_cast<uint8_t>(torch_npu::profiler::MemoryDataType::MEMORY_FREE),
294- static_cast<uint8_t>(248+ static_cast<uint8_t>(torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
295- torch_npu::profiler::MemoryDataType::MEMORY_FREE),
296- static_cast<uint8_t>(
297- torch_npu::profiler::MemoryAllocatorType::ALLOCATOR_INNER),
298 reinterpret_cast<int64_t>(block_pair.second->data_ptr),249 reinterpret_cast<int64_t>(block_pair.second->data_ptr),
299 -block_pair.second->size,250 -block_pair.second->size,
300 stats.allocated_bytes.current,251 stats.allocated_bytes.current,
@@ -307,19 +258,12 @@ class DeviceWorkspaceAllocator {
307 }258 }
308 259 
309 blocks.clear();260 blocks.clear();
310- ASCEND_LOGD(261+ ASCEND_LOGD("NPUWorkspaceAllocator end empty cache with check_error = %d", check_error);
311- "NPUWorkspaceAllocator end empty cache with check_error = %d",
312- check_error);
313 }262 }
314 263 
315- void record_history(264+ void record_history(bool enabled, CreateContextFn context_recorder, RecordContext when) {
316- bool enabled,
317- CreateContextFn context_recorder,
318- RecordContext when) {
319 std::lock_guard<std::recursive_mutex> lock(mutex);265 std::lock_guard<std::recursive_mutex> lock(mutex);
320- TORCH_CHECK(266+ TORCH_CHECK(when == RecordContext::NEVER || context_recorder, PTA_ERROR(ErrCode::INTERNAL));
321- when == RecordContext::NEVER || context_recorder,
322- PTA_ERROR(ErrCode::INTERNAL));
323 record_flag = enabled;267 record_flag = enabled;
324 context_recorder_.store(record_flag ? context_recorder : nullptr);268 context_recorder_.store(record_flag ? context_recorder : nullptr);
325 record_context_ = enabled ? when : RecordContext::NEVER;269 record_context_ = enabled ? when : RecordContext::NEVER;
@@ -340,9 +284,7 @@ class DeviceWorkspaceAllocator {
340 block_pair.second->size,284 block_pair.second->size,
341 block_pair.first,285 block_pair.first,
342 MempoolId_t{0, 0},286 MempoolId_t{0, 0},
343- record_context_ >= RecordContext::ALLOC287+ record_context_ >= RecordContext::ALLOC ? block_pair.second->context_when_allocated : nullptr);
344- ? block_pair.second->context_when_allocated
345- : nullptr);
346 alloc_trace.emplace_back(te);288 alloc_trace.emplace_back(te);
347 289 
348 te = TraceEntry(290 te = TraceEntry(
@@ -352,9 +294,7 @@ class DeviceWorkspaceAllocator {
352 block_pair.second->size,294 block_pair.second->size,
353 block_pair.first,295 block_pair.first,
354 MempoolId_t{0, 0},296 MempoolId_t{0, 0},
355- record_context_ >= RecordContext::ALLOC297+ record_context_ >= RecordContext::ALLOC ? block_pair.second->context_when_allocated : nullptr);
356- ? block_pair.second->context_when_allocated
357- : nullptr);
358 alloc_trace.emplace_back(te);298 alloc_trace.emplace_back(te);
359 299 
360 te = TraceEntry(300 te = TraceEntry(
@@ -364,9 +304,7 @@ class DeviceWorkspaceAllocator {
364 block_pair.second->size,304 block_pair.second->size,
365 block_pair.first,305 block_pair.first,
366 MempoolId_t{0, 0},306 MempoolId_t{0, 0},
367- record_context_ >= RecordContext::ALLOC307+ record_context_ >= RecordContext::ALLOC ? block_pair.second->context_when_allocated : nullptr);
368- ? block_pair.second->context_when_allocated
369- : nullptr);
370 alloc_trace.emplace_back(te);308 alloc_trace.emplace_back(te);
371 }309 }
372#endif310#endif
@@ -381,13 +319,11 @@ class DeviceWorkspaceAllocator {
381 result.emplace_back();319 result.emplace_back();
382 SegmentInfo& segment_info = result.back();320 SegmentInfo& segment_info = result.back();
383 segment_info.device = device;321 segment_info.device = device;
384- segment_info.address =322+ segment_info.address = reinterpret_cast<int64_t>(block_pair.second->data_ptr);
385- reinterpret_cast<int64_t>(block_pair.second->data_ptr);
386 segment_info.stream = block_pair.first;323 segment_info.stream = block_pair.first;
387 segment_info.is_large = true;324 segment_info.is_large = true;
388 segment_info.is_expandable = false;325 segment_info.is_expandable = false;
389- segment_info.context_when_allocated =326+ segment_info.context_when_allocated = block_pair.second->context_when_allocated;
390- block_pair.second->context_when_allocated;
391 327 
392 const WorkspaceBlock* block = block_pair.second;328 const WorkspaceBlock* block = block_pair.second;
393 segment_info.blocks.emplace_back();329 segment_info.blocks.emplace_back();
@@ -484,26 +420,18 @@ class NpuWorkspaceAllocator : public c10::Allocator {
484 }420 }
485 421 
486 void malloc(void** new_ptr, int device, size_t size, aclrtStream stream) {422 void malloc(void** new_ptr, int device, size_t size, aclrtStream stream) {
487- auto src_ptr =423+ auto src_ptr = static_cast<void*>(device_allocator[device]->getStreamPtr(stream));
488- static_cast<void*>(device_allocator[device]->getStreamPtr(stream));424+ *new_ptr = static_cast<void*>(device_allocator[device]->malloc(size, stream));
489- *new_ptr =
490- static_cast<void*>(device_allocator[device]->malloc(size, stream));
491 425 
492 if ((*new_ptr) == nullptr) {426 if ((*new_ptr) == nullptr) {
493 size_t device_free;427 size_t device_free;
494 size_t device_total;428 size_t device_total;
495- NPU_CHECK_ERROR(429+ NPU_CHECK_ERROR(aclrtGetMemInfo(ACL_HBM_MEM, &device_free, &device_total));
496- aclrtGetMemInfo(ACL_HBM_MEM, &device_free, &device_total));
497 430 
498- auto retmsg =431+ auto retmsg = std::string("NPU out of memory. NPUWorkspaceAllocator tried to allocate ") + format_size(size) +
499- std::string(432+ "(NPU " + std::to_string(device) + "; " + format_size(device_total) + " total capacity; " +
500- "NPU out of memory. NPUWorkspaceAllocator tried to allocate ") +433+ format_size(device_free) + " free). If you want to reduce memory usage, " +
501- format_size(size) + "(NPU " + std::to_string(device) + "; " +434+ "take a try to set the environment variable TASK_QUEUE_ENABLE=1.\n" + PTA_ERROR(ErrCode::MEMORY);
502- format_size(device_total) + " total capacity; " +
503- format_size(device_free) +
504- " free). If you want to reduce memory usage, " +
505- "take a try to set the environment variable TASK_QUEUE_ENABLE=1.\n" +
506- PTA_ERROR(ErrCode::MEMORY);
507 ASCEND_LOGE("%s", retmsg.c_str());435 ASCEND_LOGE("%s", retmsg.c_str());
508 TORCH_CHECK_WITH(OutOfMemoryError, false, retmsg.c_str());436 TORCH_CHECK_WITH(OutOfMemoryError, false, retmsg.c_str());
509 }437 }
@@ -518,10 +446,7 @@ class NpuWorkspaceAllocator : public c10::Allocator {
518 allocated_ptrs.clear();446 allocated_ptrs.clear();
519 }447 }
520 448 
521- void record_history(449+ void record_history(bool enabled, CreateContextFn context_recorder, RecordContext when) {
522- bool enabled,
523- CreateContextFn context_recorder,
524- RecordContext when) {
525 for (auto& allocator : device_allocator) {450 for (auto& allocator : device_allocator) {
526 allocator->record_history(enabled, context_recorder, when);451 allocator->record_history(enabled, context_recorder, when);
527 }452 }
@@ -543,11 +468,7 @@ class NpuWorkspaceAllocator : public c10::Allocator {
543 NPU_CHECK_ERROR(c10_npu::GetDevice(&device));468 NPU_CHECK_ERROR(c10_npu::GetDevice(&device));
544 void* dev_ptr = nullptr;469 void* dev_ptr = nullptr;
545 void (*delete_func)(void*) = &local_raw_delete;470 void (*delete_func)(void*) = &local_raw_delete;
546- return {471+ return {dev_ptr, dev_ptr, delete_func, c10::Device(c10::DeviceType::PrivateUse1, device)};
547- dev_ptr,
548- dev_ptr,
549- delete_func,
550- c10::Device(c10::DeviceType::PrivateUse1, device)};
551 }472 }
552 473 
553 c10::DataPtr allocate_with_stream(size_t size, aclrtStream stream) {474 c10::DataPtr allocate_with_stream(size_t size, aclrtStream stream) {
@@ -560,21 +481,15 @@ class NpuWorkspaceAllocator : public c10::Allocator {
560 delete_func = &uncached_delete;481 delete_func = &uncached_delete;
561 if (size != 0) {482 if (size != 0) {
562 size_t alloc_size = size + 32;483 size_t alloc_size = size + 32;
563- NPU_CHECK_ERROR(c10_npu::acl::AclrtMallocAlign32(484+ NPU_CHECK_ERROR(
564- &dev_ptr,485+ c10_npu::acl::AclrtMallocAlign32(&dev_ptr, alloc_size, aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_ONLY));
565- alloc_size,
566- aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_ONLY));
567 }486 }
568 } else {487 } else {
569 if (size != 0) {488 if (size != 0) {
570 this->malloc(&dev_ptr, device, size, stream);489 this->malloc(&dev_ptr, device, size, stream);
571 }490 }
572 }491 }
573- return {492+ return {dev_ptr, dev_ptr, delete_func, c10::Device(c10::DeviceType::PrivateUse1, device)};
574- dev_ptr,
575- dev_ptr,
576- delete_func,
577- c10::Device(c10::DeviceType::PrivateUse1, device)};
578 }493 }
579 494 
580 c10::DeleterFnPtr raw_deleter() const override {495 c10::DeleterFnPtr raw_deleter() const override {
@@ -586,8 +501,7 @@ class NpuWorkspaceAllocator : public c10::Allocator {
586 501 
587 // Note [COW/lazy_clone is not supported yet]502 // Note [COW/lazy_clone is not supported yet]
588 void copy_data(void* dest, const void* src, std::size_t count) const final {503 void copy_data(void* dest, const void* src, std::size_t count) const final {
589- NPU_CHECK_ERROR(504+ NPU_CHECK_ERROR(aclrtMemcpy(dest, count, src, count, ACL_MEMCPY_DEVICE_TO_DEVICE));
590- aclrtMemcpy(dest, count, src, count, ACL_MEMCPY_DEVICE_TO_DEVICE));
591 }505 }
592 506 
593 void assertValidDevice(int device) {507 void assertValidDevice(int device) {
@@ -663,10 +577,7 @@ void emptyCache(int device, bool check_error) {
663 workspace_allocator.empty_cache(device, check_error);577 workspace_allocator.empty_cache(device, check_error);
664}578}
665 579 
666-void recordHistory(580+void recordHistory(bool enabled, CreateContextFn context_recorder, RecordContext when) {
667- bool enabled,
668- CreateContextFn context_recorder,
669- RecordContext when) {
670 workspace_allocator.record_history(enabled, context_recorder, when);581 workspace_allocator.record_history(enabled, context_recorder, when);
671}582}
672SnapshotInfo snapshot() {583SnapshotInfo snapshot() {
@@ -7,9 +7,9 @@
7#include "torch_npu/csrc/core/npu/NPUException.h"7#include "torch_npu/csrc/core/npu/NPUException.h"
8#include "torch_npu/csrc/core/npu/NPUFunctions.h"8#include "torch_npu/csrc/core/npu/NPUFunctions.h"
9#include "torch_npu/csrc/core/npu/NPUStream.h"9#include "torch_npu/csrc/core/npu/NPUStream.h"
10-#include "third_party/acl/inc/acl/acl.h"10+#include <acl/acl.h>
11-#include "third_party/acl/inc/acl/acl_base.h"11+#include <acl/acl_base.h>
12-#include "third_party/acl/inc/acl/acl_rt.h"12+#include <acl/acl_rt.h>
13 13 
14namespace c10_npu {14namespace c10_npu {
15namespace impl {15namespace impl {
@@ -2,12 +2,12 @@
2 2 
3#include <c10/core/Device.h>3#include <c10/core/Device.h>
4 4 
5-#include "third_party/acl/inc/acl/acl_rt.h"5+#include <acl/acl_rt.h>
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7-#include "third_party/acl/inc/acl/acl_mdl.h"7+#include <acl/acl_mdl.h>
8-#include "third_party/acl/inc/acl/acl_prof.h"8+#include <acl/acl_prof.h>
9#include "torch_npu/csrc/core/npu/interface/HcclInterface.h"9#include "torch_npu/csrc/core/npu/interface/HcclInterface.h"
10-#include "third_party/acl/inc/acl/acl.h"10+#include <acl/acl.h>
11 11 
12using aclrtHostFunc = void (*)(void* args);12using aclrtHostFunc = void (*)(void* args);
13struct aclrtMemUsageInfo;13struct aclrtMemUsageInfo;
@@ -6,7 +6,7 @@
6#include <ATen/record_function.h>6#include <ATen/record_function.h>
7#include "torch_npu/csrc/framework/utils/NpuUtils.h"7#include "torch_npu/csrc/framework/utils/NpuUtils.h"
8#include "torch_npu/csrc/framework/NPUDefine.h"8#include "torch_npu/csrc/framework/NPUDefine.h"
9-#include "third_party/acl/inc/acl/acl_rt.h"9+#include <acl/acl_rt.h>
10#ifndef BUILD_LIBTORCH10#ifndef BUILD_LIBTORCH
11#include "torch_npu/csrc/sanitizer/NPUTrace.h"11#include "torch_npu/csrc/sanitizer/NPUTrace.h"
12#endif12#endif
@@ -2,7 +2,7 @@
2 2 
3#include "c10/core/Storage.h"3#include "c10/core/Storage.h"
4#include "torch_npu/csrc/core/npu/NPUStream.h"4#include "torch_npu/csrc/core/npu/NPUStream.h"
5-#include "third_party/acl/inc/acl/acl_rt.h"5+#include <acl/acl_rt.h>
6 6 
7namespace c10_npu {7namespace c10_npu {
8namespace queue {8namespace queue {
@@ -1,5 +1,5 @@
1#pragma once1#pragma once
2-#include "third_party/acl/inc/aml/aml_fwk_detect.h"2+#include <aml/aml_fwk_detect.h>
3 3 
4namespace c10_npu {4namespace c10_npu {
5namespace amlapi {5namespace amlapi {
@@ -1,8 +1,8 @@
1#pragma once1#pragma once
2 2 
3-#include "third_party/acl/inc/acl/super_kernel.h"3+#include <acl/super_kernel.h>
4-#include "third_party/acl/inc/acl/acl_base.h"4+#include <acl/acl_base.h>
5-#include "third_party/acl/inc/acl/acl_mdl.h"5+#include <acl/acl_mdl.h>
6 6 
7namespace c10_npu {7namespace c10_npu {
8namespace skapi {8namespace skapi {
@@ -3,7 +3,7 @@
3#include <iostream>3#include <iostream>
4#include <string>4#include <string>
5#include <stdio.h>5#include <stdio.h>
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7#include "torch_npu/csrc/core/npu/register/OptionsManager.h"7#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
8 8 
9#if defined(__GNUC__) && __GNUC__ >= 129#if defined(__GNUC__) && __GNUC__ >= 12
@@ -22,8 +22,8 @@
22#include "torch_npu/csrc/core/npu/register/OptionsManager.h"22#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
23#include "torch_npu/csrc/core/npu/NpuVariables.h"23#include "torch_npu/csrc/core/npu/NpuVariables.h"
24#include "torch_npu/csrc/distributed/symm_mem/NPUSHMEMInterface.h"24#include "torch_npu/csrc/distributed/symm_mem/NPUSHMEMInterface.h"
25-#include "third_party/acl/inc/acl/acl_op_compiler.h"25+#include <acl/acl_op_compiler.h>
26-#include "third_party/acl/inc/acl/acl_rt.h"26+#include <acl/acl_rt.h>
27#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"27#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"
28#include "torch_npu/csrc/framework/LazyInitAclops.h"28#include "torch_npu/csrc/framework/LazyInitAclops.h"
29#include "torch_npu/csrc/core/npu/NPUFunctions.h"29#include "torch_npu/csrc/core/npu/NPUFunctions.h"
@@ -42,10 +42,8 @@ const uint32_t kMaxOpExecuteTimeOut = 547U;
42const size_t kMaxPathLen = 4096U;42const size_t kMaxPathLen = 4096U;
43 43 
44void SetDefaultAllowInternalFromatDisable() {44void SetDefaultAllowInternalFromatDisable() {
45- auto allow_internal_format =45+ auto allow_internal_format = c10_npu::option::GetOption("ALLOW_INTERNAL_FORMAT");
46- c10_npu::option::GetOption("ALLOW_INTERNAL_FORMAT");46+ if (allow_internal_format.has_value() && allow_internal_format.value() != "") {
47- if (allow_internal_format.has_value() &&
48- allow_internal_format.value() != "") {
49 return;47 return;
50 }48 }
51 49 
@@ -54,8 +52,7 @@ void SetDefaultAllowInternalFromatDisable() {
54}52}
55 53 
56void SetDeterministicFromLevel() {54void SetDeterministicFromLevel() {
57- at_npu::native::ApplyDeterministicSnapshot(55+ at_npu::native::ApplyDeterministicSnapshot(c10_npu::CaptureDeterministicSnapshot(), true);
58- c10_npu::CaptureDeterministicSnapshot(), true);
59}56}
60 57 
61#ifndef BUILD_LIBTORCH58#ifndef BUILD_LIBTORCH
@@ -88,8 +85,7 @@ std::string GetAclConfigJsonPath() {
88 const char* acl_init_path = c10_npu::option::OptionsManager::GetAclInitPath();85 const char* acl_init_path = c10_npu::option::OptionsManager::GetAclInitPath();
89 if (acl_init_path != nullptr) {86 if (acl_init_path != nullptr) {
90 std::string json_path = std::string(acl_init_path);87 std::string json_path = std::string(acl_init_path);
91- std::string json_path_str =88+ std::string json_path_str = torch_npu::toolkit::profiler::Utils::RealPath(json_path);
92- torch_npu::toolkit::profiler::Utils::RealPath(json_path);
93 if (json_path_str.empty()) {89 if (json_path_str.empty()) {
94 TORCH_CHECK(90 TORCH_CHECK(
95 false,91 false,
@@ -125,8 +121,7 @@ std::string GetAclConfigJsonPath() {
125 }121 }
126 122 
127 if (c10_npu::is_lazy_set_device()) {123 if (c10_npu::is_lazy_set_device()) {
128- if (!config.contains("defaultDevice") ||124+ if (!config.contains("defaultDevice") || !config["defaultDevice"].is_object()) {
129- !config["defaultDevice"].is_object()) {
130 TORCH_CHECK(125 TORCH_CHECK(
131 false,126 false,
132 "User acl json ",127 "User acl json ",
@@ -136,8 +131,7 @@ std::string GetAclConfigJsonPath() {
136 PTA_ERROR(ErrCode::VALUE));131 PTA_ERROR(ErrCode::VALUE));
137 }132 }
138 const auto& default_dev = config["defaultDevice"];133 const auto& default_dev = config["defaultDevice"];
139- if (!default_dev.contains("default_device") ||134+ if (!default_dev.contains("default_device") || !default_dev["default_device"].is_string() ||
140- !default_dev["default_device"].is_string() ||
141 default_dev["default_device"].get<std::string>() != "0") {135 default_dev["default_device"].get<std::string>() != "0") {
142 TORCH_CHECK(136 TORCH_CHECK(
143 false,137 false,
@@ -172,8 +166,7 @@ std::string GetAclConfigJsonPath() {
172 } else {166 } else {
173 json_path = npu_path.append("torch_npu/acl.json");167 json_path = npu_path.append("torch_npu/acl.json");
174 }168 }
175- std::string json_path_str =169+ std::string json_path_str = torch_npu::toolkit::profiler::Utils::RealPath(json_path);
176- torch_npu::toolkit::profiler::Utils::RealPath(json_path);
177 if (json_path_str == "") {170 if (json_path_str == "") {
178 ASCEND_LOGW("this path:%s is not exist!", json_path.c_str());171 ASCEND_LOGW("this path:%s is not exist!", json_path.c_str());
179 }172 }
@@ -263,11 +256,9 @@ NpuSysCtrl::SysStatus NpuSysCtrl::Initialize(int device_id) {
263 256 
264 if (!c10_npu::is_lazy_set_device()) {257 if (!c10_npu::is_lazy_set_device()) {
265 if (c10_npu::IsSupportInfNan()) {258 if (c10_npu::IsSupportInfNan()) {
266- c10_npu::acl::AclrtSetDeviceSatMode(259+ c10_npu::acl::AclrtSetDeviceSatMode(aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_INFNAN);
267- aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_INFNAN);
268 } else {260 } else {
269- c10_npu::acl::AclrtSetDeviceSatMode(261+ c10_npu::acl::AclrtSetDeviceSatMode(aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_SATURATION);
270- aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_SATURATION);
271 }262 }
272 }263 }
273 264 
@@ -287,8 +278,7 @@ NpuSysCtrl::SysStatus NpuSysCtrl::Initialize(int device_id) {
287 278 
288 if (!c10_npu::is_lazy_set_device()) {279 if (!c10_npu::is_lazy_set_device()) {
289 SetDeterministicFromLevel();280 SetDeterministicFromLevel();
290- NPU_CHECK_ERROR(281+ NPU_CHECK_ERROR(c10_npu::acl::AclrtSetOpExecuteTimeOut(kMaxOpExecuteTimeOut));
291- c10_npu::acl::AclrtSetOpExecuteTimeOut(kMaxOpExecuteTimeOut));
292 }282 }
293 283 
294 // lazy call for the setoption284 // lazy call for the setoption
@@ -329,11 +319,9 @@ NpuSysCtrl::SysStatus NpuSysCtrl::LazyInitialize(int device_id) {
329 auto ret = aclrtGetDevice(&device_id_);319 auto ret = aclrtGetDevice(&device_id_);
330 320 
331 if (c10_npu::IsSupportInfNan()) {321 if (c10_npu::IsSupportInfNan()) {
332- c10_npu::acl::AclrtSetDeviceSatMode(322+ c10_npu::acl::AclrtSetDeviceSatMode(aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_INFNAN);
333- aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_INFNAN);
334 } else {323 } else {
335- c10_npu::acl::AclrtSetDeviceSatMode(324+ c10_npu::acl::AclrtSetDeviceSatMode(aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_SATURATION);
336- aclrtFloatOverflowMode::ACL_RT_OVERFLOW_MODE_SATURATION);
337 }325 }
338 326 
339 SetDeterministicFromLevel();327 SetDeterministicFromLevel();
@@ -358,8 +346,7 @@ NpuSysCtrl::SysStatus NpuSysCtrl::BackwardsInit() {
358 346 
359NpuSysCtrl::SysStatus NpuSysCtrl::OverflowSwitchEnable() {347NpuSysCtrl::SysStatus NpuSysCtrl::OverflowSwitchEnable() {
360 if (!c10_npu::IsSupportInfNan()) {348 if (!c10_npu::IsSupportInfNan()) {
361- c10_npu::acl::AclrtSetStreamOverflowSwitch(349+ c10_npu::acl::AclrtSetStreamOverflowSwitch(c10_npu::getCurrentNPUStream(), 1);
362- c10_npu::getCurrentNPUStream(), 1);
363 ASCEND_LOGI("Npu overflow check switch set successfully.");350 ASCEND_LOGI("Npu overflow check switch set successfully.");
364 }351 }
365 return INIT_SUCC;352 return INIT_SUCC;
@@ -430,22 +417,15 @@ int NpuSysCtrl::InitializedDeviceID() {
430 if (GetInitFlag()) {417 if (GetInitFlag()) {
431 return device_id_;418 return device_id_;
432 }419 }
433- TORCH_CHECK(420+ TORCH_CHECK(false, "no npu device has been initialized!", PTA_ERROR(ErrCode::INTERNAL));
434- false,
435- "no npu device has been initialized!",
436- PTA_ERROR(ErrCode::INTERNAL));
437 return -1;421 return -1;
438}422}
439 423 
440-void NpuSysCtrl::RegisterLazyFn(424+void NpuSysCtrl::RegisterLazyFn(const option::OptionCallBack& call_, const std::string& in) {
441- const option::OptionCallBack& call_,
442- const std::string& in) {
443 lazy_fn_.emplace_back(std::make_pair(call_, in));425 lazy_fn_.emplace_back(std::make_pair(call_, in));
444}426}
445 427 
446-void NpuSysCtrl::RegisterReleaseFn(428+void NpuSysCtrl::RegisterReleaseFn(ReleaseFn release_fn, ReleasePriority priority) {
447- ReleaseFn release_fn,
448- ReleasePriority priority) {
449 const auto& iter = this->release_fn_.find(priority);429 const auto& iter = this->release_fn_.find(priority);
450 if (iter != release_fn_.end()) {430 if (iter != release_fn_.end()) {
451 release_fn_[priority].emplace_back(release_fn);431 release_fn_[priority].emplace_back(release_fn);
@@ -456,12 +436,10 @@ void NpuSysCtrl::RegisterReleaseFn(
456 436 
457aclError SetCurrentDevice() {437aclError SetCurrentDevice() {
458 if (c10_npu::NpuSysCtrl::GetInstance().GetInitFlag()) {438 if (c10_npu::NpuSysCtrl::GetInstance().GetInitFlag()) {
459- c10_npu::SetDevice(439+ c10_npu::SetDevice(c10_npu::NpuSysCtrl::GetInstance().InitializedDeviceID());
460- c10_npu::NpuSysCtrl::GetInstance().InitializedDeviceID());
461 return ACL_SUCCESS;440 return ACL_SUCCESS;
462 }441 }
463- TORCH_CHECK(442+ TORCH_CHECK(false, "npu device has not been inited.", PTA_ERROR(ErrCode::INTERNAL));
464- false, "npu device has not been inited.", PTA_ERROR(ErrCode::INTERNAL));
465}443}
466 444 
467} // namespace c10_npu445} // namespace c10_npu
@@ -1,6 +1,6 @@
1#pragma once1#pragma once
2 2 
3-#include <third_party/acl/inc/acl/acl.h>3+#include <acl/acl.h>
4#include <map>4#include <map>
5#include <string>5#include <string>
6#include <vector>6#include <vector>
@@ -7,7 +7,7 @@
7#include "torch_npu/csrc/core/npu/NPUMacros.h"7#include "torch_npu/csrc/core/npu/NPUMacros.h"
8#include "torch_npu/csrc/core/npu/NPUException.h"8#include "torch_npu/csrc/core/npu/NPUException.h"
9#include "torch_npu/csrc/framework/utils/OpPreparation.h"9#include "torch_npu/csrc/framework/utils/OpPreparation.h"
10-#include "third_party/acl/inc/acl/acl_base.h"10+#include <acl/acl_base.h>
11 11 
12namespace c10_npu {12namespace c10_npu {
13const int g_toAclOffset = 256;13const int g_toAclOffset = 256;
@@ -33,8 +33,8 @@
33#include <arpa/inet.h>33#include <arpa/inet.h>
34 34 
35#include "op_plugin/OpInterface.h"35#include "op_plugin/OpInterface.h"
36-#include "third_party/acl/inc/acl/acl.h"36+#include <acl/acl.h>
37-#include "third_party/acl/inc/acl/acl_base.h"37+#include <acl/acl_base.h>
38#include "torch_npu/csrc/aten/CustomFunctions.h"38#include "torch_npu/csrc/aten/CustomFunctions.h"
39#include "torch_npu/csrc/aten/NPUNativeFunctions.h"39#include "torch_npu/csrc/aten/NPUNativeFunctions.h"
40#include "torch_npu/csrc/core/npu/GetCANNInfo.h"40#include "torch_npu/csrc/core/npu/GetCANNInfo.h"
@@ -3,7 +3,7 @@
3#include <cstddef>3#include <cstddef>
4#include <cstdint>4#include <cstdint>
5#include "third_party/shmem/include/shmem_host_def.h"5#include "third_party/shmem/include/shmem_host_def.h"
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7 7 
8namespace c10d {8namespace c10d {
9namespace symmetric_memory {9namespace symmetric_memory {
@@ -4,7 +4,7 @@
4#include <ATen/ATen.h>4#include <ATen/ATen.h>
5 5 
6#include "torch_npu/csrc/framework/utils/NPUDefinition.h"6#include "torch_npu/csrc/framework/utils/NPUDefinition.h"
7-#include "third_party/acl/inc/acl/acl_base.h"7+#include <acl/acl_base.h>
8 8 
9namespace at_npu {9namespace at_npu {
10namespace native {10namespace native {
@@ -5,7 +5,7 @@
5#include <vector>5#include <vector>
6#include <string>6#include <string>
7#include <utility>7#include <utility>
8-#include "third_party/acl/inc/acl/acl_op_compiler.h"8+#include <acl/acl_op_compiler.h>
9 9 
10namespace at_npu {10namespace at_npu {
11namespace aclops {11namespace aclops {
@@ -4,8 +4,8 @@
4#include <ATen/ATen.h>4#include <ATen/ATen.h>
5 5 
6#include "torch_npu/csrc/framework/utils/NpuUtils.h"6#include "torch_npu/csrc/framework/utils/NpuUtils.h"
7-#include "third_party/acl/inc/acl/acl.h"7+#include <acl/acl.h>
8-#include "third_party/acl/inc/acl/acl_base.h"8+#include <acl/acl_base.h>
9 9 
10namespace at_npu {10namespace at_npu {
11namespace native {11namespace native {
@@ -4,7 +4,7 @@
4#include "torch_npu/csrc/core/npu/NPUStream.h"4#include "torch_npu/csrc/core/npu/NPUStream.h"
5#include "torch_npu/csrc/logging/LogContext.h"5#include "torch_npu/csrc/logging/LogContext.h"
6 6 
7-#include "third_party/acl/inc/acl/acl_base.h"7+#include <acl/acl_base.h>
8#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"8#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"
9#include "torch_npu/csrc/framework/NPUDefine.h"9#include "torch_npu/csrc/framework/NPUDefine.h"
10#include "torch_npu/csrc/framework/utils/ForceJitCompileList.h"10#include "torch_npu/csrc/framework/utils/ForceJitCompileList.h"
@@ -2,7 +2,7 @@
2#define __NATIVE_NPU_TOOLS_AOEUTILS__2#define __NATIVE_NPU_TOOLS_AOEUTILS__
3 3 
4#include <unordered_set>4#include <unordered_set>
5-#include <third_party/acl/inc/acl/acl_op_compiler.h>5+#include <acl/acl_op_compiler.h>
6#include "torch_npu/csrc/core/npu/NPUException.h"6#include "torch_npu/csrc/core/npu/NPUException.h"
7 7 
8namespace at_npu {8namespace at_npu {
@@ -3,7 +3,7 @@
3 3 
4#include <c10/util/SmallVector.h>4#include <c10/util/SmallVector.h>
5 5 
6-#include "third_party/acl/inc/acl/acl_base.h"6+#include <acl/acl_base.h>
7#include "torch_npu/csrc/framework/utils/NpuUtils.h"7#include "torch_npu/csrc/framework/utils/NpuUtils.h"
8#include "torch_npu/csrc/framework/utils/NPUDefinition.h"8#include "torch_npu/csrc/framework/utils/NPUDefinition.h"
9 9 
@@ -17,7 +17,6 @@ constexpr int MAX_DIM = 5;
17// Define the discontiguous cases vector to be optimized17// Define the discontiguous cases vector to be optimized
18using OptimizationCases = c10::SmallVector<std::string, MAX_CASES>;18using OptimizationCases = c10::SmallVector<std::string, MAX_CASES>;
19 19 
20- 
21struct ContiguousTensorDesc {20struct ContiguousTensorDesc {
22 bool is_contiguous_;21 bool is_contiguous_;
23 c10::SmallVector<int64_t, MAX_DIM> sizes_;22 c10::SmallVector<int64_t, MAX_DIM> sizes_;
@@ -30,8 +29,8 @@ struct ContiguousTensorDesc {
30 aclFormat npu_format_;29 aclFormat npu_format_;
31 OptimizationCases opt_cases_;30 OptimizationCases opt_cases_;
32 void refresh_contiguous_using_size_and_stride();31 void refresh_contiguous_using_size_and_stride();
33- void reset_optimization_cases(const OptimizationCases &opt_cases);32+ void reset_optimization_cases(const OptimizationCases& opt_cases);
34- void add_optimization_case(const std::string &opt_case);33+ void add_optimization_case(const std::string& opt_case);
35 void find_match_optimization_cases();34 void find_match_optimization_cases();
36 size_t hash_src_desc;35 size_t hash_src_desc;
37 bool cached_contiguous;36 bool cached_contiguous;
@@ -1,10 +1,10 @@
1#ifndef __TORCH_NPU_INTERFACE_ACLINTERFACE__1#ifndef __TORCH_NPU_INTERFACE_ACLINTERFACE__
2#define __TORCH_NPU_INTERFACE_ACLINTERFACE__2#define __TORCH_NPU_INTERFACE_ACLINTERFACE__
3 3 
4-#include "third_party/acl/inc/acl/acl_rt.h"4+#include <acl/acl_rt.h>
5-#include <third_party/acl/inc/acl/acl_base.h>5+#include <acl/acl_base.h>
6-#include <third_party/acl/inc/acl/acl_prof.h>6+#include <acl/acl_prof.h>
7-#include <third_party/acl/inc/acl/acl_op.h>7+#include <acl/acl_op.h>
8 8 
9namespace at_npu {9namespace at_npu {
10namespace native {10namespace native {
@@ -5,7 +5,7 @@
5#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"5#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"
6#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"6#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"
7#include "torch_npu/csrc/core/npu/register/OptionsManager.h"7#include "torch_npu/csrc/core/npu/register/OptionsManager.h"
8-#include "third_party/acl/inc/acl/acl_base.h"8+#include <acl/acl_base.h>
9 9 
10namespace at_npu {10namespace at_npu {
11namespace native {11namespace native {
@@ -1,7 +1,7 @@
1#ifndef __PLUGIN_NATIVE_NPU_INTERFACE_ACLOPCOMPILE__1#ifndef __PLUGIN_NATIVE_NPU_INTERFACE_ACLOPCOMPILE__
2#define __PLUGIN_NATIVE_NPU_INTERFACE_ACLOPCOMPILE__2#define __PLUGIN_NATIVE_NPU_INTERFACE_ACLOPCOMPILE__
3#include <c10/util/Optional.h>3#include <c10/util/Optional.h>
4-#include "third_party/acl/inc/acl/acl_op_compiler.h"4+#include <acl/acl_op_compiler.h>
5 5 
6typedef struct aclOpExecutor aclOpExecutor;6typedef struct aclOpExecutor aclOpExecutor;
7 7 
@@ -7,7 +7,7 @@
7#include <string>7#include <string>
8#include "torch_npu/csrc/core/npu/NPUException.h"8#include "torch_npu/csrc/core/npu/NPUException.h"
9 9 
10-#include "third_party/acl/inc/acl/acl_mdl.h"10+#include <acl/acl_mdl.h>
11#include "torch_npu/csrc/framework/utils/ForceJitCompileList.h"11#include "torch_npu/csrc/framework/utils/ForceJitCompileList.h"
12#include "torch_npu/csrc/framework/utils/ForceAclnnList.h"12#include "torch_npu/csrc/framework/utils/ForceAclnnList.h"
13#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"13#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"
@@ -1,7 +1,7 @@
1#include "torch_npu/csrc/framework/interface/MsProfilerInterface.h"1#include "torch_npu/csrc/framework/interface/MsProfilerInterface.h"
2#include "torch_npu/csrc/core/npu/NPUException.h"2#include "torch_npu/csrc/core/npu/NPUException.h"
3#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"3#include "torch_npu/csrc/core/npu/register/FunctionLoader.h"
4-#include "third_party/acl/inc/acl/acl_prof.h"4+#include <acl/acl_prof.h>
5 5 
6namespace at_npu {6namespace at_npu {
7namespace native {7namespace native {
@@ -1,7 +1,7 @@
1#ifndef __TORCH_NPU_MSPROFILERINTERFACE__1#ifndef __TORCH_NPU_MSPROFILERINTERFACE__
2#define __TORCH_NPU_MSPROFILERINTERFACE__2#define __TORCH_NPU_MSPROFILERINTERFACE__
3 3 
4-#include <third_party/acl/inc/acl/acl_prof.h>4+#include <acl/acl_prof.h>
5#include "torch_npu/csrc/core/npu/NPUException.h"5#include "torch_npu/csrc/core/npu/NPUException.h"
6 6 
7namespace at_npu {7namespace at_npu {
@@ -1,7 +1,7 @@
1#include <ATen/record_function.h>1#include <ATen/record_function.h>
2 2 
3-#include "third_party/acl/inc/acl/acl_base.h"3+#include <acl/acl_base.h>
4-#include "third_party/acl/inc/acl/acl_rt.h"4+#include <acl/acl_rt.h>
5#include "torch_npu/csrc/aten/mirror/NPUMemoryOverlap.h"5#include "torch_npu/csrc/aten/mirror/NPUMemoryOverlap.h"
6#include "torch_npu/csrc/core/NPUBridge.h"6#include "torch_npu/csrc/core/NPUBridge.h"
7#include "torch_npu/csrc/core/NPUStorageImpl.h"7#include "torch_npu/csrc/core/NPUStorageImpl.h"
@@ -26,9 +26,7 @@ constexpr float EPSILON = 1e-6;
26static const string CUBE_MATH_TYPE = "CUBE_MATH_TYPE";26static const string CUBE_MATH_TYPE = "CUBE_MATH_TYPE";
27 27 
28// check all at::ScalarType is not negative28// check all at::ScalarType is not negative
29-#define ENUM_PAIR_FUNC(_1, n) \29+#define ENUM_PAIR_FUNC(_1, n) static_assert(static_cast<int64_t>(at::ScalarType::n) >= 0, #n " is negative");
30- static_assert( \
31- static_cast<int64_t>(at::ScalarType::n) >= 0, #n " is negative");
32AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(ENUM_PAIR_FUNC)30AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(ENUM_PAIR_FUNC)
33#undef ENUM_PAIR_FUNC31#undef ENUM_PAIR_FUNC
34 32 
@@ -39,8 +37,7 @@ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(ENUM_PAIR_FUNC)
39// passes on 2.13 rc14 (where BComplex32 does not exist) and on 2.14 (where37// passes on 2.13 rc14 (where BComplex32 does not exist) and on 2.14 (where
40// it does). Ascend has no native BComplex32 dtype, so map to ACL_DT_UNDEFINED.38// it does). Ascend has no native BComplex32 dtype, so map to ACL_DT_UNDEFINED.
41#if TORCH_NPU_VERSION_GE(2, 14)39#if TORCH_NPU_VERSION_GE(2, 14)
42-#define AT_SCALAR_TYPE_V214_ADDS(_) \40+#define AT_SCALAR_TYPE_V214_ADDS(_) _(at::ScalarType::BComplex32, ACL_DT_UNDEFINED)
43- _(at::ScalarType::BComplex32, ACL_DT_UNDEFINED)
44#else41#else
45#define AT_SCALAR_TYPE_V214_ADDS(_) /* nothing on <2.14 */42#define AT_SCALAR_TYPE_V214_ADDS(_) /* nothing on <2.14 */
46#endif43#endif
@@ -96,61 +93,57 @@ AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(ENUM_PAIR_FUNC)
96 _(at::ScalarType::Undefined, ACL_DT_UNDEFINED) \93 _(at::ScalarType::Undefined, ACL_DT_UNDEFINED) \
97 _(at::ScalarType::NumOptions, ACL_DT_UNDEFINED)94 _(at::ScalarType::NumOptions, ACL_DT_UNDEFINED)
98 95 
99-constexpr aclDataType kATenScalarTypeToAclDataTypeTable96+constexpr aclDataType kATenScalarTypeToAclDataTypeTable[static_cast<int64_t>(at::ScalarType::NumOptions) + 1] = {
100- [static_cast<int64_t>(at::ScalarType::NumOptions) + 1] = {
101#define DEFINE_ENUM(_1, n) n,97#define DEFINE_ENUM(_1, n) n,
102- AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR(DEFINE_ENUM)98+ AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR(DEFINE_ENUM)
103#undef DEFINE_ENUM99#undef DEFINE_ENUM
104};100};
105 101 
106// check at::ScalarType has been changed or not102// check at::ScalarType has been changed or not
107-#define ENUM_PAIR_FUNC(at_dtype, acl_dtype) \103+#define ENUM_PAIR_FUNC(at_dtype, acl_dtype) \
108- static_assert( \104+ static_assert( \
109- kATenScalarTypeToAclDataTypeTable[static_cast<int64_t>(at_dtype)] == \105+ kATenScalarTypeToAclDataTypeTable[static_cast<int64_t>(at_dtype)] == (acl_dtype), \
110- (acl_dtype), \106+ #at_dtype " and " #acl_dtype \
111- #at_dtype " and " #acl_dtype \107+ " is not match any more, please check " \
112- " is not match any more, please check " \
113 "AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR and modify it");108 "AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR and modify it");
114AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR(ENUM_PAIR_FUNC)109AT_ALL_SCALAR_TYPE_AND_ACL_DATATYPE_PAIR(ENUM_PAIR_FUNC)
115#undef DEFINE_ENUM110#undef DEFINE_ENUM
116 111 
117-static std::map<const std::string, const aclDataType>112+static std::map<const std::string, const aclDataType> STRING_SCALAR_TYPE_TO_ACL_TYPE_MAP = {
118- STRING_SCALAR_TYPE_TO_ACL_TYPE_MAP = {113+ {"uint16", ACL_UINT16},
119- {"uint16", ACL_UINT16},114+ {"uint8", ACL_UINT8},
120- {"uint8", ACL_UINT8},115+ {"uint64", ACL_UINT64},
121- {"uint64", ACL_UINT64},116+ {"string", ACL_STRING}};
122- {"string", ACL_STRING}};
123 117 
124-static std::unordered_map<const aclDataType, const at::ScalarType>118+static std::unordered_map<const aclDataType, const at::ScalarType> ACL_TYPE_TO_SCALAR_TYPE_MAP = {
125- ACL_TYPE_TO_SCALAR_TYPE_MAP = {119+ {ACL_DT_UNDEFINED, at::ScalarType::Undefined},
126- {ACL_DT_UNDEFINED, at::ScalarType::Undefined},120+ {ACL_FLOAT, at::ScalarType::Float},
127- {ACL_FLOAT, at::ScalarType::Float},121+ {ACL_FLOAT16, at::ScalarType::Half},
128- {ACL_FLOAT16, at::ScalarType::Half},122+ {ACL_INT8, at::ScalarType::Char},
129- {ACL_INT8, at::ScalarType::Char},123+ {ACL_INT32, at::ScalarType::Int},
130- {ACL_INT32, at::ScalarType::Int},124+ {ACL_UINT8, at::ScalarType::Byte},
131- {ACL_UINT8, at::ScalarType::Byte},125+ {ACL_INT16, at::ScalarType::Short},
132- {ACL_INT16, at::ScalarType::Short},126+ {ACL_UINT16, at::ScalarType::UInt16},
133- {ACL_UINT16, at::ScalarType::UInt16},127+ {ACL_UINT32, at::ScalarType::UInt32},
134- {ACL_UINT32, at::ScalarType::UInt32},128+ {ACL_INT64, at::ScalarType::Long},
135- {ACL_INT64, at::ScalarType::Long},129+ {ACL_UINT64, at::ScalarType::UInt64},
136- {ACL_UINT64, at::ScalarType::UInt64},130+ {ACL_DOUBLE, at::ScalarType::Double},
137- {ACL_DOUBLE, at::ScalarType::Double},131+ {ACL_BOOL, at::ScalarType::Bool},
138- {ACL_BOOL, at::ScalarType::Bool},132+ {ACL_STRING, at::ScalarType::Undefined},
139- {ACL_STRING, at::ScalarType::Undefined},133+ {ACL_COMPLEX64, at::ScalarType::ComplexFloat},
140- {ACL_COMPLEX64, at::ScalarType::ComplexFloat},134+ {ACL_COMPLEX128, at::ScalarType::ComplexDouble},
141- {ACL_COMPLEX128, at::ScalarType::ComplexDouble},135+ {ACL_BF16, at::ScalarType::BFloat16},
142- {ACL_BF16, at::ScalarType::BFloat16},136+ {ACL_INT4, at::ScalarType::Undefined},
143- {ACL_INT4, at::ScalarType::Undefined},137+ {ACL_UINT1, at::ScalarType::Undefined},
144- {ACL_UINT1, at::ScalarType::Undefined},138+ {ACL_COMPLEX32, at::ScalarType::ComplexHalf},
145- {ACL_COMPLEX32, at::ScalarType::ComplexHalf},139+ {ACL_HIFLOAT8, at::ScalarType::Byte},
146- {ACL_HIFLOAT8, at::ScalarType::Byte},140+ {ACL_FLOAT8_E5M2, at::ScalarType::Float8_e5m2},
147- {ACL_FLOAT8_E5M2, at::ScalarType::Float8_e5m2},141+ {ACL_FLOAT8_E4M3FN, at::ScalarType::Float8_e4m3fn},
148- {ACL_FLOAT8_E4M3FN, at::ScalarType::Float8_e4m3fn},142+ {ACL_FLOAT8_E8M0, at::ScalarType::Float8_e8m0fnu},
149- {ACL_FLOAT8_E8M0, at::ScalarType::Float8_e8m0fnu},143+ {ACL_FLOAT6_E3M2, at::ScalarType::Byte},
150- {ACL_FLOAT6_E3M2, at::ScalarType::Byte},144+ {ACL_FLOAT6_E2M3, at::ScalarType::Byte},
151- {ACL_FLOAT6_E2M3, at::ScalarType::Byte},145+ {ACL_FLOAT4_E2M1, at::ScalarType::Float4_e2m1fn_x2},
152- {ACL_FLOAT4_E2M1, at::ScalarType::Float4_e2m1fn_x2},146+ {ACL_FLOAT4_E1M2, at::ScalarType::Byte}};
153- {ACL_FLOAT4_E1M2, at::ScalarType::Byte}};
154 147 
155aclError AclrtMemcpyAsyncParamCheck(148aclError AclrtMemcpyAsyncParamCheck(
156 void* dst,149 void* dst,
@@ -163,12 +156,7 @@ aclError AclrtMemcpyAsyncParamCheck(
163 return ret;156 return ret;
164}157}
165 158 
166-aclError AclrtMemcpyParamCheck(159+aclError AclrtMemcpyParamCheck(void* dst, size_t destMax, const void* src, size_t count, aclrtMemcpyKind kind) {
167- void* dst,
168- size_t destMax,
169- const void* src,
170- size_t count,
171- aclrtMemcpyKind kind) {
172 auto ret = aclrtMemcpy(dst, destMax, src, count, kind);160 auto ret = aclrtMemcpy(dst, destMax, src, count, kind);
173 return ret;161 return ret;
174}162}
@@ -179,8 +167,7 @@ namespace native {
179aclDataType CalcuOpUtil::ConvertToAclDataType(const at::ScalarType& data_type) {167aclDataType CalcuOpUtil::ConvertToAclDataType(const at::ScalarType& data_type) {
180 int64_t dtype_index = static_cast<int64_t>(data_type);168 int64_t dtype_index = static_cast<int64_t>(data_type);
181 TORCH_CHECK(169 TORCH_CHECK(
182- dtype_index >= 0 &&170+ dtype_index >= 0 && dtype_index < static_cast<int64_t>(at::ScalarType::NumOptions) + 1,
183- dtype_index < static_cast<int64_t>(at::ScalarType::NumOptions) + 1,
184 "data_type enum value (",171 "data_type enum value (",
185 dtype_index,172 dtype_index,
186 ") is out of range: [0, ",173 ") is out of range: [0, ",
@@ -195,13 +182,10 @@ aclDataType CalcuOpUtil::ConvertToAclDataType(const at::ScalarType& data_type) {
195 return acl_dtype;182 return acl_dtype;
196}183}
197 184 
198-aclDataType CalcuOpUtil::ConvertToAclDataType(185+aclDataType CalcuOpUtil::ConvertToAclDataType(const at::ScalarType& data_type, const std::string& realDataType) {
199- const at::ScalarType& data_type,
200- const std::string& realDataType) {
201 int64_t dtype_index = static_cast<int64_t>(data_type);186 int64_t dtype_index = static_cast<int64_t>(data_type);
202 TORCH_CHECK(187 TORCH_CHECK(
203- dtype_index >= 0 &&188+ dtype_index >= 0 && dtype_index < static_cast<int64_t>(at::ScalarType::NumOptions) + 1,
204- dtype_index < static_cast<int64_t>(at::ScalarType::NumOptions) + 1,
205 "data_type enum value (",189 "data_type enum value (",
206 dtype_index,190 dtype_index,
207 ") is out of range: [0, ",191 ") is out of range: [0, ",
@@ -250,11 +234,8 @@ c10::Scalar CalcuOpUtil::ConvertTensorToScalar(const at::Tensor& tensor) {
250 return expScalar;234 return expScalar;
251}235}
252 236 
253-at::Tensor CalcuOpUtil::CopyScalarToDevice(237+at::Tensor CalcuOpUtil::CopyScalarToDevice(const c10::Scalar& cpu_scalar, at::ScalarType scalar_data_type) {
254- const c10::Scalar& cpu_scalar,238+ return CalcuOpUtil::CopyTensorHostToDevice(scalar_to_tensor(cpu_scalar).to(scalar_data_type));
255- at::ScalarType scalar_data_type) {
256- return CalcuOpUtil::CopyTensorHostToDevice(
257- scalar_to_tensor(cpu_scalar).to(scalar_data_type));
258}239}
259 240 
260at::Tensor CalcuOpUtil::CopyTensorHostToDevice(const at::Tensor& cpu_tensor) {241at::Tensor CalcuOpUtil::CopyTensorHostToDevice(const at::Tensor& cpu_tensor) {
@@ -262,10 +243,7 @@ at::Tensor CalcuOpUtil::CopyTensorHostToDevice(const at::Tensor& cpu_tensor) {
262 int deviceIndex = 0;243 int deviceIndex = 0;
263 NPU_CHECK_ERROR(c10_npu::GetDevice(&deviceIndex));244 NPU_CHECK_ERROR(c10_npu::GetDevice(&deviceIndex));
264 return cpuPinMemTensor.to(245 return cpuPinMemTensor.to(
265- c10::Device(c10::DeviceType::PrivateUse1, deviceIndex),246+ c10::Device(c10::DeviceType::PrivateUse1, deviceIndex), cpuPinMemTensor.scalar_type(), true, true);
266- cpuPinMemTensor.scalar_type(),
267- true,
268- true);
269}247}
270 248 
271NPUStatus CalcuOpUtil::AclrtMemcpyAsync(249NPUStatus CalcuOpUtil::AclrtMemcpyAsync(
@@ -274,12 +252,9 @@ NPUStatus CalcuOpUtil::AclrtMemcpyAsync(
274 const std::pair<at::Tensor, int64_t>& src,252 const std::pair<at::Tensor, int64_t>& src,
275 size_t src_size,253 size_t src_size,
276 aclrtMemcpyKind kind) {254 aclrtMemcpyKind kind) {
277- void* dst_ptr = reinterpret_cast<uint8_t*>(dst.first.data_ptr()) +255+ void* dst_ptr = reinterpret_cast<uint8_t*>(dst.first.data_ptr()) + dst.second * dst.first.itemsize();
278- dst.second * dst.first.itemsize();256+ void* src_ptr = reinterpret_cast<uint8_t*>(src.first.data_ptr()) + src.second * src.first.itemsize();
279- void* src_ptr = reinterpret_cast<uint8_t*>(src.first.data_ptr()) +257+ NPU_CHECK_ERROR(c10_npu::queue::LaunchAsyncCopyTask(dst_ptr, dst_size, const_cast<void*>(src_ptr), src_size, kind));
280- src.second * src.first.itemsize();
281- NPU_CHECK_ERROR(c10_npu::queue::LaunchAsyncCopyTask(
282- dst_ptr, dst_size, const_cast<void*>(src_ptr), src_size, kind));
283 258 
284 return NPU_STATUS_SUCCESS;259 return NPU_STATUS_SUCCESS;
285}260}
@@ -290,12 +265,9 @@ aclError CalcuOpUtil::AclrtMemcpyWithModeSwitch(
290 const StorageAndOffsetMemSizePair& src,265 const StorageAndOffsetMemSizePair& src,
291 size_t count,266 size_t count,
292 aclrtMemcpyKind kind) {267 aclrtMemcpyKind kind) {
293- void* dst_ptr = static_cast<void*>(268+ void* dst_ptr = static_cast<void*>(static_cast<uint8_t*>(const_cast<void*>(dst.first->data())) + dst.second);
294- static_cast<uint8_t*>(const_cast<void*>(dst.first->data())) + dst.second);269+ void* src_ptr = static_cast<void*>(static_cast<uint8_t*>(const_cast<void*>(src.first->data())) + src.second);
295- void* src_ptr = static_cast<void*>(270+ return AclrtMemcpyParamCheck(dst_ptr, dstMax, const_cast<void*>(src_ptr), count, kind);
296- static_cast<uint8_t*>(const_cast<void*>(src.first->data())) + src.second);
297- return AclrtMemcpyParamCheck(
298- dst_ptr, dstMax, const_cast<void*>(src_ptr), count, kind);
299}271}
300 272 
301aclError CalcuOpUtil::AclrtMemcpyWithModeSwitch(273aclError CalcuOpUtil::AclrtMemcpyWithModeSwitch(
@@ -304,8 +276,7 @@ aclError CalcuOpUtil::AclrtMemcpyWithModeSwitch(
304 const void* src,276 const void* src,
305 size_t count,277 size_t count,
306 aclrtMemcpyKind kind) {278 aclrtMemcpyKind kind) {
307- void* dst_ptr = static_cast<void*>(279+ void* dst_ptr = static_cast<void*>(static_cast<uint8_t*>(const_cast<void*>(dst.first->data())) + dst.second);
308- static_cast<uint8_t*>(const_cast<void*>(dst.first->data())) + dst.second);
309 return AclrtMemcpyParamCheck(dst_ptr, dstMax, src, count, kind);280 return AclrtMemcpyParamCheck(dst_ptr, dstMax, src, count, kind);
310}281}
311 282 
@@ -315,10 +286,8 @@ aclError CalcuOpUtil::AclrtMemcpyWithModeSwitch(
315 const StorageAndOffsetMemSizePair& src,286 const StorageAndOffsetMemSizePair& src,
316 size_t count,287 size_t count,
317 aclrtMemcpyKind kind) {288 aclrtMemcpyKind kind) {
318- void* src_ptr = static_cast<void*>(289+ void* src_ptr = static_cast<void*>(static_cast<uint8_t*>(const_cast<void*>(src.first->data())) + src.second);
319- static_cast<uint8_t*>(const_cast<void*>(src.first->data())) + src.second);290+ return AclrtMemcpyParamCheck(dst, dstMax, const_cast<void*>(src_ptr), count, kind);
320- return AclrtMemcpyParamCheck(
321- dst, dstMax, const_cast<void*>(src_ptr), count, kind);
322}291}
323 292 
324aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(293aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
@@ -327,8 +296,7 @@ aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
327 const at::Tensor& src,296 const at::Tensor& src,
328 size_t count,297 size_t count,
329 aclrtMemcpyKind kind) {298 aclrtMemcpyKind kind) {
330- aclError ret = c10_npu::queue::LaunchAsyncCopyTask(299+ aclError ret = c10_npu::queue::LaunchAsyncCopyTask(dst.data_ptr(), dstMax, src.data_ptr(), count, kind);
331- dst.data_ptr(), dstMax, src.data_ptr(), count, kind);
332 return ret;300 return ret;
333}301}
334 302 
@@ -338,8 +306,7 @@ aclError CalcuOpUtil::LaunchAsyncCopyTaskWithModeSwitch(
338 void* src,306 void* src,
339 size_t count,307 size_t count,
340 aclrtMemcpyKind kind) {308 aclrtMemcpyKind kind) {
341- aclError ret = c10_npu::queue::LaunchAsyncCopyTask(309+ aclError ret = c10_npu::queue::LaunchAsyncCopyTask(const_cast<void*>(dst.data()), dstMax, src, count, kind);
342- const_cast<void*>(dst.data()), dstMax, src, count, kind);
343 return ret;310 return ret;
344}311}
345 312 
@@ -351,8 +318,7 @@ int64_t CalcuOpUtil::GetTensorNpuFormat(const at::Tensor& tensor) {
351 "device is correct.",318 "device is correct.",
352 OPS_ERROR(ErrCode::TYPE));319 OPS_ERROR(ErrCode::TYPE));
353 if (NpuUtils::check_match(&tensor) || NpuUtils::check_5d_5d_match(tensor)) {320 if (NpuUtils::check_match(&tensor) || NpuUtils::check_5d_5d_match(tensor)) {
354- const torch_npu::NPUStorageDesc& tensor_desc =321+ const torch_npu::NPUStorageDesc& tensor_desc = torch_npu::NPUBridge::GetNpuStorageImpl(tensor)->npu_desc_;
355- torch_npu::NPUBridge::GetNpuStorageImpl(tensor)->npu_desc_;
356 return tensor_desc.npu_format_;322 return tensor_desc.npu_format_;
357 } else if (tensor.data_ptr() == nullptr) {323 } else if (tensor.data_ptr() == nullptr) {
358 // transforming faketensor into realtensor and assigning format ND324 // transforming faketensor into realtensor and assigning format ND
@@ -362,9 +328,7 @@ int64_t CalcuOpUtil::GetTensorNpuFormat(const at::Tensor& tensor) {
362 }328 }
363}329}
364 330 
365-void CalcuOpUtil::CheckMemoryOverLaps(331+void CalcuOpUtil::CheckMemoryOverLaps(c10::ArrayRef<at::Tensor> inputs, c10::ArrayRef<at::Tensor> outputs) {
366- c10::ArrayRef<at::Tensor> inputs,
367- c10::ArrayRef<at::Tensor> outputs) {
368 for (const auto i : c10::irange(outputs.size())) {332 for (const auto i : c10::irange(outputs.size())) {
369 if (!outputs[i].defined()) {333 if (!outputs[i].defined()) {
370 continue;334 continue;
@@ -379,8 +343,7 @@ void CalcuOpUtil::CheckMemoryOverLaps(
379}343}
380 344 
381bool CalcuOpUtil::IsScalarWrappedToTensor(const at::Tensor& tensor) {345bool CalcuOpUtil::IsScalarWrappedToTensor(const at::Tensor& tensor) {
382- return tensor.unsafeGetTensorImpl()->is_wrapped_number() &&346+ return tensor.unsafeGetTensorImpl()->is_wrapped_number() && (!torch_npu::utils::is_npu(tensor));
383- (!torch_npu::utils::is_npu(tensor));
384}347}
385 348 
386float CalcuOpUtil::GetScalarFloatValue(const c10::Scalar& scalar) {349float CalcuOpUtil::GetScalarFloatValue(const c10::Scalar& scalar) {
@@ -394,8 +357,7 @@ float CalcuOpUtil::GetScalarFloatValue(const c10::Scalar& scalar) {
394 return value;357 return value;
395}358}
396 359 
397-c10::SmallVector<int64_t, SHAPE_SIZE> CalcuOpUtil::360+c10::SmallVector<int64_t, SHAPE_SIZE> CalcuOpUtil::ConvertIntArrayRefToSmallVector(c10::IntArrayRef intArray) {
398- ConvertIntArrayRefToSmallVector(c10::IntArrayRef intArray) {
399 c10::SmallVector<int64_t, SHAPE_SIZE> intVec;361 c10::SmallVector<int64_t, SHAPE_SIZE> intVec;
400 for (const auto i : c10::irange(intArray.size())) {362 for (const auto i : c10::irange(intArray.size())) {
401 intVec.emplace_back(intArray[i]);363 intVec.emplace_back(intArray[i]);
@@ -419,19 +381,13 @@ static std::unordered_map<uint8_t, aclCubeMathType> ACL_CUBE_MATH_TYPE_MAP = {
419 {0b10, USE_HF32},381 {0b10, USE_HF32},
420 {0b11, ALLOW_FP32_DOWN_PRECISION}};382 {0b11, ALLOW_FP32_DOWN_PRECISION}};
421 383 
422-static std::unordered_map<uint8_t, aclCubeMathType>384+static std::unordered_map<uint8_t, aclCubeMathType> ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH =
423- ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH = {385+ {{0b00, KEEP_DTYPE}, {0b01, ALLOW_FP32_DOWN_PRECISION}, {0b10, USE_FP16}, {0b11, USE_HF32}, {0b100, USE_FP32_ADD}};
424- {0b00, KEEP_DTYPE},
425- {0b01, ALLOW_FP32_DOWN_PRECISION},
426- {0b10, USE_FP16},
427- {0b11, USE_HF32},
428- {0b100, USE_FP32_ADD}};
429 386 
430int8_t CalcuOpUtil::GetCubeMathType() {387int8_t CalcuOpUtil::GetCubeMathType() {
431 auto option_key = c10_npu::option::GetOption(CUBE_MATH_TYPE);388 auto option_key = c10_npu::option::GetOption(CUBE_MATH_TYPE);
432 if (option_key.has_value() && !option_key.value().empty()) {389 if (option_key.has_value() && !option_key.value().empty()) {
433- uint8_t cube_math_type =390+ uint8_t cube_math_type = static_cast<uint8_t>(std::stoi(option_key.value().c_str()));
434- static_cast<uint8_t>(std::stoi(option_key.value().c_str()));
435 auto iter = ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH.find(cube_math_type);391 auto iter = ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH.find(cube_math_type);
436 if (iter != ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH.end()) {392 if (iter != ACL_CUBE_MATH_TYPE_MAP_PASSTHROUGH.end()) {
437 return iter->second;393 return iter->second;
@@ -442,8 +398,7 @@ int8_t CalcuOpUtil::GetCubeMathType() {
442 398 
443int8_t CalcuOpUtil::GetCubeMathType(bool allowHf32) {399int8_t CalcuOpUtil::GetCubeMathType(bool allowHf32) {
444 bool allowFp32ToFp16 = native::env::IsAllowFP32ToFP16();400 bool allowFp32ToFp16 = native::env::IsAllowFP32ToFP16();
445- uint8_t CubeMathTypeCode = (static_cast<uint8_t>(allowHf32) << 1) +401+ uint8_t CubeMathTypeCode = (static_cast<uint8_t>(allowHf32) << 1) + static_cast<uint8_t>(allowFp32ToFp16);
446- static_cast<uint8_t>(allowFp32ToFp16);
447 auto iter = ACL_CUBE_MATH_TYPE_MAP.find(CubeMathTypeCode);402 auto iter = ACL_CUBE_MATH_TYPE_MAP.find(CubeMathTypeCode);
448 if (iter == ACL_CUBE_MATH_TYPE_MAP.end()) {403 if (iter == ACL_CUBE_MATH_TYPE_MAP.end()) {
449 return ALLOW_FP32_DOWN_PRECISION;404 return ALLOW_FP32_DOWN_PRECISION;
@@ -456,8 +411,7 @@ at::ScalarType CalcuOpUtil::ConvertToScalarType(const aclDataType data_type) {
456 if (iter == ACL_TYPE_TO_SCALAR_TYPE_MAP.end()) {411 if (iter == ACL_TYPE_TO_SCALAR_TYPE_MAP.end()) {
457 TORCH_CHECK(412 TORCH_CHECK(
458 false,413 false,
459- std::string("aclDataType:") + std::to_string(data_type) +414+ std::string("aclDataType:") + std::to_string(data_type) + " has not been supported",
460- " has not been supported",
461 OPS_ERROR(ErrCode::NOT_SUPPORT))415 OPS_ERROR(ErrCode::NOT_SUPPORT))
462 }416 }
463 417 
@@ -13,8 +13,8 @@
13#include "torch_npu/csrc/framework/utils/NpuUtils.h"13#include "torch_npu/csrc/framework/utils/NpuUtils.h"
14#include "torch_npu/csrc/core/npu/interface/AclInterface.h"14#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
15#include "torch_npu/csrc/core/npu/npu_log.h"15#include "torch_npu/csrc/core/npu/npu_log.h"
16-#include "third_party/acl/inc/acl/acl_base.h"16+#include <acl/acl_base.h>
17-#include "third_party/acl/inc/acl/acl.h"17+#include <acl/acl.h>
18 18 
19using std::string;19using std::string;
20using std::vector;20using std::vector;
@@ -2,7 +2,7 @@
2#define __PLUGIN_NATIVE_UTILS_NPU_CONFIG__2#define __PLUGIN_NATIVE_UTILS_NPU_CONFIG__
3 3 
4#include <c10/util/SmallVector.h>4#include <c10/util/SmallVector.h>
5-#include <third_party/acl/inc/graph/operator.h>5+#include <graph/operator.h>
6 6 
7#include <functional>7#include <functional>
8#include <vector>8#include <vector>
@@ -7,9 +7,9 @@
7#include <ATen/ATen.h>7#include <ATen/ATen.h>
8#include "torch_npu/csrc/core/npu/npu_log.h"8#include "torch_npu/csrc/core/npu/npu_log.h"
9 9 
10-#include "third_party/acl/inc/acl/acl.h"10+#include <acl/acl.h>
11-#include "third_party/acl/inc/acl/acl_base.h"11+#include <acl/acl_base.h>
12-#include "third_party/acl/inc/acl/acl_op.h"12+#include <acl/acl_op.h>
13 13 
14#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"14#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
15#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"15#include "torch_npu/csrc/framework/interface/AclOpCompileInterface.h"
@@ -2,8 +2,8 @@
2 2 
3#ifdef USE_NPU3#ifdef USE_NPU
4 4 
5-#include "third_party/acl/inc/acl/acl_base.h"5+#include <acl/acl_base.h>
6-#include "third_party/acl/inc/acl/acl_rt.h"6+#include <acl/acl_rt.h>
7 7 
8typedef void* NPUdeviceptr;8typedef void* NPUdeviceptr;
9 9 
@@ -6,7 +6,7 @@
6// C ABI defined in torch_npu/csrc/inductor/aoti_torch/c/shim.h. The same rule6// C ABI defined in torch_npu/csrc/inductor/aoti_torch/c/shim.h. The same rule
7// applies to other files under torch_npu/csrc/inductor/aoti_runtime/.7// applies to other files under torch_npu/csrc/inductor/aoti_runtime/.
8 8 
9-#include <third_party/acl/inc/acl/acl_base.h>9+#include <acl/acl_base.h>
10#include <torch/csrc/inductor/aoti_runtime/utils.h>10#include <torch/csrc/inductor/aoti_runtime/utils.h>
11#include <torch_npu/csrc/inductor/aoti_torch/c/shim_npu.h>11#include <torch_npu/csrc/inductor/aoti_torch/c/shim_npu.h>
12namespace torch::aot_inductor {12namespace torch::aot_inductor {
@@ -8,8 +8,8 @@
8#include <torch_npu/csrc/framework/OpCommand.h>8#include <torch_npu/csrc/framework/OpCommand.h>
9#include <torch_npu/csrc/profiler/profiler_mgr.h>9#include <torch_npu/csrc/profiler/profiler_mgr.h>
10 10 
11-#include "third_party/acl/inc/profiling/prof_api.h"11+#include <profiling/prof_api.h>
12-#include "third_party/acl/inc/profiling/prof_common.h"12+#include <profiling/prof_common.h>
13 13 
14struct TilingMem {14struct TilingMem {
15 std::unique_ptr<void, decltype(&aclrtFreeHost)> arg_tiling_host;15 std::unique_ptr<void, decltype(&aclrtFreeHost)> arg_tiling_host;
@@ -1,8 +1,8 @@
1#ifndef BUILD_LIBTORCH1#ifndef BUILD_LIBTORCH
2#include <Python.h>2#include <Python.h>
3#include <functional>3#include <functional>
4-#include "third_party/acl/inc/acl/acl_base.h"4+#include <acl/acl_base.h>
5-#include "third_party/acl/inc/acl/acl_rt.h"5+#include <acl/acl_rt.h>
6#include "torch_npu/csrc/inductor/mlir/hacl_rt.h"6#include "torch_npu/csrc/inductor/mlir/hacl_rt.h"
7 7 
8rtError_t common_launch(8rtError_t common_launch(
@@ -8,8 +8,8 @@
8#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"8#include "torch_npu/csrc/core/npu/interface/AsyncTaskQueueInterface.h"
9#include "torch_npu/csrc/ipc/NPUIPCTypes.h"9#include "torch_npu/csrc/ipc/NPUIPCTypes.h"
10 10 
11-#include "third_party/acl/inc/acl/acl_base.h"11+#include <acl/acl_base.h>
12-#include "third_party/acl/inc/acl/acl_rt.h"12+#include <acl/acl_rt.h>
13 13 
14namespace torch_npu {14namespace torch_npu {
15namespace ipc {15namespace ipc {
@@ -22,8 +22,8 @@
22#include "torch_npu/csrc/ipc/NPUIPCTypes.h"22#include "torch_npu/csrc/ipc/NPUIPCTypes.h"
23#include "torch_npu/csrc/ipc/StorageSharing.h"23#include "torch_npu/csrc/ipc/StorageSharing.h"
24 24 
25-#include "third_party/acl/inc/acl/acl_base.h"25+#include <acl/acl_base.h>
26-#include "third_party/acl/inc/acl/acl_rt.h"26+#include <acl/acl_rt.h>
27 27 
28namespace torch_npu {28namespace torch_npu {
29namespace reductions {29namespace reductions {
@@ -5,9 +5,9 @@
5#include <torch/csrc/jit/python/pybind_utils.h>5#include <torch/csrc/jit/python/pybind_utils.h>
6#include <torch/csrc/utils/pybind.h>6#include <torch/csrc/utils/pybind.h>
7 7 
8-#include "third_party/acl/inc/acl/acl_base.h"8+#include <acl/acl_base.h>
9-#include "third_party/acl/inc/acl/acl_rt.h"9+#include <acl/acl_rt.h>
10-#include "third_party/acl/inc/acl/super_kernel.h"10+#include <acl/super_kernel.h>
11 11 
12struct PendingTensorData {12struct PendingTensorData {
13 PendingTensorData(uintptr_t dataPtr, Py_ssize_t nbytes, PyObject* shape, PyObject* dtype)13 PendingTensorData(uintptr_t dataPtr, Py_ssize_t nbytes, PyObject* shape, PyObject* dtype)
@@ -27,7 +27,7 @@
27#include <torch_npu/csrc/inductor/aoti_runner/pybind.h>27#include <torch_npu/csrc/inductor/aoti_runner/pybind.h>
28 28 
29#include <op_plugin/utils/custom_functions/opapi/FFTCommonOpApi.h>29#include <op_plugin/utils/custom_functions/opapi/FFTCommonOpApi.h>
30-#include <third_party/acl/inc/acl/acl.h>30+#include <acl/acl.h>
31#include <third_party/fmt/include/fmt/format.h>31#include <third_party/fmt/include/fmt/format.h>
32#include <torch_npu/csrc/aten/NPUGeneratorImpl.h>32#include <torch_npu/csrc/aten/NPUGeneratorImpl.h>
33#include <torch_npu/csrc/aten/NPUNativeFunctions.h>33#include <torch_npu/csrc/aten/NPUNativeFunctions.h>
@@ -3,9 +3,9 @@
3#include <torch/csrc/Device.h>3#include <torch/csrc/Device.h>
4#include <torch/csrc/THP.h>4#include <torch/csrc/THP.h>
5 5 
6-#include "third_party/acl/inc/acl/acl.h"6+#include <acl/acl.h>
7-#include "third_party/acl/inc/acl/acl_base.h"7+#include <acl/acl_base.h>
8-#include "third_party/acl/inc/acl/acl_rt.h"8+#include <acl/acl_rt.h>
9#include "torch_npu/csrc/core/npu/NPUGuard.h"9#include "torch_npu/csrc/core/npu/NPUGuard.h"
10#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"10#include "torch_npu/csrc/core/npu/sys_ctrl/npu_sys_ctrl.h"
11#include "torch_npu/csrc/npu/Module.h"11#include "torch_npu/csrc/npu/Module.h"
@@ -7,7 +7,7 @@
7#include <vector>7#include <vector>
8#include <tuple>8#include <tuple>
9 9 
10-#include "third_party/acl/inc/acl/acl_prof.h"10+#include <acl/acl_prof.h>
11 11 
12#include "torch_npu/csrc/toolkit/profiler/common/singleton.h"12#include "torch_npu/csrc/toolkit/profiler/common/singleton.h"
13#include "torch_npu/csrc/toolkit/profiler/common/utils.h"13#include "torch_npu/csrc/toolkit/profiler/common/utils.h"
@@ -7,8 +7,8 @@
7 7 
8#include <ATen/record_function.h>8#include <ATen/record_function.h>
9 9 
10-#include "third_party/acl/inc/acl/acl_base.h"10+#include <acl/acl_base.h>
11-#include "third_party/acl/inc/acl/acl_rt.h"11+#include <acl/acl_rt.h>
12 12 
13#include "torch_npu/csrc/toolkit/profiler/inc/data_reporter.h"13#include "torch_npu/csrc/toolkit/profiler/inc/data_reporter.h"
14#include "torch_npu/csrc/profiler/profiler_mgr.h"14#include "torch_npu/csrc/profiler/profiler_mgr.h"
@@ -4,7 +4,7 @@
4#include <mutex>4#include <mutex>
5#include <map>5#include <map>
6 6 
7-#include "third_party/acl/inc/acl/acl_prof.h"7+#include <acl/acl_prof.h>
8 8 
9#include "torch_npu/csrc/toolkit/profiler/common/singleton.h"9#include "torch_npu/csrc/toolkit/profiler/common/singleton.h"
10#include "torch_npu/csrc/toolkit/profiler/inc/data_dumper.h"10#include "torch_npu/csrc/toolkit/profiler/inc/data_dumper.h"
@@ -7,7 +7,7 @@
7#include "torch_npu/csrc/core/npu/NPUStream.h"7#include "torch_npu/csrc/core/npu/NPUStream.h"
8#include "torch_npu/csrc/core/npu/NPUGuard.h"8#include "torch_npu/csrc/core/npu/NPUGuard.h"
9#include "torch_npu/csrc/core/npu/interface/AclInterface.h"9#include "torch_npu/csrc/core/npu/interface/AclInterface.h"
10-#include "third_party/acl/inc/acl/acl_rt.h"10+#include <acl/acl_rt.h>
11 11 
12namespace torch_npu {12namespace torch_npu {
13namespace profiler {13namespace profiler {
@@ -1,7 +1,6 @@
1import os1import os
2import setuptools2import setuptools
3 3 
4-import torch
5import torch.utils.cpp_extension as TorchExtension4import torch.utils.cpp_extension as TorchExtension
6 5 
7import torch_npu6import torch_npu
@@ -38,7 +37,6 @@ def NpuExtension(name, sources, *args, **kwargs):
38 torch_npu_dir = PYTORCH_NPU_INSTALL_PATH37 torch_npu_dir = PYTORCH_NPU_INSTALL_PATH
39 include_dirs = kwargs.get('include_dirs', [])38 include_dirs = kwargs.get('include_dirs', [])
40 include_dirs.append(os.path.join(torch_npu_dir, 'include'))39 include_dirs.append(os.path.join(torch_npu_dir, 'include'))
41- include_dirs.append(os.path.join(torch_npu_dir, 'include', 'third_party', 'acl', 'inc'))
42 include_dirs.append(os.path.join(torch_npu_dir, 'include', 'third_party', 'hccl', 'inc'))40 include_dirs.append(os.path.join(torch_npu_dir, 'include', 'third_party', 'hccl', 'inc'))
43 include_dirs += TorchExtension.include_paths()41 include_dirs += TorchExtension.include_paths()
44 kwargs['include_dirs'] = include_dirs42 kwargs['include_dirs'] = include_dirs