| 205 | } |
| 206 | |
| 207 | Optional<Stride> ComputeStride(const Shape& shape, const Stride& stride, |
| 208 | const Shape& target_shape) { |
| 209 | /************************************************* |
| 210 | * Description: in some case, view operate is not allowed, so need to check it's validation, |
| 211 | * the check refers to torch(aten/src/ATen/native/TensorShape.cpp) |
| 212 | *************************************************/ |
| 213 | if (stride.size() == 0) { |
| 214 | // for scalar input tensor |
| 215 | return Stride(target_shape.NumAxes(), 1); |
| 216 | } |
| 217 | int64_t elem_count = shape.elem_cnt(); |
| 218 | int64_t ndim = shape.NumAxes(); |
| 219 | int64_t tgt_ndim = target_shape.NumAxes(); |
| 220 | DimVector shape_vec = shape.dim_vec(); |
| 221 | DimVector tgt_shape_vec = target_shape.dim_vec(); |
| 222 | if (elem_count == 0) { return NullOpt; } |
| 223 | |
| 224 | int64_t view_d = tgt_ndim - 1; |
| 225 | int64_t chunk_base_stride = stride.back(); |
| 226 | Stride target_stride(tgt_ndim); |
| 227 | // stride for each subspace in the chunk |
| 228 | // numel in current chunk |
| 229 | int64_t tensor_numel = 1; |
| 230 | int64_t view_numel = 1; |
| 231 | for (int64_t tensor_d = ndim - 1; tensor_d >= 0; tensor_d--) { |
| 232 | tensor_numel *= shape_vec[tensor_d]; |
| 233 | // if end of tensor size chunk, check view |
| 234 | if ((tensor_d == 0) |
| 235 | || (shape_vec[tensor_d - 1] != 1 |
| 236 | && stride[tensor_d - 1] != tensor_numel * chunk_base_stride)) { |
| 237 | while (view_d >= 0 && (view_numel < tensor_numel || tgt_shape_vec[view_d] == 1)) { |
| 238 | target_stride[view_d] = view_numel * chunk_base_stride; |
| 239 | view_numel *= tgt_shape_vec[view_d]; |
| 240 | view_d--; |
| 241 | } |
| 242 | if (view_numel != tensor_numel) { return NullOpt; } |
| 243 | if (tensor_d > 0) { |
| 244 | chunk_base_stride = stride[tensor_d - 1]; |
| 245 | tensor_numel = 1; |
| 246 | view_numel = 1; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | if (view_d != -1) { return NullOpt; } |
| 251 | return target_stride; |
| 252 | } |
| 253 | |
| 254 | Maybe<Shape> InferShapeUnspecifiedDim(const int64_t& elem_count, const Shape& shape) { |
| 255 | int need_infer_axis = -1; |
no test coverage detected