Automated Code Change
Some checks failed
ARM CI / build (3.10) (push) Has been cancelled
Creates a GitHub Issue when a PR Rolled back via Commit to Master / create-issue-on-pr-rollback (push) Has been cancelled
Scorecards supply-chain security / Scorecards analysis (push) Has been cancelled
OSV-Scanner Scheduled Scan / scan-scheduled (push) Has been cancelled

PiperOrigin-RevId: 826818362
This commit is contained in:
A. Unique TensorFlower 2025-11-01 03:44:27 -07:00 committed by TensorFlower Gardener
parent 4f3f2c9444
commit ac51b90ed6
3 changed files with 89 additions and 83 deletions

View File

@ -47,7 +47,7 @@ limitations under the License.
namespace tensorflow {
namespace tfprof {
void completion(const char* buf, linenoiseCompletions* lc) {
string buf_str = buf;
std::string buf_str = buf;
if (buf_str.find(' ') == buf_str.npos) {
for (const char* opt : kCmds) {
if (absl::StartsWith(opt, buf_str)) {
@ -57,11 +57,12 @@ void completion(const char* buf, linenoiseCompletions* lc) {
return;
}
string prefix;
std::string prefix;
int last_dash = buf_str.find_last_of(' ');
if (last_dash != string::npos) {
if (last_dash != std::string::npos) {
prefix = buf_str.substr(0, last_dash + 1);
buf_str = buf_str.substr(last_dash + 1, kint32max);
buf_str =
buf_str.substr(last_dash + 1, std::numeric_limits<int32_t>::max());
}
for (const char* opt : kOptions) {
if (absl::StartsWith(opt, buf_str)) {
@ -71,11 +72,11 @@ void completion(const char* buf, linenoiseCompletions* lc) {
}
int Run(int argc, char** argv) {
string FLAGS_profile_path = "";
string FLAGS_graph_path = "";
string FLAGS_run_meta_path = "";
string FLAGS_op_log_path = "";
string FLAGS_checkpoint_path = "";
std::string FLAGS_profile_path = "";
std::string FLAGS_graph_path = "";
std::string FLAGS_run_meta_path = "";
std::string FLAGS_op_log_path = "";
std::string FLAGS_checkpoint_path = "";
int32_t FLAGS_max_depth = 10;
int64_t FLAGS_min_bytes = 0;
int64_t FLAGS_min_peak_bytes = 0;
@ -88,15 +89,15 @@ int Run(int argc, char** argv) {
int64_t FLAGS_min_float_ops = 0;
int64_t FLAGS_min_occurrence = 0;
int64_t FLAGS_step = -1;
string FLAGS_order_by = "name";
string FLAGS_account_type_regexes = ".*";
string FLAGS_start_name_regexes = ".*";
string FLAGS_trim_name_regexes = "";
string FLAGS_show_name_regexes = ".*";
string FLAGS_hide_name_regexes;
std::string FLAGS_order_by = "name";
std::string FLAGS_account_type_regexes = ".*";
std::string FLAGS_start_name_regexes = ".*";
std::string FLAGS_trim_name_regexes = "";
std::string FLAGS_show_name_regexes = ".*";
std::string FLAGS_hide_name_regexes;
bool FLAGS_account_displayed_op_only = false;
string FLAGS_select = "micros";
string FLAGS_output = "";
std::string FLAGS_select = "micros";
std::string FLAGS_output = "";
for (int i = 0; i < argc; i++) {
absl::FPrintF(stderr, "%s\n", argv[i]);
}
@ -137,7 +138,7 @@ int Run(int argc, char** argv) {
Flag("select", &FLAGS_select, "select"),
Flag("output", &FLAGS_output, "output"),
};
string usage = Flags::Usage(argv[0], flag_list);
std::string usage = Flags::Usage(argv[0], flag_list);
bool parse_ok = Flags::Parse(&argc, argv, flag_list);
if (!parse_ok) {
absl::PrintF("%s", usage);
@ -153,37 +154,37 @@ int Run(int argc, char** argv) {
return 1;
}
std::vector<string> account_type_regexes =
std::vector<std::string> account_type_regexes =
absl::StrSplit(FLAGS_account_type_regexes, ',', absl::SkipEmpty());
std::vector<string> start_name_regexes =
std::vector<std::string> start_name_regexes =
absl::StrSplit(FLAGS_start_name_regexes, ',', absl::SkipEmpty());
std::vector<string> trim_name_regexes =
std::vector<std::string> trim_name_regexes =
absl::StrSplit(FLAGS_trim_name_regexes, ',', absl::SkipEmpty());
std::vector<string> show_name_regexes =
std::vector<std::string> show_name_regexes =
absl::StrSplit(FLAGS_show_name_regexes, ',', absl::SkipEmpty());
std::vector<string> hide_name_regexes =
std::vector<std::string> hide_name_regexes =
absl::StrSplit(FLAGS_hide_name_regexes, ',', absl::SkipEmpty());
std::vector<string> select =
std::vector<std::string> select =
absl::StrSplit(FLAGS_select, ',', absl::SkipEmpty());
string output_type;
std::map<string, string> output_options;
std::string output_type;
std::map<std::string, std::string> output_options;
absl::Status s = ParseOutput(FLAGS_output, &output_type, &output_options);
CHECK(s.ok()) << s;
string cmd = "";
std::string cmd = "";
if (argc == 1 && FLAGS_graph_path.empty() && FLAGS_profile_path.empty() &&
FLAGS_run_meta_path.empty()) {
PrintHelp();
return 0;
} else if (argc > 1) {
if (string(argv[1]) == kCmds[6]) {
if (std::string(argv[1]) == kCmds[6]) {
PrintHelp();
return 0;
}
if (string(argv[1]) == kCmds[0] || string(argv[1]) == kCmds[1] ||
string(argv[1]) == kCmds[2] || string(argv[1]) == kCmds[3] ||
string(argv[1]) == kCmds[4]) {
if (std::string(argv[1]) == kCmds[0] || std::string(argv[1]) == kCmds[1] ||
std::string(argv[1]) == kCmds[2] || std::string(argv[1]) == kCmds[3] ||
std::string(argv[1]) == kCmds[4]) {
cmd = argv[1];
}
}
@ -221,7 +222,7 @@ int Run(int argc, char** argv) {
std::unique_ptr<OpLogProto> op_log = std::make_unique<OpLogProto>();
if (!FLAGS_op_log_path.empty()) {
string op_log_str;
std::string op_log_str;
s = ReadFileToString(Env::Default(), FLAGS_op_log_path, &op_log_str);
if (!s.ok()) {
absl::FPrintF(stderr, "Failed to read op_log_path: %s\n", s.ToString());
@ -235,7 +236,7 @@ int Run(int argc, char** argv) {
tf_stat = std::make_unique<TFStats>(
std::move(graph), nullptr, std::move(op_log), std::move(ckpt_reader));
std::vector<string> run_meta_files =
std::vector<std::string> run_meta_files =
absl::StrSplit(FLAGS_run_meta_path, ',', absl::SkipEmpty());
for (int i = 0; i < run_meta_files.size(); ++i) {
std::unique_ptr<RunMetadata> run_meta = std::make_unique<RunMetadata>();
@ -292,7 +293,7 @@ int Run(int argc, char** argv) {
break;
}
looped = true;
string line_s = line;
std::string line_s = line;
free(line);
if (line_s.empty()) {

View File

@ -32,8 +32,8 @@ limitations under the License.
namespace tensorflow {
namespace tfprof {
namespace {
string KeyValueToStr(const std::map<string, string>& kv_map) {
std::vector<string> kv_vec;
std::string KeyValueToStr(const std::map<std::string, std::string>& kv_map) {
std::vector<std::string> kv_vec;
kv_vec.reserve(kv_map.size());
for (const auto& pair : kv_map) {
kv_vec.push_back(absl::StrCat(pair.first, "=", pair.second));
@ -42,18 +42,19 @@ string KeyValueToStr(const std::map<string, string>& kv_map) {
}
} // namespace
absl::Status ParseOutput(const string& output_opt, string* output_type,
std::map<string, string>* output_options) {
absl::Status ParseOutput(const std::string& output_opt,
std::string* output_type,
std::map<std::string, std::string>* output_options) {
// The default is to use stdout.
if (output_opt.empty()) {
*output_type = kOutput[1];
return absl::OkStatus();
}
std::set<string> output_types(kOutput,
kOutput + sizeof(kOutput) / sizeof(*kOutput));
std::set<std::string> output_types(
kOutput, kOutput + sizeof(kOutput) / sizeof(*kOutput));
auto opt_split = output_opt.find(':');
std::vector<string> kv_split;
std::vector<std::string> kv_split;
if (opt_split == output_opt.npos) {
if (output_types.find(output_opt) == output_types.end()) {
return absl::Status(
@ -74,8 +75,8 @@ absl::Status ParseOutput(const string& output_opt, string* output_type,
absl::SkipEmpty());
}
std::set<string> valid_options;
std::set<string> required_options;
std::set<std::string> valid_options;
std::set<std::string> required_options;
if (*output_type == kOutput[0]) {
valid_options.insert(
kTimelineOpts,
@ -99,8 +100,8 @@ absl::Status ParseOutput(const string& output_opt, string* output_type,
sizeof(kPprofRequiredOpts) / sizeof(*kPprofRequiredOpts));
}
for (const string& kv_str : kv_split) {
const std::vector<string> kv =
for (const std::string& kv_str : kv_split) {
const std::vector<std::string> kv =
absl::StrSplit(kv_str, '=', absl::SkipEmpty());
if (kv.size() < 2) {
return absl::Status(
@ -113,11 +114,11 @@ absl::Status ParseOutput(const string& output_opt, string* output_type,
absl::StrFormat("Unrecognized options %s for output_type: %s\n",
kv[0], *output_type));
}
const std::vector<string> kv_without_key(kv.begin() + 1, kv.end());
const std::vector<std::string> kv_without_key(kv.begin() + 1, kv.end());
(*output_options)[kv[0]] = absl::StrJoin(kv_without_key, "=");
}
for (const string& opt : required_options) {
for (const std::string& opt : required_options) {
if (output_options->find(opt) == output_options->end()) {
return absl::Status(
absl::StatusCode::kInvalidArgument,
@ -129,7 +130,7 @@ absl::Status ParseOutput(const string& output_opt, string* output_type,
return absl::OkStatus();
}
absl::Status Options::FromProtoStr(const string& opts_proto_str,
absl::Status Options::FromProtoStr(const std::string& opts_proto_str,
Options* opts) {
OptionsProto opts_pb;
if (!opts_pb.ParseFromString(opts_proto_str)) {
@ -139,8 +140,8 @@ absl::Status Options::FromProtoStr(const string& opts_proto_str,
opts_proto_str));
}
string output_type;
std::map<string, string> output_options;
std::string output_type;
std::map<std::string, std::string> output_options;
absl::Status s = ParseOutput(opts_pb.output(), &output_type, &output_options);
if (!s.ok()) return s;
@ -162,18 +163,19 @@ absl::Status Options::FromProtoStr(const string& opts_proto_str,
opts_pb.min_micros(), opts_pb.min_accelerator_micros(),
opts_pb.min_cpu_micros(), opts_pb.min_params(), opts_pb.min_float_ops(),
opts_pb.min_occurrence(), opts_pb.step(), opts_pb.order_by(),
std::vector<string>(opts_pb.account_type_regexes().begin(),
std::vector<std::string>(opts_pb.account_type_regexes().begin(),
opts_pb.account_type_regexes().end()),
std::vector<string>(opts_pb.start_name_regexes().begin(),
std::vector<std::string>(opts_pb.start_name_regexes().begin(),
opts_pb.start_name_regexes().end()),
std::vector<string>(opts_pb.trim_name_regexes().begin(),
std::vector<std::string>(opts_pb.trim_name_regexes().begin(),
opts_pb.trim_name_regexes().end()),
std::vector<string>(opts_pb.show_name_regexes().begin(),
std::vector<std::string>(opts_pb.show_name_regexes().begin(),
opts_pb.show_name_regexes().end()),
std::vector<string>(opts_pb.hide_name_regexes().begin(),
std::vector<std::string>(opts_pb.hide_name_regexes().begin(),
opts_pb.hide_name_regexes().end()),
opts_pb.account_displayed_op_only(),
std::vector<string>(opts_pb.select().begin(), opts_pb.select().end()),
std::vector<std::string>(opts_pb.select().begin(),
opts_pb.select().end()),
output_type, output_options);
return absl::OkStatus();
}

View File

@ -102,7 +102,8 @@ static const char* const kPprofRequiredOpts[] = {
struct Options {
public:
static absl::Status FromProtoStr(const string& opts_proto_str, Options* opts);
static absl::Status FromProtoStr(const std::string& opts_proto_str,
Options* opts);
virtual ~Options() = default;
Options()
@ -113,15 +114,16 @@ struct Options {
int64_t min_residual_bytes, int64_t min_output_bytes,
int64_t min_micros, int64_t min_accelerator_micros,
int64_t min_cpu_micros, int64_t min_params, int64_t min_float_ops,
int64_t min_occurrence, int64_t step, const string& order_by,
const std::vector<string>& account_type_regexes,
const std::vector<string>& start_name_regexes,
const std::vector<string>& trim_name_regexes,
const std::vector<string>& show_name_regexes,
const std::vector<string>& hide_name_regexes,
bool account_displayed_op_only, const std::vector<string>& select,
const string& output_type,
const std::map<string, string>& output_options)
int64_t min_occurrence, int64_t step, const std::string& order_by,
const std::vector<std::string>& account_type_regexes,
const std::vector<std::string>& start_name_regexes,
const std::vector<std::string>& trim_name_regexes,
const std::vector<std::string>& show_name_regexes,
const std::vector<std::string>& hide_name_regexes,
bool account_displayed_op_only,
const std::vector<std::string>& select,
const std::string& output_type,
const std::map<std::string, std::string>& output_options)
: max_depth(max_depth),
min_bytes(min_bytes),
min_peak_bytes(min_peak_bytes),
@ -145,7 +147,7 @@ struct Options {
output_type(output_type),
output_options(output_options) {}
string ToString() const;
std::string ToString() const;
int max_depth;
int64_t min_bytes;
@ -159,26 +161,27 @@ struct Options {
int64_t min_float_ops;
int64_t min_occurrence;
int64_t step;
string order_by;
std::string order_by;
std::vector<string> account_type_regexes;
std::vector<string> start_name_regexes;
std::vector<string> trim_name_regexes;
std::vector<string> show_name_regexes;
std::vector<string> hide_name_regexes;
std::vector<std::string> account_type_regexes;
std::vector<std::string> start_name_regexes;
std::vector<std::string> trim_name_regexes;
std::vector<std::string> show_name_regexes;
std::vector<std::string> hide_name_regexes;
bool account_displayed_op_only;
std::set<string> select;
std::set<std::string> select;
string output_type;
std::map<string, string> output_options;
std::string output_type;
std::map<std::string, std::string> output_options;
};
// Parse the -output option.
// 'output_opt': User input string with format: output_type:key=value,key=value.
// 'output_type' and 'output_options' are extracted from 'output_opt'.
absl::Status ParseOutput(const string& output_opt, string* output_type,
std::map<string, string>* output_options);
absl::Status ParseOutput(const std::string& output_opt,
std::string* output_type,
std::map<std::string, std::string>* output_options);
} // namespace tfprof
} // namespace tensorflow