Transformers documentation

ExecuTorch

You are viewing v5.24.1 version. A newer version v5.25.0 is available.
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

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"

Transformers の統合

エクスポートプロセスでは、Transformersの複数のコンポーネントが使用されます。

  1. from_pretrained() は、safetensors 形式でモデルの重みをロードします。
  2. Optimum はグラフ最適化を適用し、torch.export を実行して、お使いのハードウェアバックエンドを対象とした model.pte ファイルを作成します。
  3. AutoTokenizer または AutoProcessor は、トークナイザーまたはプロセッサーのファイルを読み込み、推論時に実行されます。
  4. 実行時において、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;
}

リソース

Update on GitHub