Home | History | Annotate | Download | only in internal
      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 #ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_TENSOR_H_
     16 #define TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_TENSOR_H_
     17 
     18 #include <vector>
     19 #include "tensorflow/contrib/lite/context.h"
     20 #include "tensorflow/contrib/lite/kernels/internal/types.h"
     21 
     22 namespace tflite {
     23 
     24 template <typename T>
     25 inline T* GetTensorData(TfLiteTensor* tensor);
     26 
     27 template <>
     28 inline float* GetTensorData(TfLiteTensor* tensor) {
     29   return tensor != nullptr ? tensor->data.f : nullptr;
     30 }
     31 
     32 template <>
     33 inline uint8_t* GetTensorData(TfLiteTensor* tensor) {
     34   return tensor != nullptr ? tensor->data.uint8 : nullptr;
     35 }
     36 
     37 template <>
     38 inline int32_t* GetTensorData(TfLiteTensor* tensor) {
     39   return tensor != nullptr ? tensor->data.i32 : nullptr;
     40 }
     41 
     42 template <>
     43 inline int64_t* GetTensorData(TfLiteTensor* tensor) {
     44   return tensor != nullptr ? tensor->data.i64 : nullptr;
     45 }
     46 
     47 inline int RemapDim(int max_dimensions, int d) {
     48   return max_dimensions - d - 1;
     49 }
     50 
     51 // TODO(ahentz): the implementations in kernels/internal/ take a Dims<4> object
     52 // even if the original tensors were not 4D. We should consider rewriting them
     53 // to take a more generic 'shape' object.
     54 inline Dims<4> GetTensorDims(const int data[], const int size) {
     55   Dims<4> d;
     56   for (int i = 0; i < 4; ++i) {
     57     int src = size - i - 1;
     58     if (src >= 0) {
     59       d.sizes[i] = data[src];
     60     } else {
     61       d.sizes[i] = 1;
     62     }
     63   }
     64   d.strides[0] = 1;
     65   for (int i = 1; i < 4; i++) {
     66     d.strides[i] = d.strides[i - 1] * d.sizes[i - 1];
     67   }
     68   return d;
     69 }
     70 
     71 inline Dims<4> GetTensorDims(std::vector<int32_t> data) {
     72   return GetTensorDims(data.data(), data.size());
     73 }
     74 
     75 inline Dims<4> GetTensorDims(const TfLiteTensor* tensor) {
     76   if (tensor == nullptr) {
     77     return Dims<4>();
     78   }
     79 
     80   auto* dims = tensor->dims;
     81   return GetTensorDims(dims->data, dims->size);
     82 }
     83 
     84 // A list of tensors in a format that can be used by kernels like split and
     85 // concatenation.
     86 template <typename T>
     87 class VectorOfTensors {
     88  public:
     89   // Build with the tensors in 'tensor_list'.
     90   VectorOfTensors(const TfLiteContext& context,
     91                   const TfLiteIntArray& tensor_list) {
     92     int num_tensors = tensor_list.size;
     93 
     94     all_data_.reserve(num_tensors);
     95     all_dims_.reserve(num_tensors);
     96     all_dims_ptr_.reserve(num_tensors);
     97 
     98     for (int i = 0; i < num_tensors; ++i) {
     99       TfLiteTensor* t = &context.tensors[tensor_list.data[i]];
    100       all_data_.push_back(GetTensorData<T>(t));
    101       all_dims_.push_back(GetTensorDims(t));
    102     }
    103 
    104     // Taking the pointer from inside a std::vector is only OK if the vector is
    105     // never modified, so we populate all_dims in the previous loop and then we
    106     // are free to grab iterators here.
    107     for (int i = 0; i < num_tensors; ++i) {
    108       all_dims_ptr_.push_back(&all_dims_[i]);
    109     }
    110   }
    111   // Return a pointer to the data pointers of all tensors in the list. For
    112   // example:
    113   //   float* const* f = v.data();
    114   //   f[0][1] is the second element of the first tensor.
    115   T* const* data() const { return all_data_.data(); }
    116 
    117   // Return a pointer the dim pointers of all tensors in the list. For
    118   // example:
    119   //   const Dims<4>* const* d = v.dims();
    120   //   dims[1] are the dimensions of the second tensor in the list.
    121   const Dims<4>* const* dims() const { return all_dims_ptr_.data(); }
    122 
    123  private:
    124   std::vector<T*> all_data_;
    125   std::vector<Dims<4>> all_dims_;
    126   std::vector<Dims<4>*> all_dims_ptr_;
    127 };
    128 
    129 }  // namespace tflite
    130 
    131 #endif  // TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_TENSOR_H_
    132