비즈니스 로직 스크립팅

비즈니스 로직 스크립팅 (BLS)

모델 파이프라인은 단순히 모델을 순서대로 이어붙인 것만으로는 부족할 때가 있어요. 반복(loop), 조건 분기(if-then-else), 데이터에 의존하는 제어 흐름처럼 로직이 모델 실행과 섞여야 하는 경우가 있죠. Triton의 BLS(Business Logic Scripting) 는 이런 커스텀 로직과 모델 실행을 결합해서, Python 모델 안에서 다른 모델에 추론 요청을 보낼 수 있게 해 줍니다.

출처: 공식문서

본문

Triton의 앙상블 기능은 여러 모델을 파이프라인(더 일반적으로는 DAG, 방향성 비순환 그래프)으로 조합하는 많은 사용 사례를 지원해요. 하지만 모델 파이프라인의 일부로 반복, 조건 분기, 데이터 의존적 제어 흐름과 같은 커스텀 로직이 모델 실행과 섞여야 하는 경우는 앙상블로 지원되지 않습니다. 이런 커스텀 로직과 모델 실행의 결합을 비즈니스 로직 스크립팅(BLS) 이라고 불러요.

21.08 릴리스부터 Python 모델에서 BLS를 구현할 수 있어요. 새로운 유틸리티 함수 집합으로, Python 모델 실행 중에 Triton이 서빙 중인 다른 모델에 추론 요청을 실행할 수 있습니다. BLS는 반드시 execute 함수 안에서만 사용해야 하고, initializefinalize 메서드에서는 지원되지 않아요.

사용 예시를 보겠습니다. pb_utils.InferenceRequest 객체를 만들고 exec() 메서드로 실행해 응답을 받아요. model_name, requested_output_names, inputs가 필수 인자이고, inputspb_utils.Tensor 객체 리스트여야 합니다.

import triton_python_backend_utils as pb_utils

class TritonPythonModel:
    ...
    def execute(self, requests):
        ...
        # Create an InferenceRequest object. `model_name`,
        # `requested_output_names`, and `inputs` are the required arguments and
        # must be provided when constructing an InferenceRequest object. Make
        # sure to replace `inputs` argument with a list of `pb_utils.Tensor`
        # objects.
        inference_request = pb_utils.InferenceRequest(
            model_name='model_name',
            requested_output_names=['REQUESTED_OUTPUT_1', 'REQUESTED_OUTPUT_2'],
            inputs=[<pb_utils.Tensor object>])

        # `pb_utils.InferenceRequest` supports request_id, correlation_id,
        # model version, timeout and preferred_memory in addition to the
        # arguments described above.
        # Note: Starting from the 24.03 release, the `correlation_id` parameter
        # supports both string and unsigned integer values.
        # These arguments are optional.

        # Execute the inference_request and wait for the response
        inference_response = inference_request.exec()

        # Check if the inference response has an error
        if inference_response.has_error():
            raise pb_utils.TritonModelException(
                inference_response.error().message())
        else:
            # Extract the output tensors from the inference response.
            output1 = pb_utils.get_output_tensor_by_name(
                inference_response, 'REQUESTED_OUTPUT_1')

pb_utils.InferenceRequest는 위 필수 인자 외에 request_id, correlation_id, model version, timeout, preferred_memory를 선택적으로 받을 수 있어요. 참고로 preferred_memorypb_utils.PreferredMemory(pb_utils.TRITONSERVER_MEMORY_GPU, 0) 또는 pb_utils.TRITONSERVER_MEMORY_CPU처럼 지정합니다.

exec()는 요청을 실행하고 응답을 기다려요. 응답에 오류가 있으면 has_error()로 감지해 TritonModelException을 던지고, 정상이면 get_output_tensor_by_name(inference_response, 'REQUESTED_OUTPUT_1')로 출력 텐서를 꺼냅니다.

더 알아보기