ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Triton Inference Server 统计扩展(Statistics Extension)协议深度解析:HTTP/REST 与 gRPC 接口全解

2026/9/24 0:55:56 拓冰建站 浏览量
Triton Inference Server 统计扩展(Statistics Extension)协议深度解析:HTTP/REST 与 gRPC 接口全解 模型推理服务AI 应用后端【免费下载链接】serverThe Triton Inference Server provides an optimized cloud and edge inferencing solution.项目地址https://gitcode.com/gh_mirrors/server117/server点击查看免费下载Triton Inference Server 的统计扩展Statistics Extension用于报告每个模型按版本区分从服务器启动以来的累计活动统计是评估模型吞吐、时延与资源占用的事实数据来源。本文以 docs/protocol/extension_statistics.md 为骨架完整解析其 HTTP/REST 与 gRPC 两种接口的请求/响应结构、全部字段语义并结合 src/http_server.cc 与 src/grpc/grpc_server.cc 的源码实现帮助读者准确理解 inference_count、execution_count、各阶段 Duration 统计的真实含义从而正确解读与二次开发统计能力。一、什么是统计扩展Triton 的统计扩展Statistics Extension提供按模型按版本维度的累计统计这些统计聚合了自 Triton 启动以来发生在某个特定模型版本上的所有活动信息。其核心价值在于按模型/版本精确追踪每个统计对象都归属于具体的模型名称与版本便于对比同一模型不同版本之间的性能差异累计计数 累计时长统计以“count 总时长(ns)”形式呈现既可计算长期运行的平均值也可在两个采样点之间做差值得到增量平均值从服务器元数据即可发现能力由于 Triton 支持该扩展其 Server MetadataGET v2的extensions字段中会包含statistics标识客户端可以据此探测服务器是否具备统计能力。从实现层面看统计能力由编译选项TRITON_ENABLE_STATS控制。src/http_server.cc 中统计处理逻辑被#ifdef TRITON_ENABLE_STATS包裹未启用该编译选项时请求会返回“the server does not support model statistics”的不可用错误这一点在部署自编译服务器时需特别留意。二、HTTP/REST 接口2.1 端点与 URL 语义统计端点通过 HTTP GET 方法暴露URL 模板如下GET v2/models[/${MODEL_NAME}[/versions/${MODEL_VERSION}]]/statsURL 中三个段落的可选性决定了返回数据的范围URL 形式返回内容GET v2/models/stats所有模型的所有版本的统计GET v2/models/${MODEL_NAME}/stats指定模型的所有版本的统计GET v2/models/${MODEL_NAME}/versions/${MODEL_VERSION}/stats指定模型指定版本的统计这一点与源码中的路由实现一致。src/http_server.cc 首先对/v2/models/stats这一全模型路径做精确匹配并直接分发到HandleModelStats(req)随后src/http_server.cc通过正则model_regex_形如/v2/models/([^/])(?:/versions/([0-9]))?(?:/(infer|generate|generate_stream|ready|config|stats|trace/setting))?解析出模型名、版本与资源类型当kind stats时调用HandleModelStats(req, model_name, version)。实际的请求处理函数 HandleModelStats 还做了几项关键约束与校验仅接受GET方法其他方法返回405 Method Not Allowed若未启用TRITON_ENABLE_STATS返回503且错误信息为the server does not support model statistics版本字符串会先经GetModelVersionFromString解析为int64_t再调用核心 C APITRITONSERVER_ServerModelStatistics获取统计消息最后通过TRITONSERVER_MessageSerializeToJson序列化为 JSON 写入响应体。2.2 请求示例不带任何模型限定获取全部模型的统计curl -X GET http://localhost:8000/v2/models/stats仅获取某个模型如resnet50全部版本的统计curl -X GET http://localhost:8000/v2/models/resnet50/stats获取指定模型指定版本的统计curl -X GET http://localhost:8000/v2/models/resnet50/versions/1/stats2.3 成功响应$stats_model_response成功的统计请求以 HTTP 200 状态码标识响应体为$stats_model_response对象$stats_model_response { model_stats : [ $model_stat, ... ] }其中model_stats数组中的每个$model_stat对象对应一个具体模型的一个具体版本的统计信息结构如下$model_stat { name : $string, version : $string #optional, last_inference : $number, inference_count : $number, execution_count : $number, inference_stats : $inference_stats, response_stats : { $string : $response_stats, ... }, batch_stats : [ $batch_stats, ... ], memory_usage : [ $memory_usage, ...] }各字段语义如下name模型名称。version模型版本。该字段为可选项——对于不支持版本概念的服务器可以不返回。last_inference该模型最后一次收到推理请求的时间戳单位为自 epoch 以来的毫秒数。inference_count该模型累计成功的推理请求数。注意其计数口径批量请求中的每一个推理都单独计数。例如客户端发送一个 batch size 为 64 的请求inference_count增加 64如果客户端发送 64 个 batch size 为 1 的独立请求inference_count同样增加 64。该值不包含响应缓存Response Cache命中带来的推理。execution_count该模型累计成功的推理执行次数。它与inference_count的差异集中体现在动态批处理Dynamic Batching场景启用动态批处理后一次模型执行可以为多个推理请求服务。例如64 个 batch size 为 1 的请求被动态批处理器合并为一个大 batch 执行则execution_count只增加 1若未启用动态批处理64 个请求各自独立执行则execution_count增加 64。该值不包含缓存命中的执行。inference_stats模型的聚合推理统计例如inference_stats.success表示该模型成功的推理请求数量与累计时长。response_stats模型的聚合响应统计面向解耦/流式模型。例如{ key : { response_stats : success } }表示该模型在key处成功响应的聚合统计其中key用于标识模型在一次请求中产生的不同响应。对于一个产生三条响应的模型key 依次可为0、1、2。batch_stats每种被实际执行过的 batch size 的聚合统计用于揭示不同 batch size 带来的执行差异例如更大的 batch 通常计算耗时更长。memory_usage模型加载期间检测到的内存占用可用于估算模型卸载后释放的内存。注意该估算由性能剖析工具与框架自身的内存模型推断而来官方建议先做实验以理解哪些场景下报告的内存数值是可靠的。作为起点ONNX Runtime 后端与 TensorRT 后端的模型 GPU 内存占用通常是对齐的。2.4$inference_stats推理阶段耗时分解$inference_stats { success : $duration_stat, fail : $duration_stat, queue : $duration_stat, compute_input : $duration_stat, compute_infer : $duration_stat, compute_output : $duration_stat, cache_hit: $duration_stat, cache_miss: $duration_stat }字段含义success所有成功推理请求的计数与累计时长。包含缓存命中。fail所有失败推理请求的计数与累计时长。queue推理请求在调度队列或其他队列中等待的计数与累计时长。包含缓存命中。compute_input按模型框架/后端要求准备输入张量数据的计数与累计时长例如应包括将输入张量数据拷贝到 GPU 的时间。不包含缓存命中。compute_infer执行模型的计数与累计时长。不包含缓存命中。compute_output提取模型框架/后端产生的输出张量数据的计数与累计时长例如应包括将输出张量数据从 GPU 拷贝出来的时间。不包含缓存命中。cache_hit响应缓存命中的次数以及在命中时查找并提取缓存中输出张量数据的累计时长例如应包括将输出张量数据从响应缓存拷贝到响应对象的时间。命中时 Triton 无需再访问模型/后端获取输出数据因此compute_input、compute_infer、compute_output不更新。cache_miss响应缓存未命中的次数以及在未命中时查找并写入输出张量数据的累计时长例如应包括将输出张量数据从响应对象拷贝到响应缓存的时间。2.5$response_stats按响应索引的统计$response_stats { compute_infer : $duration_stat, compute_output : $duration_stat, success : $duration_stat, fail : $duration_stat, empty_response : $duration_stat, cancel : $duration_stat }字段含义compute_infer计算一个响应的计数与累计时长。compute_output提取已计算响应的输出张量的计数与累计时长。success成功推理的计数与累计时长时长为 infer 时长与 output 时长之和。fail失败推理的计数与累计时长时长为 infer 时长与 output 时长之和。empty_response空/无响应推理的计数与累计时长时长为 infer 时长。cancel推理被取消的计数与累计时长时长用于清理被取消推理请求所持有的资源。2.6$batch_stats按 batch size 的统计$batch_stats { batch_size : $number, compute_input : $duration_stat, compute_infer : $duration_stat, compute_output : $duration_stat }字段含义batch_size批次的大小。count该 batch size 在模型上被执行过的次数一次模型执行服务整个请求批次启用动态批处理时一次执行可以服务多个请求。compute_input以该 batch size 准备输入张量数据的计数与累计时长例如包括拷贝输入到 GPU 的时间。compute_infer以该 batch size 执行模型的计数与累计时长。compute_output以该 batch size 提取输出张量数据的计数与累计时长例如包括从 GPU 拷贝输出的时间。2.7$duration_stat累计时长统计基元$duration_stat是上述所有时长统计的基础对象报告一个计数与一个总时长$duration_stat { count : $number, ns : $number }字段含义count该统计被收集的次数。ns该统计的总时长单位为纳秒。由于同时提供计数与总时长该格式支持在两个采样点之间做差分从而计算任意时间窗口内的增量平均值incremental average而不是只能得到运行以来的长期平均。2.8$memory_usage内存占用统计$memory_usage { type : $string, id : $number, byte_size : $number }字段含义type内存类型取值可为CPU、CPU_PINNED、GPU。id内存所属设备的 id通常与type一起用于标识承载该内存的设备。byte_size内存的字节大小。2.9 失败响应$repository_statistics_error_response统计请求失败时以 HTTP 错误状态码标识典型为 400响应体必须为$repository_statistics_error_response对象$repository_statistics_error_response { error: $string }其中error为描述错误的可读消息。三、gRPC 接口统计扩展在 gRPC 侧对应GRPCInferenceService服务中的ModelStatisticsRPCservice GRPCInferenceService { … // Get the cumulative statistics for a model and version. rpc ModelStatistics(ModelStatisticsRequest) returns (ModelStatisticsResponse) {} }该 RPC 的注册与执行在 src/grpc/grpc_server.cc 中实现RegisterModelStatistics()通过service_-RequestModelStatistics注册异步请求处理实际执行时调用核心 C APITRITONSERVER_ServerModelStatistics填充响应。错误通过请求返回的google.rpc.Status指示OK表示成功其他状态码表示失败。请求与响应消息定义如下message ModelStatisticsRequest { // The name of the model. If not given returns statistics for all // models. string name 1; // The version of the model. If not given returns statistics for // all model versions. string version 2; } message ModelStatisticsResponse { // Statistics for each requested model. repeated ModelStatistics model_stats 1; }与 HTTP 接口类似name与version均为可选不填name返回全部模型统计不填version返回指定模型的全部版本统计。核心统计消息ModelStatistics完整定义如下字段编号与 HTTP JSON 字段一一对应// Statistic recording a cumulative duration metric. message StatisticDuration { // Cumulative number of times this metric occurred. uint64 count 1; // Total collected duration of this metric in nanoseconds. uint64 ns 2; } // Statistics for a specific model and version. message ModelStatistics { // The name of the model. string name 1; // The version of the model. string version 2; // The timestamp of the last inference request made for this model, // as milliseconds since the epoch. uint64 last_inference 3; // The cumulative count of successful inference requests made for this // model. Each inference in a batched request is counted as an // individual inference. uint64 inference_count 4; // The cumulative count of the number of successful inference executions // performed for the model. uint64 execution_count 5; // The aggregate statistics for the model. InferStatistics inference_stats 6; // The aggregate statistics for each different batch size that is // executed in the model. repeated InferBatchStatistics batch_stats 7; // The memory usage detected during model loading. repeated MemoryUsage memory_usage 8; // The key and value pairs for all decoupled responses statistics. The key is // a string identifying a set of response statistics aggregated together (i.e. // index of the response sent). The value is the aggregated response // statistics. mapstring, InferResponseStatistics response_stats 9; } // Inference statistics. message InferStatistics { // Cumulative count and duration for successful inference request. // Includes cache hits. StatisticDuration success 1; // Cumulative count and duration for failed inference request. StatisticDuration fail 2; // The count and cumulative duration that inference requests wait in // scheduling or other queues. Includes cache hits. StatisticDuration queue 3; // The count and cumulative duration to prepare input tensor data as // required by the model framework / backend. Does not account for // requests that were a cache hit. StatisticDuration compute_input 4; // The count and cumulative duration to execute the model. // Does not account for requests that were a cache hit. StatisticDuration compute_infer 5; // The count and cumulative duration to extract output tensor data // produced by the model framework / backend. Does not account for // requests that were a cache hit. StatisticDuration compute_output 6; // The count of response cache hits and cumulative duration to lookup // and extract output tensor data from the Response Cache on a cache hit. StatisticDuration cache_hit 7; // The count of response cache misses and cumulative duration to lookup // and insert output tensor data from the computed response to the cache. StatisticDuration cache_miss 8; } // Statistics per decoupled response. message InferResponseStatistics { // The count and cumulative duration to compute a response. StatisticDuration compute_infer 1; // The count and cumulative duration to extract the output tensors of a // response. StatisticDuration compute_output 2; // The count and cumulative duration for successful responses. StatisticDuration success 3; // The count and cumulative duration for failed responses. StatisticDuration fail 4; // The count and cumulative duration for empty responses. StatisticDuration empty_response 5; } // Inference batch statistics. message InferBatchStatistics { // The size of the batch. uint64 batch_size 1; // The count and cumulative duration to prepare input tensor data as // required by the model framework / backend with the given batch size. StatisticDuration compute_input 2; // The count and cumulative duration to execute the model with the given // batch size. StatisticDuration compute_infer 3; // The count and cumulative duration to extract output tensor data // produced by the model framework / backend with the given batch size. StatisticDuration compute_output 4; } // Memory usage. message MemoryUsage { // The type of memory, the value can be CPU, CPU_PINNED, GPU. string type 1; // The id of the memory, typically used with type to identify // a device that hosts the memory. int64_t id 2; // The byte size of the memory. uint64_t byte_size 3; }四、实战用 Python 客户端获取统计Triton 官方 Python 客户端库tritonclient.http与tritonclient.grpc均封装了统计接口示例用法如下。HTTP 客户端import tritonclient.http as httpclient client httpclient.InferenceServerClient(urllocalhost:8000) # 获取全部模型的统计 all_stats client.get_model_statistics() # 获取指定模型全部版本的统计 model_stats client.get_model_statistics(model_nameresnet50) # 获取指定模型指定版本的统计 ver_stats client.get_model_statistics( model_nameresnet50, model_version1 )gRPC 客户端import tritonclient.grpc as grpcclient client grpcclient.InferenceServerClient(urllocalhost:8001) # 不指定模型名时返回所有模型的统计 all_stats client.get_model_statistics() # 指定模型名与版本 stats client.get_model_statistics( model_nameresnet50, model_version1 )返回的统计对象可直接访问各字段例如stats.model_stats[0].inference_count、stats.model_stats[0].execution_count以及inference_stats下各阶段success、queue、compute_input、compute_infer、compute_output、cache_hit、cache_miss的count与ns字段。五、源码实现佐证与使用建议5.1 请求处理链路统计请求在服务端的完整链路可归纳为HTTPHTTPAPIServer::Handlesrc/http_server.cc根据 URL 分发到HandleModelStatssrc/http_server.cc→TRITONSERVER_ServerModelStatistics→ JSON 序列化返回gRPCCommonHandler::RegisterModelStatisticssrc/grpc/grpc_server.cc注册异步 RPC → 执行回调调用TRITONSERVER_ServerModelStatistics填充ModelStatisticsResponse。两条路径最终都汇入统一的 C APITRITONSERVER_ServerModelStatistics保证 HTTP 与 gRPC 返回的统计口径完全一致。5.2 编译开关与能力探测统计功能受TRITON_ENABLE_STATS编译选项控制。启用后服务器在GET v2返回的 Server Metadata 的extensions字段中会包含statistics客户端应优先通过该字段探测能力再决定是否调用统计接口未启用时 HTTP 请求会返回不可用错误gRPC 请求则通过google.rpc.Status的非 OK 状态码体现。5.3 解读统计时的三个要点区分inference_count与execution_count前者按“推理请求数”计数批量中的每个样本单独计数后者按“模型执行次数”计数。二者之比可以反映动态批处理的聚合效果——当动态批处理将大量请求合并执行时execution_count会显著小于inference_count。关注queue与compute_*的差异queue反映调度等待compute_input/compute_infer/compute_output反映后端实际计算通过差分可定位瓶颈在排队阶段还是计算阶段。缓存命中需单独核算success、queue包含缓存命中而compute_input、compute_infer、compute_output不包含启用响应缓存后应结合cache_hit/cache_miss的 count 与 ns 评估缓存收益。六、关联文档与进一步阅读协议原文docs/protocol/extension_statistics.mdHTTP 实现src/http_server.ccgRPC 实现src/grpc/grpc_server.cc统计相关测试qa/L0_response_statistics/response_statistics_test.py协议索引docs/protocol/README.md指标与监控指南docs/user_guide/metrics.md统计扩展提供的是服务器内置的按模型/版本的累计数据适合作为性能基线评估与容量规划的原始数据来源若需要更高频率、更细粒度的在线监控可结合 Triton 的 Metrics 体系Prometheus 格式指标使用两者互为补充。赞分享模型推理服务AI 应用后端【免费下载链接】serverThe Triton Inference Server provides an optimized cloud and edge inferencing solution.项目地址https://gitcode.com/gh_mirrors/server117/server点击查看免费下载相关推荐Triton Inference Server 的 KServe 协议扩展全景从 HTTP/REST 到 gRPC 的 11 个扩展机制详解Triton Inference Server 的 KServe 协议扩展全景从 HTTP/REST 到 gRPC 的 11 个扩展机制详解 导读 Trito模型推理服务AI 应用后端awesome-free-saas原型工具清单Proto、墨刀、Mockplus等5款免费原型工具awesome free saas原型工具清单Proto、墨刀、Mockplus等5款免费原型工具 awesome free saas https://lin文档知识库如何实现Triton Inference Server多端口健康检查HTTP与GRPC独立监控指南如何实现Triton Inference Server多端口健康检查HTTP与GRPC独立监控指南 Triton Inference Server是一款功能强模型推理服务AI 应用后端创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考