Skip to content

Commit

Permalink
Actually support the flag xla_dump_large_constants.
Browse files Browse the repository at this point in the history
It was missing a flag declaration in debug_options_flags.cc
Add a test for the dumping tool to make sure the flag works.

PiperOrigin-RevId: 642186864
  • Loading branch information
akuegel authored and tensorflower-gardener committed Jun 11, 2024
1 parent dcac32f commit 2a2b8f5
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 10 deletions.
9 changes: 9 additions & 0 deletions third_party/xla/xla/debug_options_flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ DebugOptions DefaultDebugOptionsIgnoringFlags() {
opts.set_xla_dump_max_hlo_modules(-1);
opts.set_xla_dump_module_metadata(false);
opts.set_xla_dump_hlo_as_long_text(false);
opts.set_xla_dump_large_constants(false);
opts.set_xla_dump_enable_mlir_pretty_form(true);
opts.set_xla_debug_buffer_assignment_show_max(15);
#ifdef ENABLE_MKL
Expand Down Expand Up @@ -849,6 +850,14 @@ void MakeDebugOptionsFlags(std::vector<tsl::Flag>* flag_list,
"debug_options "
"are written to the --xla_dump_to dir, or, if no dir is specified, to "
"stdout. Ignored unless xla_dump_hlo_as_text is true."));
flag_list->push_back(
tsl::Flag("xla_dump_large_constants",
bool_setter_for(&DebugOptions::set_xla_dump_large_constants),
debug_options->xla_dump_large_constants(),
"Dumps HLO modules including large constants before and after "
"optimizations. debug_options are written to the --xla_dump_to "
"dir, or, if no dir is specified, to stdout. Ignored unless "
"xla_dump_hlo_as_text is true."));
flag_list->push_back(
tsl::Flag("xla_dump_hlo_as_proto",
bool_setter_for(&DebugOptions::set_xla_dump_hlo_as_proto),
Expand Down
17 changes: 17 additions & 0 deletions third_party/xla/xla/service/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,23 @@ cc_library(
],
)

xla_cc_test(
name = "dump_test",
srcs = ["dump_test.cc"],
deps = [
":dump",
":hlo_module_config",
":hlo_parser",
"//xla:xla_proto_cc",
"//xla/tests:xla_internal_test_main",
"@com_google_absl//absl/strings",
"@local_tsl//tsl/platform:env",
"@local_tsl//tsl/platform:path",
"@local_tsl//tsl/platform:statusor",
"@local_tsl//tsl/platform:test",
],
)

cc_library(
name = "shape_inference",
srcs = ["shape_inference.cc"],
Expand Down
15 changes: 9 additions & 6 deletions third_party/xla/xla/service/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -700,21 +700,24 @@ void DumpPerModuleProtobufToFile(const HloModule& module,
DumpProtobufToFile(proto, debug_options, filename, std::move(text_formatter));
}

void DumpHloModuleIfEnabled(const HloModule& module, string_view name) {
std::vector<std::string> DumpHloModuleIfEnabled(const HloModule& module,
string_view name) {
CanonicalDebugOptions opts(module.config().debug_options());
if (opts.should_dump_module(module.name())) {
DumpHloModuleImpl(module, /*buffer_assn=*/nullptr, TimestampFor(module),
name, opts);
return DumpHloModuleImpl(module, /*buffer_assn=*/nullptr,
TimestampFor(module), name, opts);
}
return {};
}

void DumpHloModuleIfEnabled(const HloModule& module,
const BufferAssignment& buffer_assn,
string_view name) {
std::vector<std::string> DumpHloModuleIfEnabled(
const HloModule& module, const BufferAssignment& buffer_assn,
string_view name) {
CanonicalDebugOptions opts(module.config().debug_options());
if (opts.should_dump_module(module.name())) {
DumpHloModuleImpl(module, &buffer_assn, TimestampFor(module), name, opts);
}
return {};
}

bool DumpingEnabledForHloModule(string_view hlo_module_name,
Expand Down
11 changes: 7 additions & 4 deletions third_party/xla/xla/service/dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ void DumpPerModuleProtobufToFile(const HloModule& module,

// Dumps the given HLO module if dumping is enabled for the module. Exactly
// where and in what formats it's dumped is determined by the module's config.
void DumpHloModuleIfEnabled(const HloModule& module, absl::string_view name);
void DumpHloModuleIfEnabled(const HloModule& module,
const BufferAssignment& buffer_assn,
absl::string_view name);
// Returns the full file paths of all dumps of the module, or an empty vector if
// nothing was dumped.
std::vector<std::string> DumpHloModuleIfEnabled(const HloModule& module,
absl::string_view name);
std::vector<std::string> DumpHloModuleIfEnabled(
const HloModule& module, const BufferAssignment& buffer_assn,
absl::string_view name);

// Dumps the given HLO module after running one HLO pass and before running
// another, if that's enabled. Returns the full file paths of all dumps of the
Expand Down
89 changes: 89 additions & 0 deletions third_party/xla/xla/service/dump_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* 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/dump.h"

#include <memory>
#include <string>

#include "absl/strings/match.h"
#include "xla/service/hlo_module_config.h"
#include "xla/service/hlo_parser.h"
#include "xla/xla.pb.h"
#include "tsl/platform/env.h"
#include "tsl/platform/statusor.h"
#include "tsl/platform/test.h"

namespace xla {
namespace {

TEST(DumpHloIfEnabled, LargeConstantElided) {
HloModuleConfig config;
DebugOptions options = config.debug_options();
auto env = tsl::Env::Default();
std::string dump_dir;
EXPECT_TRUE(env->LocalTempFilename(&dump_dir));
options.set_xla_dump_to(dump_dir);
options.set_xla_dump_hlo_as_text(true);
options.set_xla_dump_large_constants(false);
config.set_debug_options(options);
const char* kModuleStr = R"(
HloModule m
test {
p0 = s32[11] parameter(0)
c = s32[11] constant({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
ROOT x = s32[11] multiply(p0, c)
}
)";
TF_ASSERT_OK_AND_ASSIGN(auto m,
ParseAndReturnUnverifiedModule(kModuleStr, config));
std::string dump_name = "dump";
auto paths = DumpHloModuleIfEnabled(*m, dump_name);
EXPECT_EQ(paths.size(), 1);
std::string data;
EXPECT_TRUE(ReadFileToString(env, paths[0], &data).ok());
EXPECT_TRUE(absl::StrContains(data, "{...}"));
}

TEST(DumpHloIfEnabled, LargeConstantPrinted) {
HloModuleConfig config;
DebugOptions options = config.debug_options();
auto env = tsl::Env::Default();
std::string dump_dir;
EXPECT_TRUE(env->LocalTempFilename(&dump_dir));
options.set_xla_dump_to(dump_dir);
options.set_xla_dump_hlo_as_text(true);
options.set_xla_dump_large_constants(true);
config.set_debug_options(options);
const char* kModuleStr = R"(
HloModule m
test {
p0 = s32[11] parameter(0)
c = s32[11] constant({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
ROOT x = s32[11] multiply(p0, c)
}
)";
TF_ASSERT_OK_AND_ASSIGN(auto m,
ParseAndReturnUnverifiedModule(kModuleStr, config));
std::string dump_name = "dump";
auto paths = DumpHloModuleIfEnabled(*m, dump_name);
EXPECT_EQ(paths.size(), 1);
std::string data;
EXPECT_TRUE(ReadFileToString(env, paths[0], &data).ok());
EXPECT_TRUE(!absl::StrContains(data, "{...}"));
}

} // namespace
} // namespace xla

0 comments on commit 2a2b8f5

Please sign in to comment.