File size: 11,706 Bytes
5000658 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# SPDX-FileCopyrightText: Copyright (c) 2022-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import contextlib
from dataclasses import dataclass
from typing import Any, Dict, List, Optional
# isort: off
import torch
import tensorrt as trt
# isort: on
from .._utils import torch_dtype_to_trt, trt_dtype_to_torch, trt_gte_10
from ..logger import logger
@contextlib.contextmanager
def _scoped_stream():
'''Create a scoped cuda stream, and synchronize it when the context is destroyed
'''
#TODO: delete torch, use cuda native python bindings
import torch
stream = torch.cuda.current_stream()
try:
# return a handle, trt and other lib does not recognize torch.cuda.Stream
yield stream.cuda_stream
finally:
stream.synchronize()
@dataclass
class TensorInfo:
name: str
dtype: trt.DataType
shape: tuple
# add more info like strides, formats if needed
class Session(object):
''' Session is a managed TensorRT runtime. '''
def __init__(self, **kwargs):
# use Session.from_serialized_engine to create a session
pass
def _init(self, engine_buffer=None):
'''
@brief: Setup TensorRT engines and context from a serialized engine file
@param engine_buffer: a buffer holds the serialized TRT engine
'''
self._runtime = trt.Runtime(logger.trt_logger)
if engine_buffer is not None:
self._engine = self.runtime.deserialize_cuda_engine(engine_buffer)
self._context = None
if not (trt_gte_10() and self.engine.streamable_weights_size):
self.__prepare_execution_contexts()
return self
def __prepare_execution_contexts(self):
self._context = self.engine.create_execution_context()
assert self._context is not None, "Failed to create an execution context!"
with _scoped_stream() as stream:
self._context.set_optimization_profile_async(0, stream)
@staticmethod
def from_serialized_engine(engine) -> Session:
'''
@brief: Create a session from a serialized engine
@param engine: a serialized engine
@return: a Session object
'''
session = Session()
return session._init(engine)
@staticmethod
def from_engine(engine) -> Session:
'''
@brief: Create a session from an existing ICudaEngine engine
@param engine: an ICudaEngine
@return: a Session object
'''
session = Session()
session.engine = engine
return session._init()
@property
def runtime(self) -> trt.Runtime:
return self._runtime
@property
def engine(self) -> trt.ICudaEngine:
return self._engine
@engine.setter
def engine(self, engine: trt.ICudaEngine):
self._engine = engine
@property
def context(self) -> trt.IExecutionContext:
'''
@brief: Get the default TensorRT execution context,
use self.engine.create_execution_context() to create a new context if needed
@return: one TensorRT execution context object
'''
return self._context
@property
def context_mem_size(self) -> int:
return self.engine.device_memory_size
def _print_engine_info(self):
'''print engine info for debug purpose, internal use only.
'''
refittable = self.engine.refittable
num_layers = self.engine.num_layers
device_memory_size = self.engine.device_memory_size
name = self.engine.name
nb_profiles = self.engine.num_optimization_profiles
logger.info(
f"Engine:{name=:}, {refittable=:}, {num_layers=:}, {device_memory_size=:}, {nb_profiles=:}"
)
self._print_io_info()
def _print_io_info(self):
'''print engine i/o info for debug purpose, internal use only.
'''
for i in range(self.engine.num_io_tensors):
name = self.engine.get_tensor_name(i)
mode = self.engine.get_tensor_mode(name)
shape = self.engine.get_tensor_shape(name)
dtype = self.engine.get_tensor_dtype(name)
tformat = ";".join([
self.engine.get_tensor_format_desc(name, p)
for p in range(self.engine.num_optimization_profiles)
])
logger.info(
f"Tensor:{name=:}, {mode=:}, {shape=:}, {dtype=:}, {tformat=:}")
def set_shapes(self,
tensor_dict: Dict[str, torch.Tensor],
context: Optional[trt.IExecutionContext] = None):
if context is None:
context = self.context
for i in range(self.engine.num_io_tensors):
name = self.engine.get_tensor_name(i)
if self.engine.get_tensor_mode(name) == trt.TensorIOMode.INPUT:
ok = context.set_input_shape(name, tensor_dict[name].shape)
logger.debug(
f"setting input tensor {name} with shape {tensor_dict[name].shape}"
)
if not ok:
raise ValueError(
f"Couldn't assign {name} with shape {tensor_dict[name].shape}, "
f"engine supports [min, opt, max] = {self.engine.get_tensor_profile_shape(name, context.active_optimization_profile)}"
)
def infer_shapes(
self,
inputs: List[TensorInfo],
context: Optional[trt.IExecutionContext] = None
) -> List[TensorInfo]:
'''
@brief: Set input shapes to given context, and infer the output shapes from the given input shapes.
This function should be called every time when the input shapes are changed before calling run().
Or call the context.set_input_shape on all dynamic shaped input tensors manually.
@param inputs: list of TensorInfo object, each item represents an input tensor
@param context: TensorRT execution context, if None, use the default context
@return: list of TensorInfo object, each item represents an output tensor, returns None if failed
'''
# set shape to the default context if context is not specified
if context is None:
context = self.context
for i in inputs:
if self.engine.get_tensor_mode(i.name) != trt.TensorIOMode.INPUT:
raise ValueError(f"Tensor:{i.name} is not an input tensor")
if self.engine.get_tensor_dtype(i.name) != i.dtype:
raise ValueError(f"Tensor:{i.name} has wrong dtype")
if not context.set_input_shape(i.name, i.shape):
raise RuntimeError(
f"Could not set shape {i.shape} for tensor {i.name}. Please check the profile range for which your model was build."
)
outputs = []
for i in range(self.engine.num_io_tensors):
name = self.engine.get_tensor_name(i)
if self.engine.get_tensor_mode(name) == trt.TensorIOMode.OUTPUT:
shape = context.get_tensor_shape(name)
dtype = self.engine.get_tensor_dtype(name)
outputs.append(TensorInfo(name, dtype, shape))
return outputs
def _set_weight_streaming(self, gpu_weights_percent):
assert self.engine is not None
self._context = None
if not trt_gte_10():
assert gpu_weights_percent == 1, "Weight streaming is only supported by TensorRT 10.0 or later."
return
else:
min = self.engine.minimum_weight_streaming_budget
max = self.engine.streamable_weights_size
budget = int(min + gpu_weights_percent * (max - min))
budget_config = budget if gpu_weights_percent != 1 else 0
self.engine.weight_streaming_budget = budget_config
assert self.engine.weight_streaming_budget == budget_config, "Failed to set weight streaming budget!"
logger.info(
f"Set gpu weights percent to {gpu_weights_percent}, which is {budget} bytes. Valid range: {min} bytes ~ {max} bytes."
)
if self.engine.streamable_weights_size:
try:
self.__prepare_execution_contexts()
except:
free_mem = torch.cuda.mem_get_info()[0]
if free_mem < budget:
raise torch.cuda.OutOfMemoryError(
f"Out of Memory: Memory budget is {budget} bytes but only {free_mem} bytes are available on the GPU."
)
raise
def run(self,
inputs: Dict[str, Any],
outputs: Dict[str, Any],
stream,
context=None) -> bool:
'''
@brief: Run the TensorRT engine with the given inputs and outputs
@param inputs: dict of input tensors, key is tensor name, value is tensor pointer or torch tensor
@param outputs: dict of output tensors, key is tensor name, value is tensor pointer or torch tensor
@param stream: cuda stream to enqueue the TensorRT engine on
@param context: TensorRT execution context, if None, use the default context
@return: True if enqueue succeeded, note the enqueue is an async call,
returning True does not mean the execution is finished
'''
# enqueue to the default context if context is not specified
if context is None:
context = self.context
import torch
for tensor_name in inputs:
tensor = inputs[tensor_name]
ptr = tensor.data_ptr() if isinstance(tensor,
torch.Tensor) else tensor
context.set_tensor_address(tensor_name, ptr)
for tensor_name in outputs:
tensor = outputs[tensor_name]
ptr = tensor.data_ptr() if isinstance(tensor,
torch.Tensor) else tensor
context.set_tensor_address(tensor_name, ptr)
ok = context.execute_async_v3(stream)
return ok
def _debug_run(self,
inputs: Dict[str, "torch.Tensor"],
context=None) -> Dict[str, "torch.Tensor"]:
'''Run the engine enqueue with allocated output tensors, for debug purpose, since it is a sync call and slower than run
'''
import torch
inputs_info = [
TensorInfo(name, torch_dtype_to_trt(tensor.dtype), tensor.shape)
for name, tensor in inputs.items()
]
outputs_info = self.infer_shapes(inputs_info)
outputs = {
t.name: torch.empty(tuple(t.shape),
dtype=trt_dtype_to_torch(t.dtype),
device='cuda')
for t in outputs_info
}
with _scoped_stream() as stream:
self.run(inputs=inputs,
outputs=outputs,
stream=stream,
context=context)
return outputs
|