From fe523d826dd6e2843058c6ff9ef6217bc450de0f Mon Sep 17 00:00:00 2001 From: Berkin Ilbeyi Date: Tue, 26 May 2020 15:59:48 -0700 Subject: [PATCH] [XLA:TPU] Move per-memory-space bytes read/written code to HloCostAnalysis. PiperOrigin-RevId: 313284279 Change-Id: I544c7089c51cb4dad733732149e5bb8fb3b05fa9 --- .../compiler/xla/service/hlo_cost_analysis.cc | 36 +++++++++++++++++++ .../compiler/xla/service/hlo_cost_analysis.h | 8 +++++ 2 files changed, 44 insertions(+) diff --git a/tensorflow/compiler/xla/service/hlo_cost_analysis.cc b/tensorflow/compiler/xla/service/hlo_cost_analysis.cc index 50ba2077411..8a31bc5fef4 100644 --- a/tensorflow/compiler/xla/service/hlo_cost_analysis.cc +++ b/tensorflow/compiler/xla/service/hlo_cost_analysis.cc @@ -1041,6 +1041,42 @@ float HloCostAnalysis::optimal_seconds(const HloInstruction& hlo) const { return GetPropertyForHlo(hlo, kOptimalSecondsKey, hlo_properties_); } +int64 HloCostAnalysis::GetBytesRead(const HloInstruction& hlo, + absl::optional memory_space) const { + int64 bytes_read = 0; + for (int operand_number = 0; operand_number < hlo.operand_count(); + ++operand_number) { + for (const ShapeUtil::IndexedShape& indexed_shape : + ShapeUtil::GetLeafShapes(hlo.operand(operand_number)->shape())) { + absl::optional index_memory_space; + if (indexed_shape.shape.has_layout()) { + index_memory_space = indexed_shape.shape.layout().memory_space(); + } + if (!memory_space || memory_space == index_memory_space) { + bytes_read += + operand_bytes_accessed(hlo, operand_number, indexed_shape.index); + } + } + } + return bytes_read; +} + +int64 HloCostAnalysis::GetBytesWritten( + const HloInstruction& hlo, absl::optional memory_space) const { + int64 bytes_written = 0; + for (const ShapeUtil::IndexedShape& indexed_shape : + ShapeUtil::GetLeafShapes(hlo.shape())) { + absl::optional index_memory_space; + if (indexed_shape.shape.has_layout()) { + index_memory_space = indexed_shape.shape.layout().memory_space(); + } + if (!memory_space || memory_space == index_memory_space) { + bytes_written += output_bytes_accessed(hlo, indexed_shape.index); + } + } + return bytes_written; +} + StatusOr HloCostAnalysis::ProcessSubcomputation( HloComputation* computation) { auto visitor = CreateNestedCostAnalysis(shape_size_, per_second_rates_); diff --git a/tensorflow/compiler/xla/service/hlo_cost_analysis.h b/tensorflow/compiler/xla/service/hlo_cost_analysis.h index 634a6c0572c..d9085dd7785 100644 --- a/tensorflow/compiler/xla/service/hlo_cost_analysis.h +++ b/tensorflow/compiler/xla/service/hlo_cost_analysis.h @@ -164,6 +164,14 @@ class HloCostAnalysis : public ConstDfsHloVisitor { ShapeIndex index = {}) const; float optimal_seconds(const HloInstruction& hlo) const; + // Get bytes read/written by this HLO. If memory_space is provided, it returns + // the bytes read/written from/to the given memory space only. + int64 GetBytesRead(const HloInstruction& hlo, + absl::optional memory_space = absl::nullopt) const; + int64 GetBytesWritten( + const HloInstruction& hlo, + absl::optional memory_space = absl::nullopt) const; + const Properties& properties() const { return properties_sum_; } const float property(const string& key) const { return GetProperty(key, properties());