| 359 | } |
| 360 | |
| 361 | TfLiteStatus InterpreterBuilder::ParseQuantization( |
| 362 | const QuantizationParameters* src_quantization, |
| 363 | TfLiteQuantization* quantization, const std::vector<int>& dims) { |
| 364 | quantization->type = kTfLiteNoQuantization; |
| 365 | if (!src_quantization || !src_quantization->scale() || |
| 366 | src_quantization->scale()->size() == 0) { |
| 367 | return kTfLiteOk; |
| 368 | } |
| 369 | if (!src_quantization->zero_point()) { |
| 370 | error_reporter_->Report( |
| 371 | "Quantization parameters has non-null scale but null zero_point."); |
| 372 | return kTfLiteError; |
| 373 | } |
| 374 | |
| 375 | // Ensure that the number of scales matches the number of zero_points. |
| 376 | if (src_quantization->scale()->size() != |
| 377 | src_quantization->zero_point()->size()) { |
| 378 | error_reporter_->Report( |
| 379 | "QuantizationParam has %d zero_point values and %d scale values. Must " |
| 380 | "have same number.", |
| 381 | src_quantization->zero_point()->size(), |
| 382 | src_quantization->scale()->size()); |
| 383 | return kTfLiteError; |
| 384 | } |
| 385 | |
| 386 | // Affine-quantization. |
| 387 | quantization->type = kTfLiteAffineQuantization; |
| 388 | const size_t num_scales = src_quantization->scale()->size(); |
| 389 | |
| 390 | // Ensure that the quantization dimension is valid. |
| 391 | if (src_quantization->quantized_dimension() < 0 || |
| 392 | (!dims.empty() && |
| 393 | src_quantization->quantized_dimension() >= dims.size())) { |
| 394 | error_reporter_->Report( |
| 395 | "quantized_dimension must be in range [0, %d). Was %d.", dims.size(), |
| 396 | src_quantization->quantized_dimension()); |
| 397 | return kTfLiteError; |
| 398 | } |
| 399 | |
| 400 | // Ensure that the number of scales is 1 for per-layer quantization, and |
| 401 | // matches number of quantization dimensions for per-axis quantization. |
| 402 | if (num_scales != 1 && |
| 403 | (!dims.empty() && |
| 404 | num_scales != dims[src_quantization->quantized_dimension()])) { |
| 405 | error_reporter_->Report( |
| 406 | "num_scales must be 1 for per-layer quantization, or %d for per-axis " |
| 407 | "quantization, but got %d.", |
| 408 | dims[src_quantization->quantized_dimension()], num_scales); |
| 409 | return kTfLiteError; |
| 410 | } |
| 411 | |
| 412 | auto* affine_quantization = reinterpret_cast<TfLiteAffineQuantization*>( |
| 413 | malloc(sizeof(TfLiteAffineQuantization))); |
| 414 | affine_quantization->scale = TfLiteFloatArrayCreate(num_scales); |
| 415 | affine_quantization->zero_point = TfLiteIntArrayCreate(num_scales); |
| 416 | for (size_t i = 0; i < num_scales; ++i) { |
| 417 | affine_quantization->scale->data[i] = src_quantization->scale()->Get(i); |
| 418 | affine_quantization->zero_point->data[i] = |
nothing calls this directly
no test coverage detected