forked from neo-ai/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dot product support for quantized convolution. (apache#6445)
* Add dot product support for quantized convolution. We added two new intrinsics in: topi/arm_cpu/tensor_intrin.py, namely - mmla4x4: compute a matrix multiplication between tile A(4,4) and tile B(4,4) - mmla16x4: compute a matrix multiplication between tile A(rows,4) and tile B(4,16) Then we used those intrinsics in two separate strategies. We added the strategies in topi/arm_cpu/conv2d_int8.py and implemented the schedules in topi/arm_cpu/conv2d_gemm.py. In particular: - schedule_conv2d_gemm, when accelerated, packs matrix A, compute GEMM, and unpack the resulting matrix. This uses the mmla4x4 intrinsic - schedule_conv2d_gemm_hybrid doesn't do any packing on A and C which are in native form. This uses the mmla16x4 intrinsic Please note that for the limitations of `tensorize` we need to pad matrix A in both cases (when dimensions are not multiple of the tiling shape) Change-Id: Id0d818d84ffc458c6dad7983fd350a0f3d5db395 * Add back nhwc_spatial_pack strategy as default Change-Id: I8b1826a7ae1d742956296e8d157da19955a4942c * Fix linting through Black Change-Id: Ic74ef5461a90bca9f4d4980a214137e384d5f923 * Fix python linting Change-Id: I5fb8a2ae4467a87bd3470f6b3753c074f9b7cc78 * Addressing review comments Change-Id: I284b1f2c121051e672f548d6c6ee2a3267854e31 * Fix black linting issues Change-Id: I1813b0226b536aedee0dce9eeeba27aa2d95518b * Fixing failing test and adding tests for dot-product compilation Change-Id: Ic040722abd5538fccb85af4de922394c939e7000 * Fixing linting and review comments Change-Id: If09e3baa514c85dc78d3c27c2ac2fa2e01773d89 * Fixing black linting and address comments Change-Id: I857b28b6f9b23307d8c1eebc509de6ad2783c756 * Address review comments Change-Id: I63d1a639d4a72abeb33148fd2868cd356ef84122
- Loading branch information
Giuseppe Rossini
authored and
Tushar Dey
committed
Oct 15, 2020
1 parent
15e0f65
commit c8e8ff5
Showing
8 changed files
with
874 additions
and
183 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name,unused-variable,unused-argument,no-member | ||
"""Arm target utility functions""" | ||
|
||
import re | ||
import tvm | ||
|
||
|
||
def get_arch_version(target_mattr): | ||
"""Parse the LLVM target -mattr, and return | ||
the architecture version in a decimal representation | ||
(e.g., if -mattr=v8.4a, return 8.4) | ||
""" | ||
|
||
arch_version = 8.0 | ||
m = re.compile(r"\+v(.*)\.(.*)a") | ||
for attr in target_mattr: | ||
match_obj = m.match(attr) | ||
if match_obj: | ||
major = int(match_obj.group(1)) | ||
minor = int(match_obj.group(2)) | ||
decimal = 10 | ||
if minor >= 10: | ||
decimal = 100 | ||
arch_version = major + float(minor) / decimal | ||
|
||
return arch_version | ||
|
||
|
||
def is_dotprod_available(): | ||
""" Checks whether the hardware has support for fast Int8 arithmetic operations. """ | ||
target = tvm.target.Target.current(allow_none=False) | ||
arch_version = get_arch_version(target.mattr) | ||
return arch_version >= 8.4 or ((arch_version in (8.2, 8.3)) and "+dotprod" in target.mattr) | ||
|
||
|
||
def is_aarch64_arm(): | ||
""" Checks whether we are compiling for an AArch64 target. """ | ||
target = tvm.target.Target.current(allow_none=False) | ||
return "aarch64" in target.attrs.get("mtriple", "") | ||
|
||
|
||
def get_tiling_B_interleaved_t(interleave_A): | ||
"""Compute the tiling information for matrix B', where B' | ||
is the transposed and interleaved version of matrix B in C=A*B. | ||
The tiling information is chosen to maximize register usage during the | ||
tile computation. | ||
Please refer to: | ||
- https://discuss.tvm.apache.org/t/rfc-accelerate-quantized-convolution-through-dot-product | ||
- Conv2DGemmWeightTransformRel in src/relay/op/nn/convolution.h | ||
In order to have more information | ||
Parameters | ||
---------- | ||
interleave_A: bool | ||
determines if A is expected to be interleaved | ||
Returns | ||
---------- | ||
tile_rows_B: the output tile rows of B' | ||
tile_cols_B: the output tile columns of B' | ||
""" | ||
if is_dotprod_available(): | ||
# The number of tile rows of B' vary depending on the | ||
# strategy: | ||
# * If we are interleaving A, then we select 12 columns from B'(i.e., | ||
# 12 rows from B). | ||
# * If we are not interleaving A, then we select 16 columns from B'(i.e., | ||
# 16 rows from B). | ||
tile_rows_B = 12 if interleave_A else 16 | ||
|
||
# Dot product instruction groups 2 (u)int16x8 vectors in | ||
# groups of 4 and compute the dot product among those groups | ||
# This means that the number of columns in a tile of B' (i.e., the | ||
# rows of the original matrix B) need to be 4. | ||
tile_cols_B = 4 | ||
else: | ||
# If dot product is not available, A must be interleaved. In this case | ||
# we load 4 rows of B' (i.e., 4 columns of B). Each of them will contain 16 elements | ||
tile_rows_B = 4 | ||
tile_cols_B = 16 | ||
|
||
return tile_rows_B, tile_cols_B |
This file contains 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
Oops, something went wrong.