Skip to main content

TGAI ORV — Origin Requiem Vehicle(原生之初)

用纯 Python 跑深度学习训练,不需要 PyTorch。

自动微分、张量算子、优化器、训练循环都是自带的;CUDA 后端直接调 驱动 + cuBLAS + NVRTC, 所以 pip install 之后就能用,不用装 CUDA Toolkit、不用编译任何 C++ 扩展。

同一份代码也支持 CPU 与 Vulkan 后端(本次实测以 CUDA / V100 为准)。


安装

pip install tgai-orv
  • Windows / Linux,Python 3.9+
  • 有 NVIDIA 卡时:驱动需支持 CUDA 12(运行时由驱动提供,不需要 Toolkit)
  • 纯 CPU 也能跑,只是慢

装完自检:

import tgai_go.train as T
print(T.__version__)
print(T.amp_state())      # 看 CUDA 是否可用

30 秒上手

完整可运行脚本见 examples/beginner_pipeline.py(不依赖 GPU):

import tgai_go.train as T

# ① 导入语料(纯文本或 .jsonl)
text = T.load_corpus("my_corpus.txt")

# ② 在自有语料上训练分词表(不需要外部 tokenizer)
tok = T.train_tokenizer(text, vocab_size=4000)
ids = tok.encode(text)

# ③ 切块并建模型
data  = T.tokens(ids, seq=128)
model = T.preset.transformer(vocab=tok.vocab_size, d_model=256, n_layers=6, n_heads=8)

# ④ 预训练
hist = T.fit(model, data, steps=2000, lr=3e-4)
print(hist["loss"][0], "->", hist["loss"][-1])

执行档位:显存与速度的取舍

ORV 把「用多少显存换多少速度」做成四组预设,算子在运行时读策略决定行为, 所以同一份模型代码可以在四档之间切换,不需要改模型。

import tgai_go.train as T

T.set_policy("speed")        # 切档:standard / speed / extreme / memory
print(T.get_policy())        # 看当前这一档的全部参数

T.set_policy_value("attn_tile", 1024)   # 单改某一项
print(T.policy("attn_tile"))            # 读某一项(带默认值:T.policy(k, 默认))

四档是什么

档位 注意力实现 梯度检查点 定位
standard 物化(一次性算完 S×S) 开 目前与 extreme 同参
speed 物化 关 用显存换速度
extreme 物化 开 目前与 standard 相同
memory 分块(flash 风格,不落 S²) 开 用速度换显存

该选哪一档

你的处境 选 这一档实测是什么样
显存够、想最快 speed 比 PyTorch 快约 2.1×,代价是约 1.8× 显存
想两头都不输 PyTorch extreme 显存约 0.80×、速度约 0.93×,两轴同时赢
显存吃紧 / 长上下文 memory 显存约 0.68×,且几乎不随上下文增长
不确定、先跑通再说 standard 目前与 extreme 是同一组参数(物化注意力 + 检查点), 也就是"两头都不输"那一档

standard 与 extreme 现在参数完全相同。保留两个名字,是为了将来把 standard 调成更保守的默认 (例如自动按可用显存选档),而不动已有用户写的 extreme。

注意:set_policy 要在建图之前调用;中途切换只影响之后新建的算子。 显存与速度的完整测量见 docs/THREE_TARGETS.md。


实测性能(V100 32GB)

模型:seq=2048 / 8 层 / d=512 / 8 头 / top_k=2 / 共享专家,真实训练步(前向+反向+AdamW)。 对照:同构 PyTorch = 4,633 MiB / 0.507 s。

档位 峰值显存 步时 相对 PyTorch
speed 8,329 MiB 0.24 s 速度 快 2.1×,显存 1.8×
extreme 3,692 MiB 0.47 s 显存 0.80×、速度 0.93×
memory 3,169 MiB 0.81 s 显存 0.68×、速度 1.6×

上下文变长时

seq ORV speed PyTorch ORV memory
512 0.21 s 0.286 s —
2048 0.40 s → 0.24 s* 0.360 s 0.93 s → 0.81 s*
8192 跑不了(需 ~80 GB) 1.267 s 3.30 s / 4,349 MiB

* 两列是修复一处主机往返前后的对比,见 CHANGELOG。

  • 短上下文(≤2048):ORV 速度占优
  • 长上下文(≥4096):PyTorch 的融合注意力更快,但 ORV 的 memory 档显存几乎不涨

插件系统

算子、后端、损失函数、优化器、分词器、调度器 —— 六条扩展轴共用一个命名空间, 一个文件就能同时适配 CPU / CUDA / Vulkan:

