Merged
torch更新:确定性、opapi查找 #8115
丛吉钰Add on Jul 30
torch更新:确定性、opapi查找 #8115
Merged
Merged fromcong-jiyu/ops-nntorchintocann/ops-nnmaster
Total: 4 files changed+92-25
| @@ -233,3 +233,33 @@ def new_operator(input1, input2, param1=0, param2=""): | |||
| 233 | | **Meta 分发** | 允许 PyTorch 在不运行 NPU 代码的情况下推导输出形状/类型 | | 233 | | **Meta 分发** | 允许 PyTorch 在不运行 NPU 代码的情况下推导输出形状/类型 | |
| 234 | | **PrivateUse1** | PyTorch 路由 NPU 特定操作使用的后端分发键 | | 234 | | **PrivateUse1** | PyTorch 路由 NPU 特定操作使用的后端分发键 | |
| 235 | | **ACLNN_CMD 宏** | 自动处理 `at::Tensor` → `aclTensor*` 类型转换、workspace 申请/释放、流调度 | | 235 | | **ACLNN_CMD 宏** | 自动处理 `at::Tensor` → `aclTensor*` 类型转换、workspace 申请/释放、流调度 | |
| 236 | + | ||
| 237 | +### 确定性计算 | ||
| 238 | + | ||
| 239 | +通过 `torch.use_deterministic_algorithms(True)` 开启确定性模式后,所有经由 `ACLNN_CMD` 调用的算子会自动将确定性标志传递给 CANN 运行时,确保同一输入产生相同输出: | ||
| 240 | + | ||
| 241 | +```python | ||
| 242 | +import torch | ||
| 243 | +import cann_ops_nn | ||
| 244 | + | ||
| 245 | +torch.use_deterministic_algorithms(True) | ||
| 246 | +# 后续算子调用将使用确定性实现 | ||
| 247 | +``` | ||
| 248 | + | ||
| 249 | +实现原理:`ACLNN_CMD` 宏在每次算子调用前读取 `at::globalContext().deterministicAlgorithms()`,通过 `aclrtCtxSetSysParamOpt(ACL_OPT_DETERMINISTIC, ...)` 将标志写入当前 device context,算子根据该标志选择确定性实现路径。 | ||
| 250 | + | ||
| 251 | +### 算子库查找机制 | ||
| 252 | + | ||
| 253 | +JIT 编译的 `.so` 在运行时按以下顺序查找算子符号(`dlopen`/`dlsym`): | ||
| 254 | + | ||
| 255 | +1. **`libcust_opapi.so`**(vendor 单算子包):遍历 `ASCEND_CUSTOM_OPP_PATH` 中的每个路径,依次加载 `<path>/op_api/lib/libcust_opapi.so` | ||
| 256 | +2. **`libopapi_nn.so`**(仓库构建安装的算子库) | ||
| 257 | +3. **`libopapi.so`**(旧版遗留总库,fallback) | ||
| 258 | + | ||
| 259 | +```sh | ||
| 260 | +# 设置 vendor 包路径(安装 vendor 包后自动生成) | ||
| 261 | +source <vendor_path>/set_env.bash | ||
| 262 | +# 或手动设置 | ||
| 263 | +export LD_LIBRARY_PATH=<vendor_path>/op_api/lib:$LD_LIBRARY_PATH | ||
| 264 | +export ASCEND_CUSTOM_OPP_PATH=<vendor_path>:$ASCEND_CUSTOM_OPP_PATH | ||
| 265 | +``` | ||
| @@ -21,6 +21,8 @@ | |||
| 21 | #include <vector> | 21 | #include <vector> |
| 22 | #include <functional> | 22 | #include <functional> |
| 23 | #include <type_traits> | 23 | #include <type_traits> |
| 24 | +#include <sstream> | ||
| 25 | +#include <climits> | ||
| 24 | #include <ATen/Tensor.h> | 26 | #include <ATen/Tensor.h> |
| 25 | #include <acl/acl_base.h> | 27 | #include <acl/acl_base.h> |
| 26 | #include <acl/acl_rt.h> | 28 | #include <acl/acl_rt.h> |
| @@ -259,6 +261,8 @@ inline const char* GetOpApiLibName(void) { return "libopapi.so"; } | |||
| 259 | 261 | ||
| 260 | inline const char* GetCustOpApiLibName(void) { return "libcust_opapi.so"; } | 262 | inline const char* GetCustOpApiLibName(void) { return "libcust_opapi.so"; } |
| 261 | 263 | ||
| 264 | +inline const char* GetNnOpApiLibName(void) { return "libopapi_nn.so"; } | ||
| 265 | + | ||
| 262 | inline void* GetOpApiFuncAddrInLib(void* handler, const char* lib_name, const char* api_name) | 266 | inline void* GetOpApiFuncAddrInLib(void* handler, const char* lib_name, const char* api_name) |
| 263 | { | 267 | { |
| 264 | auto func_addr = dlsym(handler, api_name); | 268 | auto func_addr = dlsym(handler, api_name); |
| @@ -277,11 +281,54 @@ inline void* GetOpApiLibHandler(const char* lib_name) | |||
| 277 | return handler; | 281 | return handler; |
| 278 | } | 282 | } |
| 279 | 283 | ||
| 284 | +inline std::vector<void*> GetCustOpApiHandlers() | ||
| 285 | +{ | ||
| 286 | + std::vector<void*> handlers; | ||
| 287 | + const char* env = std::getenv("ASCEND_CUSTOM_OPP_PATH"); | ||
| 288 | + if (env != nullptr) { | ||
| 289 | + std::string env_str(env); | ||
| 290 | + std::istringstream iss(env_str); | ||
| 291 | + std::string path; | ||
| 292 | + while (std::getline(iss, path, ':')) { | ||
| 293 | + if (path.empty()) { | ||
| 294 | + continue; | ||
| 295 | + } | ||
| 296 | + std::string so_path_str = path + "/op_api/lib/" + GetCustOpApiLibName(); | ||
| 297 | + char so_path[PATH_MAX] = {0}; | ||
| 298 | + if (realpath(so_path_str.c_str(), so_path) == nullptr) { | ||
| 299 | + ASCEND_LOGW("realpath failed for %s.", so_path_str.c_str()); | ||
| 300 | + continue; | ||
| 301 | + } | ||
| 302 | + auto handler = dlopen(so_path, RTLD_LAZY); | ||
| 303 | + if (handler != nullptr) { | ||
| 304 | + handlers.push_back(handler); | ||
| 305 | + } else { | ||
| 306 | + ASCEND_LOGW("dlopen %s failed, error:%s.", so_path, dlerror()); | ||
| 307 | + } | ||
| 308 | + } | ||
| 309 | + } | ||
| 310 | + if (handlers.empty()) { | ||
| 311 | + auto handler = GetOpApiLibHandler(GetCustOpApiLibName()); | ||
| 312 | + if (handler != nullptr) { | ||
| 313 | + handlers.push_back(handler); | ||
| 314 | + } | ||
| 315 | + } | ||
| 316 | + return handlers; | ||
| 317 | +} | ||
| 318 | + | ||
| 280 | inline void* GetOpApiFuncAddr(const char* api_name) | 319 | inline void* GetOpApiFuncAddr(const char* api_name) |
| 281 | { | 320 | { |
| 282 | - static auto cust_op_api_handler = GetOpApiLibHandler(GetCustOpApiLibName()); | 321 | + static auto cust_handlers = GetCustOpApiHandlers(); |
| 283 | - if (cust_op_api_handler != nullptr) { | 322 | + for (auto handler : cust_handlers) { |
| 284 | - auto func_addr = GetOpApiFuncAddrInLib(cust_op_api_handler, GetCustOpApiLibName(), api_name); | 323 | + auto func_addr = GetOpApiFuncAddrInLib(handler, GetCustOpApiLibName(), api_name); |
| 324 | + if (func_addr != nullptr) { | ||
| 325 | + return func_addr; | ||
| 326 | + } | ||
| 327 | + } | ||
| 328 | + | ||
| 329 | + static auto nn_op_api_handler = GetOpApiLibHandler(GetNnOpApiLibName()); | ||
| 330 | + if (nn_op_api_handler != nullptr) { | ||
| 331 | + auto func_addr = GetOpApiFuncAddrInLib(nn_op_api_handler, GetNnOpApiLibName(), api_name); | ||
| 285 | if (func_addr != nullptr) { | 332 | if (func_addr != nullptr) { |
| 286 | return func_addr; | 333 | return func_addr; |
| 287 | } | 334 | } |
| @@ -900,6 +947,9 @@ auto DecodeDevice(Ts&... args) -> at::Device | |||
| 900 | if (init_mem_func) { \ | 947 | if (init_mem_func) { \ |
| 901 | init_mem_func(nullptr, false); \ | 948 | init_mem_func(nullptr, false); \ |
| 902 | } \ | 949 | } \ |
| 950 | + auto deterministic = at::globalContext().deterministicAlgorithms(); \ | ||
| 951 | + auto sys_ret = aclrtCtxSetSysParamOpt(aclSysParamOpt::ACL_OPT_DETERMINISTIC, deterministic ? 1 : 0); \ | ||
| 952 | + TORCH_CHECK(sys_ret == 0, "set ACL_OPT_DETERMINISTIC failed, ret=", sys_ret); \ | ||
| 903 | auto converted_params = ConvertTypes(__VA_ARGS__, workspace_size_addr, executor_addr); \ | 953 | auto converted_params = ConvertTypes(__VA_ARGS__, workspace_size_addr, executor_addr); \ |
| 904 | static auto get_workspace_size_func = ConvertToOpApiFunc(converted_params, get_workspace_size_func_addr); \ | 954 | static auto get_workspace_size_func = ConvertToOpApiFunc(converted_params, get_workspace_size_func_addr); \ |
| 905 | auto workspace_status = call(get_workspace_size_func, converted_params); \ | 955 | auto workspace_status = call(get_workspace_size_func, converted_params); \ |
| @@ -1,22 +0,0 @@ | |||
| 1 | -/** | ||
| 2 | - * Copyright (c) 2026 Huawei Technologies Co., Ltd. | ||
| 3 | - * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | - * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | - * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | - * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | - * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | - */ | ||
| 10 | - | ||
| 11 | -#pragma once | ||
| 12 | -#include <ATen/ATen.h> | ||
| 13 | -#include <acl/acl_base.h> | ||
| 14 | - | ||
| 15 | -namespace cann_ops_nn { | ||
| 16 | -namespace common { | ||
| 17 | - | ||
| 18 | -at::ScalarType AclDataTypeToScalarType(aclDataType acl_type); | ||
| 19 | -aclDataType ScalarTypeToAclDataType(at::ScalarType scalar_type); | ||
| 20 | - | ||
| 21 | -} // namespace common | ||
| 22 | -} // namespace cann_ops_nn | ||
| @@ -232,6 +232,15 @@ class BuildPyWithOps(_build_py): | |||
| 232 | dst = os.path.join(build_pkg, rel_path) | 232 | dst = os.path.join(build_pkg, rel_path) |
| 233 | os.makedirs(os.path.dirname(dst), exist_ok=True) | 233 | os.makedirs(os.path.dirname(dst), exist_ok=True) |
| 234 | shutil.copy2(src_abs, dst) | 234 | shutil.copy2(src_abs, dst) |
| 235 | + common_src_dir = os.path.join(_src_path, "common") | ||
| 236 | + if os.path.isdir(common_src_dir): | ||
| 237 | + for root, _, filenames in os.walk(common_src_dir): | ||
| 238 | + for filename in filenames: | ||
| 239 | + src_abs = os.path.join(root, filename) | ||
| 240 | + rel_path = os.path.relpath(src_abs, _src_path) | ||
| 241 | + dst = os.path.join(build_pkg, rel_path) | ||
| 242 | + os.makedirs(os.path.dirname(dst), exist_ok=True) | ||
| 243 | + shutil.copy2(src_abs, dst) | ||
| 235 | 244 | ||
| 236 | 245 | ||
| 237 | _all_packages = find_packages() + _extra_packages | 246 | _all_packages = find_packages() + _extra_packages |