Skip to content

Commit

Permalink
Fix a bug due to lazy initailization in debugging model.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 598476395
  • Loading branch information
tensorflower-gardener committed Jan 15, 2024
1 parent d269532 commit 416b230
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
2 changes: 2 additions & 0 deletions third_party/xla/xla/lazy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Lazy {
explicit Lazy(absl::AnyInvocable<T() &&> func)
: maybe_value_(std::move(func)) {}

bool has_value() const { return std::holds_alternative<T>(maybe_value_); }

const T& get() const {
if (!std::holds_alternative<T>(maybe_value_)) {
maybe_value_ =
Expand Down
4 changes: 2 additions & 2 deletions third_party/xla/xla/service/hlo_dataflow_analysis.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1648,8 +1648,8 @@ void HloDataflowAnalysis::OptimizePhiValues() {
HloValue::Id phi_id = values[0]->id();
HloValue::Id new_id = phi_graph_.FindOptimizedValue(phi_id);
if (new_id != phi_id) {
VLOG(1) << "Replacing " << values[0]->ToString() << " with "
<< GetValue(new_id).ToString();
VLOG(1) << "Replacing " << values[0]->ToShortString() << " with "
<< GetValue(new_id).ToShortString();
value_set->Clear();
const HloValue& new_value = GetValue(new_id);
value_set->AddValue(&new_value);
Expand Down
22 changes: 13 additions & 9 deletions third_party/xla/xla/service/hlo_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ limitations under the License.
#include "xla/service/hlo_value.h"

#include <algorithm>
#include <memory>
#include <cstdint>
#include <ostream>
#include <string>
#include <utility>
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/inlined_vector.h"
#include "absl/log/check.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/types/span.h"
#include "xla/hlo/ir/hlo_computation.h"
#include "xla/hlo/ir/hlo_instruction.h"
#include "xla/hlo/ir/hlo_module.h"
#include "xla/hlo/ir/hlo_opcode.h"
#include "xla/map_util.h"
#include "xla/service/buffer_value.h"
#include "xla/shape.h"
#include "xla/shape_util.h"
#include "xla/status.h"
#include "xla/types.h"
#include "xla/util.h"
#include "tsl/platform/errors.h"
#include "tsl/platform/logging.h"

namespace xla {
Expand Down Expand Up @@ -95,9 +95,13 @@ std::string HloValue::ToString(int indent) const {
for (const HloPosition& position : positions()) {
StrAppend(&out, indentation, " ", position.ToString(), "\n");
}
StrAppend(&out, indentation, " uses:\n");
for (const HloUse& use : GetUses()) {
StrAppend(&out, indentation, " ", use.ToString(), "\n");
if (uses_.has_value()) {
StrAppend(&out, indentation, " uses:\n");
for (const HloUse& use : GetUses()) {
StrAppend(&out, indentation, " ", use.ToString(), "\n");
}
} else {
StrAppend(&out, indentation, " uses are not initialized yet.\n");
}
StrAppend(&out, indentation, " from instruction:", instruction()->ToString(),
"\n");
Expand Down
10 changes: 7 additions & 3 deletions third_party/xla/xla/service/hlo_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,22 @@ limitations under the License.

#include <stddef.h>

#include <cstdint>
#include <ostream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "absl/algorithm/container.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/inlined_vector.h"
#include "absl/types/span.h"
#include "xla/hlo/ir/hlo_instruction.h"
#include "xla/lazy.h"
#include "xla/service/buffer_value.h"
#include "xla/shape.h"
#include "xla/shape_tree.h"
#include "xla/shape_util.h"
#include "xla/types.h"
#include "xla/xla_data.pb.h"
#include "tsl/platform/logging.h"

Expand Down Expand Up @@ -164,6 +166,8 @@ class HloValue : public BufferValue {

// Return a single-line string representation of the value.
std::string ToShortString() const;
// The returned string doesn't include `uses` if the ToString is called before
// `GetUses` is called.
std::string ToString(int indent) const;
std::string ToString() const override { return ToString(0); }

Expand Down Expand Up @@ -250,7 +254,7 @@ class HloValueSet {
std::vector<const HloValue*> values_;
};

std::ostream& operator<<(std::ostream& out, const HloValueSet& hlo_value);
std::ostream& operator<<(std::ostream& out, const HloValueSet& value_set);

// A class collecting the HloValues which might be contained in the output of
// an HLO instruction. For array-shaped instructions, an InstructionValueSet
Expand Down

0 comments on commit 416b230

Please sign in to comment.