From ac51b90ed68b0ab2ff7c06f7613f719ed44abe21 Mon Sep 17 00:00:00 2001 From: "A. Unique TensorFlower" Date: Sat, 1 Nov 2025 03:44:27 -0700 Subject: [PATCH] Automated Code Change PiperOrigin-RevId: 826818362 --- tensorflow/core/profiler/profiler.cc | 69 +++++++++++----------- tensorflow/core/profiler/tfprof_options.cc | 56 +++++++++--------- tensorflow/core/profiler/tfprof_options.h | 47 ++++++++------- 3 files changed, 89 insertions(+), 83 deletions(-) diff --git a/tensorflow/core/profiler/profiler.cc b/tensorflow/core/profiler/profiler.cc index 8ad7c154ce2..3e1dee0cd94 100644 --- a/tensorflow/core/profiler/profiler.cc +++ b/tensorflow/core/profiler/profiler.cc @@ -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::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 account_type_regexes = + std::vector account_type_regexes = absl::StrSplit(FLAGS_account_type_regexes, ',', absl::SkipEmpty()); - std::vector start_name_regexes = + std::vector start_name_regexes = absl::StrSplit(FLAGS_start_name_regexes, ',', absl::SkipEmpty()); - std::vector trim_name_regexes = + std::vector trim_name_regexes = absl::StrSplit(FLAGS_trim_name_regexes, ',', absl::SkipEmpty()); - std::vector show_name_regexes = + std::vector show_name_regexes = absl::StrSplit(FLAGS_show_name_regexes, ',', absl::SkipEmpty()); - std::vector hide_name_regexes = + std::vector hide_name_regexes = absl::StrSplit(FLAGS_hide_name_regexes, ',', absl::SkipEmpty()); - std::vector select = + std::vector select = absl::StrSplit(FLAGS_select, ',', absl::SkipEmpty()); - string output_type; - std::map output_options; + std::string output_type; + std::map 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 op_log = std::make_unique(); 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( std::move(graph), nullptr, std::move(op_log), std::move(ckpt_reader)); - std::vector run_meta_files = + std::vector run_meta_files = absl::StrSplit(FLAGS_run_meta_path, ',', absl::SkipEmpty()); for (int i = 0; i < run_meta_files.size(); ++i) { std::unique_ptr run_meta = std::make_unique(); @@ -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()) { diff --git a/tensorflow/core/profiler/tfprof_options.cc b/tensorflow/core/profiler/tfprof_options.cc index a31fddbcef3..5bc5ea2f53b 100644 --- a/tensorflow/core/profiler/tfprof_options.cc +++ b/tensorflow/core/profiler/tfprof_options.cc @@ -32,8 +32,8 @@ limitations under the License. namespace tensorflow { namespace tfprof { namespace { -string KeyValueToStr(const std::map& kv_map) { - std::vector kv_vec; +std::string KeyValueToStr(const std::map& kv_map) { + std::vector 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& kv_map) { } } // namespace -absl::Status ParseOutput(const string& output_opt, string* output_type, - std::map* output_options) { +absl::Status ParseOutput(const std::string& output_opt, + std::string* output_type, + std::map* output_options) { // The default is to use stdout. if (output_opt.empty()) { *output_type = kOutput[1]; return absl::OkStatus(); } - std::set output_types(kOutput, - kOutput + sizeof(kOutput) / sizeof(*kOutput)); + std::set output_types( + kOutput, kOutput + sizeof(kOutput) / sizeof(*kOutput)); auto opt_split = output_opt.find(':'); - std::vector kv_split; + std::vector 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 valid_options; - std::set required_options; + std::set valid_options; + std::set 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 kv = + for (const std::string& kv_str : kv_split) { + const std::vector 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 kv_without_key(kv.begin() + 1, kv.end()); + const std::vector 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 output_options; + std::string output_type; + std::map 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(opts_pb.account_type_regexes().begin(), - opts_pb.account_type_regexes().end()), - std::vector(opts_pb.start_name_regexes().begin(), - opts_pb.start_name_regexes().end()), - std::vector(opts_pb.trim_name_regexes().begin(), - opts_pb.trim_name_regexes().end()), - std::vector(opts_pb.show_name_regexes().begin(), - opts_pb.show_name_regexes().end()), - std::vector(opts_pb.hide_name_regexes().begin(), - opts_pb.hide_name_regexes().end()), + std::vector(opts_pb.account_type_regexes().begin(), + opts_pb.account_type_regexes().end()), + std::vector(opts_pb.start_name_regexes().begin(), + opts_pb.start_name_regexes().end()), + std::vector(opts_pb.trim_name_regexes().begin(), + opts_pb.trim_name_regexes().end()), + std::vector(opts_pb.show_name_regexes().begin(), + opts_pb.show_name_regexes().end()), + std::vector(opts_pb.hide_name_regexes().begin(), + opts_pb.hide_name_regexes().end()), opts_pb.account_displayed_op_only(), - std::vector(opts_pb.select().begin(), opts_pb.select().end()), + std::vector(opts_pb.select().begin(), + opts_pb.select().end()), output_type, output_options); return absl::OkStatus(); } diff --git a/tensorflow/core/profiler/tfprof_options.h b/tensorflow/core/profiler/tfprof_options.h index a518db50022..821a868b3e7 100644 --- a/tensorflow/core/profiler/tfprof_options.h +++ b/tensorflow/core/profiler/tfprof_options.h @@ -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& account_type_regexes, - const std::vector& start_name_regexes, - const std::vector& trim_name_regexes, - const std::vector& show_name_regexes, - const std::vector& hide_name_regexes, - bool account_displayed_op_only, const std::vector& select, - const string& output_type, - const std::map& output_options) + int64_t min_occurrence, int64_t step, const std::string& order_by, + const std::vector& account_type_regexes, + const std::vector& start_name_regexes, + const std::vector& trim_name_regexes, + const std::vector& show_name_regexes, + const std::vector& hide_name_regexes, + bool account_displayed_op_only, + const std::vector& select, + const std::string& output_type, + const std::map& 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 account_type_regexes; - std::vector start_name_regexes; - std::vector trim_name_regexes; - std::vector show_name_regexes; - std::vector hide_name_regexes; + std::vector account_type_regexes; + std::vector start_name_regexes; + std::vector trim_name_regexes; + std::vector show_name_regexes; + std::vector hide_name_regexes; bool account_displayed_op_only; - std::set select; + std::set select; - string output_type; - std::map output_options; + std::string output_type; + std::map 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* output_options); +absl::Status ParseOutput(const std::string& output_opt, + std::string* output_type, + std::map* output_options); } // namespace tfprof } // namespace tensorflow