Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions tsl/profiler/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ cc_library(
"//learning/brain/tfrc/executor/stream_executor:__pkg__",
]),
deps = [
":multi_pass_profiler_controller",
":profiler_controller",
":profiler_interface",
"//tsl/profiler/protobuf:profiler_options_proto_cc",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/log",
"@com_google_absl//absl/synchronization",
],
alwayslink = True,
Expand All @@ -129,6 +131,7 @@ tsl_cc_test(
"//tsl/profiler/protobuf:xplane_proto_cc",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
"@xla//xla/tsl/platform:macros",
"@xla//xla/tsl/platform:test",
Expand All @@ -148,6 +151,8 @@ cc_library(
]),
deps = [
"//tsl/profiler/protobuf:xplane_proto_cc",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@xla//xla/tsl/platform:status",
],
)
Expand Down Expand Up @@ -183,6 +188,54 @@ tsl_cc_test(
],
)

cc_library(
name = "multi_pass_profiler_controller",
srcs = ["multi_pass_profiler_controller.cc"],
hdrs = ["multi_pass_profiler_controller.h"],
visibility = internal_visibility([
"@xla//xla/tsl/profiler:internal",
]),
deps = [
":profiler_interface",
"//tsl/profiler/protobuf:xplane_proto_cc",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@xla//xla/tsl/platform:logging",
],
)

cc_library(
name = "profiler_passes",
srcs = ["profiler_passes.cc"],
hdrs = ["profiler_passes.h"],
visibility = internal_visibility([
"@xla//xla/tsl/profiler:internal",
"@xla//xla/tsl/profiler:friends",
]),
deps = [
":profiler_collection",
":profiler_factory",
":profiler_interface",
":profiler_lock",
"//tsl/platform:platform_port",
"//tsl/profiler/protobuf:profiler_options_proto_cc",
"//tsl/profiler/protobuf:xplane_proto_cc",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:status_macros",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/synchronization",
"@xla//xla/tsl/platform:env",
"@xla//xla/tsl/platform:logging",
"@xla//xla/tsl/profiler/convert:post_process_single_host_xplane",
"@xla//xla/tsl/profiler/utils:time_utils",
"@xla//xla/tsl/profiler/utils:xplane_builder",
"@xla//xla/tsl/profiler/utils:xplane_schema",
"@xla//xla/tsl/profiler/utils:xplane_utils",
],
)

