-
Notifications
You must be signed in to change notification settings - Fork 75.3k
Expand file tree
/
Copy pathoffset_counter.cc
More file actions
70 lines (59 loc) · 2.32 KB
/
offset_counter.cc
File metadata and controls
70 lines (59 loc) · 2.32 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
/* Copyright 2022 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.
==============================================================================*/
// Usage:
// offset_counter [ --out_path FILENAME ] [ SRC_FILE1 SRC_FILE2 ... ]
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "absl/strings/string_view.h"
#include "xla/tsl/platform/errors.h"
#include "xla/tsl/platform/types.h"
#include "xla/tsl/util/command_line_flags.h"
#include "tensorflow/python/framework/offset_counter_helper.h"
#include "tensorflow/python/framework/op_reg_offset.pb.h"
#include "tsl/platform/init_main.h"
#include "tsl/platform/strcat.h"
inline constexpr absl::string_view kUsage =
"offset_counter reads C++ source codes, scans for the location of where "
"the REGISTER_OP gets called, and outputs a OpRegOffsets proto to stdout "
"or a file.";
int main(int argc, char* argv[]) {
std::string out_path;
std::vector<tsl::Flag> flag_list = {
tsl::Flag("out_path", &out_path, "Output file path."),
};
const std::string usage_string =
absl::StrCat(kUsage, "\n\n", tsl::Flags::Usage(argv[0], flag_list));
const bool parse_result = tsl::Flags::Parse(&argc, argv, flag_list);
tsl::port::InitMain(usage_string.c_str(), &argc, &argv);
if (!parse_result) {
LOG(ERROR) << usage_string;
return -1;
}
tensorflow::OpRegOffsets op_reg_offsets;
for (size_t i = 1; i < argc; ++i) {
TF_CHECK_OK(tensorflow::FindOpRegistationFromFile(argv[i], op_reg_offsets));
}
if (out_path.empty()) {
std::cout << op_reg_offsets.SerializeAsString();
} else {
std::ofstream f(out_path);
if (f.bad()) {
TF_CHECK_OK(tsl::errors::IOError(
absl::StrCat("Cannot open file: ", out_path), errno));
}
f << op_reg_offsets.SerializeAsString();
f.close();
}
return 0;
}