tensorflow/lite/kernels/internal/depthwiseconv_float_test.cc
// This function picks some random DepthwiseConv params, which may or may not
// be legal. If they're not legal, it returns false. If they're legal,
// it runs the DepthwiseConv test and returns true. This allows the caller
// to loop until a test has been run.
This function picks some random DepthwiseConv params, which may or may not
be legal. If they're not legal, it returns false. If they're legal,
it runs the DepthwiseConv test and returns true. This allows the caller
to loop until a test has been run.
This function picks some random DepthwiseConv params which may or may not be legal . If they ' re not legal it returns false . If they ' re legal it runs the DepthwiseConv test and returns true . This allows the caller to loop until a test has been run .
bool TryTestOneDepthwiseConv() {
  const int batch = UniformRandomInt(1, 2);
  const int input_depth = ExponentialRandomPositiveInt(0.9f, 6, 50);
  const int input_width = ExponentialRandomPositiveInt(0.9f, 20, 200);
  const int input_height = ExponentialRandomPositiveInt(0.9f, 20, 200);
  const int filter_width = ExponentialRandomPositiveInt(0.9f, 4, 10);
  const int filter_height = ExponentialRandomPositiveInt(0.9f, 4, 10);
  const int depth_multiplier = ExponentialRandomPositiveInt(0.8f, 6, 50);
  const int stride = ExponentialRandomPositiveInt(0.9f, 3, 8);
  const int output_depth = input_depth * depth_multiplier;
  const int dilation_width_factor = RandomElement(std::vector<int>({1, 2, 4}));
  const int dilation_height_factor = RandomElement(std::vector<int>({1, 2, 4}));
  float output_activation_min, output_activation_max;
  FusedActivationFunctionType ac =
      RandomElement(std::vector<FusedActivationFunctionType>(
          {FusedActivationFunctionType::kNone,
           FusedActivationFunctionType::kRelu,
           FusedActivationFunctionType::kRelu1,
           FusedActivationFunctionType::kRelu6}));
  GetActivationMinMax(ac, &output_activation_min, &output_activation_max);
  const int kMaxSupportedOutputDepth = 1024;
  if (output_depth > kMaxSupportedOutputDepth) {
    return false;
  }
  RuntimeShape input_shape_inference(
      {batch, input_height, input_width, input_depth});
  RuntimeShape output_shape_inference;
  int pad_width, pad_height;
  const auto padding_type =
      UniformRandomInt(0, 1) ? PaddingType::kSame : PaddingType::kValid;
  if (!ComputeConvSizes(input_shape_inference, output_depth, filter_width,
                        filter_height, stride, dilation_width_factor,
                        dilation_height_factor, padding_type,
                        &output_shape_inference, &pad_width, &pad_height)) {
    return false;
  }
  RuntimeShape filter_shape_inference(
      {1, filter_height, filter_width, output_depth});
  RuntimeShape bias_shape_inference({1, 1, 1, output_depth});
  const int input_buffer_size = input_shape_inference.FlatSize();
  const int filter_buffer_size = filter_shape_inference.FlatSize();
  std::vector<float> input_data(input_buffer_size);
  std::vector<float> filter_data(filter_buffer_size);
  std::vector<float> bias_data(output_depth);
  const float input_amplitude = 1.f;
  const float filter_amplitude = 1.f;
  const float bias_amplitude =
      filter_width * filter_height * input_amplitude * filter_amplitude;
  FillRandom(&input_data, -input_amplitude, input_amplitude);
  FillRandom(&filter_data, -filter_amplitude, filter_amplitude);
  FillRandom(&bias_data, -bias_amplitude, bias_amplitude);
  DepthwiseParams op_params;
  op_params.padding_type = PaddingType::kSame;
  op_params.padding_values.width = pad_width;
  op_params.padding_values.height = pad_height;
  op_params.stride_width = stride;
  op_params.stride_height = stride;
  op_params.dilation_width_factor = dilation_width_factor;
  op_params.dilation_height_factor = dilation_height_factor;
  op_params.depth_multiplier = depth_multiplier;
  op_params.float_activation_min = output_activation_min;
  op_params.float_activation_max = output_activation_max;
  TestOneDepthwiseConv(op_params, input_shape_inference, input_data.data(),
                       filter_shape_inference, filter_data.data(),
                       bias_shape_inference, bias_data.data(),
                       output_shape_inference);
  return true;
}
bool TryTestOneDepthwiseConv ( ) { const int batch = UniformRandomInt ( 1 , 2 ) ; const int input_depth = ExponentialRandomPositiveInt ( 0.9f , 6 , 50 ) ; const int input_width = ExponentialRandomPositiveInt ( 0.9f , 20 , 200 ) ; const int input_height = ExponentialRandomPositiveInt ( 0.9f , 20 , 200 ) ; const int filter_width = ExponentialRandomPositiveInt ( 0.9f , 4 , 10 ) ; const int filter_height = ExponentialRandomPositiveInt ( 0.9f , 4 , 10 ) ; const int depth_multiplier = ExponentialRandomPositiveInt ( 0.8f , 6 , 50 ) ; const int stride = ExponentialRandomPositiveInt ( 0.9f , 3 , 8 ) ; const int output_depth = input_depth * depth_multiplier ; const int dilation_width_factor = RandomElement ( std :: vector < int > ( { 1 , 2 , 4 } ) ) ; const int dilation_height_factor = RandomElement ( std :: vector < int > ( { 1 , 2 , 4 } ) ) ; float output_activation_min , output_activation_max ; FusedActivationFunctionType ac = RandomElement ( std :: vector < FusedActivationFunctionType > ( { FusedActivationFunctionType :: kNone , FusedActivationFunctionType :: kRelu , FusedActivationFunctionType :: kRelu1 , FusedActivationFunctionType :: kRelu6 } ) ) ; GetActivationMinMax ( ac , & output_activation_min , & output_activation_max ) ; const int kMaxSupportedOutputDepth = 1024 ; if ( output_depth > kMaxSupportedOutputDepth ) { return false ; } RuntimeShape input_shape_inference ( { batch , input_height , input_width , input_depth } ) ; RuntimeShape output_shape_inference ; int pad_width , pad_height ; const auto padding_type = UniformRandomInt ( 0 , 1 ) ? PaddingType :: kSame : PaddingType :: kValid ; if ( ! ComputeConvSizes ( input_shape_inference , output_depth , filter_width , filter_height , stride , dilation_width_factor , dilation_height_factor , padding_type , & output_shape_inference , & pad_width , & pad_height ) ) { return false ; } RuntimeShape filter_shape_inference ( { 1 , filter_height , filter_width , output_depth } ) ; RuntimeShape bias_shape_inference ( { 1 , 1 , 1 , output_depth } ) ; const int input_buffer_size = input_shape_inference . FlatSize ( ) ; const int filter_buffer_size = filter_shape_inference . FlatSize ( ) ; std :: vector < float > input_data ( input_buffer_size ) ; std :: vector < float > filter_data ( filter_buffer_size ) ; std :: vector < float > bias_data ( output_depth ) ; const float input_amplitude = 1.f ; const float filter_amplitude = 1.f ; const float bias_amplitude = filter_width * filter_height * input_amplitude * filter_amplitude ; FillRandom ( & input_data , - input_amplitude , input_amplitude ) ; FillRandom ( & filter_data , - filter_amplitude , filter_amplitude ) ; FillRandom ( & bias_data , - bias_amplitude , bias_amplitude ) ; DepthwiseParams op_params ; op_params . padding_type = PaddingType :: kSame ; op_params . padding_values . width = pad_width ; op_params . padding_values . height = pad_height ; op_params . stride_width = stride ; op_params . stride_height = stride ; op_params . dilation_width_factor = dilation_width_factor ; op_params . dilation_height_factor = dilation_height_factor ; op_params . depth_multiplier = depth_multiplier ; op_params . float_activation_min = output_activation_min ; op_params . float_activation_max = output_activation_max ; TestOneDepthwiseConv ( op_params , input_shape_inference , input_data . data ( ) , filter_shape_inference , filter_data . data ( ) , bias_shape_inference , bias_data . data ( ) , output_shape_inference ) ; return true ; }
This function picks some random DepthwiseConv params, which may or may not
be legal.
This function picks some random DepthwiseConv params which may or may not be legal .
// We have to pick a lot of positive values, where we are particularly // interested in small values because they are most likely to be special // cases in optimized implementations, and secondarily because they allow // tests to run fast, which means we can run more tests and get more // coverage. // The optimized DepthwiseConv implementation currently uses a fixed-size // accumulator buffer on the stack, with that size. This currently means // that it does not support larger output depths. It CHECK's for it, // so it's safe in the sense that if a larger output depth was encountered, // it would explicitly fail. We just need to adjust our testing to that // constraint.