programming3 MIN READ

[MLOps] PyTriton

[MLOps] PyTriton

PyTriton이란?

AI모델을 서빙하기위한 triton inference server를 python개발환경에서 더 쉽게 적용할수있게 만든 인터페이스. flask나 fast api처럼 api 엔드포인트를 제공하는 python 함수 정의가 가능하다. model repo , port 셋업하는부분 빠짐. 기존 inference pipeline code를 수정없이 사용 가능. JAX같은 새로운타입 프레임웍이나, triton inference server로 서빙하기에 다소 복잡한 파이프라인을 가진 모델들 서빙하기에 좋을것 ㅇㅇ

  • triton서버를 한줄의 코드로 사용가능
  • model format 셋업할필요없음
  • 기존 inf pipeline code 수정 없이 사용가능
  • @batch 같이 input에대한 다양한 데코레이터 지원

Installation

pip install -U nvidia-pytriton

엥? 안깔림…

Example

import logging

import numpy as np
from transformers import BertTokenizer, FlaxBertModel

from pytriton.decorators import batch
from pytriton.model_config import ModelConfig, Tensor
from pytriton.triton import Triton

logger = logging.getLogger("examples.huggingface_bert_jax.server")
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s: %(message)s")

tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
model = FlaxBertModel.from_pretrained("bert-base-uncased")


@batch
def _infer_fn(**inputs: np.ndarray):
  (sequence_batch,) = inputs.values()

  # need to convert dtype=object to bytes first
  # end decode unicode bytes
  sequence_batch = np.char.decode(sequence_batch.astype("bytes"), "utf-8")

  last_hidden_states = []
  for sequence_item in sequence_batch:
        tokenized_sequence = tokenizer(sequence_item.item(), return_tensors="jax")
        results = model(**tokenized_sequence)
        last_hidden_states.append(results.last_hidden_state)
  last_hidden_states = np.array(last_hidden_states, dtype=np.float32)
  return [last_hidden_states]


with Triton() as triton:
    logger.info("Loading BERT model.")
    triton.bind(
        model_name="BERT",
        infer_func=_infer_fn,
        inputs=[
            Tensor(name="sequence", dtype=np.bytes_, shape=(1,)),
        ],
        outputs=[
            Tensor(name="last_hidden_state", dtype=np.float32, shape=(-1,)),
        ],
        config=ModelConfig(max_batch_size=16),
    )
    logger.info("Serving inference")
    triton.serve()

Decorator

제공되는 데코레이터들

  • batch
  • sample
  • group_by_keys
  • group_by_values
  • fll_optionals
  • pad_batch
  • first_value
  • triton_context 데코레이터들을 여러개를 사용하는것도 가능

https://triton-inference-server.github.io/pytriton/latest/
https://github.com/triton-inference-server/pytriton