tensorflow/compiler/mlir/lite/quantization/lite/quantize_weights_test.cc
// Finds the match of the quantized tensor from the possible tensors. Each
// possible tensors can be used only once. It checks shape and name if the
// tensor is quantized and also checks buffer conetens and tensor type if not
// quantized. For the quantized case, tensor type and quantizaction params are
// expected to be checked in the test body with the match.
Finds the match of the quantized tensor from the possible tensors. Each
possible tensors can be used only once. It checks shape and name if the
tensor is quantized and also checks buffer conetens and tensor type if not
quantized. For the quantized case, tensor type and quantizaction params are
expected to be checked in the test body with the match.
Finds the match of the quantized tensor from the possible tensors . Each possible tensors can be used only once . It checks shape and name if the tensor is quantized and also checks buffer conetens and tensor type if not quantized . For the quantized case tensor type and quantizaction params are expected to be checked in the test body with the match .
const Tensor* FindMatchingExpectedTensor(
    const Model* quantized_model, const Model* expected_model,
    const Tensor* quantized_tensor,
    const flatbuffers::Vector<flatbuffers::Offset<Tensor>>* possible_tensors,
    std::vector<int>& used_tensors, bool quantized = false) {
  std::unique_ptr<ModelT> quant_model =
      CreateMutableModelFromFile(quantized_model);
  std::unique_ptr<ModelT> float_model =
      CreateMutableModelFromFile(expected_model);
  for (int i = 0; i < possible_tensors->size(); i++) {
    auto it = std::find(used_tensors.begin(), used_tensors.end(), i);
    if (it != used_tensors.end()) continue;
    const Tensor* float_tensor = possible_tensors->Get(i);
    if (ExpectEqualTensor(quantized_tensor, float_tensor)) {
      if (quantized && quantized_tensor->name()->str().find("weights")) {
        used_tensors.push_back(i);
        return float_tensor;
      } else {
        const std::vector<uint8_t> quantized_buffer =
            quant_model->buffers[quantized_tensor->buffer()].get()->data;
        const std::vector<uint8_t> float_buffer =
            float_model->buffers[float_tensor->buffer()].get()->data;
        if ((quantized_buffer == float_buffer) &&
            (quantized_tensor->type() == float_tensor->type())) {
          used_tensors.push_back(i);
          return float_tensor;
        }
      }
    }
  }
  return nullptr;
}
const Tensor * FindMatchingExpectedTensor ( const Model * quantized_model , const Model * expected_model , const Tensor * quantized_tensor , const flatbuffers :: Vector < flatbuffers :: Offset < Tensor > > * possible_tensors , std :: vector < int > & used_tensors , bool quantized = false ) { std :: unique_ptr < ModelT > quant_model = CreateMutableModelFromFile ( quantized_model ) ; std :: unique_ptr < ModelT > float_model = CreateMutableModelFromFile ( expected_model ) ; for ( int i = 0 ; i < possible_tensors -> size ( ) ; i ++ ) { auto it = std :: find ( used_tensors . begin ( ) , used_tensors . end ( ) , i ) ; if ( it != used_tensors . end ( ) ) continue ; const Tensor * float_tensor = possible_tensors -> Get ( i ) ; if ( ExpectEqualTensor ( quantized_tensor , float_tensor ) ) { if ( quantized && quantized_tensor -> name ( ) -> str ( ) . find ( " " ) ) { used_tensors . push_back ( i ) ; return float_tensor ; } else { const std :: vector < uint8_t > quantized_buffer = quant_model -> buffers [ quantized_tensor -> buffer ( ) ] . get ( ) -> data ; const std :: vector < uint8_t > float_buffer = float_model -> buffers [ float_tensor -> buffer ( ) ] . get ( ) -> data ; if ( ( quantized_buffer == float_buffer ) && ( quantized_tensor -> type ( ) == float_tensor -> type ( ) ) ) { used_tensors . push_back ( i ) ; return float_tensor ; } } } } return nullptr ; }
Finds the match of the quantized tensor from the possible tensors.
Finds the match of the quantized tensor from the possible tensors .
// Skip if the tensor is already used for match. // If tensor is quantized, data type and buffer contents can be // different between float and quantized tensors. So do those tests // separately in the test body without checking them here. // Otherwise, do additional checks for data type and buffer contents.