Home | History | Annotate | Download | only in xla
      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 "tensorflow/compiler/xla/array.h"
     28 #include "tensorflow/compiler/xla/types.h"
     29 #include "tensorflow/core/lib/core/bits.h"
     30 #include "tensorflow/core/lib/strings/str_util.h"
     31 #include "tensorflow/core/lib/strings/strcat.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:
     41   Array2D() : Array<T>(std::vector<int64>{0, 0}) {}
     42 
     43   Array2D(const int64 n1, const int64 n2)
     44       : Array<T>(std::vector<int64>{n1, n2}) {}
     45 
     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.
     52   Array2D(std::initializer_list<std::initializer_list<T>> values)
     53       : Array<T>(values) {}
     54 
     55   // Creates an array of Eigen::half from the given nested initializer list of
     56   // float values.
     57   template <typename T2, typename = typename std::enable_if<
     58                              std::is_same<T, Eigen::half>::value &&
     59                              std::is_same<T2, float>::value>::type>
     60   Array2D(std::initializer_list<std::initializer_list<T2>> values)
     61       : Array<T>(values) {}
     62 
     63   Array2D(const Array2D<T>& other) : Array<T>(other) {}
     64 
     65   int64 n1() const { return this->dim(0); }
     66   int64 n2() const { return this->dim(1); }
     67 
     68   int64 height() const { return this->dim(0); }
     69   int64 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     for (int64 i0 = 0; i0 < n1(); ++i0) {
     78       for (int64 i1 = 0; i1 < n2(); ++i1) {
     79         (*this)(i0, i1) =
     80             ((i0 << tensorflow::Log2Ceiling64(n2())) | i1) + start_value;
     81       }
     82     }
     83   }
     84 
     85   // Applies f to all cells in this array, in row-major order.
     86   void Each(std::function<void(int64, int64, T*)> f) {
     87     for (int64 i0 = 0; i0 < n1(); ++i0) {
     88       for (int64 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 std::unique_ptr<Array2D<float>> MakeLinspaceArray2D(float from, float to,
     98                                                     int64 n1, int64 n2);
     99 
    100 }  // namespace xla
    101 
    102 #endif  // TENSORFLOW_COMPILER_XLA_ARRAY2D_H_
    103