| 169 | const int in_logical_axis = axes[out_logical_axis]; |
| 170 | const int out_ggml_axis = static_cast<int>(value.shape.rank) - 1 - static_cast<int>(out_logical_axis); |
| 171 | ggml_axes[out_ggml_axis] = core::logical_axis_to_ggml_axis(value.shape.rank, in_logical_axis); |
| 172 | } |
| 173 | |
| 174 | return core::wrap_tensor( |
| 175 | ggml_permute(ctx.ggml, value.tensor, ggml_axes[0], ggml_axes[1], ggml_axes[2], ggml_axes[3]), |
| 176 | transpose_last_two(value.shape), |
| 177 | value.type); |
| 178 | } |
| 179 | |
| 180 | int normalize_reduce_axis(int axis, size_t rank) { |
| 181 | if (rank == 0) { |
| 182 | throw std::runtime_error("Reduce input rank must be positive"); |
| 183 | } |
| 184 | const int normalized = axis < 0 ? static_cast<int>(rank) + axis : axis; |
| 185 | if (normalized < 0 || normalized >= static_cast<int>(rank)) { |
| 186 | throw std::runtime_error("Reduce axis out of range"); |
| 187 | } |
| 188 | return normalized; |
| 189 | } |
| 190 | |
| 191 | core::TensorShape reduced_shape(const core::TensorShape & shape, int axis) { |
| 192 | core::TensorShape out = shape; |
| 193 | out.dims[static_cast<size_t>(axis)] = 1; |
| 194 | return out; |
| 195 | } |
| 196 | |
| 197 | template <typename Fn> |
| 198 | core::TensorValue reduce_axis( |
| 199 | core::ModuleBuildContext & ctx, |
| 200 | const core::TensorValue & input, |
no test coverage detected