A config builder for generating a Program. input_type : (np.dtype, default=None), the inputs will be casted to input_type before fed into TRT engine. If set to None, no casting will be performed. no_cast_list : (list[str], default=None), specify the tensors that will skip the
| 242 | |
| 243 | |
| 244 | class ProgramConfig: |
| 245 | '''A config builder for generating a Program. |
| 246 | input_type : (np.dtype, default=None), the inputs will be casted to input_type before |
| 247 | fed into TRT engine. If set to None, no casting will be performed. |
| 248 | no_cast_list : (list[str], default=None), specify the tensors that will skip the casting |
| 249 | ''' |
| 250 | |
| 251 | def __init__( |
| 252 | self, |
| 253 | ops: list[OpConfig], |
| 254 | weights: dict[str, TensorConfig], |
| 255 | inputs: dict[str, TensorConfig], |
| 256 | outputs: list[str], |
| 257 | input_type: np.dtype | None = None, |
| 258 | no_cast_list: list[str] | None = None, |
| 259 | ): |
| 260 | self.ops = ops |
| 261 | # if no weight need to save, we create a place_holder to help serialize params. |
| 262 | if not weights: |
| 263 | |
| 264 | def generate_weight(): |
| 265 | return np.array([1]).astype(np.float32) |
| 266 | |
| 267 | self.weights = { |
| 268 | "place_holder_weight": TensorConfig(data_gen=generate_weight) |
| 269 | } |
| 270 | else: |
| 271 | self.weights = weights |
| 272 | self.inputs = inputs |
| 273 | self.outputs = outputs |
| 274 | self.input_type = input_type |
| 275 | self.no_cast_list = [] if no_cast_list is None else no_cast_list |
| 276 | self.supported_cast_type = [np.float32, np.float16] |
| 277 | |
| 278 | def __repr__(self): |
| 279 | log_str = '' |
| 280 | for i in range(len(self.ops)): |
| 281 | if i != len(self.ops) - 1: |
| 282 | log_str += repr(self.ops[i]) + ' + ' |
| 283 | else: |
| 284 | log_str += repr(self.ops[i]) |
| 285 | log_str += ' -- ' |
| 286 | for t, v in self.inputs.items(): |
| 287 | log_str += '[' + t + ': ' + str(v) + ']' |
| 288 | for t, v in self.weights.items(): |
| 289 | log_str += '[' + t + ': ' + str(v) + ']' |
| 290 | log_str += f"['input_type': {self.input_type}]" |
| 291 | return log_str |
| 292 | |
| 293 | def set_input_type(self, _type: np.dtype) -> None: |
| 294 | assert _type in self.supported_cast_type or _type is None, ( |
| 295 | "PaddleTRT only supports FP32 / FP16 IO" |
| 296 | ) |
| 297 | |
| 298 | ver = paddle.inference.get_trt_compile_version() |
| 299 | trt_version = ver[0] * 1000 + ver[1] * 100 + ver[2] * 10 |
| 300 | if trt_version < 8600: |
| 301 | logging.info("set_input_type is ignored for TRT version < 8600") |
no outgoing calls