Transformers documentation

ExecuTorch

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ExecuTorch

ExecuTorch là một runtime nhẹ để suy luận mô hình trên các thiết bị biên. Nó xuất một mô hình PyTorch sang định dạng di động, biên dịch trước. Một runtime C++ nhỏ lập kế hoạch bộ nhớ và điều phối các thao tác đến các backend phần cứng cụ thể. Hành vi thực thi và bộ nhớ được xác định trước khi mô hình chạy trên thiết bị, do đó chi phí suy luận thấp.

Xuất mô hình Transformers bằng thư viện optimum-executorch.

CLI
Python
optimum-cli export executorch \
    --model "HuggingFaceTB/SmolLM2-135M-Instruct" \
    --task "text-generation" \
    --recipe "xnnpack" \
    --output_dir="./smollm2_exported"

Tích hợp Transformers

Quy trình xuất sử dụng một số thành phần của Transformers.

  1. from_pretrained() tải trọng số mô hình ở định dạng safetensors.
  2. Optimum áp dụng các tối ưu hóa đồ thị và chạy torch.export để tạo tệp model.pte nhắm đến backend phần cứng của bạn.
  3. AutoTokenizer hoặc AutoProcessor sẽ nạp các tệp tokenizer hoặc processor và chạy trong quá trình suy luận.
  4. Vào thời điểm thực thi, một lớp runner C++ sẽ thực thi tệp .pte trên runtime ExecuTorch.
#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;
}

Tài nguyên

Update on GitHub