Transformers documentation

ExecuTorch

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v5.24.0).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ExecuTorch

ExecuTorch — это лёгкий рантайм для вывода моделей на устройствах edge. Он экспортирует модель PyTorch в переносимый формат ahead-of-time. Небольшой C++-рантайм планирует память и направляет операции в аппаратно-специфичные бэкенды. Поведение выполнения и памяти известно до запуска модели на устройстве, поэтому накладные расходы на вывод минимальны.

Экспортируйте модель Transformers с помощью библиотеки optimum-executorch.

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++ выполняет файл .pte в рантайме 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;
}

Ресурсы

Update on GitHub