Skip to content

Commit 4dd3b1e

Browse files
Pani122copybara-github
authored andcommitted
Introduce a unified Xprof workflow.
PiperOrigin-RevId: 914142746
1 parent b5d88d5 commit 4dd3b1e

6 files changed

Lines changed: 89 additions & 5 deletions

xprof/convert/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ cc_library(
7070
"@com_google_absl//absl/base:core_headers",
7171
"@com_google_absl//absl/base:no_destructor",
7272
"@com_google_absl//absl/container:flat_hash_map",
73+
"@com_google_absl//absl/flags:flag",
7374
"@com_google_absl//absl/functional:any_invocable",
7475
"@com_google_absl//absl/log",
7576
"@com_google_absl//absl/strings",
@@ -1670,7 +1671,10 @@ cc_library(
16701671
":repository",
16711672
":tool_options",
16721673
":unified_hlo_stats_processor",
1674+
":unified_profile_processor",
1675+
":unified_profile_processor_factory",
16731676
"@com_github_grpc_grpc//:grpc++",
1677+
"@com_google_absl//absl/flags:flag",
16741678
"@com_google_absl//absl/log",
16751679
"@com_google_absl//absl/status",
16761680
"@com_google_absl//absl/status:statusor",

xprof/convert/unified_hlo_stats_processor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class UnifiedHloStatsProcessor : public BaseOpStatsProcessor {
3838
const tensorflow::profiler::ToolOptions& options) override;
3939
};
4040

41-
REGISTER_UNIFIED_PROFILE_PROCESSOR("hlo_stats",
42-
UnifiedHloStatsProcessor);
41+
REGISTER_UNIFIED_PROFILE_PROCESSOR("hlo_stats", UnifiedHloStatsProcessor);
4342

4443
} // namespace xprof
4544

xprof/convert/unified_profile_processor_factory.cc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
#include "absl/base/no_destructor.h"
2121
#include "absl/functional/any_invocable.h"
2222
#include "absl/log/log.h"
23+
#include "absl/strings/match.h"
2324
#include "absl/strings/string_view.h"
25+
#include "absl/flags/flag.h"
2426
#include "absl/synchronization/mutex.h"
2527
#include "xprof/convert/tool_options.h"
2628
#include "xprof/convert/unified_profile_processor.h"
2729

