Skip to content

Hard to manipulate Eigen::DSizes when rank of dimension is dynamic #4091

@reyoung

Description

@reyoung
template <size_t N>
using EigenDim = Eigen::DSizes<Eigen::DenseIndex, N>;

The rank of a tensor could be set by user configuration with type std::vector<int>. However, the rank of Eigen::DSizes is a template argument. It must be decided when compiling C++ but std::vector<int>::size() is a runtime method. It is hard to cast std::vector<int> to Eigen::DSizes.

For example, the reshape operator's shape is configured by a user, and Paddle reads that attribute in runtime. That attribute is a vector<int>. To cast vector<int> to EigenDim, the code could be

vector<int> dim_attr;
switch(dim_attr.size()) {
  case 1:
    out_tensor.reshape(EigenDim<1>(dim_attr));
    break;
  case 2:
    out_tensor.reshape(EigenDim<2>(dim_attr));
    break;
  ...
}

We may provide a proper way to manipulate EigenDim<N>. It could be just like DDim to Dim.

  • Create a type EigenDDim as a alias of boost::variable<EigenDim<1>, ...>
  • Use boost::static_visitor to access EigenDim<N>.

The reshape logic could be

struct ReshapeVisitor : public boost::static_visitor<void> {
  ReshapeVisitor(EigenTensor& tensor) : tensor_(tensor) {}
  EigenTensor& tensor_;

  template <typename EigenDim>
  void operator()(const EigenDim& dim) const {
    tensor_.reshape(dim);
  }
};

vector<int> dim_attr;
EigenDDim ddim(dim_attr);
boost::apply(ReshapeVisitor(out_tensor), ddim);

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions