MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / QuantizeBiases

Function QuantizeBiases

tensorflow/lite/tools/optimize/quantize_model.cc:586–637  ·  view source on GitHub ↗

Quantize bias.

Source from the content-addressed store, hash-verified

584
585// Quantize bias.
586TfLiteStatus QuantizeBiases(ModelT* model, ErrorReporter* error_reporter) {
587 for (size_t subgraph_idx = 0; subgraph_idx < model->subgraphs.size();
588 subgraph_idx++) {
589 SubGraphT* subgraph = model->subgraphs.at(subgraph_idx).get();
590 for (size_t op_idx = 0; op_idx < subgraph->operators.size(); op_idx++) {
591 OperatorT* op = subgraph->operators[op_idx].get();
592 const BuiltinOperator op_code =
593 model->operator_codes[op->opcode_index]->builtin_code;
594 operator_property::OperatorProperty property =
595 operator_property::GetOperatorProperty(op_code);
596 if (!property.quantizable) {
597 continue;
598 }
599 for (const int bias_idx : property.biases) {
600 if (bias_idx >= op->inputs.size()) {
601 error_reporter->Report(
602 "Required input index %d is larger than the input length of "
603 "op %s at index %d in subgraph %d",
604 bias_idx, op->inputs.size(), EnumNameBuiltinOperator(op_code),
605 op_idx, subgraph_idx);
606 return kTfLiteError;
607 }
608 // Quantize if it is not quantized already as the
609 // output of another op or input of another op.
610 TensorT* bias_tensor = subgraph->tensors[op->inputs[bias_idx]].get();
611 if (!utils::QuantizationParametersExist(bias_tensor)) {
612 if (utils::HasBuffer(model, subgraph, op->inputs[bias_idx])) {
613 if (property.inputs.size() != 2) {
614 error_reporter->Report(
615 "Expect the input length of "
616 "op %s at index %d in subgraph %d to be 2",
617 bias_idx, op->inputs.size(), EnumNameBuiltinOperator(op_code),
618 op_idx, subgraph_idx);
619 return kTfLiteError;
620 }
621 TensorT* input_tensor =
622 subgraph->tensors[op->inputs[property.inputs[0].first]].get();
623 TensorT* weight_tensor =
624 subgraph->tensors[op->inputs[property.inputs[1].first]].get();
625 operator_property::TensorProperty weight_property =
626 property.inputs[1].second;
627 TF_LITE_ENSURE_STATUS(
628 QuantizeBias(model, input_tensor, weight_tensor, bias_tensor,
629 weight_property.per_axis,
630 weight_property.per_axis_index, error_reporter));
631 }
632 }
633 }
634 }
635 }
636 return kTfLiteOk;
637}
638
639} // namespace
640

Callers 1

QuantizeModelFunction · 0.85

Calls 9

GetOperatorPropertyFunction · 0.85
EnumNameBuiltinOperatorFunction · 0.85
HasBufferFunction · 0.85
QuantizeBiasFunction · 0.85
sizeMethod · 0.45
getMethod · 0.45
atMethod · 0.45
ReportMethod · 0.45

Tested by

no test coverage detected