* @brief Reshapes a simplified shape (non-strided dimensions are collapsed) to a target shape if possible. * Calculates the output strides. * * @param[in] in_rank * @param[in] in_shape * @param[in] in_strides * @param[in] target_rank * @param[in] target_shape * @param[out] out_strides * * @return true if reshape is possible, false otherwise */
| 246 | * @return true if reshape is possible, false otherwise |
| 247 | */ |
| 248 | static bool ReshapeSimplified(int in_rank, const int64_t *in_shape, const int64_t *in_strides, int target_rank, |
| 249 | const int64_t *target_shape, int64_t *out_strides) |
| 250 | { |
| 251 | int i = 0, j = 0; |
| 252 | for (; i < in_rank && j < target_rank; i++) |
| 253 | { |
| 254 | int64_t in_e = in_shape[i]; |
| 255 | int64_t out_v = 1; |
| 256 | int group_start = j; |
| 257 | while (j < target_rank && (out_v * target_shape[j]) <= in_e) out_v *= target_shape[j++]; |
| 258 | |
| 259 | if (out_v != in_e) |
| 260 | return false; // reshape is not possible |
| 261 | |
| 262 | int64_t s = in_strides[i]; |
| 263 | for (int d = j - 1; d >= group_start; d--) |
| 264 | { |
| 265 | out_strides[d] = s; |
| 266 | s *= target_shape[d]; |
| 267 | } |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | static std::string ShapeStr(int rank, const int64_t *sh) |
| 273 | { |