-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Checking the correct dtypes for choosing the Intel int8 instructions. #3516
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
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
66fbd42
Checking the correct dtypes for choosing the Intel int8 instructions.
anijain2305 a5269e7
Add weight layout conversion from 4D to 7D
4f10a90
Add comments and modify variable names
e62fccc
Stricter check for int8 compute.
bfc418c
Adding dispatch ctx.
anijain2305 057fa8d
Fixing pylint error.
anijain2305 1b12be9
Adding one more test for Intel older generations. Fixing the top tests.
anijain2305 a364f28
Removing the X86 dependent code from target agnostic nn/conv2d.py file.
anijain2305 c6ff329
Int8 topi tests changes.
anijain2305 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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 was deleted.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -37,6 +37,29 @@ | |
|
|
||
| logger = logging.getLogger('topi') | ||
|
|
||
| def _is_int8_hw_support(data_dtype, kernel_dtype, target): | ||
| """ | ||
| Checks to ensure that we can use Intel DLBoost instructions | ||
| 1) The datatypes are correct. | ||
| 2) LLVM version has support for the instructions. | ||
| 3) Target is skylake and above. | ||
| """ | ||
| # 1) Check datatypes | ||
| is_dtype_support = data_dtype == 'uint8' and kernel_dtype == 'int8' | ||
|
|
||
| # 2) Check LLVM support | ||
| llvm_intrin_fast_int8 = "llvm.x86.avx512.pmaddubs.w.512" | ||
| llvm_id = tvm.codegen.llvm_lookup_intrinsic_id(llvm_intrin_fast_int8) | ||
| is_llvm_support = llvm_id != 0 | ||
|
|
||
| # 3) Check target | ||
| is_target_support = False | ||
| for opt in target.options: | ||
| if opt == '-mcpu=skylake-avx512': | ||
| is_target_support = True | ||
|
|
||
| return is_dtype_support and is_llvm_support and is_target_support | ||
|
|
||
| def _get_default_config(cfg, data, kernel, strides, padding, out_dtype, is_depthwise=False, | ||
| layout='NCHW'): | ||
| """ | ||
|
|
@@ -68,7 +91,8 @@ def _create_tuning_space(cfg, data, kernel, strides, padding, dilation, layout): | |
| kh, kw, oc, _ = kshape | ||
| elif pat.match(layout) is not None: | ||
| n, ic_chunk, h, w, ic_bn = dshape | ||
| if data.dtype == 'uint8': | ||
| target = tvm.target.current_target(allow_none=False) | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| oc_chunk, k_ic, kh, kw, k_ic_f, oc_bn, k_ic_s = kshape | ||
| ic = ic_chunk*ic_bn | ||
| assert ic == k_ic*k_ic_f*kic_s | ||
|
|
@@ -275,7 +299,8 @@ def traverse(op): | |
| data = data_pad.op.input_tensors[0] | ||
|
|
||
| args = [s, cfg, data_vec, conv_out, outs[0]] | ||
| if data.dtype == 'uint8': | ||
| target = tvm.target.current_target(allow_none=False) | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| # int8 conv kernel is 7-dim | ||
| kh, kw, _, _, _ = get_const_tuple(kernel.shape) | ||
| if kh == 1 and kw == 1: | ||
|
|
@@ -453,19 +478,33 @@ def _alter_conv2d_layout(attrs, inputs, tinfo, F): | |
| new_workload = autotvm.task.args_to_workload( | ||
| [new_data, new_kernel, strides, padding, dilation, new_attrs[layout_name], | ||
| new_attrs['out_layout'], out_dtype], depthwise_conv2d_NCHWc) | ||
| dispatch_ctx.update(target, new_workload, cfg) | ||
| else: | ||
| out_channel, _, kh, kw = get_const_tuple(kernel.shape) | ||
| # (oc, ic, h, w) -> (OC, IC, h, w, ic, oc) | ||
| new_attrs['kernel_layout'] = 'OIHW%di%do' % (ic_bn, oc_bn) | ||
|
|
||
| # Store altered operator's config | ||
| new_kernel = tvm.placeholder((out_channel//oc_bn, in_channel//ic_bn, kh, kw, ic_bn, oc_bn), | ||
| dtype=kernel.dtype) | ||
| new_workload = autotvm.task.args_to_workload( | ||
| [new_data, new_kernel, strides, padding, dilation, new_attrs[layout_name], | ||
| new_attrs['out_layout'], out_dtype], conv2d_NCHWc) | ||
|
|
||
| dispatch_ctx.update(target, new_workload, cfg) | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| # Convert kernel data layout from 4D to 7D | ||
| n_elems = 4 | ||
| out_channel, _, kh, kw = get_const_tuple(kernel.shape) | ||
| [data_func, kernel_func] = [s for s in inputs] | ||
| kernel_IHWO = F.transpose(kernel_func, axes=(1, 2, 3, 0)) | ||
| kernel_IHWOo = F.reshape(kernel_IHWO, (in_channel, kh, kw, out_channel//oc_bn, oc_bn)) | ||
| kernel_OHWoI = F.transpose(kernel_IHWOo, axes=(3, 1, 2, 4, 0)) | ||
| kernel_OHWoIi = F.reshape(kernel_OHWoI, (out_channel//oc_bn, kh, kw, oc_bn, | ||
| in_channel//ic_bn, ic_bn)) | ||
| kernel_OHWoIie = F.reshape(kernel_OHWoIi, (out_channel//oc_bn, kh, kw, oc_bn, | ||
| in_channel//ic_bn, ic_bn//n_elems, n_elems)) | ||
| kernel_OIHWioe = F.transpose(kernel_OHWoIie, axes=(0, 4, 1, 2, 5, 3, 6)) | ||
| copy_inputs = [data_func, kernel_OIHWioe] | ||
|
Member
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. also need to do
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. Ah, thanks. Added that now |
||
| else: | ||
| out_channel, _, kh, kw = get_const_tuple(kernel.shape) | ||
| # (oc, ic, h, w) -> (OC, IC, h, w, ic, oc) | ||
| new_attrs['kernel_layout'] = 'OIHW%di%do' % (ic_bn, oc_bn) | ||
| # Store altered operator's config | ||
| new_kernel = tvm.placeholder((out_channel//oc_bn, in_channel//ic_bn, | ||
| kh, kw, ic_bn, oc_bn), dtype=kernel.dtype) | ||
| new_workload = autotvm.task.args_to_workload( | ||
| [new_data, new_kernel, strides, padding, dilation, new_attrs[layout_name], | ||
| new_attrs['out_layout'], out_dtype], conv2d_NCHWc) | ||
| dispatch_ctx.update(target, new_workload, cfg) | ||
|
|
||
| if is_depthwise: | ||
| if F.__name__ == 'nnvm.symbol': | ||
|
|
@@ -505,7 +544,8 @@ def _declaration_conv_NCHWc(cfg, data, kernel, strides, | |
|
|
||
| n, ic_chunk, ih, iw, ic_bn = get_const_tuple(data.shape) | ||
| in_channel = ic_chunk * ic_bn | ||
| if data.dtype == 'uint8': | ||
| target = tvm.target.current_target(allow_none=False) | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| oc_chunk, ic_chunk_group, kernel_height, kernel_width, _, oc_bn, _ = \ | ||
| get_const_tuple(kernel.shape) | ||
| else: | ||
|
|
@@ -539,7 +579,7 @@ def _declaration_conv_NCHWc(cfg, data, kernel, strides, | |
| kh = tvm.reduce_axis((0, kernel_height), name='kh') | ||
| kw = tvm.reduce_axis((0, kernel_width), name='kw') | ||
|
|
||
| if data.dtype == 'uint8' and groups == 1: | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target) and groups == 1: | ||
| assert out_dtype == "int32", \ | ||
| "INT8 convolution requires input dtype = uint8 and output dtype=int32" | ||
| # Intel performs dot product of 2 "4" Int8 values | ||
|
|
@@ -559,7 +599,8 @@ def _declaration_conv_NCHWc(cfg, data, kernel, strides, | |
| oc_block, ic_s_inner].astype(out_dtype), | ||
| axis=[kh, kw, ic_outer, ic_f_inner, ic_s_inner]), | ||
| name='conv2d_NCHWc_int8', tag="conv2d_NCHWc_int8") | ||
| if data.dtype == 'uint8': | ||
|
|
||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| # for int8 group conv support | ||
| n_elems = 4 | ||
| ic_chunk = in_channel//ic_bn | ||
|
|
@@ -615,7 +656,9 @@ def traverse(op): | |
| data = data_pad.op.input_tensors[0] | ||
|
|
||
| args = [s, cfg, data_vec, conv_out, outs[0]] | ||
| if data.dtype == 'uint8': | ||
| # VNNI takes u8 x s8 | ||
| target = tvm.target.current_target(allow_none=False) | ||
| if _is_int8_hw_support(data.dtype, kernel.dtype, target): | ||
| # int8 conv kernel is 7-dim | ||
| _, _, kh, kw, _, _, _ = get_const_tuple(kernel.shape) | ||
| if kh == 1 and kw == 1: | ||
|
|
||
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
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.