tensorflow/compiler/mlir/lite/quantization/tools/op_quant_spec_getters_gen.cc
// The function below has a non-constant reference as that is required by LLVM's
// TableGenMain.
// NOLINTNEXTLINE
The function below has a non-constant reference as that is required by LLVM's
TableGenMain.
NOLINTNEXTLINE
The function below has a non - constant reference as that is required by LLVM ' s TableGenMain . NOLINTNEXTLINE
static bool OpQuantSpecWriter(raw_ostream &os, RecordKeeper &records) {
  llvm::Regex acc_uniform_trait_regex{"AccumulatorUniformScale<([0-9]*),"};
  llvm::Regex coeff_index_trait_regex{"AffineOpCoefficient<(-?[0-9]*),"};
  llvm::Regex fixed_uniform_trait_regex{
      "FixedResultUniformScale<([0-9]+).*(true|false)>"};
  emitSourceFileHeader("Generated Ops Quant Spec Getters", os);
  std::vector<Record *> defs = records.getAllDerivedDefinitions("Op");
  llvm::sort(defs, LessRecord());
  OUT(0) << "static std::unique_ptr<quant::OpQuantSpec> "
            "GetOpQuantSpec(mlir::Operation *op) {\n";
  OUT(2) << "if (auto lstm_op = llvm::dyn_cast<TFL::LSTMOp>(op)) {\n";
  OUT(4) << "return GetLstmOpQuantSpec<TFL::LSTMOp>(lstm_op);\n";
  OUT(2) << "} else if (auto lstm_op = "
            "llvm::dyn_cast<TFL::UnidirectionalSequenceLSTMOp>(op)) {\n";
  OUT(4) << "return "
            "GetLstmOpQuantSpec<TFL::UnidirectionalSequenceLSTMOp>(lstm_op);\n";
  OUT(2) << "}\n";
  OUT(2) << "auto spec = absl::make_unique<quant::OpQuantSpec>();\n";
  llvm::SmallVector<llvm::StringRef, 3> matches;
  for (auto *def : defs) {
    Operator op(def);
    for (const auto t : op.getTraits()) {
      if (auto opTrait = llvm::dyn_cast<mlir::tblgen::NativeTrait>(&t)) {
        auto trait_str = opTrait->getFullyQualifiedTraitName();
        if (!llvm::StringRef{trait_str}.consume_front(
                "::mlir::OpTrait::quant::"))
          continue;
        OUT(2) << "if (auto tfl = llvm::dyn_cast<" << op.getQualCppClassName()
               << ">(op)) {\n";
        if (fixed_uniform_trait_regex.match(trait_str, &matches)) {
          OUT(4) << "for (int i = 0, e = op->getNumResults(); i != e; ++i)\n";
          OUT(6) << "spec->restricted_output_params[std::make_pair("
                 << matches[1] << ", " << matches[2]
                 << ")].push_back(tfl.::mlir::OpTrait::quant::" << trait_str
                 << "<" << op.getQualCppClassName()
                 << ">::GetResultQuantizedType(i));\n";
          matches.clear();
        }
        if (acc_uniform_trait_regex.match(trait_str, &matches)) {
          OUT(4) << "spec->biases_params.emplace(std::make_pair(" << matches[1]
                 << ", std::make_pair(tfl.GetAllNonBiasOperands(),"
                 << "quant::GetUniformQuantizedTypeForBias)));\n";
          matches.clear();
        }
        if (coeff_index_trait_regex.match(trait_str, &matches)) {
          OUT(4) << "spec->coeff_op_quant_dim[tfl.GetCoefficientOperandIndex()"
                 << "] = tfl.GetQuantizationDim();\n";
          matches.clear();
        }
        OUT(2) << "}\n";
      }
    }
  }
  OUT(2) << "return spec;\n";
  OUT(0) << "}\n";
  return false;
}
static bool OpQuantSpecWriter ( raw_ostream & os , RecordKeeper & records ) { llvm :: Regex acc_uniform_trait_regex { " " } ; llvm :: Regex coeff_index_trait_regex { " " } ; llvm :: Regex fixed_uniform_trait_regex { " " } ; emitSourceFileHeader ( " " , os ) ; std :: vector < Record * > defs = records . getAllDerivedDefinitions ( " " ) ; llvm :: sort ( defs , LessRecord ( ) ) ; OUT ( 0 ) << " " " \n " ; OUT ( 2 ) << " \n " ; OUT ( 4 ) << " \n " ; OUT ( 2 ) << " " " \n " ; OUT ( 4 ) << " " " \n " ; OUT ( 2 ) << " \n " ; OUT ( 2 ) << " \n " ; llvm :: SmallVector < llvm :: StringRef , 3 > matches ; for ( auto * def : defs ) { Operator op ( def ) ; for ( const auto t : op . getTraits ( ) ) { if ( auto opTrait = llvm :: dyn_cast < mlir :: tblgen :: NativeTrait > ( & t ) ) { auto trait_str = opTrait -> getFullyQualifiedTraitName ( ) ; if ( ! llvm :: StringRef { trait_str } . consume_front ( " " ) ) continue ; OUT ( 2 ) << " " << op . getQualCppClassName ( ) << " \n " ; if ( fixed_uniform_trait_regex . match ( trait_str , & matches ) ) { OUT ( 4 ) << " \n " ; OUT ( 6 ) << " " << matches [ 1 ] << " " << matches [ 2 ] << " " << trait_str << " " << op . getQualCppClassName ( ) << " \n " ; matches . clear ( ) ; } if ( acc_uniform_trait_regex . match ( trait_str , & matches ) ) { OUT ( 4 ) << " " << matches [ 1 ] << " " << " \n " ; matches . clear ( ) ; } if ( coeff_index_trait_regex . match ( trait_str , & matches ) ) { OUT ( 4 ) << " " << " \n " ; matches . clear ( ) ; } OUT ( 2 ) << " \n " ; } } } OUT ( 2 ) << " \n " ; OUT ( 0 ) << " \n " ; return false ; }
The function below has a non-constant reference as that is required by LLVM's
TableGenMain.
The function below has a non - constant reference as that is required by LLVM ' s TableGenMain .
// Retrieve all the definitions derived from Op definition and sort by record // name. // TODO(b/176258587): Move to OpTrait if this should be generalized. // Add special handling for LSTM. // There is a "FixedResultUniformScale" trait, set the type for result. // There is a "AccumulatorUniformScale" trait, set the type for bias. // There is a "QuantChannelDim" trait, set the quantization dimension.