| 462 | |
| 463 | template <typename T> |
| 464 | int AddTensor(TensorData t, std::initializer_list<T> data, |
| 465 | bool is_variable = false) { |
| 466 | int id = tensors_.size(); |
| 467 | |
| 468 | // This is slightly different depending on whether we are adding a |
| 469 | // quantized or a regular tensor. |
| 470 | bool is_quantized = (t.min != 0 || t.max != 0 || t.scale != 0); |
| 471 | |
| 472 | flatbuffers::Offset<QuantizationParameters> q_params = 0; |
| 473 | |
| 474 | if (is_quantized) { |
| 475 | if (t.min != 0 || t.max != 0) { |
| 476 | if (t.type == TensorType_UINT8) { |
| 477 | std::tie(t.scale, t.zero_point) = |
| 478 | QuantizationParams<uint8_t>(t.min, t.max); |
| 479 | } else if (t.type == TensorType_INT8) { |
| 480 | std::tie(t.scale, t.zero_point) = |
| 481 | QuantizationParams<int8_t>(t.min, t.max); |
| 482 | } else if (t.type == TensorType_INT32) { |
| 483 | std::tie(t.scale, t.zero_point) = |
| 484 | QuantizationParams<int32_t>(t.min, t.max); |
| 485 | } else if (t.type == TensorType_INT16) { |
| 486 | std::tie(t.scale, t.zero_point) = |
| 487 | QuantizationParams<int16_t>(t.min, t.max); |
| 488 | } else { |
| 489 | LOG(FATAL) << "No support for the requested quantized type"; |
| 490 | } |
| 491 | t.min = 0; |
| 492 | t.max = 0; |
| 493 | } |
| 494 | |
| 495 | q_params = CreateQuantizationParameters( |
| 496 | builder_, /*min=*/0, /*max=*/0, |
| 497 | builder_.CreateVector<float>({t.scale}), |
| 498 | builder_.CreateVector<int64_t>({t.zero_point})); |
| 499 | } |
| 500 | |
| 501 | int buffer_id = 0; |
| 502 | if (data.size()) { |
| 503 | // Initialize buffers list with empty buffer to allow for non-const |
| 504 | // tensors. |
| 505 | if (buffers_.empty()) { |
| 506 | buffers_.push_back(CreateBuffer(builder_, builder_.CreateVector({}))); |
| 507 | } |
| 508 | |
| 509 | // Add data as a Buffer to buffers list. |
| 510 | buffer_id = buffers_.size(); |
| 511 | auto data_buffer = |
| 512 | builder_.CreateVector(reinterpret_cast<const uint8_t*>(data.begin()), |
| 513 | sizeof(T) * data.size()); |
| 514 | buffers_.push_back(CreateBuffer(builder_, data_buffer)); |
| 515 | } |
| 516 | |
| 517 | tensors_.push_back(CreateTensor(builder_, |
| 518 | builder_.CreateVector<int>(t.shape), t.type, |
| 519 | /*buffer=*/buffer_id, |
| 520 | /*name=*/0, q_params, is_variable)); |
| 521 |
nothing calls this directly
no test coverage detected