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