-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Closed
Description
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
EigenDDimas a alias ofboost::variable<EigenDim<1>, ...> - Use
boost::static_visitorto accessEigenDim<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
Assignees
Labels
No labels