Transformers documentation
ExecuTorch
はじめに
ベースクラス
モデル
プリプロセッサ
推論
Pipeline API
Generate API
最適化
モデルとチャットする
サービング
トレーニング
ハードウェア
量子化
エコシステム統合
リソース
API
You are viewing v5.24.1 version. A newer version v5.25.0 is available.
ExecuTorch
ExecuTorchは、エッジデバイスでのモデル推論のための軽量ランタイムです。PyTorchモデルをポータブルなAOT(Ahead-of-Time)形式にエクスポートします。小型のC++ランタイムがメモリを計画し、オペレーションをハードウェア固有のバックエンドにディスパッチします。モデルがデバイス上で実行される前に実行とメモリの挙動が確定しているため、推論のオーバーヘッドが低くなります。
optimum-executorch ライブラリを使用して Transformers モデルをエクスポートします。
CLI
Python
optimum-cli export executorch \
--model "HuggingFaceTB/SmolLM2-135M-Instruct" \
--task "text-generation" \
--recipe "xnnpack" \
--output_dir="./smollm2_exported"from transformers import AutoTokenizer
from optimum.executorch import ExecuTorchModelForCausalLM
model = ExecuTorchModelForCausalLM.from_pretrained(
"HuggingFaceTB/SmolLM2-135M-Instruct",
recipe="xnnpack",
)
model.save_pretrained("./smollm2_exported")
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceTB/SmolLM2-135M-Instruct")Transformers の統合
エクスポートプロセスでは、Transformersの複数のコンポーネントが使用されます。
- from_pretrained() は、safetensors 形式でモデルの重みをロードします。
- Optimum はグラフ最適化を適用し、torch.export を実行して、お使いのハードウェアバックエンドを対象とした
model.pteファイルを作成します。 - AutoTokenizer または AutoProcessor は、トークナイザーまたはプロセッサーのファイルを読み込み、推論時に実行されます。
- 実行時において、C++のランナークラスがExecuTorchランタイム上で
.pteファイルを実行します。
#include <executorch/extension/llm/runner/text_llm_runner.h>
using namespace executorch::extension::llm;
int main() {
// Load tokenizer and create runner
auto tokenizer = load_tokenizer("path/to/tokenizer.json", nullptr, std::nullopt, 0, 0);
auto runner = create_text_llm_runner("path/to/model.pte", std::move(tokenizer));
// Load the model
runner->load();
// Configure generation
GenerationConfig config;
config.max_new_tokens = 100;
config.temperature = 0.8f;
// Generate text with streaming output
runner->generate("The capital of France is", config,
[](const std::string& token) { std::cout << token << std::flush; },
nullptr);
return 0;
}リソース
- ExecuTorch ドキュメント
- torch.exportのドキュメント
- 本番環境へのエクスポートガイド