| 241 | } |
| 242 | |
| 243 | Tensor Tensor::select(int64_t dim, int64_t index) const { |
| 244 | dim = wrap_size(dim, this->dim()); |
| 245 | HMP_REQUIRE(dim < this->dim(), "select : dim({}) is out of range {}", dim, |
| 246 | this->dim()); |
| 247 | |
| 248 | index = wrap_size(index, size(dim)); |
| 249 | HMP_REQUIRE(index < size(dim), |
| 250 | "select: index {} is out of range, need less than {}", index, |
| 251 | size(dim)); |
| 252 | |
| 253 | auto out = slice(dim, index, index + 1); |
| 254 | |
| 255 | // squeeze |
| 256 | auto newShape = out.shape(); |
| 257 | auto newStrides = out.strides(); |
| 258 | HMP_REQUIRE(newShape[dim] == 1, "select: internal error"); |
| 259 | newShape.erase(newShape.begin() + dim); |
| 260 | newStrides.erase(newStrides.begin() + dim); |
| 261 | |
| 262 | out.tensorInfo()->setSizesAndStrides(newShape, newStrides, |
| 263 | out.tensorInfo()->bufferOffset()); |
| 264 | |
| 265 | return out; |
| 266 | } |
| 267 | |
| 268 | // factory functions |
| 269 | Tensor empty(const SizeArray &shape, const TensorOptions &options) { |