ddim.h 4.5 KB
Newer Older
W
wangliu 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
朔-望's avatar
朔-望 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once

#include <assert.h>
#include <initializer_list>
#include <stdexcept>
#include <vector>
朔-望's avatar
朔-望 已提交
21 22
#include "common/variant.h"
#include "dim.h"
朔-望's avatar
朔-望 已提交
23 24

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
25 26 27 28 29 30 31 32
namespace framework {

/**
 * \brief A dynamically sized dimension.
 *
 * The number of dimensions must be between [1, 9].
 */
struct DDim {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  typedef Variant<Dim<0>, Dim<1>, Dim<2>, Dim<3>, Dim<4>, Dim<5>, Dim<6>,
                  Dim<7>, Dim<8>, Dim<9>>
      DDimVar;
  DDimVar var;

  template <typename Vistor>
  static typename Vistor::type_t ApplyVistor(Vistor vistor, const DDim &d) {
    if (d.var.TypeId() == typeid(Dim<0>).hash_code()) {
      return vistor(d.var.Get<Dim<0>>());
    } else if (d.var.TypeId() == typeid(Dim<1>).hash_code()) {
      return vistor(d.var.Get<Dim<1>>());
    } else if (d.var.TypeId() == typeid(Dim<2>).hash_code()) {
      return vistor(d.var.Get<Dim<2>>());
    } else if (d.var.TypeId() == typeid(Dim<3>).hash_code()) {
      return vistor(d.var.Get<Dim<3>>());
    } else if (d.var.TypeId() == typeid(Dim<4>).hash_code()) {
      return vistor(d.var.Get<Dim<4>>());
    } else if (d.var.TypeId() == typeid(Dim<5>).hash_code()) {
      return vistor(d.var.Get<Dim<5>>());
    } else if (d.var.TypeId() == typeid(Dim<6>).hash_code()) {
      return vistor(d.var.Get<Dim<6>>());
    } else if (d.var.TypeId() == typeid(Dim<7>).hash_code()) {
      return vistor(d.var.Get<Dim<7>>());
    } else if (d.var.TypeId() == typeid(Dim<8>).hash_code()) {
      return vistor(d.var.Get<Dim<8>>());
    } else if (d.var.TypeId() == typeid(Dim<9>).hash_code()) {
      return vistor(d.var.Get<Dim<9>>());
    } else {
      printf(" dim not support  \n");
      throw std::bad_exception();
      //        return typename Vistor::type_t();
朔-望's avatar
朔-望 已提交
64
    }
65
  }
朔-望's avatar
朔-望 已提交
66

67
  DDim() { var.Set<Dim<1>>(Dim<1>()); }
朔-望's avatar
朔-望 已提交
68

朔-望's avatar
朔-望 已提交
69 70 71 72
  template <int D>
  explicit DDim(const Dim<D> &in) {
    var.Set<Dim<D>>(in);
  }
朔-望's avatar
朔-望 已提交
73

74
  /*implicit*/ DDim(std::initializer_list<int64_t> init_list);
朔-望's avatar
朔-望 已提交
75

朔-望's avatar
朔-望 已提交
76 77
  template <int D>
  DDim &operator=(const Dim<D> &in) {
78 79 80
    var.Set<Dim<D>>(in);
    return *this;
  }
朔-望's avatar
朔-望 已提交
81

82
  int64_t &operator[](int idx);
朔-望's avatar
朔-望 已提交
83

84
  int64_t operator[](int idx) const;
朔-望's avatar
朔-望 已提交
85

86 87 88 89 90 91 92 93 94 95
  //  template <typename Visitor>
  //  typename Visitor::result_type apply_visitor(Visitor& visitor) {
  //    return var.apply_visitor(visitor);
  //  }
  //
  //  template <typename Visitor>
  //  typename Visitor::result_type apply_visitor(Visitor& visitor)
  //  const {
  //    return var.apply_visitor(visitor);
  //  }
朔-望's avatar
朔-望 已提交
96

97
  DDimVar getVar() { return var; }
朔-望's avatar
朔-望 已提交
98

99
  bool operator==(DDim d) const;
朔-望's avatar
朔-望 已提交
100

101
  bool operator!=(DDim d) const;
朔-望's avatar
朔-望 已提交
102

103
  DDim operator+(DDim d) const;
朔-望's avatar
朔-望 已提交
104

105
  DDim operator*(DDim d) const;
朔-望's avatar
朔-望 已提交
106

107
  int size() const;
朔-望's avatar
朔-望 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
};

/**
 * \brief Make a DDim from std::vector<int64_t>
 *
 * \param dims An vector of ints. Must be sized between [1, 9]
 */
DDim make_ddim(const std::vector<int64_t> &dims);

DDim make_ddim(const std::vector<int> &dims);

/**
 * \brief Make a DDim from an initializer list
 *
 * \param dims An initializer list of ints. Must be sized between [1, 9]
 *
 */
DDim make_ddim(std::initializer_list<int64_t> dims);

int64_t get(const DDim &dim, int idx);

void set(DDim &dim, int idx, int val);

std::vector<int64_t> vectorize(const DDim &ddim);

std::vector<int> vectorize2int(const DDim &ddim);

int64_t product(const DDim &ddim);

/**
 * \brief Slice a ddim
 *
 * Slice dim with [begin, end).
 * e.g.  DDim d = make_ddim({1,2,3,4,5});
 *       slice_ddim(d, 1, 3); ====> {2,3}
 */
DDim slice_ddim(const DDim &dim, int begin, int end);

/**
 * \brief What is the length of this dimension?
 *
 * \param Dynamic dimension to inspect
 */
朔-望's avatar
朔-望 已提交
151

朔-望's avatar
朔-望 已提交
152
int arity(const DDim &ddim);
朔-望's avatar
朔-望 已提交
153

朔-望's avatar
朔-望 已提交
154
std::ostream &operator<<(std::ostream &, const DDim &);
朔-望's avatar
朔-望 已提交
155

朔-望's avatar
朔-望 已提交
156 157 158 159
// Reshape a tensor to a matrix. The matrix's first dimension(column
// length)
// will be the product of tensor's first `num_col_dims` dimensions.
DDim flatten_to_2d(const DDim &src, int num_col_dims);
朔-望's avatar
朔-望 已提交
160

朔-望's avatar
朔-望 已提交
161
DDim flatten_to_1d(const DDim &src);
朔-望's avatar
朔-望 已提交
162

朔-望's avatar
朔-望 已提交
163
DDim stride(const DDim &ddim);
朔-望's avatar
朔-望 已提交
164

朔-望's avatar
朔-望 已提交
165
DDim stride_numel(const DDim &ddim);
朔-望's avatar
朔-望 已提交
166 167
}  // namespace framework
}  // namespace paddle_mobile