def fill_defaults(self, network, default_shape_value=None): """ Fill this profile with sane default values for any bindings whose shapes have not been set explicitly. Args: network (trt.INetworkDefinition): The TensorRT network this profile is meant for. This will be used to determine model inputs and their shapes. default_shape_value (int): The value to use to override dynamic dimensions. Returns: Profile: Self """ default_shape_value = util.default(default_shape_value, constants.DEFAULT_SHAPE_VALUE) for idx in range(network.num_inputs): inp = network.get_input(idx) if inp.name in self: continue with G_LOGGER.verbosity( G_LOGGER.CRITICAL): # WAR for spam from TRT is_shape_tensor = inp.is_shape_tensor if is_shape_tensor: rank = inp.shape[0] shape = (default_shape_value, ) * rank G_LOGGER.warning( "{:} | No values provided; Will use input values: {:} for min/opt/max in profile.\n" .format(trt_util.str_from_tensor(inp, is_shape_tensor), shape, rank), mode=LogMode.ONCE, ) G_LOGGER.warning( "This will cause the shape-tensor to have static values. If this is incorrect, please " "set the range of values for this input shape-tensor.", mode=LogMode.ONCE, ) else: shape = util.override_dynamic_shape(inp.shape, default_shape_value) if shape != inp.shape: G_LOGGER.warning( "{:} | No shapes provided; Will use shape: {:} for min/opt/max in profile.\n" .format(trt_util.str_from_tensor(inp, is_shape_tensor), shape), mode=LogMode.ONCE, ) G_LOGGER.warning( "This will cause the tensor to have a static shape. If this is incorrect, please " "set the range of shapes for this input tensor.", mode=LogMode.ONCE, ) self.add(inp.name, shape, shape, shape) return self
def to_trt(self, builder, network): """ Creates a TensorRT IOptimizationProfile based on the values set in this Profile. Args: builder (trt.Builder): A TensorRT builder. This will be used to construct the IOptimizationProfile. network (trt.INetworkDefinition): The TensorRT network the profile applies to. Returns: trt.IOptimizationProfile: A TensorRT optimization profile. """ trt_profile = builder.create_optimization_profile() unused_keys = set(self.keys()) available_inputs = set() for idx in range(network.num_inputs): inp = network.get_input(idx) if inp.name in unused_keys: unused_keys.remove(inp.name) available_inputs.add(inp.name) with G_LOGGER.verbosity(): # WAR for spam from TRT is_shape_tensor = inp.is_shape_tensor if is_shape_tensor: if inp.name in self: shapes = self[inp.name] trt_profile.set_shape_input(inp.name, shapes.min, shapes.opt, shapes.max) G_LOGGER.verbose( "{:} | Setting input shape-tensor value range to: {:}". format(trt_util.str_from_tensor(inp, is_shape_tensor), shapes)) else: G_LOGGER.warning( "{:} | No values provided. " "Assuming this is not a dynamic shape-tensor.".format( trt_util.str_from_tensor(inp, is_shape_tensor)), mode=LogMode.ONCE, ) else: shapes = self[inp.name] trt_profile.set_shape(inp.name, shapes.min, shapes.opt, shapes.max) G_LOGGER.verbose( "{:} | Setting input tensor shapes to: {:}".format( trt_util.str_from_tensor(inp, is_shape_tensor), shapes)) if unused_keys: G_LOGGER.error( "Invalid inputs were provided to the optimization profile: {:}\n" "Note: Inputs available in the TensorRT network are: {:}". format(unused_keys, available_inputs)) return trt_util.check_profile(trt_profile)