ExecuTorch

ExecuTorch

ExecuTorch은 엣지 디바이스에서 모델 추론을 위한 경량 런타임이에요. PyTorch 모델을 이식 가능한 AOT(ahead-of-time) 형식으로 내보내요. 작은 C++ 런타임이 메모리를 계획하고 연산을 하드웨어별 백엔드에 디스패치해요. 모델이 디바이스에서 실행되기 전에 실행·메모리 동작이 알려져 있으므로 추론 오버헤드가 낮아요.

출처: 문서

본문

optimum-executorch 라이브러리로 Transformers 모델을 내보내요.

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 integration)

내보내기 과정은 여러 Transformers 컴포넌트를 사용해요.

  1. from_pretrained()이 safetensors 형식으로 모델 가중치를 로드해요.
  2. Optimum이 그래프 최적화를 적용하고 torch.export를 실행해서 하드웨어 백엔드를 겨냥한 model.pte 파일을 만들어요.
  3. AutoTokenizer나 AutoProcessor가 tokenizer·processor 파일을 로드하고 추론 중에 실행돼요.
  4. 런타임에서 C++ 러너(runner) 클래스가 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;
}

리소스 (Resources)

더 알아보기 (Learn more)