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/computation_layout.h"
17
18 #include <algorithm>
19
20 #include "absl/strings/str_cat.h"
21 #include "absl/strings/str_join.h"
22 #include "tensorflow/compiler/xla/types.h"
23 #include "tensorflow/core/lib/hash/hash.h"
24
25 namespace xla {
26
ComputationLayout(const ProgramShape & program_shape,bool ignore_layouts)27 ComputationLayout::ComputationLayout(const ProgramShape& program_shape,
28 bool ignore_layouts)
29 : result_layout_(program_shape.result()) {
30 for (auto& shape : program_shape.parameters()) {
31 parameter_layouts_.emplace_back(shape);
32 }
33 if (ignore_layouts) {
34 SetToDefaultLayout();
35 } else {
36 SetToDefaultLayoutIfEmpty();
37 }
38 }
39
SetToDefaultLayout()40 void ComputationLayout::SetToDefaultLayout() {
41 for (auto& parameter_layout : parameter_layouts_) {
42 parameter_layout.SetToDefaultLayout();
43 }
44 result_layout_.SetToDefaultLayout();
45 }
46
SetToDefaultLayoutIfEmpty()47 void ComputationLayout::SetToDefaultLayoutIfEmpty() {
48 for (auto& parameter_layout : parameter_layouts_) {
49 if (!parameter_layout.LayoutIsSet()) {
50 parameter_layout.SetToDefaultLayout();
51 }
52 }
53 if (!result_layout_.LayoutIsSet()) {
54 result_layout_.SetToDefaultLayout();
55 }
56 }
57
LayoutIsSet() const58 bool ComputationLayout::LayoutIsSet() const {
59 return absl::c_all_of(parameter_layouts_,
60 [](const ShapeLayout& s) { return s.LayoutIsSet(); }) &&
61 result_layout_.LayoutIsSet();
62 }
63
ToString() const64 string ComputationLayout::ToString() const {
65 std::vector<string> params;
66 for (auto& param_layout : parameter_layouts_) {
67 params.push_back(param_layout.ToString());
68 }
69 return absl::StrCat("(", absl::StrJoin(params, ", "), ") => ",
70 result_layout_.ToString());
71 }
72
ComputeProgramShape() const73 ProgramShape ComputationLayout::ComputeProgramShape() const {
74 ProgramShape program_shape;
75 for (int64_t i = 0; i < parameter_layouts_.size(); ++i) {
76 *program_shape.add_parameters() = parameter_layouts_[i].shape();
77 *program_shape.add_parameter_names() = absl::StrCat("p", i);
78 }
79 *program_shape.mutable_result() = result_layout_.shape();
80 return program_shape;
81 }
82
operator ==(const ComputationLayout & other) const83 bool ComputationLayout::operator==(const ComputationLayout& other) const {
84 return result_layout() == other.result_layout() &&
85 parameter_layouts() == other.parameter_layouts();
86 }
87
operator !=(const ComputationLayout & other) const88 bool ComputationLayout::operator!=(const ComputationLayout& other) const {
89 return result_layout() != other.result_layout() ||
90 parameter_layouts() != other.parameter_layouts();
91 }
92
Hash() const93 uint64 ComputationLayout::Hash() const {
94 uint64 hash_value = ShapeUtil::Hash(result_layout_.shape());
95 for (const auto& parameter_layout : parameter_layouts_) {
96 hash_value = tensorflow::Hash64Combine(
97 hash_value, ShapeUtil::Hash(parameter_layout.shape()));
98 }
99 return hash_value;
100 }
101
102 } // namespace xla
103