from tgai_go.train import ops_plugin

@ops_plugin.register("my_op", backends=("cpu", "cuda", "vulkan"))
def my_op(x):
    ...

能力不足时必须报错而不是静默降级。细节见 docs/。


从 PyTorch 迁移

四步:换 import → 搬权重 → 改自定义 forward → 接训练循环。

# import torch as T
import tgai_go.train as T
  • 权重可用 T.from_numpy(...) / safetensors 直接搬,不要重新训练
  • Tensor 语义与 PyTorch 接近,但只支持 fp32
  • 不支持的能力会显式报错,不会静默算错

迁移检查清单见 docs/。


项目结构

主产物是 python/tgai_go/train/ 这个 Python 包(pip install 装的就是它); 其余目录是后端源码、构建脚本、文档与开发工具。

python/tgai_go/train/     ← 核心:自动微分 + 算子 + 优化器 + 训练循环
  __init__.py              Tensor / autograd / nn / optim / 执行档位(约 24 万字符)
  cuda.py                  CUDA 运行时 + 全部内核(NVRTC 源码字符串,约 35 万字符)
  v3_ops.py / v3_moe.py    V3 模型的算子与 MoE 分派
  v3_flash_op.py           分块注意力(flash 风格)
  nn_ops.py / checkpoints.py / plugin.py / ops_plugin.py
  dist.py / doctor.py      分布式 / 环境自检

src/                       原生后端 C 源码(launcher、matmul、tgai_backend)
cuda/                      .cu 后端(与 NVRTC 运行时并存的另一条路径)
cpu/                       CPU 后端构建
shaders/                   Vulkan compute shader(.comp 与编译产物 .spv)
embedded/ tcc/ third_party/ 嵌入式运行时、TCC 工具链、Vulkan 依赖

tests/                     测试(含 fixtures 与算子对拍)
bench/                     逐模块微基准(一个模块一个文件,CUDA event 计时)
tools/                     仓库维护脚本(发版、探针、远程运维)
examples/                  可直接运行的示例
scripts/                   一键上手脚本
docs/                      设计文档、计划、实测报告
.github/                   CI(单元测试 + 打包自检)

各平台的构建脚本在根目录:build_cuda*.bat、build_vulkan*.bat、rebuild_cpu.bat; 发版用 build_dist.bat 与 publish_pypi.bat。

只想用的话不需要碰 src/ cuda/ shaders/:pip install 装到的 Python 包已自带 NVRTC 与全部内核源码,运行时现编译。


文档

文件 内容
docs/THREE_TARGETS.md 三档目标的完整实测、上下文曲线、每次优化的前后对比
docs/ORV_KERNEL_INVENTORY.md 引擎里已有哪些内核/入口(避免重复造轮子)
docs/OP_COVERAGE.md 算子支持矩阵
CHANGELOG.md 逐版本变更
bench/ 逐模块微基准(CUDA event 计时),可单独运行

许可

MIT,见 LICENSE。

Metadata

Release files for tgai-orv 0.9.65

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tgai-orv 0.9.65
File Size Uploaded Provenance Actions
tgai_orv-0.9.65.tar.gz 1.4 MB Without provenance Details for tgai_orv-0.9.65.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tgai-orv 0.9.65
File Interpreter ABI Platform Actions
tgai_orv-0.9.65-py3-none-any.whl Python 3 none any Details for tgai_orv-0.9.65-py3-none-any.whl

Total release size: 2.6 MB

Release files / tgai_orv-0.9.65.tar.gz

Download URL tgai_orv-0.9.65.tar.gz
Size 1.4 MB
Tags Source
SHA-256 checksum
How to use checksums
05678f5689ddb3e8cd309edebf8e8ee8e2daddc1e13f04d33e3fc56a6dc6a56f
BLAKE2b-256 checksum
How to use checksums
4665002e2fc51257a38acd1aa97ac44b8b92e836e40d720b33a337b4470f005a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / tgai_orv-0.9.65-py3-none-any.whl

Download URL tgai_orv-0.9.65-py3-none-any.whl
Size 1.2 MB
Tags Python 3
SHA-256 checksum
How to use checksums
49ff10104896ede197ec6853cf0b25da6b3232c771a5b92b8c02075eb77e583b
BLAKE2b-256 checksum
How to use checksums
0173f3eb8bc0cf67c2a9cf7823d4bc56f99e9e2f49ad4613860a0e9069ccacb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for tgai-orv 0.9.65

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.9.65 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page