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_ARRAY3D_H_ 17 #define TENSORFLOW_COMPILER_XLA_ARRAY3D_H_ 18 19 #include <algorithm> 20 #include <functional> 21 #include <initializer_list> 22 #include <iterator> 23 #include <memory> 24 #include <numeric> 25 #include <random> 26 27 #include "tensorflow/compiler/xla/array.h" 28 #include "tensorflow/compiler/xla/types.h" 29 #include "tensorflow/core/platform/logging.h" 30 #include "tensorflow/core/platform/macros.h" 31 #include "tensorflow/core/platform/types.h" 32 33 namespace xla { 34 35 // Simple 3D array structure. 36 template <typename T> 37 class Array3D : public Array<T> { 38 public: Array3D()39 Array3D() : Array<T>(std::vector<int64>{0, 0, 0}) {} 40 41 // Creates an array of dimensions n1 x n2 x n3, uninitialized values. Array3D(const int64 n1,const int64 n2,const int64 n3)42 Array3D(const int64 n1, const int64 n2, const int64 n3) 43 : Array<T>(std::vector<int64>{n1, n2, n3}) {} 44 45 // Creates an array of dimensions n1 x n2 x n3, initialized to value. Array3D(const int64 n1,const int64 n2,const int64 n3,const T value)46 Array3D(const int64 n1, const int64 n2, const int64 n3, const T value) 47 : Array<T>(std::vector<int64>{n1, n2, n3}, value) {} 48 49 // Creates an array from the given nested initializer list. The outer 50 // initializer list is the first dimension, and so on. 51 // 52 // For example {{{1, 2}, {3, 4}, {5, 6}, {7, 8}}, 53 // {{9, 10}, {11, 12}, {13, 14}, {15, 16}}, 54 // {{17, 18}, {19, 20}, {21, 22}, {23, 24}}} 55 // results in an array with n1=3, n2=4, n3=2. Array3D(std::initializer_list<std::initializer_list<std::initializer_list<T>>> values)56 Array3D(std::initializer_list<std::initializer_list<std::initializer_list<T>>> 57 values) 58 : Array<T>(values) {} 59 60 // Creates an array of a floating-point type (half, bfloat16, float, 61 // or double) from the given nested initializer list of float values. 62 template <typename T2, typename = typename std::enable_if< 63 (std::is_same<T, Eigen::half>::value || 64 std::is_same<T, bfloat16>::value || 65 std::is_same<T, float>::value || 66 std::is_same<T, double>::value) && 67 std::is_same<T2, float>::value>::type> Array3D(std::initializer_list<std::initializer_list<std::initializer_list<T2>>> values)68 Array3D( 69 std::initializer_list<std::initializer_list<std::initializer_list<T2>>> 70 values) 71 : Array<T>(values) {} 72 n1()73 int64 n1() const { return this->dim(0); } n2()74 int64 n2() const { return this->dim(1); } n3()75 int64 n3() const { return this->dim(2); } 76 }; 77 78 } // namespace xla 79 80 #endif // TENSORFLOW_COMPILER_XLA_ARRAY3D_H_ 81