Home | History | Annotate | Download | only in toco
      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 #include <cstdio>
     16 #include <memory>
     17 #include <string>
     18 
     19 #include "absl/strings/string_view.h"
     20 #include "tensorflow/contrib/lite/toco/model.h"
     21 #include "tensorflow/contrib/lite/toco/model_cmdline_flags.h"
     22 #include "tensorflow/contrib/lite/toco/model_flags.pb.h"
     23 #include "tensorflow/contrib/lite/toco/toco_cmdline_flags.h"
     24 #include "tensorflow/contrib/lite/toco/toco_flags.pb.h"
     25 #include "tensorflow/contrib/lite/toco/toco_port.h"
     26 #include "tensorflow/contrib/lite/toco/toco_tooling.h"
     27 #include "tensorflow/contrib/lite/toco/toco_types.h"
     28 #include "tensorflow/core/platform/logging.h"
     29 
     30 #ifndef CHECK_OK
     31 #define CHECK_OK(val) CHECK_EQ((val).ok(), true)
     32 #define QCHECK_OK(val) QCHECK_EQ((val).ok(), true)
     33 #endif
     34 
     35 namespace toco {
     36 namespace {
     37 
     38 #define QCHECK_REQUIRE_TOCO_FLAG(arg) \
     39   QCHECK(parsed_toco_flags.arg.specified()) << "Missing required flag: " #arg;
     40 
     41 void CheckFilePermissions(const ParsedTocoFlags& parsed_toco_flags,
     42                           const ParsedModelFlags& parsed_model_flags,
     43                           const TocoFlags& toco_flags) {
     44   port::CheckInitGoogleIsDone("InitGoogle is not done yet");
     45 
     46   QCHECK_REQUIRE_TOCO_FLAG(input_file)
     47   QCHECK_OK(port::file::Exists(parsed_toco_flags.input_file.value(),
     48                                port::file::Defaults()))
     49       << "Specified input_file does not exist: "
     50       << parsed_toco_flags.input_file.value();
     51   QCHECK_OK(port::file::Readable(parsed_toco_flags.input_file.value(),
     52                                  port::file::Defaults()))
     53       << "Specified input_file exists, but is not readable: "
     54       << parsed_toco_flags.input_file.value();
     55 
     56   QCHECK_REQUIRE_TOCO_FLAG(output_file);
     57   QCHECK_OK(port::file::Writable(parsed_toco_flags.output_file.value()))
     58       << "parsed_toco_flags.input_file.value() output_file is not writable: "
     59       << parsed_toco_flags.output_file.value();
     60 }
     61 
     62 void ToolMain(const ParsedTocoFlags& parsed_toco_flags,
     63               const ParsedModelFlags& parsed_model_flags) {
     64   ModelFlags model_flags;
     65   ReadModelFlagsFromCommandLineFlags(parsed_model_flags, &model_flags);
     66 
     67   TocoFlags toco_flags;
     68   ReadTocoFlagsFromCommandLineFlags(parsed_toco_flags, &toco_flags);
     69 
     70   CheckFilePermissions(parsed_toco_flags, parsed_model_flags, toco_flags);
     71 
     72   string input_file_contents;
     73   CHECK_OK(port::file::GetContents(parsed_toco_flags.input_file.value(),
     74                                    &input_file_contents,
     75                                    port::file::Defaults()));
     76   std::unique_ptr<Model> model =
     77       Import(toco_flags, model_flags, input_file_contents);
     78   Transform(toco_flags, model.get());
     79   string output_file_contents;
     80   Export(toco_flags, *model, toco_flags.allow_custom_ops(),
     81          &output_file_contents);
     82   CHECK_OK(port::file::SetContents(parsed_toco_flags.output_file.value(),
     83                                    output_file_contents,
     84                                    port::file::Defaults()));
     85 }
     86 
     87 }  // namespace
     88 }  // namespace toco
     89 
     90 int main(int argc, char** argv) {
     91   toco::string msg;
     92   toco::ParsedTocoFlags parsed_toco_flags;
     93   toco::ParsedModelFlags parsed_model_flags;
     94 
     95   // If no args were specified, give a help string to be helpful.
     96   int* effective_argc = &argc;
     97   char** effective_argv = argv;
     98   if (argc == 1) {
     99     // No arguments, so manufacture help argv.
    100     static int dummy_argc = 2;
    101     static char* dummy_argv[] = {argv[0], const_cast<char*>("--help")};
    102     effective_argc = &dummy_argc;
    103     effective_argv = dummy_argv;
    104   }
    105 
    106   // Parse toco flags and command flags in sequence, each one strips off args,
    107   // giving InitGoogle a chance to handle all remaining arguments.
    108   bool toco_success = toco::ParseTocoFlagsFromCommandLineFlags(
    109       effective_argc, effective_argv, &msg, &parsed_toco_flags);
    110   bool model_success = toco::ParseModelFlagsFromCommandLineFlags(
    111       effective_argc, effective_argv, &msg, &parsed_model_flags);
    112   if (!toco_success || !model_success || !msg.empty()) {
    113     fprintf(stderr, "%s", msg.c_str());
    114     fflush(stderr);
    115     return 1;
    116   }
    117   toco::port::InitGoogle(argv[0], effective_argc, &effective_argv, true);
    118   toco::ToolMain(parsed_toco_flags, parsed_model_flags);
    119 }
    120