|
| 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 | +| 函数 | 定义位置 | 说明 | |
| 51 | +| --- | --- | --- | |
| 52 | +| `add(input, other, *, out=None)` | `python/infinicore/ops/add.py` | 按元素加法,兼容广播与非连续张量。 | |
| 53 | +| `matmul(input, other, *, out=None)` | `python/infinicore/ops/matmul.py` | GEMM 封装,底层复用 *InfiniOP* 描述符。 | |
| 54 | +| `rearrange(input, other=None, *, out=None)` | `python/infinicore/ops/rearrange.py` | 生成连续副本或重排数据;`other` 参数当前保留未用。 | |
| 55 | +| `attention(q, k, v, k_cache, v_cache, pos, *, out=None)` | `python/infinicore/ops/attention.py` | 解码阶段注意力,管理 KV cache 并支持可选输出复用。 | |
| 56 | + |
| 57 | +更详细的参数说明与示例请参考 [`infinicore.ops` 文档](ops/README.md)。 |
| 58 | + |
| 59 | +更多示例见下方端到端样例或源码注释。 |
| 60 | + |
| 61 | +## 函数式算子 (`infinicore.nn.functional`) |
| 62 | + |
| 63 | +函数式 API 集中在 `infinicore.nn.functional`: |
| 64 | + |
| 65 | +| 函数 | 定义位置 | 说明 | |
| 66 | +| --- | --- | --- | |
| 67 | +| `causal_softmax(input, out=None)` | `python/infinicore/nn/functional.py` | 对最后一维执行因果掩码 Softmax,可原位写入 `out`。 | |
| 68 | +| `rms_norm(input, normalized_shape, weight, eps=1e-5, *, out=None)` | 同上 | Root Mean Square LayerNorm,执行前会断言 `normalized_shape == weight.shape`。 | |
| 69 | +| `silu(input, inplace=False, *, out=None)` | 同上 | SiLU 激活;`inplace=True` 时直接覆盖输入,且在满足条件时可委托 `ntops`。 | |
| 70 | +| `swiglu(input, other, *, out=None)` | 同上 | SwiGLU 前向门控,要求 `input` 与 `other` 形状、dtype、设备一致。 | |
| 71 | + |
| 72 | +`silu` 会优先调用 `ntops` 后端以复用已有优化,`swiglu`、`rms_norm` 等函数内部调用 `_infinicore` 绑定完成计算。详细说明与注意事项见 [`nn.functional` 文档](nn/functional/README.md)。 |
| 73 | + |
| 74 | +## 运行时上下文 |
| 75 | + |
| 76 | +- `_infinicore` 在进程内维护运行时状态;创建张量时请显式传入 `device`,并保持算子的所有输入位于同一设备。 |
| 77 | +- 如需强制同步,可调用 `infinicore.lib._infinicore.sync_stream()`、`sync_device()` 等底层绑定。 |
| 78 | +- 在同一执行流内串行调用算子通常无需额外同步。 |
| 79 | + |
| 80 | +## 与 `ntops` 的协作 |
| 81 | + |
| 82 | +- 导入 `ntops` 成功后,`infinicore.use_ntops` 会被设置为 `True`,并可通过 `infinicore.ntops` 访问原始模块。 |
| 83 | +- `nn.functional.silu` 在 `use_ntops=True` 且设备类型为 `"cuda"`/`"musa"` 且未传 `out` 时,会委托 `ntops.torch.silu`。 |
| 84 | +- 若想强制禁用,可直接设置 `infinicore.use_ntops = False`。 |
| 85 | + |
| 86 | +## 端到端示例 |
| 87 | + |
| 88 | +```python |
| 89 | +import infinicore as ic |
| 90 | + |
| 91 | +device = ic.device("cuda:0") |
| 92 | + |
| 93 | +q = ic.empty((8, 1, 128), dtype=ic.float16, device=device) |
| 94 | +k = ic.empty((2, 1, 128), dtype=ic.float16, device=device) |
| 95 | +v = ic.empty((2, 1, 128), dtype=ic.float16, device=device) |
| 96 | +k_cache = ic.empty((2, 128, 128), dtype=ic.float16, device=device) |
| 97 | +v_cache = ic.empty((2, 128, 128), dtype=ic.float16, device=device) |
| 98 | + |
| 99 | +out = ic.attention(q, k, v, k_cache, v_cache, pos=0) |
| 100 | + |
| 101 | +if ic.use_ntops: |
| 102 | + # 在部分设备上,SiLU 会委托给 ntops 的高性能实现 |
| 103 | + out = ic.nn.functional.silu(out) |
| 104 | + |
| 105 | +out.debug() |
| 106 | +``` |
| 107 | + |
| 108 | +## 相关链接 |
| 109 | + |
| 110 | +- [`infinicore.ops 顶层算子`](ops/README.md) |
| 111 | +- [`nn.functional 函数式文档`](nn/functional/README.md) |
| 112 | +- [`InfiniOP` 统一算子库](/infiniop/README.md) |
0 commit comments