|
|
|
@ -130,15 +130,19 @@ inline Tensor Tensor::Slice(const int& begin_idx, const int& end_idx) const {
|
|
|
|
|
PADDLE_ENFORCE_LE(end_idx, dims_[0], "Slice end index is out of bound.");
|
|
|
|
|
PADDLE_ENFORCE_LT(begin_idx, end_idx,
|
|
|
|
|
"Begin index must be less than end index.");
|
|
|
|
|
PADDLE_ENFORCE_NE(dims_[0], 1, "Can not slice a tensor with dims_[0] = 1.");
|
|
|
|
|
size_t base = numel() / dims_[0];
|
|
|
|
|
Tensor dst;
|
|
|
|
|
dst.holder_ = holder_;
|
|
|
|
|
DDim dst_dims = dims_;
|
|
|
|
|
dst_dims[0] = end_idx - begin_idx;
|
|
|
|
|
dst.Resize(dst_dims);
|
|
|
|
|
dst.offset_ = offset_ + begin_idx * base * sizeof(T);
|
|
|
|
|
return dst;
|
|
|
|
|
|
|
|
|
|
if (dims_[0] == 1) {
|
|
|
|
|
return *this;
|
|
|
|
|
} else {
|
|
|
|
|
size_t base = numel() / dims_[0];
|
|
|
|
|
Tensor dst;
|
|
|
|
|
dst.holder_ = holder_;
|
|
|
|
|
DDim dst_dims = dims_;
|
|
|
|
|
dst_dims[0] = end_idx - begin_idx;
|
|
|
|
|
dst.Resize(dst_dims);
|
|
|
|
|
dst.offset_ = offset_ + begin_idx * base * sizeof(T);
|
|
|
|
|
return dst;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline Tensor& Tensor::Resize(const DDim& dims) {
|
|
|
|
|