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 <memory>
17 #include <string>
18 #include <utility>
19 #include <vector>
20
21 #include "absl/strings/match.h"
22 #include "absl/strings/str_join.h"
23 #include "absl/strings/string_view.h"
24 #include "tensorflow/compiler/aot/codegen.h"
25 #include "tensorflow/compiler/aot/compile.h"
26 #include "tensorflow/compiler/aot/flags.h"
27 #include "tensorflow/compiler/tf2xla/tf2xla.pb.h"
28 #include "tensorflow/compiler/tf2xla/tf2xla_util.h"
29 #include "tensorflow/compiler/xla/debug_options_flags.h"
30 #include "tensorflow/compiler/xla/service/compiler.h"
31 #include "tensorflow/core/framework/function.h"
32 #include "tensorflow/core/framework/graph.pb.h"
33 #include "tensorflow/core/framework/tensor_shape.h"
34 #include "tensorflow/core/framework/types.h"
35 #include "tensorflow/core/graph/graph.h"
36 #include "tensorflow/core/graph/tensor_id.h"
37 #include "tensorflow/core/lib/core/errors.h"
38 #include "tensorflow/core/lib/strings/numbers.h"
39 #include "tensorflow/core/platform/env.h"
40 #include "tensorflow/core/platform/init_main.h"
41 #include "tensorflow/core/platform/logging.h"
42 #include "tensorflow/core/platform/protobuf.h"
43 #include "tensorflow/core/util/command_line_flags.h"
44
45 namespace tensorflow {
46 namespace tfcompile {
47
48 const char kUsageHeader[] =
49 "tfcompile performs ahead-of-time compilation of a TensorFlow graph,\n"
50 "resulting in an object file compiled for your target architecture, and a\n"
51 "header file that gives access to the functionality in the object file.\n"
52 "A typical invocation looks like this:\n"
53 "\n"
54 " $ tfcompile --graph=mygraph.pb --config=myfile.pbtxt "
55 "--cpp_class=\"mynamespace::MyComputation\"\n"
56 "\n";
57
58 } // end namespace tfcompile
59 } // end namespace tensorflow
60
main(int argc,char ** argv)61 int main(int argc, char** argv) {
62 tensorflow::tfcompile::MainFlags flags;
63 flags.target_triple = "x86_64-pc-linux";
64 flags.out_function_object = "out_model.o";
65 flags.out_metadata_object = "out_helper.o";
66 flags.out_header = "out.h";
67 flags.entry_point = "entry";
68 flags.debug_info_path_begin_marker = "";
69
70 // Note that tfcompile.bzl's tf_library macro sets fast math flags as that is
71 // generally the preferred case.
72 std::vector<tensorflow::Flag> flag_list;
73 AppendMainFlags(&flag_list, &flags);
74 xla::AppendDebugOptionsFlags(&flag_list);
75
76 tensorflow::string usage = tensorflow::tfcompile::kUsageHeader;
77 usage += tensorflow::Flags::Usage(argv[0], flag_list);
78 if (argc > 1 && absl::string_view(argv[1]) == "--help") {
79 std::cerr << usage << "\n";
80 return 0;
81 }
82 bool parsed_flags_ok = tensorflow::Flags::Parse(&argc, argv, flag_list);
83 QCHECK(parsed_flags_ok) << "\n" << usage;
84
85 tensorflow::port::InitMain(usage.c_str(), &argc, &argv);
86 QCHECK(argc == 1) << "\nERROR: This command does not take any arguments "
87 "other than flags. See --help.\n\n";
88 tensorflow::Status status = tensorflow::tfcompile::Main(flags);
89 if (status.code() == tensorflow::error::INVALID_ARGUMENT) {
90 std::cerr << "INVALID ARGUMENTS: " << status.error_message() << "\n\n";
91 return 1;
92 } else {
93 TF_QCHECK_OK(status);
94 }
95 return 0;
96 }
97