Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 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_KERNELS_REVERSE_SEQUENCE_OP_H_
     17 #define TENSORFLOW_KERNELS_REVERSE_SEQUENCE_OP_H_
     18 // Generator definition for ReverseSequenceOp, must be compilable by nvcc.
     19 
     20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     21 #include "tensorflow/core/framework/tensor_types.h"
     22 #include "tensorflow/core/platform/types.h"
     23 
     24 namespace tensorflow {
     25 
     26 namespace generator {
     27 
     28 template <typename T, typename Tlen, size_t Dims>
     29 class ReverseGenerator {
     30  public:
     31   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
     32   ReverseGenerator(typename TTypes<T, Dims>::ConstTensor input, int32 batch_dim,
     33                    int32 seq_dim, typename TTypes<Tlen>::ConstVec seq_lengths)
     34       : input_(input),
     35         batch_dim_(batch_dim),
     36         seq_dim_(seq_dim),
     37         seq_lengths_(seq_lengths) {}
     38 
     39   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T
     40   operator()(const Eigen::array<Eigen::DenseIndex, Dims>& coords) const {
     41     Eigen::array<Eigen::DenseIndex, Dims> new_coords = coords;
     42     if (coords[seq_dim_] < seq_lengths_(coords[batch_dim_])) {
     43       new_coords[seq_dim_] =
     44           seq_lengths_(coords[batch_dim_]) - coords[seq_dim_] - 1;
     45     }
     46 
     47     return input_(new_coords);
     48   }
     49 
     50  private:
     51   typename TTypes<T, Dims>::ConstTensor input_;
     52   int32 batch_dim_;
     53   int32 seq_dim_;
     54   typename TTypes<Tlen>::ConstVec seq_lengths_;
     55 };
     56 
     57 }  // namespace generator
     58 
     59 namespace functor {
     60 
     61 template <typename Device, typename T, typename Tlen, size_t Dims>
     62 struct ReverseSequence {
     63   EIGEN_ALWAYS_INLINE static void Compute(
     64       const Device& d, typename TTypes<T, Dims>::ConstTensor input,
     65       int32 batch_dim, int32 seq_dim,
     66       typename TTypes<Tlen>::ConstVec seq_lengths,
     67       typename TTypes<T, Dims>::Tensor output) {
     68     generator::ReverseGenerator<T, Tlen, Dims> generator(input, batch_dim,
     69                                                          seq_dim, seq_lengths);
     70     output.device(d) = input.generate(generator);
     71   }
     72 };
     73 
     74 }  // namespace functor
     75 
     76 }  // namespace tensorflow
     77 
     78 #endif  // TENSORFLOW_KERNELS_REVERSE_SEQUENCE_OP_H_
     79