Home | History | Annotate | Download | only in tools
      1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #include <cassert>
     17 #include <fstream>
     18 #include <map>
     19 #include <sstream>
     20 #include <string>
     21 #include <vector>
     22 
     23 #include "absl/strings/strip.h"
     24 #include "tensorflow/contrib/lite/tools/gen_op_registration.h"
     25 #include "tensorflow/core/platform/init_main.h"
     26 #include "tensorflow/core/util/command_line_flags.h"
     27 
     28 const char kInputModelFlag[] = "input_model";
     29 const char kOutputRegistrationFlag[] = "output_registration";
     30 const char kTfLitePathFlag[] = "tflite_path";
     31 
     32 using tensorflow::Flag;
     33 using tensorflow::Flags;
     34 using tensorflow::string;
     35 
     36 void ParseFlagAndInit(int argc, char** argv, string* input_model,
     37                       string* output_registration, string* tflite_path) {
     38   std::vector<tensorflow::Flag> flag_list = {
     39       Flag(kInputModelFlag, input_model, "path to the tflite model"),
     40       Flag(kOutputRegistrationFlag, output_registration,
     41            "filename for generated registration code"),
     42       Flag(kTfLitePathFlag, tflite_path, "Path to tensorflow lite dir"),
     43   };
     44 
     45   Flags::Parse(&argc, argv, flag_list);
     46   tensorflow::port::InitMain(argv[0], &argc, &argv);
     47 }
     48 
     49 namespace {
     50 
     51 void GenerateFileContent(const std::string& tflite_path,
     52                          const std::string& filename,
     53                          const std::vector<string>& builtin_ops,
     54                          const std::vector<string>& custom_ops) {
     55   std::ofstream fout(filename);
     56 
     57   fout << "#include \"" << tflite_path << "/model.h\"\n";
     58   fout << "#include \"" << tflite_path << "/tools/mutable_op_resolver.h\"\n";
     59 
     60   fout << "namespace tflite {\n";
     61   fout << "namespace ops {\n";
     62   if (!builtin_ops.empty()) {
     63     fout << "namespace builtin {\n";
     64     fout << "// Forward-declarations for the builtin ops.\n";
     65     for (const auto& op : builtin_ops) {
     66       fout << "TfLiteRegistration* Register_" << op << "();\n";
     67     }
     68     fout << "}  // namespace builtin\n";
     69   }
     70 
     71   if (!custom_ops.empty()) {
     72     fout << "namespace custom {\n";
     73     fout << "// Forward-declarations for the custom ops.\n";
     74     for (const auto& op : custom_ops) {
     75       fout << "TfLiteRegistration* Register_"
     76            << ::tflite::NormalizeCustomOpName(op) << "();\n";
     77     }
     78     fout << "}  // namespace custom\n";
     79   }
     80   fout << "}  // namespace ops\n";
     81   fout << "}  // namespace tflite\n";
     82 
     83   fout << "void RegisterSelectedOps(::tflite::MutableOpResolver* resolver) {\n";
     84   for (const auto& op : builtin_ops) {
     85     fout << "  resolver->AddBuiltin(::tflite::BuiltinOperator_" << op
     86          << ", ::tflite::ops::builtin::Register_" << op << "());\n";
     87   }
     88   for (const auto& op : custom_ops) {
     89     fout << "  resolver->AddCustom(\"" << op
     90          << "\", ::tflite::ops::custom::Register_"
     91          << ::tflite::NormalizeCustomOpName(op) << "());\n";
     92   }
     93   fout << "}\n";
     94   fout.close();
     95 }
     96 }  // namespace
     97 
     98 int main(int argc, char** argv) {
     99   string input_model;
    100   string output_registration;
    101   string tflite_path;
    102   ParseFlagAndInit(argc, argv, &input_model, &output_registration,
    103                    &tflite_path);
    104 
    105   std::vector<string> builtin_ops;
    106   std::vector<string> custom_ops;
    107   std::ifstream fin(input_model);
    108   std::stringstream content;
    109   content << fin.rdbuf();
    110   // Need to store content data first, otherwise, it won't work in bazel.
    111   string content_str = content.str();
    112   const ::tflite::Model* model = ::tflite::GetModel(content_str.data());
    113   ::tflite::ReadOpsFromModel(model, &builtin_ops, &custom_ops);
    114   GenerateFileContent(tflite_path, output_registration, builtin_ops,
    115                       custom_ops);
    116   return 0;
    117 }
    118