torchnative
Start시작

Getting started시작하기

Install a wheel, confirm that torch is this build, and generate text with a real Hugging Face model.

휠을 설치하고, torch 가 이 빌드인지 확인하고, 진짜 Hugging Face 모델로 텍스트를 생성합니다.

1 · Install1 · 설치

uv add --prerelease allow torchnative

Every published version is a pre-release. Each platform wheel (cp313-abi3) carries the _C extension and upstream's Python tree, so import torch resolves to this build. It cannot share an interpreter with an installed PyTorch. Use a fresh virtual environment.

공개된 버전은 모두 프리릴리스입니다. 각 플랫폼 휠(cp313-abi3)은 _C 확장과 upstream 파이썬 트리를 함께 싣고 있어 import torch 가 이 빌드로 해석됩니다. 설치된 PyTorch 와 한 인터프리터를 공유할 수 없습니다. 새 가상환경을 쓰십시오.

Avoid 0.0.1a0: it is a py3-none-any skeleton with no _C, which installs and then fails to import.

0.0.1a0 은 피하십시오: _C 가 없는 py3-none-any 껍데기라 설치는 되지만 import 에 실패합니다.

2 · Check which torch you have2 · 어떤 torch 인지 확인

import torch
assert hasattr(torch._C, "_aten_implemented")   # the shim, not upstream

a = torch.ones(2, 3)
print((a @ a.T).sum())                            # tensor(12.)

If the assertion fails, the interpreter found upstream torch first. Every probe in this project asserts the same thing.

단언이 실패하면 인터프리터가 upstream torch 를 먼저 찾은 것입니다. 이 프로젝트의 모든 프로브가 같은 것을 단언합니다.

3 · Generate3 · 생성

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

name  = "HuggingFaceTB/SmolLM2-135M"
model = AutoModelForCausalLM.from_pretrained(name, dtype=torch.float32)
tok   = AutoTokenizer.from_pretrained(name)

ids = tok("On-device inference is", return_tensors="pt")
out = model.generate(**ids, max_new_tokens=32)
print(tok.decode(out[0], skip_special_tokens=True))

This is the real transformers: from_pretrained loads bit-identical weights, and in float32 the tokens are the ones upstream emits.

진짜 transformers 입니다: from_pretrained 는 비트 단위로 같은 가중치를 읽고, float32 에서는 upstream 과 같은 토큰이 나옵니다.

Build from source소스에서 빌드

Needs a Rust toolchain and CPython 3.13+.

Rust 툴체인과 CPython 3.13+ 가 필요합니다.

bash scripts/vendor/vendor_torch.sh     # assemble the vendored torch tree
bash scripts/vendor/install_shim.sh     # build the extension and install it
python scripts/wheel/build.py                            # -> dist/*.whl
python scripts/wheel/verify.py dist/torchnative-*.whl    # clean venv, real import