• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/gpu/thunk.h"
17 
18 namespace xla {
19 namespace gpu {
20 
GetGlobalDeviceId() const21 StatusOr<GlobalDeviceId> Thunk::ExecuteParams::GetGlobalDeviceId() const {
22   int64_t local_device_ordinal = stream->parent()->device_ordinal();
23   if (gpu_global_device_ids) {
24     TF_RET_CHECK(0 <= local_device_ordinal &&
25                  local_device_ordinal < gpu_global_device_ids->size());
26     return (*gpu_global_device_ids)[local_device_ordinal];
27   } else {
28     // No local -> global mapping was provided; assume the identity mapping.
29     return GlobalDeviceId(local_device_ordinal);
30   }
31 }
32 
KindToString(Thunk::Kind kind)33 /*static*/ absl::string_view Thunk::KindToString(Thunk::Kind kind) {
34   switch (kind) {
35     case Thunk::kCholesky:
36       return "kCholesky";
37     case Thunk::kCollectivePermute:
38       return "kCollectivePermute";
39     case Thunk::kConditional:
40       return "kConditional";
41     case Thunk::kConvolution:
42       return "kConvolution";
43     case Thunk::kCopy:
44       return "kCopy";
45     case Thunk::kCudnnBatchNormBackward:
46       return "kCudnnBatchNormBackward";
47     case Thunk::kCudnnBatchNormForwardInference:
48       return "kCudnnBatchNormForwardInference";
49     case Thunk::kCudnnBatchNormForwardTraining:
50       return "kCudnnBatchNormForwardTraining";
51     case Thunk::kCustomCall:
52       return "kCustomCall";
53     case Thunk::kNcclAllGather:
54       return "kNcclAllGather";
55     case Thunk::kNcclAllReduce:
56       return "kNcclAllReduce";
57     case Thunk::kNcclAllReduceStart:
58       return "kNcclAllReduceStart";
59     case Thunk::kNcclAllReduceDone:
60       return "kNcclAllReduceDone";
61     case Thunk::kNcclReduceScatter:
62       return "kNcclReduceScatter";
63     case Thunk::kNcclAllToAll:
64       return "kNcclAllToAll";
65     case Thunk::kFft:
66       return "kFft";
67     case Thunk::kGemm:
68       return "kGemm";
69     case Thunk::kInfeed:
70       return "kInfeed";
71     case Thunk::kKernel:
72       return "kKernel";
73     case Thunk::kMemset32BitValue:
74       return "kMemset32BitValue";
75     case Thunk::kMemzero:
76       return "kMemzero";
77     case Thunk::kOutfeed:
78       return "kOutfeed";
79     case Thunk::kReplicaId:
80       return "kReplicaId";
81     case Thunk::kPartitionId:
82       return "kPartitionId";
83     case Thunk::kSequential:
84       return "kSequential";
85     case Thunk::kTriangularSolve:
86       return "kTriangularSolve";
87     case Thunk::kWhile:
88       return "kWhile";
89   }
90 }
91 
operator <<(std::ostream & os,Thunk::Kind kind)92 std::ostream& operator<<(std::ostream& os, Thunk::Kind kind) {
93   return os << Thunk::KindToString(kind);
94 }
95 
ToString(int indent,std::function<std::string (const Thunk *)> get_thunk_annotation) const96 std::string ThunkSequence::ToString(
97     int indent,
98     std::function<std::string(const Thunk*)> get_thunk_annotation) const {
99   const std::string indent_str(" ", indent * 2);
100   if (empty()) return indent_str + "No thunks.";
101 
102   auto thunk_with_longest_kind = absl::c_max_element(
103       *this,
104       [](const std::unique_ptr<Thunk>& a, const std::unique_ptr<Thunk>& b) {
105         return Thunk::KindToString(a->kind()).length() <
106                Thunk::KindToString(b->kind()).length();
107       });
108   int64_t max_thunk_kind_len =
109       Thunk::KindToString(thunk_with_longest_kind->get()->kind()).length();
110   std::string result;
111   for (const std::unique_ptr<Thunk>& thunk : *this) {
112     // Write out the thunk kind, padded out to max_thunk_kind_len.
113     absl::string_view kind_str = Thunk::KindToString(thunk->kind());
114     absl::StrAppend(&result, indent_str, kind_str,
115                     string(max_thunk_kind_len - kind_str.length(), ' '), "\t");
116     if (get_thunk_annotation) {
117       absl::StrAppend(&result, get_thunk_annotation(thunk.get()));
118     }
119     absl::StrAppend(&result, thunk->ToStringExtra(indent));
120     absl::StrAppend(&result, "\n");
121   }
122   return result;
123 }
124 
125 }  // namespace gpu
126 }  // namespace xla
127