MoonBit 华为昇腾 CANN 绑定 — AscendCL 设备/上下文/流/内存管理 + OM 模型加载与推理 + Mock 后端 + Tensor 工具
cann.mbt/ # 模块根
├── cann.mbt # 根包文档入口
├── core/ # 零依赖:CannError + ACL 错误码 + DType + 形状 + 句柄 + AclApi trait
│ ├── cann_error.mbt # CannError suberror + check_acl/raise_*/describe_error
│ ├── acl_error_codes.mbt # 21 个 ACL 常量 + describe_acl_error
│ ├── dtype.mbt # pub(all) enum DType(13 变体)
│ ├── shape.mbt # numel / validate_dims
│ ├── handles.mbt # 6 个句柄类型(DevicePtr/Context/Stream/ModelDesc/Dataset/DataBuffer)
│ ├── acl_api.mbt # pub(open) trait AclApi(38 方法)
│ ├── core_test.mbt # 黑盒测试(4)
│ └── core_wbtest.mbt # 白盒测试(1)
├── mock/ # MockAclApi:纯 MoonBit 模拟后端(依赖 core)
│ ├── mock_api.mbt # 恒等模型 + 错误注入 + 设备内存模拟
│ └── mock_test.mbt # 黑盒测试(5)
├── acl/ # NativeAclApi:extern "C" 直连 libascendcl(依赖 core,仅 native)
│ ├── extern.mbt # ACL 原语 extern 绑定 + 5 个经 stub 的字符串/dims 适配
│ ├── cann_stub.c # C 胶水(UTF-16→UTF-8、dims 打包、D2H 临时缓冲)
│ └── native_api.mbt # NativeAclApi 实现
├── runtime/ # Cann[Api] / Model[Api] 高层运行时(依赖 core;测试依赖 mock)
│ ├── cann.mbt # 会话:open/sync/close(失败逆序回滚)
│ ├── model.mbt # 模型:load/from_desc/unload/execute(errdefer 清理)
│ └── runtime_test.mbt # 黑盒测试(7)
├── tensor/ # Tensor 张量(依赖 core)
│ ├── tensor.mbt # from_bytes/numel/to_bytes
│ └── tensor_test.mbt # 黑盒测试(3)
├── cmd/main/ # mock 恒等推理可执行示例(moon run cmd/main)
│ └── main.mbt
├── playground/ # 浏览器演示导出层(js 后端,#export_name 5 函数)
│ ├── playground.mbt
│ └── moon.pkg
└── docs/ # GitHub Pages 演示页(index.html + app.js + style.css + playground.js)core(零依赖)
↑
mock / acl / tensor / runtime
↑
cmd/main| 包 | wasm | wasm-gc | js | native |
|---|---|---|---|---|
| core / mock / runtime / tensor / cmd/main | ✓ | ✓ | ✓ | ✓ |
| acl(需 libascendcl) | — | — | — | ✓ |
///|
test "readme_mock_identity" {
let mock = @mock.MockAclApi::new()
let cann = @runtime.Cann::open(mock, 0)
let model = @runtime.Model::load(cann, "identity.om")
let outputs = model.execute(cann, [Bytes::make(16, b'\x00')])
assert_eq(outputs.length(), 1)
model.unload()
cann.close()
}///|
test "readme_native_backend" {
let api = @acl.NativeAclApi::new()
let cann = @runtime.Cann::open(api, 0)
let model = @runtime.Model::load(cann, "resnet50.om")
let outputs = model.execute(cann, [input_bytes])
model.unload()
cann.close()
}///|
test "readme_tensor" {
// Float32 x [2,2] = 4 元素 x 4 字节 = 16 字节
let tensor = @tensor.Tensor::from_bytes(
Float,
[2L, 2L],
Bytes::make(16, b'\x00'),
)
assert_eq(tensor.numel(), 4L)
}///|
test "readme_error_handling" {
let mock = @mock.MockAclApi::new()
mock.set_fail_next(100000) // 注入下一次 ACL 调用返回 100000
let result = try {
ignore(@runtime.Cann::open(mock, 0))
"ok"
} catch {
err => @core.describe_error(err) // "ACL 错误 100000: 参数无效"
}
}options(
"link-flags": [ "-lascendcl" ],
)moon fmt --check
moon check --target all --warn-list +73 --deny-warn
moon build --target all --warn-list +73 --deny-warn
moon test --target all --warn-list +73 --deny-warn
moon info --target alllet version : Stringcore(零依赖:错误/ACL 错误码/DType/形状/句柄/AclApi trait)
↑
mock(MockAclApi:纯 MoonBit 模拟后端,恒等模型)
acl(NativeAclApi:extern "C" 直连 libascendcl,仅 native 后端)
runtime(Cann/Model 高层封装,泛型分发后端)
tensor(Tensor:数据类型 + 形状 + 原始字节)import {
"walkzzz/cann/runtime",
}Install
Download zipMoonBit 华为昇腾 CANN 绑定 — AscendCL 设备/上下文/流/内存管理 + OM 模型加载与推理 + Mock 后端 + Tensor 工具