<commit_before>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include "absl/strings/strip.h"
#include "tensorflow/lite/tools/command_line_flags.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

const char kInputModelFlag[] = "input_model";
const char kNamespace[] = "namespace";
const char kOutputRegistrationFlag[] = "output_registration";
const char kTfLitePathFlag[] = "tflite_path";
const char kForMicro[] = "for_micro";

void ParseFlagAndInit(int* argc, char** argv, string* input_model,
                      string* output_registration, string* tflite_path,
                      string* namespace_flag, bool* for_micro) {
  std::vector<tflite::Flag> flag_list = {
      tflite::Flag::CreateFlag(kInputModelFlag, input_model,
                               "path to the tflite model"),
      tflite::Flag::CreateFlag(kOutputRegistrationFlag, output_registration,
                               "filename for generated registration code"),
      tflite::Flag::CreateFlag(kTfLitePathFlag, tflite_path,
                               "Path to tensorflow lite dir"),
      tflite::Flag::CreateFlag(
          kNamespace, namespace_flag,
          "Namespace in which to put RegisterSelectedOps."),
      tflite::Flag::CreateFlag(
          kForMicro, for_micro,
          "By default this script generate TFL registration file, but can "
          "also generate TFLM files when this flag is set to true"),
  };

  tflite::Flags::Parse(argc, const_cast<const char**>(argv), flag_list);
}

namespace {

void GenerateFileContent(const std::string& tflite_path,
                         const std::string& filename,
                         const std::string& namespace_flag,
                         const tflite::RegisteredOpMap& builtin_ops,
                         const tflite::RegisteredOpMap& custom_ops,
                         const bool for_micro) {
  std::ofstream fout(filename);

  if (for_micro) {
    if (!builtin_ops.empty()) {
      fout << "#include \"" << tflite_path
           << "/experimental/micro/kernels/micro_ops.h\"\n";
    }
    fout << "#include \"" << tflite_path
         << "/experimental/micro/micro_mutable_op_resolver.h\"\n";
  } else {
    if (!builtin_ops.empty()) {
      fout << "#include \"" << tflite_path
           << "/kernels/builtin_op_kernels.h\"\n";
    }
    fout << "#include \"" << tflite_path << "/model.h\"\n";
    fout << "#include \"" << tflite_path << "/op_resolver.h\"\n";
  }

  if (!custom_ops.empty()) {
    fout << "namespace tflite {\n";
    fout << "namespace ops {\n";
    fout << "namespace custom {\n";<commit_msg>TFL selective registration: Fix include path for micro related code.<commit_after>/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include "absl/strings/strip.h"
#include "tensorflow/lite/tools/command_line_flags.h"
#include "tensorflow/lite/tools/gen_op_registration.h"

const char kInputModelFlag[] = "input_model";
const char kNamespace[] = "namespace";
const char kOutputRegistrationFlag[] = "output_registration";
const char kTfLitePathFlag[] = "tflite_path";
const char kForMicro[] = "for_micro";

void ParseFlagAndInit(int* argc, char** argv, string* input_model,
                      string* output_registration, string* tflite_path,
                      string* namespace_flag, bool* for_micro) {
  std::vector<tflite::Flag> flag_list = {
      tflite::Flag::CreateFlag(kInputModelFlag, input_model,
                               "path to the tflite model"),
      tflite::Flag::CreateFlag(kOutputRegistrationFlag, output_registration,
                               "filename for generated registration code"),
      tflite::Flag::CreateFlag(kTfLitePathFlag, tflite_path,
                               "Path to tensorflow lite dir"),
      tflite::Flag::CreateFlag(
          kNamespace, namespace_flag,
          "Namespace in which to put RegisterSelectedOps."),
      tflite::Flag::CreateFlag(
          kForMicro, for_micro,
          "By default this script generate TFL registration file, but can "
          "also generate TFLM files when this flag is set to true"),
  };

  tflite::Flags::Parse(argc, const_cast<const char**>(argv), flag_list);
}

namespace {

void GenerateFileContent(const std::string& tflite_path,
                         const std::string& filename,
                         const std::string& namespace_flag,
                         const tflite::RegisteredOpMap& builtin_ops,
                         const tflite::RegisteredOpMap& custom_ops,
                         const bool for_micro) {
  std::ofstream fout(filename);

  if (for_micro) {
    if (!builtin_ops.empty()) {
      fout << "#include \"" << tflite_path << "/micro/kernels/micro_ops.h\"\n";
    }
    fout << "#include \"" << tflite_path
         << "/micro/micro_mutable_op_resolver.h\"\n";
  } else {
    if (!builtin_ops.empty()) {
      fout << "#include \"" << tflite_path
           << "/kernels/builtin_op_kernels.h\"\n";
    }
    fout << "#include \"" << tflite_path << "/model.h\"\n";
    fout << "#include \"" << tflite_path << "/op_resolver.h\"\n";
  }

  if (!custom_ops.empty()) {
    fout << "namespace tflite {\n";
    fout << "namespace ops {\n";
    fout << "namespace custom {\n";
{'fraction_non_alphanumeric': 0.13815987933634993, 'fraction_numerical': 0.0024132730015082957, 'mean_word_length': 3.0090689238210397, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2396278', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1877, 'n_words': 530}