30+
ABSL_FLAG(bool, enable_unified_xprof, false, "Enable unified Xprof workflow");
31+
2832
namespace xprof {
2933

3034
UnifiedProfileProcessorFactory& UnifiedProfileProcessorFactory::GetInstance() {
@@ -46,8 +50,17 @@ std::unique_ptr<UnifiedProfileProcessor>
4650
UnifiedProfileProcessorFactory::Create(
4751
absl::string_view tool_name,
4852
const tensorflow::profiler::ToolOptions& options) const {
53+
absl::string_view clean_tool_name = tool_name;
54+
if (absl::EndsWith(clean_tool_name, ".json")) {
55+
clean_tool_name.remove_suffix(5);
56+
} else if (absl::EndsWith(clean_tool_name, ".pb")) {
57+
clean_tool_name.remove_suffix(3);
58+
} else if (absl::EndsWith(clean_tool_name, ".pbtxt")) {
59+
clean_tool_name.remove_suffix(6);
60+
}
61+
4962
absl::MutexLock lock(mu_);
50-
auto it = creators_.find(tool_name);
63+
auto it = creators_.find(clean_tool_name);
5164
if (it == creators_.end()) {
5265
LOG(ERROR) << "No UnifiedProfileProcessor registered for tool: "
5366
<< tool_name;

xprof/convert/unified_profile_processor_factory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class RegisterUnifiedProfileProcessor {
6060
UnifiedProfileProcessorFactory::Creator creator);
6161
};
6262

63-
#define REGISTER_UNIFIED_PROFILE_PROCESSOR(tool_name, ClassName) \
63+
#define REGISTER_UNIFIED_PROFILE_PROCESSOR(tool_name, ClassName) \
6464
ABSL_ATTRIBUTE_UNUSED static const ::xprof::RegisterUnifiedProfileProcessor \
6565
register_##ClassName( \
6666
tool_name, [](const tensorflow::profiler::ToolOptions& options) { \

xprof/convert/unified_profile_processor_factory_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class MacroRegisteredProcessor : public UnifiedProfileProcessor {
6666
};
6767

6868
REGISTER_UNIFIED_PROFILE_PROCESSOR("macro_registered_tool",
69-
MacroRegisteredProcessor);
69+
MacroRegisteredProcessor);
7070

7171
TEST(UnifiedProfileProcessorTest, BaseClassMethods) {
7272
DummyProcessor processor;

xprof/convert/xplane_to_tools_data_with_profile_processor.cc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
#include <variant>
66
#include <vector>
77

8+
#include "absl/flags/flag.h"
89
#include "absl/log/log.h"
910
#include "absl/status/status.h"
1011
#include "absl/status/statusor.h"
12+
#include "absl/strings/str_cat.h"
1113
#include "absl/strings/string_view.h"
1214
#include "absl/time/clock.h"
1315
#include "absl/time/time.h"
@@ -17,17 +19,24 @@
1719
#include "xla/tsl/platform/errors.h"
1820
#include "xla/tsl/platform/threadpool.h"
1921
#include "tsl/platform/path.h"
22+
#include "absl/flags/declare.h"
2023
#include "xprof/convert/profile_processor.h"
2124
#include "xprof/convert/profile_processor_factory.h"
2225
#include "xprof/convert/repository.h"
2326
#include "xprof/convert/tool_options.h"
27+
#include "xprof/convert/unified_profile_processor.h"
28+
#include "xprof/convert/unified_profile_processor_factory.h"
2429
#include "plugin/xprof/protobuf/worker_service.pb.h"
2530
#include "plugin/xprof/worker/grpc_utils.h"
2631
#include "plugin/xprof/worker/stub_factory.h"
2732

33+
ABSL_DECLARE_FLAG(bool, enable_unified_xprof);
34+
2835
namespace tensorflow {
2936
namespace profiler {
37+
3038
namespace {
39+
using xprof::UnifiedProfileProcessor;
3140

3241
constexpr absl::string_view kXplaneFileName = ".xplane.pb";
3342

@@ -133,6 +142,43 @@ absl::Status ProcessSession(xprof::ProfileProcessor* processor,
133142
return absl::OkStatus();
134143
}
135144

145+
absl::Status RunUnifiedMapReduce(const SessionSnapshot& session_snapshot,
146+
absl::string_view tool_name,
147+
UnifiedProfileProcessor* processor,
148+
const ToolOptions& options) {
149+
const int num_hosts = session_snapshot.XSpaceSize();
150+
std::vector<absl::StatusOr<std::string>> map_outputs(num_hosts);
151+
152+
{
153+
tsl::thread::ThreadPool thread_pool(tsl::Env::Default(), __FUNCTION__,
154+
num_hosts);
155+
for (int i = 0; i < num_hosts; ++i) {
156+
thread_pool.Schedule([&session_snapshot, &tool_name, &options,
157+
&map_outputs, i] {
158+
std::string hostname = session_snapshot.GetHostname(i);
159+
std::string xspace_path = GetXSpaceFilePath(session_snapshot, hostname);
160+
map_outputs[i] = CallWorkerService(xspace_path, tool_name, options);
161+
});
162+
}
163+
}
164+
165+
std::vector<std::string> map_output_files;
166+
map_output_files.reserve(num_hosts);
167+
for (int i = 0; i < num_hosts; ++i) {
168+
TF_RETURN_IF_ERROR(map_outputs[i].status());
169+
map_output_files.push_back(*std::move(map_outputs[i]));
170+
}
171+
LOG(INFO) << "Started reducing outputs for tool: " << tool_name
172+
<< " num_hosts: " << num_hosts;
173+
absl::Time start_time = absl::Now();
174+
absl::Status reduce_status =
175+
processor->Reduce(session_snapshot, map_output_files);
176+
absl::Duration reduce_time = absl::Now() - start_time;
177+
LOG(INFO) << "Finished reducing outputs for tool: " << tool_name
178+
<< " num_hosts: " << num_hosts << " time taken: " << reduce_time;
179+
return reduce_status;
180+
}
181+
136182
} // namespace
137183

138184
absl::StatusOr<std::string> ConvertMultiXSpacesToToolDataWithProfileProcessor(
@@ -146,6 +192,28 @@ absl::StatusOr<std::string> ConvertMultiXSpacesToToolDataWithProfileProcessor(
146192

147193
absl::Time start_time = absl::Now();
148194

195+
if (absl::GetFlag(FLAGS_enable_unified_xprof)) {
196+
auto unified_processor =
197+
xprof::UnifiedProfileProcessorFactory::GetInstance().Create(
198+
tool_name, options);
199+
if (!unified_processor) {
200+
LOG(WARNING) << "Unified processor not found for tool: " << tool_name
201+
<< ", falling back to legacy.";
202+
} else {
203+
LOG(INFO) << "Using unified workflow for tool: " << tool_name;
204+
if (unified_processor->ShouldUseWorkerService(session_snapshot,
205+
options)) {
206+
TF_RETURN_IF_ERROR(RunUnifiedMapReduce(session_snapshot, tool_name,
207+
unified_processor.get(),
208+
options));
209+
} else {
210+
TF_RETURN_IF_ERROR(
211+
unified_processor->ProcessSession(session_snapshot, options));
212+
}
213+
return unified_processor->GetData();
214+
}
215+
}
216+
149217
auto processor =
150218
xprof::ProfileProcessorFactory::GetInstance().Create(tool_name, options);
151219
if (!processor) {

0 commit comments

Comments
 (0)