1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/compiler/xla/service/hlo_execution_profile.h" 17 18 #include <algorithm> 19 #include <utility> 20 #include <vector> 21 22 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 23 #include "tensorflow/compiler/xla/service/hlo_module.h" 24 #include "tensorflow/compiler/xla/service/human_readable_profile_builder.h" 25 #include "tensorflow/compiler/xla/types.h" 26 #include "tensorflow/compiler/xla/util.h" 27 28 namespace xla { 29 HloProfileIndexMap::HloProfileIndexMap(const HloModule& module) { 30 size_t current_profile_index = 0; 31 for (xla::HloComputation* computation : module.MakeComputationPostOrder()) { 32 InsertOrDie(&computation_to_profile_idx_, computation, 33 current_profile_index++); 34 for (const HloInstruction* instruction : computation->instructions()) { 35 // For simplicity we track all instructions here, but we could skip 36 // non-executing instructions like constants and parameters. 37 InsertOrDie(&instruction_to_profile_idx_, instruction, 38 current_profile_index++); 39 } 40 } 41 } 42 43 std::unique_ptr<HloProfilePrinterData> CreateHloProfilePrinterData( 44 const HloProfileIndexMap& hlo_profile_index_map, 45 const HloCostAnalysis& cost_analysis) { 46 using HloComputationInfo = HloProfilePrinterData::HloComputationInfo; 47 using HloInstructionInfo = HloProfilePrinterData::HloInstructionInfo; 48 49 size_t profile_counters_size = hlo_profile_index_map.total_count(); 50 51 std::unique_ptr<HloProfilePrinterData> profile_printer_data = 52 MakeUnique<HloProfilePrinterData>(); 53 profile_printer_data->set_profile_counters_size(profile_counters_size); 54 profile_printer_data->mutable_computation_infos()->Reserve( 55 hlo_profile_index_map.computation_count()); 56 57 const auto& computation_to_profile_idx_map = 58 hlo_profile_index_map.computation_to_profile_idx(); 59 60 // computation_to_profile_idx_map's order is not deterministic so create a 61 // deterministic computation_and_profile_idx_list so that we end up with a 62 // deterministic HloProfilePrinterData protobuf. 63 64 std::vector<std::pair<const HloComputation*, int64>> 65 computation_and_profile_idx_list(computation_to_profile_idx_map.begin(), 66 computation_to_profile_idx_map.end()); 67 68 // The profile indices were computed deterministically in 69 // HloProfileIndexMap::HloProfileIndexMap. 70 c_sort(computation_and_profile_idx_list, 71 [](const std::pair<const HloComputation*, int64>& left, 72 const std::pair<const HloComputation*, int64>& right) { 73 return left.second < right.second; 74 }); 75 76 for (const auto& pair : computation_and_profile_idx_list) { 77 CHECK_LT(pair.second, profile_counters_size); 78 const HloComputation* computation = pair.first; 79 HloComputationInfo* computation_info = 80 profile_printer_data->add_computation_infos(); 81 82 computation_info->set_name(computation->name()); 83 computation_info->set_profile_index(pair.second); 84 computation_info->mutable_instruction_infos()->Reserve( 85 computation->instruction_count()); 86 87 for (const HloInstruction* hlo : computation->instructions()) { 88 HloInstructionInfo* instruction_info = 89 computation_info->add_instruction_infos(); 90 instruction_info->set_long_name(hlo->ToString()); 91 instruction_info->set_short_name( 92 hlo->ToString(HloPrintOptions().set_compact_operands(true))); 93 instruction_info->set_category(hlo->ToCategory()); 94 instruction_info->set_flop_count(cost_analysis.flop_count(*hlo)); 95 instruction_info->set_transcendental_count( 96 cost_analysis.transcendental_count(*hlo)); 97 instruction_info->set_bytes_accessed(cost_analysis.bytes_accessed(*hlo)); 98 instruction_info->set_optimal_seconds( 99 cost_analysis.optimal_seconds(*hlo)); 100 instruction_info->set_profile_index( 101 hlo_profile_index_map.GetProfileIndexFor(*hlo)); 102 } 103 } 104 105 return profile_printer_data; 106 } 107 108 HloExecutionProfile::HloExecutionProfile( 109 const HloProfilePrinterData* hlo_profile_printer_data, 110 const HloProfileIndexMap* hlo_profile_index_map) 111 : hlo_profile_printer_data_(*hlo_profile_printer_data), 112 hlo_profile_index_map_(*hlo_profile_index_map), 113 profile_counters_( 114 /*count*/ hlo_profile_index_map_.total_count(), 115 /*value*/ 0) {} 116 117 void HloExecutionProfile::SetCyclesTakenBy(const HloInstruction* hlo, 118 uint64 cycles_taken) { 119 profile_counters_[hlo_profile_index_map_.GetProfileIndexFor(*hlo)] = 120 cycles_taken; 121 } 122 123 uint64 HloExecutionProfile::GetCyclesTakenBy(const HloInstruction& hlo) const { 124 return profile_counters_[hlo_profile_index_map_.GetProfileIndexFor(hlo)]; 125 } 126 127 } // namespace xla 128