forked from tensorflow/tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_op_gen_main.cc
More file actions
106 lines (93 loc) · 3.72 KB
/
python_op_gen_main.cc
File metadata and controls
106 lines (93 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright 2015 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 "tensorflow/python/framework/python_op_gen.h"
#include <memory>
#include <string>
#include <vector>
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_def.pb.h"
#include "tensorflow/core/lib/core/errors.h"
#include "tensorflow/core/lib/io/inputbuffer.h"
#include "tensorflow/core/lib/strings/scanner.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/logging.h"
namespace tensorflow {
namespace {
Status ReadHiddenOpsFromFile(const string& filename,
std::vector<string>* hidden_ops) {
std::unique_ptr<RandomAccessFile> file;
TF_CHECK_OK(Env::Default()->NewRandomAccessFile(filename, &file));
std::unique_ptr<io::InputBuffer> input_buffer(
new io::InputBuffer(file.get(), 256 << 10));
string line_contents;
Status s = input_buffer->ReadLine(&line_contents);
while (s.ok()) {
// The parser assumes that the op name is the first string on each
// line with no preceding whitespace, and ignores lines that do
// not start with an op name as a comment.
strings::Scanner scanner{StringPiece(line_contents)};
StringPiece op_name;
if (scanner.One(strings::Scanner::LETTER_DIGIT_DOT)
.Any(strings::Scanner::LETTER_DIGIT_DASH_DOT_SLASH_UNDERSCORE)
.GetResult(nullptr, &op_name)) {
hidden_ops->emplace_back(op_name.ToString());
}
s = input_buffer->ReadLine(&line_contents);
}
if (!errors::IsOutOfRange(s)) return s;
return Status::OK();
}
// The argument parsing is deliberately simplistic to support our only
// known use cases:
//
// 1. Read all op names from a file.
// 2. Read all op names from the arg as a comma-delimited list.
//
// Expected command-line argument syntax:
// ARG ::= '@' FILENAME
// | OP_NAME [',' OP_NAME]*
Status ParseHiddenOpsCommandLine(const char* arg,
std::vector<string>* hidden_ops) {
std::vector<string> op_names = str_util::Split(arg, ',');
if (op_names.size() == 1 && op_names[0].substr(0, 1) == "@") {
const string filename = op_names[0].substr(1);
return tensorflow::ReadHiddenOpsFromFile(filename, hidden_ops);
} else {
*hidden_ops = std::move(op_names);
}
return Status::OK();
}
void PrintAllPythonOps(const std::vector<string>& hidden_ops,
bool require_shapes) {
OpList ops;
OpRegistry::Global()->Export(false, &ops);
PrintPythonOps(ops, hidden_ops, require_shapes);
}
} // namespace
} // namespace tensorflow
int main(int argc, char* argv[]) {
tensorflow::port::InitMain(argv[0], &argc, &argv);
// Usage:
// gen_main [ @FILENAME | OpName[,OpName]* ] (0 | 1)
if (argc == 2) {
tensorflow::PrintAllPythonOps({}, tensorflow::string(argv[1]) == "1");
} else if (argc == 3) {
std::vector<tensorflow::string> hidden_ops;
TF_CHECK_OK(tensorflow::ParseHiddenOpsCommandLine(argv[1], &hidden_ops));
tensorflow::PrintAllPythonOps(hidden_ops,
tensorflow::string(argv[2]) == "1");
} else {
return -1;
}
return 0;
}