Skip to main content

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

训推一体,不依赖 PyTorch。· Train and infer with one codebase, no PyTorch.

version python license selftest speed


⚡ 先看这个 / Read This First

同一份训练代码、同种子、同一个模型、各跑 600 步。 Same training code, same seed, same model, 600 steps each. No cherry-picking.

显卡 GPU 模型 ORV PyTorch 结果
RTX 4090 10M · B78 S64 18.9 ms/step 27.1 ms 🔥 1.43× 更快
Tesla V100 10M · B78 S64 41.4 ms/step 52.4 ms 1.27× 更快
NVIDIA A10 10M · B78 S64 49.3 ms/step 57.0 ms 1.16× 更快
NVIDIA A10 25.2M · D512 L6 FF2048 36.8 ms/step 39.8 ms 1.08× 更快
NVIDIA A10 25.2M · bf16 30.0 ms/step 22.3 ms 💀 0.74×,我们输

最后一行红得刺眼,但我们不放小字:PyTorch 的 autocast 会缓存权重的 bf16 影子副本, 而我们是每次 GEMM 现 cast。赢的地方写赢,输的地方写输。 The last row is a loss, stated plainly. PyTorch caches bf16 weight shadows; we cast per GEMM.

我们不是来打酱油的,但也没赢下全部。剩下的,往下看。


序章 · 何为 ORV / Prologue

Origin —— 内核、autograd、优化器,一行一行从零写起。"原生"不是形容词,是事实陈述。 Requiem —— 写给那个"想训模型就必须先装 PyTorch"的年代。挽歌不凄惨,是告别。 Vehicle —— 载具。同一个模型、同一份量化、同一份词表,从训练场一路开到部署现场,中途不换船。

这不是又一个把 cuBLAS 包了一层的壳子。三千多个 CUDA 算子内核在 NVRTC 里现编现用,autograd 自己推,AdamW 自己更。


一句话 · 改掉那行 import

# import torch as T                 # 旧时代的咒语 / the old incantation
import tgai_go.train as T           # 现在 / now — 其余代码一行不用改

Tensor / autograd / optim.AdamW / Module / functional —— 名字一样,语义一样,只是不再是 PyTorch。


外来模型对齐 / Foreign Model Parity

我们不是只能跑自己写的结构。真实开源权重 Qwen2.5-0.5B-instruct(GQA 14:2、qkv bias、RoPE θ=1e6, 与我们的模型结构不同)跑在我们自己的张量算子上,与 HuggingFace transformers 逐位比对:

max|Δ| = 1.278e-04      相对误差 6.29e-06 ≤ 1e-5   ✅ PASS
我们:  [0.95818734, 6.6607723, 3.1660562, 1.2813374, 3.2732353]
参考:  [0.9581473,  6.66074,   3.1660404, 1.281328,  3.2732046]

复现(纯 CPU,不需要 GPU):

python tools/qwen_ref.py        # 用 HF 生成参考 logits(一次性)
python tools/qwen2_parity.py    # 我们的实现 vs 参考,判据 ≤1e-5

LLaMA 结构训练对齐(RMSNorm + RoPE + SwiGLU + GQA,20 步): 与 PyTorch 孪生逐步 loss 相对偏差 1.59e-05、训练后参数相对偏差 4.90e-05、loss 下降 99.1%。

算子覆盖率(以 PyTorch 公开面为基准,见 docs/OP_COVERAGE.md): Tier-1 训练必需算子 63 项,已具备 62 项(98%)。


模型转换 / Convert

训练或部署别人的模型,第一步是把权重换成我们的格式:

python tools/gguf_to_tg.py --input qwen2.5-0.5b-fp16.gguf --output qwen25.TG --dtype fp16
python tools/verify_gguf_tg.py --gguf qwen2.5-0.5b-fp16.gguf --tg qwen25.TG   # 转完就验

实测 Qwen2.5-0.5B:16.6 秒、944 MB(FP16)、290/290 张量逐位一致(max|Δ|=0)。 支持 llama / mistral / qwen2 / qwen3 / gemma2 / phi3;细节见 docs/GGUF_TO_TG.md。


一切皆插件 / Everything Is a Plugin

核心只保留 LLM 训练必需的算子;图形学、科研、私有算子不进核心,走插件。 这样核心永远精简可自检,生态由插件自由生长。

from tgai_go.train import ops_plugin

# 组合式:只用已有算子,反向自动生效
ops_plugin.register("softplus", lambda x: (x.exp() + 1.0).log(), kind="tensor")
x.softplus()          # 立即可用,梯度也是对的
# 自带反向:numpy 前向 + 解析反向(精确 GELU 需要 erf,就不该进核心)
ops_plugin.register_numpy("gelu_exact", fwd, bwd, kind="tensor")

实测(examples/plugins/,与 torch 对照):

插件 前向 反向
softplus(组合式,一行反向都没写) 2.4e-07 6.0e-08
gelu_exact(自带解析反向) 2.4e-07 1.2e-07

