共享内存扩展#
本文档描述了 Triton 的共享内存扩展。共享内存扩展允许客户端通过系统或 CUDA 共享内存来传递输入和输出张量。对于某些用例,使用共享内存而不是通过 GRPC 或 REST 接口发送张量数据可以显著提高性能。由于 Triton 支持这两种扩展,因此在其服务器元数据的扩展字段中报告了 “system_shared_memory” 和 “cuda_shared_memory”。
共享内存扩展使用一组通用参数来指示输入或输出张量是通过共享内存传递的。这些参数及其类型是
“shared_memory_region” : 字符串值是先前注册的共享内存区域的名称。
“shared_memory_offset” : int64 值是数据在张量开始处的区域内的偏移量(以字节为单位)。
“shared_memory_byte_size” : int64 值是数据的大小(以字节为单位)。
“shared_memory_offset” 参数是可选的,默认为零。其他两个参数是必需的。如果仅给出两个参数中的一个,Triton 将返回错误。
请注意,Windows 尚不支持共享内存。Jetson 仅支持系统共享内存。
HTTP/REST#
在本文档中显示的所有 JSON 模式中,$number
、$string
、$boolean
、$object
和 $array
指的是基本的 JSON 类型。“#optional” 表示一个可选的 JSON 字段。
共享内存参数可以在 $request_input
参数中使用,以指示相应的输入是通过共享内存传递的。这些参数可以在 $request_output
参数中使用,以指示请求的输出应通过共享内存传递。
当为输入张量设置这些参数时,不得设置 $request_input
的 “data” 字段。如果设置了 “data” 字段,Triton 将返回错误。当为请求的输出张量设置这些参数时,返回的 $response_output
不得定义 “data” 字段。
共享内存区域必须由客户端创建,然后向 Triton 注册,之后才能使用 “shared_memory_region” 参数引用它们。系统和 CUDA 共享内存扩展各自需要一组不同的 API 来注册共享内存区域。
系统共享内存#
系统共享内存扩展需要状态、注册和取消注册 API。
Triton 公开了以下 URL 来注册和取消注册系统共享内存区域。
GET v2/systemsharedmemory[/region/${REGION_NAME}]/status
POST v2/systemsharedmemory/region/${REGION_NAME}/register
POST v2/systemsharedmemory[/region/${REGION_NAME}]/unregister
状态#
系统共享内存状态请求通过 HTTP GET 发送到状态端点。在相应的响应中,HTTP 正文包含响应 JSON。如果 URL 中提供了 REGION_NAME,则响应包括相应区域的状态。如果 URL 中未提供 REGION_NAME,则响应包括所有已注册区域的状态。
成功的状态请求由 200 HTTP 状态代码指示。响应对象(标识为 $system_shared_memory_status_response
)在每个成功的请求的 HTTP 正文中返回。
$system_shared_memory_status_response =
[
{
"name" : $string,
"key" : $string,
"offset" : $number,
"byte_size" : $number
},
…
]
“name”:共享内存区域的名称。
“key”:包含共享内存区域的底层内存对象的键。
“offset”:底层内存对象内到共享内存区域起点的偏移量(以字节为单位)。
“byte_size”:共享内存区域的大小(以字节为单位)。
失败的状态请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $system_shared_memory_status_error_response
对象。
$system_shared_memory_status_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
注册#
系统共享内存注册请求通过 HTTP POST 发送到注册端点。在相应的响应中,HTTP 正文包含响应 JSON。成功的注册请求由 200 HTTP 状态代码指示。
请求对象(标识为 $system_shared_memory_register_request
)必须在 HTTP 正文中提供。
$system_shared_memory_register_request =
{
"key" : $string,
"offset" : $number,
"byte_size" : $number
}
“key”:包含共享内存区域的底层内存对象的键。
“offset”:底层内存对象内到共享内存区域起点的偏移量(以字节为单位)。
“byte_size”:共享内存区域的大小(以字节为单位)。
失败的注册请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $system_shared_memory_register_error_response
对象。
$system_shared_memory_register_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
取消注册#
系统共享内存取消注册请求通过 HTTP POST 发送到取消注册端点。在请求中,HTTP 正文必须为空。
成功的注册请求由 200 HTTP 状态指示。如果 URL 中提供了 REGION_NAME,则取消注册单个区域。如果 URL 中未提供 REGION_NAME,则取消注册所有区域。
失败的取消注册请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $system_shared_memory_unregister_error_response
对象。
$system_shared_memory_unregister_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
CUDA 共享内存#
CUDA 共享内存扩展需要状态、注册和取消注册 API。
Triton 公开了以下 URL 来注册和取消注册系统共享内存区域。
GET v2/cudasharedmemory[/region/${REGION_NAME}]/status
POST v2/cudasharedmemory/region/${REGION_NAME}/register
POST v2/cudasharedmemory[/region/${REGION_NAME}]/unregister
状态#
CUDA 共享内存状态请求通过 HTTP GET 发送到状态端点。在相应的响应中,HTTP 正文包含响应 JSON。如果 URL 中提供了 REGION_NAME,则响应包括相应区域的状态。如果 URL 中未提供 REGION_NAME,则响应包括所有已注册区域的状态。
成功的状态请求由 200 HTTP 状态代码指示。响应对象(标识为 $cuda_shared_memory_status_response
)在每个成功的请求的 HTTP 正文中返回。
$cuda_shared_memory_status_response =
[
{
"name" : $string,
"device_id" : $number,
"byte_size" : $number
},
…
]
“name”:共享内存区域的名称。
“device_id”:创建 cudaIPC 句柄的 GPU 设备 ID。
“byte_size”:共享内存区域的大小(以字节为单位)。
失败的状态请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $cuda_shared_memory_status_error_response
对象。
$cuda_shared_memory_status_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
注册#
CUDA 共享内存注册请求通过 HTTP POST 发送到注册端点。在相应的响应中,HTTP 正文包含响应 JSON。成功的注册请求由 200 HTTP 状态代码指示。
请求对象(标识为 $cuda_shared_memory_register_request
)必须在 HTTP 正文中提供。
$cuda_shared_memory_register_request =
{
"raw_handle" : { "b64" : $string },
"device_id" : $number,
"byte_size" : $number
}
“raw_handle”:序列化的 cudaIPC 句柄,base64 编码。
“device_id”:创建 cudaIPC 句柄的 GPU 设备 ID。
“byte_size”:共享内存区域的大小(以字节为单位)。
失败的注册请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $cuda_shared_memory_register_error_response
对象。
$cuda_shared_memory_register_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
取消注册#
CUDA 共享内存取消注册请求通过 HTTP POST 发送到取消注册端点。在请求中,HTTP 正文必须为空。
成功的注册请求由 200 HTTP 状态指示。如果 URL 中提供了 REGION_NAME,则取消注册单个区域。如果 URL 中未提供 REGION_NAME,则取消注册所有区域。
失败的取消注册请求必须由 HTTP 错误状态(通常为 400)指示。HTTP 正文必须包含 $cuda_shared_memory_unregister_error_response
对象。
$cuda_shared_memory_unregister_error_response =
{
"error": $string
}
“error”:错误的描述性消息。
GRPC#
共享内存参数可以在 ModelInferRequest::InferInputTensor 消息中使用,以指示相应的输入是通过共享内存传递的。这些参数可以在 ModelInferRequest::InferRequestedOutputTensor 消息中使用,以指示请求的输出应通过共享内存传递。
当为输入张量设置这些参数时,不得设置 ModelInferRequest::InferInputTensor 的 “contents” 字段。如果设置了 “contents” 字段,Triton 将返回错误… 当为请求的输出张量设置这些参数时,推理响应中将不会设置 ModelInferResponse::InferOutputTensor 的 “contents” 字段。
共享内存区域必须由客户端创建,然后向 Triton 注册,之后才能使用 “shared_memory_region” 参数引用它们。系统和 CUDA 共享内存扩展各自需要一组不同的 API。对于所有 API,错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。
系统共享内存#
系统共享内存扩展需要以下 API
service GRPCInferenceService
{
…
// Get the status of all registered system-shared-memory regions.
rpc SystemSharedMemoryStatus(SystemSharedMemoryStatusRequest)
returns (SystemSharedMemoryStatusResponse) {}
// Register system-shared-memory region.
rpc SystemSharedMemoryRegister(SystemSharedMemoryRegisterRequest)
returns (SystemSharedMemoryRegisterResponse) {}
// Unregister system-shared-memory region.
rpc SystemSharedMemoryUnregister(SystemSharedMemoryUnregisterRequest)
returns (SystemSharedMemoryUnregisterResponse) {}
}
状态#
系统共享内存状态 API 提供有关已注册系统共享内存区域的信息。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。SystemSharedMemoryStatus 的请求和响应消息是
message SystemSharedMemoryStatusRequest
{
// The name of the region to get status for. If empty the
// status is returned for all registered regions.
string name = 1;
}
message SystemSharedMemoryStatusResponse
{
// Status for a shared memory region.
message RegionStatus {
// The name for the shared memory region.
string name = 1;
// The key of the underlying memory object that contains the
// shared memory region.
string key = 2;
// Offset, in bytes, within the underlying memory object to
// the start of the shared memory region.
uint64 offset = 3;
// Size of the shared memory region, in bytes.
uint64 byte_size = 4;
}
// Status for each of the registered regions, indexed by region name.
map<string, RegionStatus> regions = 1;
}
注册#
系统共享内存注册 API 用于向 Triton 注册新的共享内存区域。注册区域后,可以在输入或输出张量的 “shared_memory_region” 参数中使用它。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。SystemSharedMemoryRegister 的请求和响应消息是
message SystemSharedMemoryRegisterRequest
{
// The name of the region to register.
string name = 1;
// The key of the underlying memory object that contains the
// shared memory region.
string key = 2;
// Offset, in bytes, within the underlying memory object to
// the start of the shared memory region.
uint64 offset = 3;
// Size of the shared memory region, in bytes.
uint64 byte_size = 4;
}
message SystemSharedMemoryRegisterResponse
{
}
取消注册#
系统共享内存取消注册 API 提供从 Triton 取消注册共享内存区域的功能。取消注册区域后,将无法再使用它来传递输入和输出张量内容。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。SystemSharedMemoryStatus 的请求和响应消息是
message SystemSharedMemoryUnregisterRequest
{
// The name of the region to unregister. If empty all system shared-memory
// regions are unregistered.
string name = 1;
}
message SystemSharedMemoryUnregisterResponse
{
}
CUDA 共享内存#
CUDA 共享内存扩展需要以下 API
service GRPCInferenceService
{
…
// Get the status of all registered CUDA-shared-memory regions.
rpc CudaSharedMemoryStatus(CudaSharedMemoryStatusRequest)
returns (CudaSharedMemoryStatusResponse) {}
// Register CUDA-shared-memory region.
rpc CudaSharedMemoryRegister(CudaSharedMemoryRegisterRequest)
returns (CudaSharedMemoryRegisterResponse) {}
// Unregister CUDA-shared-memory region.
rpc CudaSharedMemorUnregister(CudaSharedMemoryUnregisterRequest)
returns (CudaSharedMemoryUnregisterResponse) {}
}
状态#
CUDA 共享内存状态 API 提供有关已注册 CUDA 共享内存区域的信息。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。CudaSharedMemoryStatus 的请求和响应消息是
message CudaSharedMemoryStatusRequest
{
// The name of the region to get status for. If empty the
// status is returned for all registered regions.
string name = 1;
}
message CudaSharedMemoryStatusResponse
{
// Status for a shared memory region.
message RegionStatus {
// The name for the shared memory region.
string name = 1;
// The GPU device ID where the cudaIPC handle was created.
uint64 device_id = 2;
// Size of the shared memory region, in bytes.
uint64 byte_size = 3;
}
// Status for each of the registered regions, indexed by region name.
map<string, RegionStatus> regions = 1;
}
注册#
CUDA 共享内存注册 API 用于向 Triton 注册新的共享内存区域。注册区域后,可以在输入或输出张量的 “shared_memory_region” 参数中使用它。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。CudaSharedMemoryRegister 的请求和响应消息是
message CudaSharedMemoryRegisterRequest
{
// The name of the region to register.
string name = 1;
// The raw serialized cudaIPC handle.
bytes raw_handle = 2;
// The GPU device ID on which the cudaIPC handle was created.
int64 device_id = 3;
// Size of the shared memory region, in bytes.
uint64 byte_size = 4;
}
message CudaSharedMemoryRegisterResponse
{
}
取消注册#
CUDA 共享内存取消注册 API 提供从 Triton 取消注册共享内存区域的功能。取消注册区域后,将无法再使用它来传递输入和输出张量内容。错误都由请求返回的 google.rpc.Status 指示。OK 代码表示成功,其他代码表示失败。CudaSharedMemoryStatus 的请求和响应消息是
message CudaSharedMemoryUnregisterRequest
{
// The name of the region to unregister. If empty all CUDA shared-memory
// regions are unregistered.
string name = 1;
}
message CudaSharedMemoryUnregisterResponse
{
}