基于 Tensor 底层数组实现的高性能矩阵运算工具
MatOps
00 Remarks
Tensor.MatMul() 的实现走的是属性索引器,在 350+ 节点 × 数十维特征的 训练循环里(上千次前向/反向)会成为明显瓶颈。这里直接操作 Tensor.Data 一维数组并按行优先顺序做循环展开,语义与 Tensor.MatMul() 完全一致, 但避免了逐元素的属性调用开销。
所有 *Into 版本都写入调用方提供的缓冲区(不重新分配),用于在训练热路径上复用 临时张量、降低 GC 压力。
01 Syntax
SMRUCC.genomics.Analysis.GEARS.Layers.MatOps
02 Methods
| Name | Overloads | Summary |
|---|---|---|
| MulInto | 1 | result = a @ b |
| Mul | 1 | 计算 result = a @ b(自动分配输出张量) |
| MulATInto | 1 | result = aᵀ @ b |
| MulAT | 1 | 计算 result = aᵀ @ b(自动分配输出张量) |
| MulBTInto | 1 | result = a @ bᵀ |
| MulBT | 1 | 计算 result = a @ bᵀ(自动分配输出张量) |
| ColSumInto | 1 | 沿行方向求和:result[1, n] = Σ_i a[i, n] |
| Accumulate | 1 | 将源张量的值累加到目标张量上:dst += src |
| Zero | 1 | 将张量的所有元素清零 |
| Zeros | 1 | 创建指定形状的全零张量 |
03 Members
MulInto(
Tensor, Tensor, Tensor)result = a @ b
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 左矩阵 [m, k] |
b | Tensor | 右矩阵 [k, n] |
result | Tensor | 输出缓冲区 [m, n],会被原地覆盖 |
Mul(
Tensor, Tensor)计算 result = a @ b(自动分配输出张量)
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 左矩阵 [m, k] |
b | Tensor | 右矩阵 [k, n] |
Returns
新的 [m, n] 张量
MulATInto(
Tensor, Tensor, Tensor)result = aᵀ @ b
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 矩阵 [m, k] |
b | Tensor | 矩阵 [m, n] |
result | Tensor | 输出缓冲区 [k, n],会被原地覆盖 |
MulAT(
Tensor, Tensor)计算 result = aᵀ @ b(自动分配输出张量)
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 矩阵 [m, k] |
b | Tensor | 矩阵 [m, n] |
Returns
新的 [k, n] 张量
MulBTInto(
Tensor, Tensor, Tensor)result = a @ bᵀ
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 矩阵 [m, k] |
b | Tensor | 矩阵 [n, k] |
result | Tensor | 输出缓冲区 [m, n],会被原地覆盖 |
MulBT(
Tensor, Tensor)计算 result = a @ bᵀ(自动分配输出张量)
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 矩阵 [m, k] |
b | Tensor | 矩阵 [n, k] |
Returns
新的 [m, n] 张量
ColSumInto(
Tensor, Tensor)沿行方向求和:result[1, n] = Σ_i a[i, n]
Parameters
| Name | Type | Description |
|---|---|---|
a | Tensor | 输入矩阵 [m, n] |
result | Tensor | 输出缓冲区 [1, n],会被原地覆盖 |
Accumulate(
Tensor, Tensor)将源张量的值累加到目标张量上:dst += src
Parameters
| Name | Type | Description |
|---|---|---|
source | Tensor | 源张量 |
target | Tensor | 目标张量(原地累加) |
Zero(
Tensor)将张量的所有元素清零
Parameters
| Name | Type | Description |
|---|---|---|
x | Tensor | 待清零的张量 |
Zeros(
Int32, Int32)创建指定形状的全零张量
Parameters
| Name | Type | Description |
|---|---|---|
rows | Int32 | 行数 |
cols | Int32 | 列数 |
Returns
[rows, cols] 的全零张量