Model ensembles

Chain pre-processing, a model and post-processing in one Triton ensemble so clients send text and receive a label.

Updated CompTech Team

Clients should not know your tensor layout. A Triton ensemble is one model made of steps: a Python pre-process, the ONNX model from Triton on OpenShift, and a Python post-process. One request to the ensemble runs all three. The client sends text and gets a label.

The steps are separate models in the repository. The ensemble's config.pbtxt is the only place that names the order. You can replace the ONNX file without changing the client, which is the property the overview is aiming at.

Prerequisites

  • The Triton Deployment from the previous lab, with the model repository on a volume
  • The softmax model loaded, input input and output probs, both float32 of length 4
  • A project that can add files under /models and reload Triton

Three models and a pipeline

preprocess is a Python backend. It takes a string and writes four floats. The function here is deliberately simple so you can see the tensor cross the boundary. A real service would tokenise in this step.

# preprocess/1/model.py
import numpy as np
import triton_python_backend_utils as pb_utils

class TritonPythonModel:
    def execute(self, requests):
        responses = []
        for request in requests:
            raw = pb_utils.get_input_tensor_by_name(request, "TEXT").as_numpy()
            text = raw[0].decode() if isinstance(raw[0], (bytes, bytearray)) else str(raw[0])
            # Four buckets, one per character class, so the vector matches softmax's input.
            vec = np.zeros(4, dtype=np.float32)
            for ch in text.lower():
                vec[min(ord(ch), 3)] += 1.0
            tensor = pb_utils.Tensor("INPUT", vec.reshape(1, 4))
            responses.append(pb_utils.InferenceResponse([tensor]))
        return responses
# preprocess/config.pbtxt
name: "preprocess"
backend: "python"
max_batch_size: 8
input [ { name: "TEXT" data_type: TYPE_STRING dims: [ 1 ] } ]
output [ { name: "INPUT" data_type: TYPE_FP32 dims: [ 4 ] } ]

postprocess reads probs and returns the index of the largest value as a label.

# postprocess/1/model.py
import numpy as np
import triton_python_backend_utils as pb_utils

LABELS = np.array(["payroll", "leave", "it", "other"])

class TritonPythonModel:
    def execute(self, requests):
        responses = []
        for request in requests:
            probs = pb_utils.get_input_tensor_by_name(request, "LOGITS").as_numpy()
            label = LABELS[int(np.argmax(probs, axis=-1)[0])].encode()
            tensor = pb_utils.Tensor("LABEL", np.array([label], dtype=object))
            responses.append(pb_utils.InferenceResponse([tensor]))
        return responses
# postprocess/config.pbtxt
name: "postprocess"
backend: "python"
max_batch_size: 8
input [ { name: "LOGITS" data_type: TYPE_FP32 dims: [ 4 ] } ]
output [ { name: "LABEL" data_type: TYPE_STRING dims: [ 1 ] } ]

The ensemble maps names between steps. model_version: -1 means the latest version of each step.

# classifier/config.pbtxt
name: "classifier"
platform: "ensemble"
max_batch_size: 8
input [ { name: "TEXT" data_type: TYPE_STRING dims: [ 1 ] } ]
output [ { name: "LABEL" data_type: TYPE_STRING dims: [ 1 ] } ]
ensemble_scheduling {
  step [
    {
      model_name: "preprocess"
      model_version: -1
      input_map { key: "TEXT" value: "TEXT" }
      output_map { key: "INPUT" value: "vector" }
    },
    {
      model_name: "softmax"
      model_version: -1
      input_map { key: "input" value: "vector" }
      output_map { key: "probs" value: "probs" }
    },
    {
      model_name: "postprocess"
      model_version: -1
      input_map { key: "LOGITS" value: "probs" }
      output_map { key: "LABEL" value: "LABEL" }
    }
  ]
}

Each Python model also needs an empty preprocess/1/ and postprocess/1/ directory. Copy the tree into the repository and load it:

oc cp models/. "$POD":/models/
curl -X POST http://triton:8000/v2/repository/models/classifier/load
curl -s http://triton:8000/v2/models/classifier

One request

curl -s -X POST http://triton:8000/v2/models/classifier/infer \
  -H 'Content-Type: application/json' \
  -d '{"inputs":[{"name":"TEXT","shape":[1,1],"datatype":"BYTES","data":["reset my password"]}]}'

The output is LABEL. The client never sends a float vector. If you change softmax to a real classifier with the same input and output names, this curl does not change.

When a step fails, Triton reports which model in the ensemble failed. Check that model's log first. Name mismatches are the common case: input_map keys are the step's own input names, and the values are the ensemble's internal tensors. vector and probs exist only inside the scheduling block.

An LLM ensemble is the same shape. Pre-process tokenises, a TensorRT-LLM or Python model generates, post-process detokenises. Build that once the text ensemble reloads cleanly. The engine itself is the TensorRT-LLM lab.

What's next

  1. The ONNX step comes from Triton on OpenShift.
  2. Swap it for an engine from TensorRT-LLM when the step is an LLM.
  3. Call the ensemble from a consumer in Kafka for AI. The consumer sends text and reads LABEL, and a model change stays inside the repository.