Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

python_backend

> 编程语言
Open source

Triton backend that enables pre-process, post-processing and other logic to be implemented in Python.

679 stars0 likes1 views
WebsiteGitHub

About

Triton backend that enables pre-process, post-processing and other logic to be implemented in Python.

Python Backend

The Triton backend for Python. The goal of Python backend is to let you serve models written in Python by Triton Inference Server without having to write any C++ code.

User Documentation

  • Python Backend
    • User Documentation
    • Quick Start
    • Building from Source
    • Usage
      • auto_complete_config
      • initialize
      • execute
        • Default Mode
        • Error Handling
        • Request Cancellation Handling
        • Decoupled mode
          • Use Cases
          • Async Execute
        • Request Rescheduling
      • finalize
      • is_ready
    • Model Config File
    • Inference Request Parameters
    • Inference Response Parameters
    • Managing Python Runtime and Libraries
      • Building Custom Python Backend Stub
      • Creating Custom Execution Environments
      • Important Notes
    • Error Handling
    • Managing Shared Memory
    • Multiple Model Instance Support
    • Running Multiple Instances of Triton Server
  • Business Logic Scripting
    • Using BLS with Decoupled Models
    • Model Loading API
    • Using BLS with Stateful Models
    • Limitation
  • Interoperability and GPU Support
    • pb_utils.Tensor.to_dlpack() -> PyCapsule
    • pb_utils.Tensor.from_dlpack() -> Tensor
    • pb_utils.Tensor.is_cpu() -> bool
    • Input Tensor Device Placement
  • Frameworks
    • PyTorch
      • PyTorch Determinism
    • TensorFlow
      • TensorFlow Determinism
  • Custom Metrics
  • Examples
    • AddSub in NumPy
    • AddSubNet in PyTorch
    • AddSub in JAX
    • Business Logic Scripting
    • Preprocessing
    • Decoupled Models
    • Model Instance Kind
    • Auto-complete config
    • Custom Metrics
  • Running with Inferentia
  • Logging
  • Development with VSCode
  • Reporting problems, asking questions

Quick Start

  1. Run the Triton Inference Server container.
bash
docker run --shm-size=1g --ulimit memlock=-1 -p 8000:8000 -p 8001:8001 -p 8002:8002 --ulimit stack=67108864 -ti nvcr.io/nvidia/tritonserver:-py3

Replace \ with the Triton version (e.g. 21.05).

  1. Inside the container, clone the Python backend repository.
bash
git clone https://github.com/triton-inference-server/python_backend -b r
  1. Install example model.
bash
cd python_backend
mkdir -p models/add_sub/1/
cp examples/add_sub/model.py models/add_sub/1/model.py
cp examples/add_sub/config.pbtxt models/add_sub/config.pbtxt
  1. Start the Triton server.
tritonserver --model-repository `pwd`/models
  1. In the host machine, start the client container.
bash
docker run -ti --net host nvcr.io/nvidia/tritonserver:-py3-sdk /bin/bash
  1. In the client container, clone the Python backend repository.
bash
git clone https://github.com/triton-inference-server/python_backend -b r
  1. Run the example client.
python3 python_backend/examples/add_sub/client.py

Building from Source

  1. Requirements
  • cmake >= 3.17
  • numpy
  • rapidjson-dev
  • libarchive-dev
  • zlib1g-dev
pip3 install numpy

On Ubuntu or Debian you can use the command below to install rapidjson, libarchive, and zlib:

bash
sudo apt-get install rapidjson-dev libarchive-dev zlib1g-dev
  1. Build Python backend. Replace \ with the GitHub branch that you want to compile. For release branches it should be r\ (e.g. r21.06).
bash
mkdir build
cd build
cmake -DTRITON_ENABLE_GPU=ON -DTRITON_BACKEND_REPO_TAG= -DTRITON_COMMON_REPO_TAG= -DTRITON_CORE_REPO_TAG= -DCMAKE_INSTALL_PREFIX:PATH=`pwd`/install ..
make install

The following required Triton repositories will be pulled and used in the build. If the CMake variables below are not specified, "main" branch of those repositories will be used. \ should be the same as the Python backend repository branch that you are trying to compile.

  • triton-inference-server/backend: -DTRITON_BACKEND_REPO_TAG=
  • triton-inference-server/common: -DTRITON_COMMON_REPO_TAG=
  • triton-inference-server/core: -DTRITON_CORE_REPO_TAG=

