-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix race condition in dumping logic in xla::Executable
This is adding a mutex to the lazy initialization that happens in a `const` member function. It also adds a test which hopefully ensures that this getter stays thread compatible. PiperOrigin-RevId: 676804217
- Loading branch information
1 parent
5fd336b
commit 7a6369b
Showing
3 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* Copyright 2024 The OpenXLA Authors. | ||
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 "xla/service/executable.h" | ||
|
||
#include <memory> | ||
#include <string> | ||
#include <string_view> | ||
#include <utility> | ||
#include <vector> | ||
|
||
#include <gtest/gtest.h> | ||
#include "absl/status/status.h" | ||
#include "absl/status/statusor.h" | ||
#include "xla/hlo/ir/hlo_module.h" | ||
#include "xla/service/hlo_execution_profile.h" | ||
#include "xla/service/service_executable_run_options.h" | ||
#include "xla/tests/hlo_test_base.h" | ||
#include "tsl/platform/env.h" | ||
#include "tsl/platform/statusor.h" | ||
#include "tsl/platform/test.h" | ||
#include "tsl/platform/threadpool.h" | ||
|
||
namespace xla { | ||
namespace { | ||
|
||
class TestExecutable : public Executable { | ||
public: | ||
explicit TestExecutable(std::shared_ptr<HloModule> module) | ||
: Executable{std::move(module)} {} | ||
|
||
absl::StatusOr<ExecutionOutput> ExecuteAsyncOnStream( | ||
const ServiceExecutableRunOptions* run_options, | ||
std::vector<ExecutionInput> arguments, | ||
HloExecutionProfile* hlo_execution_profile) override { | ||
return absl::UnimplementedError("Not needed for this test."); | ||
} | ||
}; | ||
|
||
class ExecutableTest : public HloTestBase {}; | ||
|
||
TEST_F(ExecutableTest, HloProtoGetterIsThreadCompatible) { | ||
// Executable::hlo_proto() is doing some lazy initialization of a | ||
// part of `hlo_proto_`. This test ensures that this is done in a | ||
// thread-compatible way. | ||
// Note that this test needs to run with --config=tsan to reliably | ||
// detect any potential data races. | ||
constexpr std::string_view kHloModule = R"( | ||
HloModule module | ||
ENTRY main { | ||
ROOT c = s32[] constant(1) | ||
} | ||
)"; | ||
|
||
TF_ASSERT_OK_AND_ASSIGN(std::shared_ptr<HloModule> module, | ||
ParseAndReturnVerifiedModule(kHloModule)); | ||
|
||
TestExecutable executable(module); | ||
|
||
auto proto = std::make_unique<HloProto>(); | ||
executable.set_hlo_proto(std::move(proto)); | ||
|
||
{ | ||
tsl::thread::ThreadPool pool(tsl::Env::Default(), "test", | ||
/*num_threads=*/2); | ||
for (int i = 0; i < 2; ++i) { | ||
pool.Schedule([&] { executable.hlo_proto()->SerializeAsString(); }); | ||
} | ||
} | ||
} | ||
|
||
} // namespace | ||
} // namespace xla |