API 参考
Tiny-DL-Inference 的完整 API 文档。
概述
Tiny-DL-Inference 提供了一组用于在 WebGPU 上运行神经网络推理的类和方法。
核心模块
| 模块 | 描述 |
|---|---|
| GPUContext | WebGPU 设备和资源管理 |
| Tensor | 多维数据结构和操作 |
| Operators | 神经网络层和激活函数 |
| InferenceEngine | 高层推理引擎 API |
| Utilities | 工具类和参考实现 |
快速参考
导入
typescript
import {
GPUContext,
Tensor,
InferenceEngine,
// 算子
Conv2dOperator,
ReLUOperator,
DenseOperator,
MaxPoolOperator,
SoftmaxOperator,
FlattenOperator,
Conv2dBiasReLUOperator,
// 工具
Benchmark,
// 错误类型
WebGPUNotSupportedError,
DeviceInitializationError,
InvalidShapeError,
BufferSizeError
} from 'tiny-dl-inference';1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
基本用法
typescript
// 1. 初始化 GPU 上下文
const context = new GPUContext();
await context.init();
// 2. 创建输入张量
const input = Tensor.fromArray(context,
new Float32Array([/* 数据 */]),
[1, 3, 28, 28] // NCHW 形状
);
// 3. 运行推理
const operator = new ReLUOperator(context);
const output = await operator.forward([input]);
// 4. 获取结果
const result = await output.download();
// 5. 清理资源
input.destroy();
output.destroy();
context.destroy();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
类型定义
TensorShape
typescript
type TensorShape = [number, number, number, number];1
表示 4D 张量的形状:[batch, channels, height, width]
OperatorParams
typescript
interface OperatorParams {
[key: string]: any;
}1
2
3
2
3
算子特定参数的键值对。
Layout
typescript
type Layout = 'NCHW' | 'NHWC';1
内存布局格式。原生使用 NCHW。