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 #ifndef TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
17 #define TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
18
19 #include <algorithm>
20 #include <functional>
21 #include <initializer_list>
22 #include <iterator>
23 #include <memory>
24 #include <random>
25 #include <vector>
26
27 #include "absl/memory/memory.h"
28 #include "absl/strings/str_cat.h"
29 #include "tensorflow/compiler/xla/array.h"
30 #include "tensorflow/compiler/xla/types.h"
31 #include "tensorflow/core/lib/core/bits.h"
32 #include "tensorflow/core/platform/logging.h"
33 #include "tensorflow/core/platform/macros.h"
34 #include "tensorflow/core/platform/types.h"
35
36 namespace xla {
37
38 template <typename T>
39 class Array2D : public Array<T> {
40 public:
Array2D()41 Array2D() : Array<T>(std::vector<int64>{0, 0}) {}
42
Array2D(const int64 n1,const int64 n2)43 Array2D(const int64 n1, const int64 n2)
44 : Array<T>(std::vector<int64>{n1, n2}) {}
45
Array2D(const int64 n1,const int64 n2,const T value)46 Array2D(const int64 n1, const int64 n2, const T value)
47 : Array<T>({n1, n2}, value) {}
48
49 // Creates an array from the given nested initializer list. The outer
50 // initializer list is the first dimension; the inner is the second dimension.
51 // For example, {{1, 2, 3}, {4, 5, 6}} results in an array with n1=2 and n2=3.
Array2D(std::initializer_list<std::initializer_list<T>> values)52 Array2D(std::initializer_list<std::initializer_list<T>> values)
53 : Array<T>(values) {}
54
55 // Creates an array of a floating-point type (half, bfloat16, float,
56 // or double) from the given nested initializer list of float values.
57 template <typename T2, typename = typename std::enable_if<
58 (std::is_same<T, Eigen::half>::value ||
59 std::is_same<T, bfloat16>::value ||
60 std::is_same<T, float>::value ||
61 std::is_same<T, double>::value) &&
62 std::is_same<T2, float>::value>::type>
Array2D(std::initializer_list<std::initializer_list<T2>> values)63 Array2D(std::initializer_list<std::initializer_list<T2>> values)
64 : Array<T>(values) {}
65
Array2D(const Array2D<T> & other)66 Array2D(const Array2D<T>& other) : Array<T>(other) {}
67
n1()68 int64 n1() const { return this->dim(0); }
n2()69 int64 n2() const { return this->dim(1); }
70
height()71 int64 height() const { return this->dim(0); }
width()72 int64 width() const { return this->dim(1); }
73
74 // Fills the array with a pattern of values of the form:
75 //
76 // (rowno << log2ceil(width) | colno) + start_value
77 //
78 // This makes it easy to see distinct row/column values in the array.
79 void FillUnique(T start_value = 0) {
80 for (int64 i0 = 0; i0 < n1(); ++i0) {
81 for (int64 i1 = 0; i1 < n2(); ++i1) {
82 (*this)(i0, i1) =
83 ((i0 << tensorflow::Log2Ceiling64(n2())) | i1) + start_value;
84 }
85 }
86 }
87
88 // Applies f to all cells in this array, in row-major order.
Each(std::function<void (int64,int64,T *)> f)89 void Each(std::function<void(int64, int64, T*)> f) {
90 for (int64 i0 = 0; i0 < n1(); ++i0) {
91 for (int64 i1 = 0; i1 < n2(); ++i1) {
92 f(i0, i1, &(*this)(i0, i1));
93 }
94 }
95 }
96 };
97
98 // Returns a linspace-populated Array2D in the range [from, to] (inclusive)
99 // with dimensions n1 x n2.
100 template <typename NativeT = float>
MakeLinspaceArray2D(double from,double to,int64 n1,int64 n2)101 std::unique_ptr<Array2D<NativeT>> MakeLinspaceArray2D(double from, double to,
102 int64 n1, int64 n2) {
103 auto array = absl::make_unique<Array2D<NativeT>>(n1, n2);
104 int64 count = n1 * n2;
105 NativeT step =
106 static_cast<NativeT>((count > 1) ? (to - from) / (count - 1) : 0);
107 auto set = [&array, n2](int64 index, NativeT value) {
108 (*array)(index / n2, index % n2) = value;
109 };
110 for (int64 i = 0; i < count - 1; ++i) {
111 set(i, (static_cast<NativeT>(from) +
112 static_cast<NativeT>(i) * static_cast<NativeT>(step)));
113 }
114 set(count - 1, static_cast<NativeT>(to));
115 return array;
116 }
117 } // namespace xla
118
119 #endif // TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
120