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