|
| 1 | +# `infinicore` Python 前端 |
| 2 | + |
| 3 | +*InfiniCore* 提供了与 C++ 前端一致的 Python 封装,位于 `python/infinicore/`。该模块通过 `pybind11` 将核心张量、算子与设备上下文暴露给 Python,便于在推理框架或调试脚本中快速集成。 |
| 4 | + |
| 5 | +## 模块结构 |
| 6 | + |
| 7 | +| 符号 | 说明 | |
| 8 | +| --- | --- | |
| 9 | +| `device` | 设备句柄类 (`python/infinicore/device.py`),支持 `"cuda:0"`、`device(\"cpu\", 0)` 等写法或复用已有实例。 | |
| 10 | +| `dtype` / `float16` 等 | 数据类型枚举 (`python/infinicore/dtype.py`)。 | |
| 11 | +| `Tensor` | 张量包装类 (`python/infinicore/tensor.py`),内部封装底层 `_infinicore` 对象。 | |
| 12 | +| `empty` / `zeros` / `ones` / `empty_like` 等 | 张量构造函数(`python/infinicore/tensor.py`),默认要求显式传入 `dtype` 与 `device`。 | |
| 13 | +| 顶层算子(`add`、`matmul`、`rearrange`、`attention`) | 暴露在 `infinicore` 命名空间下,对应实现位于 `python/infinicore/ops/`。 | |
| 14 | +| `infinicore.nn` | 神经网络相关模块集合,未来可扩展更多组件。 | |
| 15 | +| `infinicore.nn.functional` | 函数式算子集合 (`python/infinicore/nn/functional.py`)。 | |
| 16 | +| `use_ntops` / `infinicore.ntops` | 若系统安装 `ntops` 包,将自动置位 `use_ntops=True` 并暴露原始模块。 | |
| 17 | + |
| 18 | +所有符号在包的 `__init__.py` 中进行了显式导出,可直接通过 `import infinicore as ic` 后使用。 |
| 19 | + |
| 20 | +相关导出定义见 `InfiniCore/python/infinicore/__init__.py`。 |
| 21 | + |
| 22 | +## 张量与构造函数 |
| 23 | + |
| 24 | +`Tensor` 是对底层 `_infinicore.Tensor` 的 Python 包装,常用接口包括: |
| 25 | + |
| 26 | +- `shape` / `ndim` / `size(dim)` / `stride(dim)`:获取张量维度与步长信息。 |
| 27 | +- `dtype` / `device`:返回 `dtype` 与 `device` 包装类。 |
| 28 | +- `numel()` / `is_contiguous()`:查看张量元素数量与存储布局。 |
| 29 | +- `copy_(src)` / `to(...)`:执行数据拷贝与跨设备搬运。 |
| 30 | +- `contiguous()` / `permute(dims)` / `view(shape)` / `as_strided(size, stride)`:布局调整与视图操作。 |
| 31 | +- `debug(filename=None)`:将张量内容打印或输出到二进制文件。 |
| 32 | + |
| 33 | +常用构造函数包括 `empty`、`strided_empty`、`zeros`、`ones`、`from_blob`、`strided_from_blob`、`empty_like` 等: |
| 34 | + |
| 35 | +```python |
| 36 | +import infinicore as ic |
| 37 | + |
| 38 | +cpu = ic.device("cpu") |
| 39 | +a = ic.empty((4, 8), dtype=ic.float16, device=cpu) |
| 40 | +b = ic.ones((4, 8), dtype=ic.float16, device=cpu) |
| 41 | +a.copy_(b) |
| 42 | +``` |
| 43 | + |
| 44 | +> 注意:这些函数要求显式传入 `dtype` 与 `device`,避免隐式从 PyTorch/TensorFlow 对象推断。 |
| 45 | +
|
| 46 | +## 顶层算子 (`infinicore.*`) |
| 47 | + |
| 48 | +以下函数直接通过 `infinicore` 命名空间导出,全部支持可选的 `out` 关键字参数以复用缓冲区: |
| 49 | + |
| 50 | +详见 [`ops` 文档索引](ops/README.md)。 |
| 51 | + |
| 52 | +## 函数式算子 (`infinicore.nn.functional`) |
| 53 | + |
| 54 | +函数式 API 集中在 `infinicore.nn.functional`: |
| 55 | + |
| 56 | +详见 [`nn.functional` 文档](nn/functional/README.md)。 |
| 57 | + |
| 58 | +## 运行时上下文 |
| 59 | + |
| 60 | +- `_infinicore` 在进程内维护运行时状态;创建张量时请显式传入 `device`,并保持算子的所有输入位于同一设备。 |
| 61 | +- 如需强制同步,可调用 `infinicore.lib._infinicore.sync_stream()`、`sync_device()` 等底层绑定。 |
| 62 | +- 在同一执行流内串行调用算子通常无需额外同步。 |
| 63 | + |
| 64 | +## 与 `ntops` 的协作 |
| 65 | + |
| 66 | +- 导入 `ntops` 成功后,`infinicore.use_ntops` 会被设置为 `True`,并可通过 `infinicore.ntops` 访问原始模块。 |
| 67 | +- `nn.functional.silu` 在 `use_ntops=True` 且设备类型为 `"cuda"`/`"musa"` 且未传 `out` 时,会委托 `ntops.torch.silu`。 |
| 68 | +- 若想强制禁用,可直接设置 `infinicore.use_ntops = False`。 |
| 69 | + |
| 70 | +## 端到端示例 |
| 71 | + |
| 72 | +```python |
| 73 | +import infinicore as ic |
| 74 | + |
| 75 | +device = ic.device("cuda:0") |
| 76 | + |
| 77 | +q = ic.empty((8, 1, 128), dtype=ic.float16, device=device) |
| 78 | +k = ic.empty((2, 1, 128), dtype=ic.float16, device=device) |
| 79 | +v = ic.empty((2, 1, 128), dtype=ic.float16, device=device) |
| 80 | +k_cache = ic.empty((2, 128, 128), dtype=ic.float16, device=device) |
| 81 | +v_cache = ic.empty((2, 128, 128), dtype=ic.float16, device=device) |
| 82 | + |
| 83 | +out = ic.attention(q, k, v, k_cache, v_cache, pos=0) |
| 84 | + |
| 85 | +if ic.use_ntops: |
| 86 | + # 在部分设备上,SiLU 会委托给 ntops 的高性能实现 |
| 87 | + out = ic.nn.functional.silu(out) |
| 88 | + |
| 89 | +out.debug() |
| 90 | +``` |
| 91 | + |
| 92 | +## 相关链接 |
| 93 | + |
| 94 | +- [`infinicore.ops 顶层算子`](ops/README.md) |
| 95 | +- [`nn.functional 函数式文档`](nn/functional/README.md) |
| 96 | +- [`InfiniOP` 统一算子库](/infiniop/README.md) |
0 commit comments