Set -DCMAKE_INSTALL_PREFIX to the location where the Triton Server is installed. In the released containers, this location is /opt/tritonserver.

  1. Copy example model and configuration
mkdir -p models/add_sub/1/
cp examples/add_sub/model.py models/add_sub/1/model.py
cp examples/add_sub/config.pbtxt models/add_sub/config.pbtxt
  1. Start the Triton Server
/opt/tritonserver/bin/tritonserver --model-repository=`pwd`/models
  1. Use the client app to perform inference
python3 examples/add_sub/client.py

Usage

In order to use the Python backend, you need to create a Python file that has a structure similar to below:

…

Every Python backend can implement the following main functions:

auto_complete_config

auto_complete_config is called only once when loading the model assuming the server was not started with --disable-auto-complete-config.

Implementing this function is optional. No implementation of auto_complete_config will do nothing. This function can be used to set max_batch_size, dynamic_batching, input and output properties of the model using set_max_batch_size, set_dynamic_batching, add_input, and add_output. These properties will allow Triton to load the model with minimal model configuration in absence of a configuration file. This function returns the pb_utils.ModelConfig object with these properties. You can use the as_dict function to gain read-only access to the pb_utils.ModelConfig object. The pb_utils.ModelConfig object being returned from here will be used as the final configuration for the model.

In addition to minimal properties, you can also set model_transaction_policy through auto_complete_config using set_model_transaction_policy. For example,

python
import triton_python_backend_utils as pb_utils

class TritonPythonModel:
    @staticmethod
    def auto_complete_config(auto_complete_model_config):
      ...
      transaction_policy = {"decoupled": True}
      auto_complete_model_config.set_model_transaction_policy(transaction_policy)
      ...

Note: The Python interpreter used to invoke this function will be destroyed upon returning from this function and as a result none of the objects created here will be available in the initialize, execute, or finalize functions.

initialize

initialize is called once the model is being loaded. Implementing initialize is optional. initialize allows you to do any necessary initializations before execution. In the initialize function, you are given an args variable. args is a Python dictionary. Both keys and values for this Python dictionary are strings. You can find the available keys in the args dictionary along with their description in the table below:

key description
model_config A JSON string containing the model configuration
model_instance_kind A string containing model instance kind
model_instance_device_id A string containing model instance device ID
model_repository Model repository path
model_version Model version
model_name Model name

execute

execute function is called whenever an inference request is made. Every Python model must implement execute function. In the execute function you are given a list of InferenceRequest objects. There are two modes of implementing this function. The mode you choose should depend on your use case. That is whether or not you want to return decoupled responses from this model or not.

Default Mode

This is the most generic way you would like to implement your model and requires the execute function to return exactly one response per request. This entails that in this mode, your execute function must return a list of InferenceResponse objects that has the same length as requests. The work flow in this mode is:

  • execute function receives a batch of pb_utils.InferenceRequest as a length N array.

  • Perform inference on the pb_utils.InferenceRequest and append the corresponding pb_utils.InferenceResponse to a response list.

  • Return back the response list.

    • The length of response list being returned must be N.

    • Each element in the list should be the response for the corresponding element in the request array.

    • Each element must contain a response (a response can be either output tensors or an error); an element cannot be None.

Triton checks to ensure that these requirements on response list are satisfied and if not returns an error response for all inference requests. Upon return from the execute function all tensor data associated with the InferenceRequest objects passed to the function are deleted, and so InferenceRequest objects should not be retained by the Python model.

Starting from 24.06, models may choose to send the response using the InferenceResponseSender as illustrated on Decoupled mode. Since the model is in default mode, it must send exactly one response per request. The pb_utils.TRITONSERVER_RESPONSE_COMPLETE_FINAL flag must be sent either with the response or as a flag only response afterward.

Error Handling

In case one of the requests has an error, you can use the TritonError object to set the error message for that specific request. Below is an example of setting errors for an InferenceResponse object:

python
import triton_python_backend_utils as pb_utils

class TritonPythonModel:
    ...

    def execute(self, requests):
        responses = [

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言