Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[XLA] Add option to omit the layout string when printing HloInstructions #26930

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions tensorflow/compiler/xla/service/hlo_instruction.cc
Expand Up @@ -2158,9 +2158,15 @@ string HloInstruction::ToStringWithCanonicalNameMap(
StrAppend(&result, PrintName(name(), options), " = ");
}

// Print opcode, operand(s) and shape.
StrAppend(&result, ShapeUtil::HumanStringWithLayout(shape()), " ",
HloOpcodeString(opcode()), "(",
// Print shape.
if (options.include_layout_in_shapes()) {
StrAppend(&result, ShapeUtil::HumanStringWithLayout(shape()));
} else {
StrAppend(&result, ShapeUtil::HumanString(shape()));
}

// Print opcode, operand(s).
StrAppend(&result, " ", HloOpcodeString(opcode()), "(",
OperandsToStringWithCanonicalNameMap(options, canonical_name_map),
")");

Expand Down Expand Up @@ -2204,7 +2210,11 @@ string HloInstruction::OperandsToStringWithCanonicalNameMap(
}
std::vector<string> str;
if (options.print_operand_shape()) {
str.push_back(ShapeUtil::HumanStringWithLayout(operand->shape()));
if (options.include_layout_in_shapes()) {
str.push_back(ShapeUtil::HumanStringWithLayout(operand->shape()));
} else {
str.push_back(ShapeUtil::HumanString(operand->shape()));
}
}

// In a top-level HloInstruction::ToString() call, the operand name is not
Expand Down
10 changes: 10 additions & 0 deletions tensorflow/compiler/xla/service/hlo_instruction.h
Expand Up @@ -78,6 +78,7 @@ class HloPrintOptions {
print_metadata_(true),
print_backend_config_(true),
compact_operands_(false),
include_layout_in_shapes_(true),
print_operand_shape_(true),
print_operand_names_(true),
print_program_shape_(true),
Expand Down Expand Up @@ -176,6 +177,13 @@ class HloPrintOptions {
return *this;
}

// If true, include the layout in any shapes that are printed (instruction
// and operands).
HloPrintOptions& set_include_layout_in_shapes(bool value) {
include_layout_in_shapes_ = value;
return *this;
}

// If true, canonicalizes instructions' name. Instead of using "%foo.1" as
// the name of an instruction, we use "%tmp_1", "%tmp_2" etc.
HloPrintOptions& set_canonicalize_instruction_names(bool value) {
Expand Down Expand Up @@ -203,6 +211,7 @@ class HloPrintOptions {
bool print_metadata() const { return print_metadata_; }
bool print_backend_config() const { return print_backend_config_; }
bool compact_operands() const { return compact_operands_; }
bool include_layout_in_shapes() const { return include_layout_in_shapes_; }
bool print_operand_shape() const { return print_operand_shape_; }
bool print_operand_names() const { return print_operand_names_; }
bool print_program_shape() const { return print_program_shape_; }
Expand All @@ -222,6 +231,7 @@ class HloPrintOptions {
bool print_metadata_;
bool print_backend_config_;
bool compact_operands_;
bool include_layout_in_shapes_;
bool print_operand_shape_;
bool print_operand_names_;
bool print_program_shape_;
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/compiler/xla/service/hlo_instruction_test.cc
Expand Up @@ -1331,6 +1331,15 @@ TEST_F(HloInstructionTest, Stringification) {
"%dot = f32[5,20]{1,0} dot(f32[5,10]{1,0} %x, f32[10,20]{1,0} "
"%transpose), lhs_contracting_dims={1}, rhs_contracting_dims={0}");

auto options2 = HloPrintOptions().set_print_metadata(false)
.set_print_operand_shape(false)
.set_print_percent(false)
.set_include_layout_in_shapes(false);

EXPECT_EQ(dot->ToString(options2),
"dot = f32[5,20] dot(x, transpose), "
"lhs_contracting_dims={1}, rhs_contracting_dims={0}");

auto module = CreateNewVerifiedModule();
auto* computation = module->AddEntryComputation(builder.Build());

Expand Down