-
Notifications
You must be signed in to change notification settings - Fork 3.9k
cleanup Hexagon conv2d tests #9473
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
Changes from 4 commits
95e3756
bbff167
90b2c38
28b6502
3ef59a9
d8e7852
e8d1cac
3be018b
b71fe0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,36 +18,43 @@ | |
| """ Hexagon testing infrastructure """ | ||
|
|
||
| import tvm | ||
| from tvm import te | ||
| import numpy | ||
|
|
||
|
|
||
| def ceildiv(o, d): | ||
| return tvm.tir.floordiv(o + d - 1, d) | ||
|
|
||
|
|
||
| def get_packed_activation_layout(shape_nhwc, block_shape, packed_C=True): | ||
| def get_block_shape(): | ||
|
adstraw marked this conversation as resolved.
|
||
| return 8, 8, 32 | ||
|
|
||
|
|
||
| def get_filter_block_shape(): | ||
| return 8, 32, 4 | ||
|
adstraw marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def get_packed_shape(shape_nhwc): | ||
|
adstraw marked this conversation as resolved.
Outdated
adstraw marked this conversation as resolved.
Outdated
|
||
| assert len(shape_nhwc) == 4 | ||
| shape = [shape_nhwc[0]] | ||
| block_shape = get_block_shape() | ||
| off_h, off_w, off_c = block_shape | ||
| shape.append(ceildiv(shape_nhwc[1], off_h)) | ||
| shape.append(ceildiv(shape_nhwc[2], off_w)) | ||
| if packed_C: | ||
| shape.append(ceildiv(shape_nhwc[3], off_c)) | ||
| shape.extend(block_shape) | ||
| else: | ||
| shape.extend([off_h, off_w, shape_nhwc[3]]) | ||
| shape.append(ceildiv(shape_nhwc[3], off_c)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this code we handle Is there a good reason for doing it two different ways? Specifically: casting to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, casting to NOTE: I did change |
||
| shape.extend(block_shape) | ||
| return shape | ||
|
|
||
|
|
||
| def get_block_shape(): | ||
| return 8, 8, 32 | ||
|
|
||
|
|
||
| def get_filter_block_shape(): | ||
| return 8, 32, 4 | ||
| def get_logical_shape(shape_nhwc8h8w32c): | ||
| shape = [shape_nhwc8h8w32c[0]] | ||
| shape.append(shape_nhwc8h8w32c[1] * shape_nhwc8h8w32c[4]) | ||
| shape.append(shape_nhwc8h8w32c[2] * shape_nhwc8h8w32c[5]) | ||
| shape.append(shape_nhwc8h8w32c[3] * shape_nhwc8h8w32c[6]) | ||
| return shape | ||
|
|
||
|
|
||
| def get_packed_filter_layout(out_channel, in_channel, kernel_h, kernel_w): | ||
| def get_packed_filter_shape(out_channel, in_channel, kernel_h, kernel_w): | ||
| filter_Cio, filter_Ki, filter_Cii = get_filter_block_shape() | ||
| filter_Ci = filter_Cio * filter_Cii | ||
| return ( | ||
|
|
@@ -93,26 +100,10 @@ def get_conv2d_nhwc_shape(shape_nhwc, kernel_size, strides, padding, dilation, o | |
| ) | ||
|
|
||
|
|
||
| def verify_conv2d(output, ref_output, dtype): | ||
| # nhwc8h8w32c | ||
| if len(output.shape) == 7: | ||
| # nhwc8h8w32c -> nhwc | ||
| output = output.transpose(0, 1, 4, 2, 5, 3, 6).reshape( | ||
| output.shape[0], | ||
| output.shape[1] * output.shape[4], | ||
| output.shape[2] * output.shape[5], | ||
| output.shape[3] * output.shape[6], | ||
| ) | ||
|
|
||
| # nhwhwc | ||
| else: | ||
| # nhwhwc -> nhwc | ||
| output = output.transpose(0, 1, 3, 2, 4, 5).reshape( | ||
| output.shape[0], | ||
| output.shape[1] * output.shape[3], | ||
| output.shape[2] * output.shape[4], | ||
| output.shape[5], | ||
| ) | ||
| def conv2d_verify(output, ref_output, dtype): | ||
| # nhwc8h8w32c -> nhwc | ||
| logical_output_shape = get_logical_shape(output.shape) | ||
| output = output.transpose(0, 1, 4, 2, 5, 3, 6).reshape(logical_output_shape) | ||
|
|
||
| # slice output to match ref_output shape | ||
| # e.g. 8x8 spatial 3x3 filter = 6x6 ref output | ||
|
|
@@ -129,3 +120,64 @@ def verify_conv2d(output, ref_output, dtype): | |
| elif dtype == "float32": | ||
| tol = {"rtol": 1e-4, "atol": 2e-4} | ||
| tvm.testing.assert_allclose(output, ref_output, **tol) | ||
|
|
||
|
|
||
| def conv2d_compute(X, filt, pad, stride, dilation): | ||
| block_shape = get_block_shape() | ||
| block_H, block_W, block_C = block_shape | ||
| filter_Cio, filter_Ki, filter_Cii = get_filter_block_shape() | ||
| filter_Ci = filter_Cio * filter_Cii | ||
|
|
||
| shape_filter = filt.shape | ||
| kernel_size = tuple(shape_filter[2:4]) | ||
| out_channels = shape_filter[0] * shape_filter[5] | ||
|
|
||
| logical_input_shape = get_logical_shape(X.shape) | ||
| logical_output_shape = get_conv2d_nhwc_shape( | ||
| logical_input_shape, | ||
| kernel_size, | ||
| stride, | ||
| pad, | ||
| dilation, | ||
| out_channels, | ||
| ) | ||
|
|
||
| output_shape = get_packed_shape(logical_output_shape) | ||
| n, ho, wo, ko, hi, wi, ki = output_shape | ||
| rh = te.reduce_axis((0, kernel_size[0]), name="rh") | ||
| rw = te.reduce_axis((0, kernel_size[1]), name="rw") | ||
| rc = te.reduce_axis((0, logical_input_shape[3]), name="rc") | ||
|
|
||
| def compute(n, ho, wo, ko, hi, wi, ki): | ||
| h = ho * block_H + hi | ||
| h_contig = h * stride[0] + rh | ||
| h_block_id = h_contig // block_H | ||
| h_block_offset = h_contig % block_H | ||
|
|
||
| w = wo * block_W + wi | ||
| w_contig = w * stride[1] + rw | ||
| w_block_id = w_contig // block_W | ||
| w_block_offset = w_contig % block_W | ||
|
|
||
| c_block_id = rc // block_C | ||
| c_block_offset = rc % block_C | ||
|
|
||
| rco = rc // filter_Ci | ||
| rcio = (rc % filter_Ci) // filter_Cii | ||
| rcii = rc % filter_Cii | ||
|
|
||
| return te.sum( | ||
| X[ | ||
| n, | ||
| h_block_id, | ||
| w_block_id, | ||
| c_block_id, | ||
| h_block_offset, | ||
| w_block_offset, | ||
| c_block_offset, | ||
| ] | ||
| * filt[ko, rco, rh, rw, rcio, ki, rcii], | ||
| axis=[rh, rw, rc], | ||
| ) | ||
|
|
||
| return output_shape, compute | ||
Uh oh!
There was an error while loading. Please reload this page.