Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4k
[SME][TOPI] Add conv2d NHWC SME fp32 schedule#17003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -21,13 +21,15 @@ | ||
| import tvm | ||
| from tvm import te | ||
| from tvm import autotvm | ||
| from tvm.script import tir as T | ||
| import tvm.contrib.nnpack | ||
| from tvm.tir.schedule.analysis import has_block | ||
| from ..utils import traverse_inline, get_const_tuple | ||
| from .. import nn | ||
| from ..nn.utils import get_const_int, get_pad_tuple | ||
| from ..nn.winograd_util import winograd_transform_matrices | ||
| from .arm_utils import get_tiling_B_transformed | ||
| from .arm_utils import get_tiling_A, get_tiling_B_transformed | ||
| from .conv2d_spatial_pack import ( | ||
| conv2d_spatial_pack_nchw, | ||
| conv2d_spatial_pack_nhwc, | ||
| @@ -527,13 +529,16 @@ def compute_conv2d_NHWC( | ||
| out_dtype, | ||
| interleave_A, | ||
| use_scalable_vectors=False, | ||
| use_sme=False, | ||
| ): | ||
| """Compute definition for conv2d NHWC""" | ||
| N, IH, IW, IC = get_const_tuple(data.shape) | ||
| KH, KW, _, OC = get_const_tuple(kernel.shape) | ||
| tile_N, tile_K = get_tiling_B_transformed(interleave_A, data.dtype, use_scalable_vectors) | ||
| tile_N, tile_K = get_tiling_B_transformed( | ||
| interleave_A, data.dtype, use_scalable_vectors, use_sme | ||
| ) | ||
| kernel = nn.conv2d_gemm_weight_transform(kernel, tile_N, tile_K, use_scalable_vectors) | ||
| kernel = nn.conv2d_gemm_weight_transform(kernel, tile_N, tile_K, use_scalable_vectors, use_sme) | ||
| return compute_conv2d_gemm_without_weight_transform( | ||
| cfg, | ||
| data, | ||
| @@ -546,6 +551,7 @@ def compute_conv2d_NHWC( | ||
| OC, | ||
| interleave_A, | ||
| use_scalable_vectors, | ||
| use_sme, | ||
| ) | ||
| @@ -655,3 +661,229 @@ def compute_conv2d_NHWC_hybrid_SVE(cfg, data, kernel, strides, padding, dilation | ||
| def schedule_conv2d_NHWC_hybrid_SVE(cfg, outs): | ||
| """Interface for hybrid schedule_conv2d_NHWC_hybrid_SVE""" | ||
| return schedule_conv2d_NHWC(cfg, outs, False) | ||
| @autotvm.register_topi_compute("conv2d_NHWC_hybrid_SME.arm_cpu") | ||
| def compute_conv2d_NHWC_hybrid_SME(cfg, data, kernel, strides, padding, dilation, out_dtype): | ||
| """Interface for hybrid compute_conv2d_NHWC_hybrid_SME""" | ||
| return compute_conv2d_NHWC( | ||
| cfg, | ||
| data, | ||
| kernel, | ||
| strides, | ||
| padding, | ||
| dilation, | ||
| out_dtype, | ||
| False, | ||
| True, | ||
| True, | ||
| ) | ||
| def schedule_conv2d_NHWC_hybrid_TIR(sch: tvm.tir.Schedule): | ||
| """ | ||
| Perform TIR scheduling for conv2d NHWC. | ||
| """ | ||
| # Get ordered buffer list | ||
| primfunc = sch.mod["main"] | ||
| buffer_names = primfunc.params | ||
| buffer_list = [primfunc.buffer_map[buf] for buf in buffer_names] | ||
| dtype = buffer_list[0].dtype | ||
| # Determine PrimFunc blocks | ||
| block_list = [ | ||
| "data_pad", | ||
| "data_im2col", | ||
| "T_reshape", | ||
| "A_padded_K", | ||
| "A_padded_M", | ||
| "weight_flatten", | ||
| "C", | ||
| "conv2d_gemm_output", | ||
| ] | ||
| func_blocks = {} | ||
| for block in block_list: | ||
| func_blocks[block] = sch.get_block(block) if has_block(sch, block) else None | ||
| gemm_block = func_blocks["C"] | ||
| b, m, n, k = sch.get_loops(gemm_block) | ||
| # Get tiling information | ||
| use_scalable_vectors = sch.get(func_blocks["conv2d_gemm_output"]).annotations[ | ||
| "use_scalable_vectors" | ||
| ] | ||
| use_sme = sch.get(func_blocks["conv2d_gemm_output"]).annotations["use_sme"] | ||
| M_padded = sch.get(m).extent | ||
| N_padded = sch.get(n).extent | ||
| K_padded = sch.get(k).extent | ||
| tile_M, tile_K = get_tiling_A(False, dtype, use_sme) | ||
| tile_N, _ = get_tiling_B_transformed(False, dtype, use_scalable_vectors, use_sme) | ||
| tile_M = T.cast(tile_M, M_padded.dtype) | ||
| tile_N = T.cast(tile_N, N_padded.dtype) | ||
| tile_K = T.cast(tile_K, K_padded.dtype) | ||
| # GeMM | ||
| # Compute each tile_M x tile_N tile | ||
| # By summing up K outer products | ||
| if use_sme: | ||
| # pylint: disable=import-outside-toplevel | ||
| from tvm.topi.arm_cpu.pstate_attributes import SMEAttributes | ||
| from tvm.tir.tensor_intrin.arm_cpu import ( | ||
| ARM_SME_2SVLx2SVL_TRANSPOSE_INTERLEAVE, | ||
| ARM_SME_2SVLx2SVL_GEMM_INTERLEAVED_MOPA, | ||
| ARM_SME_INIT, | ||
| get_sme_gemm_interleaved_mopa_2svlx2svl_intrin, | ||
| ) | ||
| # Interleave the padded im2col matrix utilizing the matrix tile | ||
| interleave_t_A_block = sch.cache_read(gemm_block, 0, "global") | ||
| sch.transform_layout(interleave_t_A_block, ("write", 0), lambda b, m, k: (b, k, m)) | ||
| b, m, k = sch.get_loops(interleave_t_A_block) | ||
| mo, mi = sch.split(m, factors=(None, tile_M), disable_predication=True) | ||
| ko, ki = sch.split(k, factors=(None, tile_K), disable_predication=True) | ||
| sch.parallel(b) | ||
| sch.reorder(b, ko, mo, ki, mi) | ||
| sch.tensorize(ki, ARM_SME_2SVLx2SVL_TRANSPOSE_INTERLEAVE) | ||
| # Split and reorder the loops of the GeMM for tensorization | ||
| b, m, n, k = sch.get_loops(gemm_block) | ||
| mo, mi = sch.split(m, factors=(None, tile_M), disable_predication=True) | ||
| no, ni = sch.split(n, factors=(None, tile_N), disable_predication=True) | ||
| sch.parallel(b) | ||
| sch.reorder(b, mo, no, mi, ni, k) | ||
| # Tensorize the GeMM output matrix initialization to zero | ||
| init_block = sch.decompose_reduction(gemm_block, mi) | ||
| sch.tensorize(sch.get_loops(init_block)[-2], ARM_SME_INIT) | ||
| # Tensorize the GeMM update | ||
| sme_gemm_interleaved_intrin_name = ARM_SME_2SVLx2SVL_GEMM_INTERLEAVED_MOPA + f"_{K_padded}" | ||
| tvm.tir.TensorIntrin.register( | ||
| sme_gemm_interleaved_intrin_name, | ||
| *get_sme_gemm_interleaved_mopa_2svlx2svl_intrin(K_padded), | ||
| override=True, | ||
| ) | ||
| sch.tensorize(mi, sme_gemm_interleaved_intrin_name) | ||
| # Add pstate annotations | ||
| root_block = sch.get_block("root") | ||
| sch.annotate( | ||
| root_block, SMEAttributes.STREAMING_MODE, SMEAttributes.StreamingModeValues.ENABLED | ||
| ) | ||
| sch.annotate(root_block, SMEAttributes.ZA_STORAGE, SMEAttributes.ZAStorageValues.NEW) | ||
| elif use_scalable_vectors: | ||
| mo, mi = sch.split(m, [None, tile_M]) | ||
lhutton1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| no, ni = sch.split(n, [None, tile_N], disable_predication=True) | ||
| ko, ki = sch.split(k, [None, tile_K]) | ||
| b_mo_fused = sch.fuse(b, mo) | ||
| sch.parallel(b_mo_fused) | ||
| sch.reorder( | ||
| b_mo_fused, | ||
| no, | ||
| ko, | ||
| ki, | ||
| mi, | ||
| ni, | ||
| ) | ||
| sch.vectorize(ni) | ||
| sch.unroll(mi) | ||
| # GeMM - Init | ||
| # Initialise an entire GeMM tile at once | ||
| sch.decompose_reduction(gemm_block, ko) | ||
| else: | ||
| mo, mi = sch.split(m, [None, tile_M]) | ||
| no, ni = sch.split(n, [None, tile_N]) | ||
| ko, ki = sch.split(k, [None, tile_K]) | ||
| ni_outer, ni_inner = sch.split(ni, [4, None]) | ||
| b_mo_fused = sch.fuse(b, mo) | ||
| sch.parallel(b_mo_fused) | ||
| sch.reorder( | ||
| b_mo_fused, | ||
| no, | ||
| ko, | ||
| ki, | ||
| ni_outer, | ||
| mi, | ||
| ni_inner, | ||
| ) | ||
| sch.vectorize(ni_inner) | ||
| sch.unroll(mi) | ||
| sch.unroll(ni_outer) | ||
| # GeMM - Init | ||
| # Initialise an entire GeMM tile at once | ||
| sch.decompose_reduction(gemm_block, ko) | ||
| # Input padding | ||
| if func_blocks["data_pad"]: | ||
| input_padding_block = func_blocks["data_pad"] | ||
| b, h, w, ic = sch.get_loops(input_padding_block) | ||
| b_h_fused = sch.fuse(b, h) | ||
| sch.parallel(b_h_fused) | ||
| # Im2col + padding to tile size | ||
| # Computed outside GeMM | ||
| if func_blocks["data_im2col"]: | ||
| im2col_block = func_blocks["data_im2col"] | ||
| b1, m1, k1 = sch.get_loops(im2col_block) | ||
| b_m_fused_1 = sch.fuse(b1, m1) | ||
| if func_blocks["A_padded_K"]: | ||
| im2col_pad_K_block = func_blocks["A_padded_K"] | ||
| b2, m2, k2 = sch.get_loops(im2col_pad_K_block) | ||
| b_m_fused_2 = sch.fuse(b2, m2) | ||
| sch.parallel(b_m_fused_2) | ||
| sch.compute_at(im2col_block, b_m_fused_2) | ||
| _, k1 = sch.get_loops(sch.get_block("data_im2col")) | ||
| elif func_blocks["A_padded_M"]: | ||
| im2col_pad_M_block = func_blocks["A_padded_M"] | ||
| b2, m2, k2 = sch.get_loops(im2col_pad_M_block) | ||
| b_m_fused_2 = sch.fuse(b2, m2) | ||
| sch.parallel(b_m_fused_1) | ||
| sch.parallel(b_m_fused_2) | ||
| else: | ||
| sch.parallel(b_m_fused_1) | ||
| K = sch.get(k1).extent.value | ||
| if K % 16 == 0: | ||
| split_factor = 16 | ||
| elif K % 8 == 0: | ||
| split_factor = 8 | ||
| else: | ||
| IC = buffer_list[0].shape[3] | ||
| split_factor = IC | ||
| k_outer, k_inner = sch.split(k1, [None, split_factor]) | ||
| sch.vectorize(k_inner) | ||
| sch.unroll(k_outer) | ||
| # Reshape + padding to tile size | ||
| # Computed inside GeMM | ||
| elif func_blocks["T_reshape"]: | ||
| reshape_block = func_blocks["T_reshape"] | ||
| A_pad_block = func_blocks["A_padded_K"] if func_blocks["A_padded_K"] else None | ||
| A_pad_block = func_blocks["A_padded_M"] if func_blocks["A_padded_M"] else A_pad_block | ||
| if use_sme: | ||
| sch.compute_inline(reshape_block) | ||
| elif A_pad_block: | ||
| sch.compute_inline(reshape_block) | ||
| b, m, k = sch.get_loops(A_pad_block) | ||
| _, k_inner = sch.split(k, [None, tile_N]) | ||
| sch.vectorize(k_inner) | ||
| sch.compute_at(A_pad_block, mi) | ||
| else: | ||
| sch.compute_at(reshape_block, mi) | ||
| # Weight flattening | ||
| if func_blocks["weight_flatten"]: | ||
| weight_flatten_block = func_blocks["weight_flatten"] | ||
| sch.compute_inline(weight_flatten_block) | ||
| # Conv2d output block | ||
| output_block = func_blocks["conv2d_gemm_output"] | ||
| n, h, w, c = sch.get_loops(output_block) | ||
| n_h_fused = sch.fuse(n, h) | ||
| _, inner = sch.split(c, [None, 4]) | ||
| sch.vectorize(inner) | ||
| sch.parallel(n_h_fused) | ||
| return sch | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.