Merged
torch extension support custom path #8023
丛吉钰Add on Jul 28
torch extension support custom path #8023
Merged
丛吉钰Add on Jul 28
Total: 1 files changed+44-14
@@ -99,16 +99,39 @@ class OpBuilder(ABC):
99 @abstractmethod99 @abstractmethod
100 def register_meta(self): ...100 def register_meta(self): ...
101 101 
102+ def _custom_opp_paths(self):
103+ custom_opp_paths = []
104+ custom_opp_env = os.environ.get("ASCEND_CUSTOM_OPP_PATH", "")
105+ for custom_opp_path in custom_opp_env.split(os.pathsep):
106+ custom_opp_path = custom_opp_path.strip()
107+ if custom_opp_path and os.path.isdir(custom_opp_path):
108+ custom_opp_paths.append(custom_opp_path)
109+ vendors_dir = os.path.join(self._cann_path, "opp", "vendors")
110+ if os.path.isdir(vendors_dir):
111+ for vendor_name in sorted(os.listdir(vendors_dir)):
112+ vendor_dir = os.path.join(vendors_dir, vendor_name)
113+ if os.path.isdir(vendor_dir):
114+ custom_opp_paths.append(vendor_dir)
115+ return custom_opp_paths
116+ 
102 def include_paths(self):117 def include_paths(self):
103 self._ensure_initialized()118 self._ensure_initialized()
104- paths = [119+ paths = []
105- os.path.join(self._cann_path, "include"),120+ for vendor_dir in self._custom_opp_paths():
106- os.path.join(self._torch_npu_path, "include"),121+ inc = os.path.join(vendor_dir, "op_api", "include")
107- os.path.join(self._torch_npu_path, "include/third_party/hccl/inc"),122+ if os.path.isdir(inc):
108- os.path.join(self._torch_npu_path, "include/third_party/acl/inc"),123+ paths.append(inc)
109- os.path.join(self._torch_npu_path, "include/third_party/op-plugin"),124+ paths.extend(
110- os.path.join(self._package_path, "common"),125+ [
111- ]126+ os.path.join(self._cann_path, "include"),
127+ os.path.join(self._cann_path, "include/aclnnop"),
128+ os.path.join(self._torch_npu_path, "include"),
129+ os.path.join(self._torch_npu_path, "include/third_party/hccl/inc"),
130+ os.path.join(self._torch_npu_path, "include/third_party/acl/inc"),
131+ os.path.join(self._torch_npu_path, "include/third_party/op-plugin"),
132+ os.path.join(self._package_path, "common"),
133+ ]
134+ )
112 return paths135 return paths
113 136 
114 def cxx_args(self):137 def cxx_args(self):
@@ -132,12 +155,19 @@ class OpBuilder(ABC):
132 155 
133 def extra_ldflags(self):156 def extra_ldflags(self):
134 self._ensure_initialized()157 self._ensure_initialized()
135- flags = [158+ flags = []
136- "-L" + os.path.join(self._cann_path, "lib64"),159+ for vendor_dir in self._custom_opp_paths():
137- "-lascendcl",160+ lib = os.path.join(vendor_dir, "op_api", "lib")
范其瑞
范其瑞范其瑞Jul 29

同前,考虑优先级问题

likedislike
138- "-L" + os.path.join(self._torch_npu_path, "lib"),161+ if os.path.isdir(lib):
139- "-ltorch_npu",162+ flags.append("-L" + lib)
140- ]163+ flags.extend(
164+ [
165+ "-L" + os.path.join(self._cann_path, "lib64"),
166+ "-lascendcl",
167+ "-L" + os.path.join(self._torch_npu_path, "lib"),
168+ "-ltorch_npu",
169+ ]
170+ )
141 return flags171 return flags
142 172 
143 def load(self, verbose=True):173 def load(self, verbose=True):