Repository navigation
feat(triton): honor KServe classification on tensor outputs #14783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yinggeh
merged 6 commits into
main
from
yinggeh/tri-1448-triton-tensor-path-support-kserve-classification-class_count
Sep 16, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cbcc5aa
feat(llm): carry KServe requested outputs on tensor requests
yinggeh 507fb6c
feat(triton): honor KServe classification on tensor outputs
yinggeh a519b27
test(triton): slim handler dtype coverage and drop restating docstrings
yinggeh d46e8c3
Merge branch 'main' into yinggeh/tri-1448-triton-tensor-path-support-…
yinggeh 6a109fe
fix test
yinggeh f74557e
Merge branch 'main' into yinggeh/tri-1448-triton-tensor-path-support-…
yinggeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """KServe classification (top-K) for Triton model outputs. | ||
|
|
||
| A requested output carrying the KServe ``classification`` parameter asks for the | ||
| output's top-K classes as ``"<score>:<index>[:<label>]"`` strings instead of raw | ||
| values. Triton implements this in its own HTTP / gRPC frontends rather than in | ||
| the core, so the worker reproduces it here: the same descending-score ordering, | ||
| score formatting, and BYTES output shape. | ||
| """ | ||
|
|
||
| from collections.abc import Mapping | ||
| from typing import Any, Optional | ||
|
|
||
| import numpy as np | ||
|
|
||
| CLASSIFICATION_PARAMETER = "classification" | ||
|
|
||
| # Bound on the elements sorted per batch entry, matching Triton's own limit. A | ||
| # larger output would make the full sort below pathologically expensive. | ||
| MAX_CLASSIFICATION_ELEMENTS = 1_000_000 | ||
|
|
||
|
|
||
| def class_count(parameters: Mapping[str, Any]) -> Optional[int]: | ||
| """Number of classes a requested output asked for. | ||
|
|
||
| Returns None when the output carries no ``classification`` parameter, which | ||
| means it asked for raw values. | ||
| """ | ||
| value = parameters.get(CLASSIFICATION_PARAMETER) | ||
| if value is None: | ||
| return None | ||
|
|
||
| # Dynamo serializes a tensor parameter as a single-entry {tag: value} map. | ||
| # For example, {"classification": {"int64": 10}} becomes 10. | ||
| if isinstance(value, Mapping): | ||
| value = value.get("int64") | ||
| if isinstance(value, bool) or not isinstance(value, int): | ||
| raise ValueError( | ||
| f"invalid value type for '{CLASSIFICATION_PARAMETER}' parameter, " | ||
| "expected int64_param" | ||
| ) | ||
| if value < 1: | ||
| raise ValueError( | ||
| f"invalid value for '{CLASSIFICATION_PARAMETER}' parameter, expected >= 1" | ||
| ) | ||
| return value | ||
|
|
||
|
|
||
| def classification_label( | ||
| response: Any, output_name: str, class_index: int | ||
| ) -> Optional[str]: | ||
| """Label for ``output_name`` at ``class_index``, or None if unlabelled. | ||
|
|
||
| Labels sit on the C ``TRITONSERVER_InferenceResponse``, exposed as | ||
| each tensor's ``memory_buffer.owner``. That binding takes an output | ||
| index, in the order ``outputs`` was filled — the same | ||
| ``(response, output_idx, class_index)`` lookup ``TopkClassifications`` | ||
| makes. The high-level Python ``InferenceResponse`` does not wrap this | ||
| yet; if the owner has no lookup, class strings stay unlabeled. | ||
| """ | ||
| try: | ||
| output_idx = list(response.outputs).index(output_name) | ||
| owner = response.outputs[output_name].memory_buffer.owner | ||
| except (AttributeError, KeyError, ValueError): | ||
| return None | ||
| lookup = getattr(owner, "output_classification_label", None) | ||
| if lookup is None: | ||
| return None | ||
| label = lookup(output_idx, class_index) | ||
| return label or None | ||
|
|
||
|
|
||
| def top_k_classifications( | ||
| array: np.ndarray, | ||
| req_class_count: int, | ||
| triton_dtype: str, | ||
| batched: bool, | ||
| response: Any = None, | ||
| output_name: str = "", | ||
| ) -> np.ndarray: | ||
| """Top-K classes of one output as an object array of BYTES class strings. | ||
|
|
||
| ``batched`` marks the leading dimension of ``array`` as a batch dimension, | ||
| in which case the top-K is taken per batch entry and the result keeps that | ||
| dimension. Class indices are relative to a batch entry, as Triton reports | ||
| them. Labels are read from ``response`` when it is provided. | ||
| ``triton_dtype`` is the model's output datatype (``FP32``, ``INT32``, …), | ||
| the same string Triton's frontends pass into ``TopkClassifications``. | ||
| """ | ||
| # FP32/FP64 and INT/UINT, matching TopkClassifications. FP16 is rejected. | ||
| is_float = triton_dtype in ("FP32", "FP64") | ||
| if not is_float and not triton_dtype.startswith(("UINT", "INT")): | ||
| raise ValueError( | ||
| "class result not available for output due to unsupported type " | ||
| f"'{triton_dtype}'" | ||
| ) | ||
|
|
||
| flat = array.reshape(-1) | ||
| batch_size = int(array.shape[0]) if batched and array.ndim > 0 else 0 | ||
| element_cnt = flat.size // batch_size if batch_size else flat.size | ||
| if element_cnt > MAX_CLASSIFICATION_ELEMENTS: | ||
| raise ValueError("classification output tensor too large") | ||
|
|
||
| class_cnt = min(req_class_count, element_cnt) | ||
| class_strs = [] | ||
| for bs in range(max(1, batch_size)): | ||
| probs = flat[bs * element_cnt : (bs + 1) * element_cnt] | ||
| idx = np.argsort(probs)[::-1][:class_cnt] | ||
| for k in range(class_cnt): | ||
| kth_class_index = idx[k] | ||
| # Format the score as a string, matching Triton's std::to_string. | ||
| score = ( | ||
| f"{probs[kth_class_index]:.6f}" | ||
| if is_float | ||
| else str(int(probs[kth_class_index])) | ||
| ) | ||
| class_str = f"{score}:{kth_class_index}" | ||
| if response is not None: | ||
| label = classification_label( | ||
| response, output_name, int(kth_class_index) | ||
| ) | ||
| if label: | ||
| class_str += f":{label}" | ||
| class_strs.append(class_str.encode()) | ||
|
|
||
| shape = (batch_size, class_cnt) if batch_size else (class_cnt,) | ||
|
Vinya567 marked this conversation as resolved.
|
||
| return np.array(class_strs, dtype=object).reshape(shape) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.