发现方式:手工 import / TG_OPS_PATH 目录 / setuptools 入口点(pip install 后自动)。 细节见 docs/PLUGINS.md。


装 / Install

pip install tgai-orv              # 机器上已有 CUDA Toolkit 或 torch 的 CUDA 库
pip install "tgai-orv[cuda]"      # 裸机器:把 cuBLAS / NVRTC / CUDA runtime 一起装进 venv
python -m tgai_go.train.doctor    # 12 项自检,失败会写清哪个算子/什么形状/怎么修

已验证:全新的、没有 torch 的 venv,不设任何环境变量,pip install tgai-orv 后 doctor 12/12 PASS。引擎会自动去本机任何 Python 环境里找 nvidia/*/lib; 万一找不到,报错会直接告诉你 pip install "tgai-orv[cuda]" 或设 TGAI_CUDA_BIN(支持多路径,: 分隔)。

发布前自检:python tools/check_package.py --build —— 检查 wheel 里文件是否齐、 .cu 是否随包、装进干净 venv 后能否 import(今天两次「装上才发现缺件」都是它挡住的)。

PyPI: https://pypi.org/project/tgai-orv/ · 当前 0.2.0


实力清单 / The Arsenal

  • 🗡️ 真·融合核:RMSNorm · RoPE · SwiGLU · Attention(含 flash 路径)· MoE,外加 CUDA Graph。
  • 🧪 12 项自检:python -m tgai_go.train.doctor。崩了会告诉你哪个算子、什么形状、怎么修 —— 不许裸报错。
  • 📉 1 GB 语料只吃 600 MiB 内存:流式管线。旧实现要 17.5 GiB,相当于"一个语料吃掉十六倍内存"。
  • ♻️ 续训无损:40+40 步 vs 连续 80 步,末 loss 差 0.06%。断电重启不心疼。
  • 📐 14 组形状实测通过:dh=8~128、S=8~256、FF=1000(非 128 倍数)、L=16。
  • ⚡ 混合精度:TG_BF16=1 / TG_FP16=1;A10 上 bf16 跑 600 步 loss 偏差 0.03%。
  • 🔗 数据并行:单卡起 2 个 rank,最终参数 SHA256 完全一致 —— 数学上是对的。
  • 🚀 一条命令起步:bash scripts/quickstart.sh,自动查环境、装依赖、跑自检、告诉你下一步。

拔剑 / Quick Start

git clone https://github.com/JXW666NB/TGAI-ORV.git && cd TGAI-ORV
bash scripts/quickstart.sh          # Windows: scripts\quickstart.bat

想立刻见血 / train something right now:

PYTHONPATH=$PWD/python python3 examples/exam2_chat10m_tgai.py --device cuda --steps 600 --out chat10m

它不是什么 / What It's NOT(诚实的部分)

吹牛要有底线。 下面这张表里,🟡 那行我们没有藏。

关卡 Gate 状态
L0 · 数值正确 ✅ 前向/反向 ≤1e-5,600 步 loss 偏差 ≤2%,无 NaN
L1 · 能训 ✅ 形状 / 1 GB 语料 / 速度 / 显存 / 续训 全部通过
L2 · 能被人用 ✅ 一条自检、一页文档、报错带修法、后端一行切换
L3 · 生产级 🟡 24h 长稳与 2 卡加速比缺硬件未验证

⇒ 所以我们给自己的标签是 「可用(自用 / 小团队)」,不是"生产级"。 ⇒ Label: "usable (self / small team)" — not "production-ready".

每一个数字都在真机上测过,证据与判据全部公开:docs/PRODUCTION_READINESS.md


兵器谱 / Layout

TGAI-ORV/
├── python/tgai_go/train/     训练引擎(运行时 + autograd + 优化器)
├── training/kernels/         CUDA 算子源码(.cu,NVRTC 运行时编译)
├── examples/                 可跑示例(10M 对话模型 + 流式数据)
├── tools/                    accept / soak / profile / sync
├── scripts/                  quickstart.sh · quickstart.bat
├── docs/                     QUICKSTART_TRAIN.md · PRODUCTION_READINESS.md
├── src/ cuda/ cpu/ embedded/ shaders/ tcc/    推理引擎
└── third_party/              上游依赖

同门 / Sibling

Repo 定位
TGAI GO 纯推理引擎。跨平台,从 ESP32 的 8 MB PSRAM 到 CUDA 服务器
TGAI ORV(本仓库) 完全体。推理 + 训练,训完直接部署

License

MIT © 2026 JXW666NB —— 拿走随便用,留着这行字就行。

Metadata

Release files for tgai-orv 0.2.0

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

Built distribution (wheel)

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

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

Download URL tgai_orv-0.2.0-py3-none-any.whl
Size 245.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cef1cadc6a6156db51e7753b176c1e30f009c5e0a5c98ebdb5438a7065078e5
BLAKE2b-256 checksum
How to use checksums
5247934433f5ee832a19d5f09e7843e3cfaae36cf5d0edbb92715e4afadfe453
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.2.0

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.2.0 This release

1 release file 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