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_ARRAY4D_H_
     17 #define TENSORFLOW_COMPILER_XLA_ARRAY4D_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 #include <string>
     27 #include <vector>
     28 
     29 #include "tensorflow/compiler/xla/array.h"
     30 #include "tensorflow/compiler/xla/array2d.h"
     31 #include "tensorflow/compiler/xla/types.h"
     32 #include "tensorflow/core/lib/gtl/array_slice.h"
     33 #include "tensorflow/core/lib/strings/str_util.h"
     34 #include "tensorflow/core/lib/strings/strcat.h"
     35 #include "tensorflow/core/lib/strings/stringprintf.h"
     36 #include "tensorflow/core/platform/logging.h"
     37 #include "tensorflow/core/platform/macros.h"
     38 #include "tensorflow/core/platform/types.h"
     39 
     40 namespace xla {
     41 
     42 // Simple 4D array structure, similar in form to Array2D, for use primarily in
     43 // testing and describing to XLA APIs values in the 4D array structures used
     44 // in convolutions.
     45 //
     46 // The data layout is, in order from major to minor:
     47 //
     48 //    First dimension: plane, batch, n1
     49 //   Second dimension: depth, feature, z, n2
     50 //    Third dimension: height, y, n3
     51 //   Fourth dimension: width, x, n4
     52 //
     53 // These dimensions are referred to by various names, so that is why
     54 // more than one name is given above. See operator() for the exact
     55 // calculation of 1d indices from 4d indices.
     56 template <typename T>
     57 class Array4D : public Array<T> {
     58  public:
     59   // Creates a 4D array, uninitialized values.
     60   Array4D(int64 planes, int64 depth, int64 height, int64 width)
     61       : Array<T>(std::vector<int64>{planes, depth, height, width}) {}
     62 
     63   // Creates a 4D array, initialized to value.
     64   Array4D(int64 planes, int64 depth, int64 height, int64 width, T value)
     65       : Array<T>(std::vector<int64>{planes, depth, height, width}, value) {}
     66 
     67   // Creates a 4D array, filled with values.
     68   //
     69   // We need to set a default type for Container so that code like
     70   // Array4D(1, 1, 1, 1, {1}) will work. The template cannot infer the
     71   // initializer_list type in that case without this default.
     72   template <typename Container = std::initializer_list<T>>
     73   Array4D(int64 planes, int64 depth, int64 height, int64 width,
     74           const Container& values)
     75       : Array4D(planes, depth, height, width) {
     76     this->SetValues(values);
     77   }
     78 
     79   // Construct an Array4D with the given nested initializer list.
     80   Array4D(std::initializer_list<std::initializer_list<
     81               std::initializer_list<std::initializer_list<T>>>>
     82               values)
     83       : Array<T>(values) {}
     84 
     85   // Creates an array of Eigen::half from the given nested initializer list of
     86   // float values.
     87   template <typename T2, typename = typename std::enable_if<
     88                              std::is_same<T, Eigen::half>::value &&
     89                              std::is_same<T2, float>::value>::type>
     90   Array4D(std::initializer_list<std::initializer_list<
     91               std::initializer_list<std::initializer_list<T2>>>>
     92               values)
     93       : Array<T>(values) {}
     94 
     95   // Numerically-named aliases for the various dimensions. This matches the
     96   // dimension names used in array3d.
     97   int64 n4() const { return this->dim(3); }
     98   int64 n3() const { return this->dim(2); }
     99   int64 n2() const { return this->dim(1); }
    100   int64 n1() const { return this->dim(0); }
    101 
    102   int64 width() const { return this->dim(3); }
    103   int64 height() const { return this->dim(2); }
    104   int64 depth() const { return this->dim(1); }
    105   int64 planes() const { return this->dim(0); }
    106 
    107   // Fills all of the {p,z} with the array provided, which specifies {y,x}.
    108   void FillWithYX(const Array2D<T>& value) {
    109     CHECK_EQ(value.height(), height());
    110     CHECK_EQ(value.width(), width());
    111     for (int64 plane = 0; plane < planes(); ++plane) {
    112       for (int64 depth = 0; depth < this->depth(); ++depth) {
    113         for (int64 height = 0; height < this->height(); ++height) {
    114           for (int64 width = 0; width < this->width(); ++width) {
    115             (*this)(plane, depth, height, width) = value(height, width);
    116           }
    117         }
    118       }
    119     }
    120   }
    121 
    122   // Fills all of the {x,y} with the array provided, which specifies {p,z}.
    123   void FillWithPZ(const Array2D<T>& value) {
    124     CHECK_EQ(value.height(), planes());
    125     CHECK_EQ(value.width(), depth());
    126     for (int64 height = 0; height < this->height(); ++height) {
    127       for (int64 width = 0; width < this->width(); ++width) {
    128         for (int64 plane = 0; plane < planes(); ++plane) {
    129           for (int64 depth = 0; depth < this->depth(); ++depth) {
    130             (*this)(plane, depth, height, width) = value(plane, depth);
    131           }
    132         }
    133       }
    134     }
    135   }
    136 
    137   // Fills each of the minor-dim matrices with a number designating which minor
    138   // dim matrix is enclosed by the shape.
    139   void FillWithMinorDimNum() {
    140     LOG(INFO) << "width: " << this->width();
    141     LOG(INFO) << "height: " << this->height();
    142     LOG(INFO) << "depth: " << this->depth();
    143     LOG(INFO) << "planes: " << this->planes();
    144     for (int64 height = 0; height < this->height(); ++height) {
    145       for (int64 width = 0; width < this->width(); ++width) {
    146         for (int64 plane = 0; plane < planes(); ++plane) {
    147           for (int64 depth = 0; depth < this->depth(); ++depth) {
    148             float this_val = plane * this->depth() + depth;
    149             (*this)(plane, depth, height, width) = this_val;
    150           }
    151         }
    152       }
    153     }
    154   }
    155 };
    156 
    157 }  // namespace xla
    158 
    159 #endif  // TENSORFLOW_COMPILER_XLA_ARRAY4D_H_
    160