|
| 1 | +// clang-format off |
| 2 | +/* |
| 3 | + * SPDX-FileCopyrightText: Copyright (c) 2023-present NVIDIA CORPORATION & AFFILIATES. |
| 4 | + * All rights reserved. |
| 5 | + * SPDX-License-Identifier: BSD-3-Clause |
| 6 | + */ |
| 7 | +// clang-format on |
| 8 | + |
| 9 | +namespace nvf::block_layout { |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +// TODO: simplify this maybe?! |
| 14 | +template <int BLOCK_ROW_OUTER, int BLOCK_ROW_INNER, int BLOCK_COL> |
| 15 | +__device__ nvfuser_index_t offsetAfterSwizzlePadding( |
| 16 | + const nvfuser_index_t row_idx, |
| 17 | + const nvfuser_index_t col_idx, |
| 18 | + const nvfuser_index_t padded_col_size) { |
| 19 | + constexpr nvfuser_index_t BLOCK_ROW_SIZE = BLOCK_ROW_OUTER * BLOCK_ROW_INNER; |
| 20 | + |
| 21 | + /* logical dimension of matrix [ row_size, col_size] |
| 22 | + * |
| 23 | + * while layout is decomposed as |
| 24 | + * [ (row_tile*BLOCK_ROW_INNER*BLOCK_ROW_OUTER), (col_tile*BLOCK_COL) ] |
| 25 | + * where |
| 26 | + * row_tile = row_size / BLOCK_ROW_OUTER * BLOCK_ROW_INNER) |
| 27 | + * col_tile = col_size / BLOCK_COL |
| 28 | + */ |
| 29 | + nvfuser_index_t row_tile_idx = row_idx / BLOCK_ROW_SIZE; |
| 30 | + |
| 31 | + nvfuser_index_t row_block_idx = row_idx % BLOCK_ROW_SIZE; |
| 32 | + nvfuser_index_t row_block_inner_idx = row_block_idx / BLOCK_ROW_OUTER; |
| 33 | + nvfuser_index_t row_block_outer_idx = row_block_idx % BLOCK_ROW_OUTER; |
| 34 | + nvfuser_index_t col_tile_idx = col_idx / BLOCK_COL; |
| 35 | + nvfuser_index_t col_block_idx = col_idx % BLOCK_COL; |
| 36 | + |
| 37 | + /* layout for matrix [ row_size, col_size] |
| 38 | + * it is viewed |
| 39 | + * [row_tile, BLOCK_ROW_INNER, BLOCK_ROW_OUTER, col_tile, BLOCK_COL] |
| 40 | + * then transposed with axis (1, 3) |
| 41 | + * [row_tile, col_tile, BLOCK_ROW_OUTER, BLOCK_ROW_INNER, BLOCK_COL] |
| 42 | + * and then made contiguous |
| 43 | + */ |
| 44 | + constexpr nvfuser_index_t COL_TILE_STRIDE = BLOCK_ROW_SIZE * BLOCK_COL; |
| 45 | + constexpr nvfuser_index_t BLOCK_ROW_OUTER_STRIDE = |
| 46 | + BLOCK_ROW_INNER * BLOCK_COL; |
| 47 | + constexpr nvfuser_index_t BLOCK_ROW_INNER_STRIDE = BLOCK_COL; |
| 48 | + |
| 49 | + return row_tile_idx * padded_col_size * BLOCK_ROW_SIZE + |
| 50 | + col_tile_idx * COL_TILE_STRIDE + |
| 51 | + row_block_outer_idx * BLOCK_ROW_OUTER_STRIDE + |
| 52 | + row_block_inner_idx * BLOCK_ROW_INNER_STRIDE + col_block_idx; |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +// TODO: I think we can actually not have this handled as an opaque function. |
| 58 | +template < |
| 59 | + typename T, |
| 60 | + typename Index_T, |
| 61 | + int BLOCK_ROW_OUTER, |
| 62 | + int BLOCK_ROW_INNER, |
| 63 | + int BLOCK_COL, |
| 64 | + int UNROLL_FACTOR> |
| 65 | +__device__ void groupedBlockLayout( |
| 66 | + T* output, |
| 67 | + const T* input, |
| 68 | + const nvfuser_index_t row_idx, |
| 69 | + const nvfuser_index_t col_idx, |
| 70 | + const Index_T* expert_offsets, |
| 71 | + const Index_T* output_offsets, |
| 72 | + const nvfuser_index_t row_size, |
| 73 | + const nvfuser_index_t col_size, |
| 74 | + const nvfuser_index_t group_size) { |
| 75 | + // find corresponding expert_id |
| 76 | + int expert_id = 0; |
| 77 | + for (int i = 0; i < group_size; ++i) { |
| 78 | + if (row_idx < expert_offsets[i + 1]) { |
| 79 | + expert_id = i; |
| 80 | + break; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // row idx for current matmul |
| 85 | + nvfuser_index_t c_row_idx = row_idx - expert_offsets[expert_id]; |
| 86 | + nvfuser_index_t padded_col_size = |
| 87 | + (col_size + BLOCK_COL - 1) / BLOCK_COL * BLOCK_COL; |
| 88 | + T* out_group_offset = output + output_offsets[expert_id] * padded_col_size; |
| 89 | + |
| 90 | + // TODO: vectorized load/store; The logic could be simplified afterwards. |
| 91 | + for (int i = 0; i < UNROLL_FACTOR && col_idx + i < col_size; ++i) { |
| 92 | + nvfuser_index_t index = |
| 93 | + offsetAfterSwizzlePadding<BLOCK_ROW_OUTER, BLOCK_ROW_INNER, BLOCK_COL>( |
| 94 | + c_row_idx, col_idx + i, padded_col_size); |
| 95 | + out_group_offset[index] = input[i]; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +} // namespace nvf::block_layout |
0 commit comments