Name | l0n0lacl JSON |
Version |
2.0.14
JSON |
| download |
home_page | None |
Summary | 用于调用ascendc编写的算子 |
upload_time | 2025-07-26 23:38:39 |
maintainer | None |
docs_url | None |
author | l0n0l |
requires_python | <4,>=3.7 |
license | None |
keywords |
acl
ascendc
算子
算子开发
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 1 功能描述
由于在ascendc算子开发过程中运行算子比较复杂,为了简化算子的运行,将运行算子变成可以用python直接调用的函数。所以编写了此代码。
# 2 安装
* 请确保安装了 cann toolkit 与 官方算子包。
```
pip install l0n0lacl
```
```
source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh
```
# 3 通过创建算子库来运行算子
## 3.1 通过命令创建算子库
### 3.1.1 命令介绍
```shell
l0n0lcreateopps -h
usage: l0n0lcreateopps [-h] [--跳过自定义算子] 目标目录
为所有算子创建函数:生成算子代码并支持跳过自定义算子
positional arguments:
目标目录 指定生成算子的目标目录路径(例如:'./operators/')
options:
-h, --help show this help message and exit
--跳过自定义算子, -s 若启用,跳过
```
### 3.1.2 创建算子库
```bash
l0n0lcreateopps ./opp
```
执行完毕后 opp就成为了算子库
## 3.2 调用算子实例
```python
from l0n0lacl import *
import numpy as np
import opp
a = 张量(np.ones((2, 3)) * 2)
other = 张量(np.ones((2, 3)) * 3)
out = 张量(np.zeros([2, 3]))
opp.Add(a, other, 1.2, out)
print(out)
```
```bash
[[5.6 5.6 5.6]
[5.6 5.6 5.6]]
```
# 4 简单运行算子实例
## 4.1 先切换到cann环境,比如我的环境是:
```
source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh
```
## 4.2 设置要用到的设备
```
export ASCEND_VISIBLE_DEVICES=0,1
```
## 4.3 运行算子
```python
from l0n0lacl import *
import numpy as np
import ctypes
import time
设备.设置内存共享组([0, 1])
a = np.random.uniform(-2, -1, (1024))
a_out = np.zeros_like(a)
a = 张量(a)
a_out = 张量(a_out)
a.切换到设备(1)
# a_out.切换到设备(1)
fn = 算子运行器('Abs')
out = fn(a, a_out)
print(out[1])
fn = 算子运行器('InplaceAcos')
a = np.random.uniform(-1, 1, (2000,2000)).astype(np.float16)
print(a)
out = fn(a)
print(out[0])
fn = 算子运行器('AdaptiveAvgPool2d')
a = np.random.uniform(0, 100, (2, 100, 100)).astype(np.float32)
out = np.zeros((2, 3, 3), dtype=a.dtype)
a = 张量(a, 格式=张量格式.NCL)
out = 张量(out).变更格式(张量格式.NCL)
output = fn(a, [3, 3], out)
print(output[2])
fn = 算子运行器('Addmv')
s = np.ones(3, dtype=np.float32)
mat = np.random.uniform(-1, 1, (3, 40000)).astype(np.float32)
vec = np.random.uniform(-1, 1, 40000).astype(np.float32)
alpha = 1.2
beta = 标量(1.1)
out = np.zeros(3, dtype=np.float32)
output = fn(s, mat, vec, alpha, beta, out, ctypes.c_int8(1))
print(output[-2])
fn = 算子运行器('Any')
s = np.random.uniform(-1, -0.5, (3, 4))
out = np.zeros(3, dtype=np.bool_)
output = fn(s, [1], ctypes.c_bool(False), out)
print(output[-1])
```
## 4.3 算子查找顺序
```
如果 ${NO_VENDORS_OPP} != '1':
查找 ${ASCEND_OPP_PATH}/vendors目录(自己写的算子默认安装目录)
查找 ${ASCEND_HOME_PATH}/lib64/libopapi.so 支持的算子(也就是官方算子包)
```
* `NO_VENDORS_OPP` 如果不需要使用自定义算子, 可以添加此环境变量
* `ASCEND_OPP_PATH` cann自带环境变量 在(source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh)时设置
* `ASCEND_HOME_PATH`cann自带环境变量在(source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh)时设置
Raw data
{
"_id": null,
"home_page": null,
"name": "l0n0lacl",
"maintainer": null,
"docs_url": null,
"requires_python": "<4,>=3.7",
"maintainer_email": null,
"keywords": "acl, ascendc, \u7b97\u5b50, \u7b97\u5b50\u5f00\u53d1",
"author": "l0n0l",
"author_email": "1038352856@qq.com",
"download_url": "https://files.pythonhosted.org/packages/9b/8b/4354281b17b26e7e9a88e5337d3bc898336d1243e14bf4ba9ab486188b70/l0n0lacl-2.0.14.tar.gz",
"platform": null,
"description": "# 1 \u529f\u80fd\u63cf\u8ff0\n\u7531\u4e8e\u5728ascendc\u7b97\u5b50\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u8fd0\u884c\u7b97\u5b50\u6bd4\u8f83\u590d\u6742\uff0c\u4e3a\u4e86\u7b80\u5316\u7b97\u5b50\u7684\u8fd0\u884c\uff0c\u5c06\u8fd0\u884c\u7b97\u5b50\u53d8\u6210\u53ef\u4ee5\u7528python\u76f4\u63a5\u8c03\u7528\u7684\u51fd\u6570\u3002\u6240\u4ee5\u7f16\u5199\u4e86\u6b64\u4ee3\u7801\u3002\n\n# 2 \u5b89\u88c5\n* \u8bf7\u786e\u4fdd\u5b89\u88c5\u4e86 cann toolkit \u4e0e \u5b98\u65b9\u7b97\u5b50\u5305\u3002\n```\npip install l0n0lacl\n```\n```\nsource /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh\n```\n# 3 \u901a\u8fc7\u521b\u5efa\u7b97\u5b50\u5e93\u6765\u8fd0\u884c\u7b97\u5b50\n## 3.1 \u901a\u8fc7\u547d\u4ee4\u521b\u5efa\u7b97\u5b50\u5e93\n### 3.1.1 \u547d\u4ee4\u4ecb\u7ecd\n```shell\nl0n0lcreateopps -h\nusage: l0n0lcreateopps [-h] [--\u8df3\u8fc7\u81ea\u5b9a\u4e49\u7b97\u5b50] \u76ee\u6807\u76ee\u5f55\n\n\u4e3a\u6240\u6709\u7b97\u5b50\u521b\u5efa\u51fd\u6570\uff1a\u751f\u6210\u7b97\u5b50\u4ee3\u7801\u5e76\u652f\u6301\u8df3\u8fc7\u81ea\u5b9a\u4e49\u7b97\u5b50\n\npositional arguments:\n \u76ee\u6807\u76ee\u5f55 \u6307\u5b9a\u751f\u6210\u7b97\u5b50\u7684\u76ee\u6807\u76ee\u5f55\u8def\u5f84\uff08\u4f8b\u5982\uff1a'./operators/'\uff09\n\noptions:\n -h, --help show this help message and exit\n --\u8df3\u8fc7\u81ea\u5b9a\u4e49\u7b97\u5b50, -s \u82e5\u542f\u7528\uff0c\u8df3\u8fc7\n```\n### 3.1.2 \u521b\u5efa\u7b97\u5b50\u5e93\n```bash\nl0n0lcreateopps ./opp\n```\n\u6267\u884c\u5b8c\u6bd5\u540e opp\u5c31\u6210\u4e3a\u4e86\u7b97\u5b50\u5e93\n\n## 3.2 \u8c03\u7528\u7b97\u5b50\u5b9e\u4f8b\n```python\nfrom l0n0lacl import *\nimport numpy as np\nimport opp\na = \u5f20\u91cf(np.ones((2, 3)) * 2)\nother = \u5f20\u91cf(np.ones((2, 3)) * 3)\nout = \u5f20\u91cf(np.zeros([2, 3]))\nopp.Add(a, other, 1.2, out)\nprint(out)\n```\n```bash\n[[5.6 5.6 5.6]\n [5.6 5.6 5.6]]\n```\n\n# 4 \u7b80\u5355\u8fd0\u884c\u7b97\u5b50\u5b9e\u4f8b\n## 4.1 \u5148\u5207\u6362\u5230cann\u73af\u5883,\u6bd4\u5982\u6211\u7684\u73af\u5883\u662f:\n```\nsource /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh\n```\n## 4.2 \u8bbe\u7f6e\u8981\u7528\u5230\u7684\u8bbe\u5907\n```\nexport ASCEND_VISIBLE_DEVICES=0,1\n```\n## 4.3 \u8fd0\u884c\u7b97\u5b50\n```python\nfrom l0n0lacl import *\nimport numpy as np\nimport ctypes\nimport time\n\u8bbe\u5907.\u8bbe\u7f6e\u5185\u5b58\u5171\u4eab\u7ec4([0, 1])\na = np.random.uniform(-2, -1, (1024))\na_out = np.zeros_like(a)\na = \u5f20\u91cf(a)\na_out = \u5f20\u91cf(a_out)\na.\u5207\u6362\u5230\u8bbe\u5907(1)\n# a_out.\u5207\u6362\u5230\u8bbe\u5907(1)\nfn = \u7b97\u5b50\u8fd0\u884c\u5668('Abs')\nout = fn(a, a_out)\nprint(out[1])\n\nfn = \u7b97\u5b50\u8fd0\u884c\u5668('InplaceAcos')\na = np.random.uniform(-1, 1, (2000,2000)).astype(np.float16)\nprint(a)\nout = fn(a)\nprint(out[0])\n\nfn = \u7b97\u5b50\u8fd0\u884c\u5668('AdaptiveAvgPool2d')\na = np.random.uniform(0, 100, (2, 100, 100)).astype(np.float32)\nout = np.zeros((2, 3, 3), dtype=a.dtype)\na = \u5f20\u91cf(a, \u683c\u5f0f=\u5f20\u91cf\u683c\u5f0f.NCL)\nout = \u5f20\u91cf(out).\u53d8\u66f4\u683c\u5f0f(\u5f20\u91cf\u683c\u5f0f.NCL)\noutput = fn(a, [3, 3], out)\nprint(output[2])\n\n\nfn = \u7b97\u5b50\u8fd0\u884c\u5668('Addmv')\ns = np.ones(3, dtype=np.float32)\nmat = np.random.uniform(-1, 1, (3, 40000)).astype(np.float32)\nvec = np.random.uniform(-1, 1, 40000).astype(np.float32)\nalpha = 1.2\nbeta = \u6807\u91cf(1.1)\nout = np.zeros(3, dtype=np.float32)\noutput = fn(s, mat, vec, alpha, beta, out, ctypes.c_int8(1))\nprint(output[-2])\n\nfn = \u7b97\u5b50\u8fd0\u884c\u5668('Any')\ns = np.random.uniform(-1, -0.5, (3, 4))\nout = np.zeros(3, dtype=np.bool_)\noutput = fn(s, [1], ctypes.c_bool(False), out)\nprint(output[-1])\n\n```\n## 4.3 \u7b97\u5b50\u67e5\u627e\u987a\u5e8f\n```\n\u5982\u679c ${NO_VENDORS_OPP} != '1':\n \u67e5\u627e ${ASCEND_OPP_PATH}/vendors\u76ee\u5f55(\u81ea\u5df1\u5199\u7684\u7b97\u5b50\u9ed8\u8ba4\u5b89\u88c5\u76ee\u5f55) \n\u67e5\u627e ${ASCEND_HOME_PATH}/lib64/libopapi.so \u652f\u6301\u7684\u7b97\u5b50(\u4e5f\u5c31\u662f\u5b98\u65b9\u7b97\u5b50\u5305)\n```\n* `NO_VENDORS_OPP` \u5982\u679c\u4e0d\u9700\u8981\u4f7f\u7528\u81ea\u5b9a\u4e49\u7b97\u5b50, \u53ef\u4ee5\u6dfb\u52a0\u6b64\u73af\u5883\u53d8\u91cf\n* `ASCEND_OPP_PATH` cann\u81ea\u5e26\u73af\u5883\u53d8\u91cf \u5728(source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh)\u65f6\u8bbe\u7f6e\n* `ASCEND_HOME_PATH`cann\u81ea\u5e26\u73af\u5883\u53d8\u91cf\u5728(source /home/HwHiAiUser/Ascend/ascend-toolkit/set_env.sh)\u65f6\u8bbe\u7f6e\n",
"bugtrack_url": null,
"license": null,
"summary": "\u7528\u4e8e\u8c03\u7528ascendc\u7f16\u5199\u7684\u7b97\u5b50",
"version": "2.0.14",
"project_urls": null,
"split_keywords": [
"acl",
" ascendc",
" \u7b97\u5b50",
" \u7b97\u5b50\u5f00\u53d1"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "100ce415450d84b8f509a1f6cfcfb556b8459785eb7f6007a0bf8f8c6b865e61",
"md5": "95c10d9867964cade8b86c50c7e921c5",
"sha256": "b0cdd128993e6f62d7413732d2e230ea07fdc9824e019606ea864df4a57cddbc"
},
"downloads": -1,
"filename": "l0n0lacl-2.0.14-py3-none-any.whl",
"has_sig": false,
"md5_digest": "95c10d9867964cade8b86c50c7e921c5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4,>=3.7",
"size": 22016,
"upload_time": "2025-07-26T23:38:38",
"upload_time_iso_8601": "2025-07-26T23:38:38.414032Z",
"url": "https://files.pythonhosted.org/packages/10/0c/e415450d84b8f509a1f6cfcfb556b8459785eb7f6007a0bf8f8c6b865e61/l0n0lacl-2.0.14-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "9b8b4354281b17b26e7e9a88e5337d3bc898336d1243e14bf4ba9ab486188b70",
"md5": "62aa56bed36b3b80f506a768faeb17d6",
"sha256": "9dc2f6e9ab51e54a0a2bdc09c62827193013db9a0a255a01ae76c86fa9c8c76d"
},
"downloads": -1,
"filename": "l0n0lacl-2.0.14.tar.gz",
"has_sig": false,
"md5_digest": "62aa56bed36b3b80f506a768faeb17d6",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4,>=3.7",
"size": 21579,
"upload_time": "2025-07-26T23:38:39",
"upload_time_iso_8601": "2025-07-26T23:38:39.993568Z",
"url": "https://files.pythonhosted.org/packages/9b/8b/4354281b17b26e7e9a88e5337d3bc898336d1243e14bf4ba9ab486188b70/l0n0lacl-2.0.14.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-07-26 23:38:39",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "l0n0lacl"
}