cc_library(
name = "profiler_session",
hdrs = ["profiler_session.h"],
Expand Down Expand Up @@ -380,6 +433,7 @@ cc_library(
srcs = ["profiler_collection.cc"],
hdrs = ["profiler_collection.h"],
visibility = internal_visibility([
"@xla//xla/backends/profiler:__pkg__",
"@xla//xla/backends/profiler/plugin:__pkg__",
"//learning/brain/tfrc/executor/stream_executor:__pkg__",
"@xla//xla/backends/profiler/cpu:__pkg__",
Expand Down
176 changes: 176 additions & 0 deletions tsl/profiler/lib/multi_pass_profiler_controller.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/* Copyright 2026 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 "tsl/profiler/lib/multi_pass_profiler_controller.h"

#include <memory>
#include <utility>

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "xla/tsl/platform/logging.h"
#include "tsl/profiler/lib/profiler_interface.h"
#include "tsl/profiler/protobuf/xplane.pb.h"

namespace tsl {
namespace profiler {

MultiPassProfilerController::MultiPassProfilerController(
std::unique_ptr<MultiPassProfilerInterface> profiler)
: profiler_(std::move(profiler)) {}

MultiPassProfilerController::~MultiPassProfilerController() {
// Ensure a successfully started pass is stopped.
if (state_ == MultiPassProfilerState::kPassStarted && status_.ok()) {
profiler_->StopPass().IgnoreError();
state_ = MultiPassProfilerState::kPassStopped;
}
if (state_ == MultiPassProfilerState::kStarted ||
state_ == MultiPassProfilerState::kPassStopped) {
profiler_->Stop().IgnoreError();
state_ = MultiPassProfilerState::kStopped;
}
}

bool MultiPassProfilerController::NeedMorePasses() {
if (state_ == MultiPassProfilerState::kPassStopped ||
state_ == MultiPassProfilerState::kStarted ||
state_ == MultiPassProfilerState::kInit) {
if (!status_.ok()) return false;
return profiler_->NeedMorePasses();
}
return false;
}

absl::Status MultiPassProfilerController::StartPass() {
absl::Status status;
if (state_ == MultiPassProfilerState::kStarted ||
state_ == MultiPassProfilerState::kPassStopped) {
state_ = MultiPassProfilerState::kPassStarted;
if (status_.ok()) {
status = status_ = profiler_->StartPass();
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("StartPass called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::PushRange(absl::string_view name) {
absl::Status status;
if (state_ == MultiPassProfilerState::kPassStarted) {
if (status_.ok()) {
status = status_ = profiler_->PushRange(name);
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("PushRange called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::PopRange() {
absl::Status status;
if (state_ == MultiPassProfilerState::kPassStarted) {
if (status_.ok()) {
status = status_ = profiler_->PopRange();
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("PopRange called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::StopPass() {
absl::Status status;
if (state_ == MultiPassProfilerState::kPassStarted) {
if (status_.ok()) {
status = status_ = profiler_->StopPass();
state_ = MultiPassProfilerState::kPassStopped;
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("StopPass called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::CollectData(
tensorflow::profiler::XSpace* space) {
absl::Status status;
if (state_ == MultiPassProfilerState::kInit) {
status = absl::OkStatus();
state_ = MultiPassProfilerState::kCollectData;
} else if (state_ == MultiPassProfilerState::kStopped) {
state_ = MultiPassProfilerState::kCollectData;
if (status_.ok()) {
status = status_ = profiler_->CollectData(space);
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("CollectData called in the wrong order.");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::Start() {
absl::Status status;
if (state_ == MultiPassProfilerState::kInit) {
state_ = MultiPassProfilerState::kStarted;
if (status_.ok()) {
status = status_ = profiler_->Start();
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("Start called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

absl::Status MultiPassProfilerController::Stop() {
absl::Status status;
if (state_ == MultiPassProfilerState::kPassStarted ||
state_ == MultiPassProfilerState::kPassStopped ||
state_ == MultiPassProfilerState::kStarted) {
state_ = MultiPassProfilerState::kStopped;
if (status_.ok()) {
status = status_ = profiler_->Stop();
} else {
status = absl::AbortedError("Previous call returned an error.");
}
} else {
status = absl::AbortedError("Stop called in the wrong order");
}
if (!status.ok()) LOG(ERROR) << status;
return status;
}

} // namespace profiler

} // namespace tsl
65 changes: 65 additions & 0 deletions tsl/profiler/lib/multi_pass_profiler_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright 2026 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.
==============================================================================*/

#ifndef TENSORFLOW_TSL_PROFILER_LIB_MULTI_PASS_PROFILER_CONTROLLER_H_
#define TENSORFLOW_TSL_PROFILER_LIB_MULTI_PASS_PROFILER_CONTROLLER_H_

#include <memory>

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "tsl/profiler/lib/profiler_interface.h"
#include "tsl/profiler/protobuf/xplane.pb.h"

namespace tsl {
namespace profiler {

class MultiPassProfilerController : public MultiPassProfilerInterface {
public:
explicit MultiPassProfilerController(
std::unique_ptr<MultiPassProfilerInterface> profiler);
~MultiPassProfilerController() override;

// MultiPassProfilerInterface methods
bool NeedMorePasses() override;
absl::Status StartPass() override;
absl::Status PushRange(absl::string_view name) override;
absl::Status PopRange() override;
absl::Status StopPass() override;

// ProfilerInterface methods
absl::Status Start() override;
absl::Status Stop() override;
absl::Status CollectData(tensorflow::profiler::XSpace* space) override;

private:
enum class MultiPassProfilerState {
kInit = 0,
kStarted = 1,
kPassStarted = 2,
kPassStopped = 3,
kStopped = 4,
kCollectData = 5,
};

MultiPassProfilerState state_ = MultiPassProfilerState::kInit;
std::unique_ptr<MultiPassProfilerInterface> profiler_;
absl::Status status_; // result of calls to profiler_
};

} // namespace profiler
} // namespace tsl

#endif // TENSORFLOW_TSL_PROFILER_LIB_MULTI_PASS_PROFILER_CONTROLLER_H_
Loading
Loading