test_gemm_acc16.py 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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=import-self, invalid-name, unused-argument, too-many-lines, len-as-condition
import tvm
19
from tvm import te
20
import numpy as np
21
from topi.x86.tensor_intrin import dot_16x1x16_uint8_int8_int16
22 23 24 25 26 27 28


def benchmark_fc_int8_acc16():
    m = 128
    n = 128
    k = 128

29 30
    X = te.placeholder((m, k), name='X', dtype="uint8")
    W = te.placeholder((n, k), name='W', dtype="int8")
31 32 33 34 35 36

    peak = 512/16*2*2*2
    gops_per_mm = 2*n*m*k
    print("Peak {} Gops/s \n".format(peak))

    def verify(target="llvm -mcpu=skylake-avx512"):
37
        if not tvm.runtime.enabled(target):
38 39 40 41
            print("skip because %s is not enabled..." % target)
            return

        ctx = tvm.context(target, 0)
42 43
        X = te.placeholder((m, k), name='X', dtype="uint8")
        W = te.placeholder((n, k), name='W', dtype="int8")
44
        pc = dot_16x1x16_uint8_int8_int16()
45
        ak = te.reduce_axis((0, k), name='k')
46

47 48
        packedW = te.placeholder((n//128, 128*(k//2), 2), name='packedW', dtype="int8")
        t_fc = te.compute((m, n), lambda i, j: te.sum(X[i, ak].astype("int16") * packedW[j//128, (ak//2)*128+j%128, ak%2].astype("int16"), axis=ak), name="F")
49

50
        t_sch = te.create_schedule(t_fc.op)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
        a_x, a_y = t_fc.op.axis
        a_k, = t_fc.op.reduce_axis

        a_yo, a_yi = t_sch[t_fc].split(a_y, factor=128)
        a_ko, a_ki = t_sch[t_fc].split(a_k, factor=2)

        a_xo, a_xi = t_sch[t_fc].split(a_x, factor=128)
        a_koo, a_koi = t_sch[t_fc].split(a_ko, factor=32)
        t_sch[t_fc].reorder(a_yo, a_xo, a_koo, a_xi, a_koi, a_yi, a_ki)

       	t_sch[t_fc].tensorize(a_yi, pc)
        # print(tvm.lower(t_sch, [X, packedW, t_fc], simple_mode=True))
        t_func = tvm.build(t_sch, [X, packedW, t_fc], target, name="intrinsic")
        t_evaluator = t_func.time_evaluator(t_func.entry_name, ctx, number=10)

	    # generate the plain data
        a_ = np.random.uniform(1, 10, size=(m, k)).astype("uint8")
        b_ = np.random.uniform(1, 10,  size=(n, k)).astype("int8")

70
        packW = np.random.uniform(1, 10,  size=(n//128, 128*(k//2), 2)).astype("int8")
71
        # This occurs in pre_compute stage
72 73
        for r_idx in range(n//128):
            for s_idx in range(128*(k//2)):
74
                for t_idx in range(2):
75
                    packW[r_idx][s_idx][t_idx] = b_[r_idx*128+s_idx%128][s_idx//128*2+t_idx]
76 77 78 79 80 81 82 83 84 85

        x = tvm.nd.array(a_, ctx)
        w = tvm.nd.array(packW, ctx)
        y = tvm.nd.array(np.zeros((m, n), dtype="int16"), ctx)

        result = t_evaluator(x, w, y)
        gops_per_sec = gops_per_mm/result.mean/1e9
        tvm.testing.assert_allclose(
           y.asnumpy(), np.dot(a_, b_.T), rtol=1e-5)
        print('Tensorization: running time: {:.3f} ms, {:.2f} Gops/s, effiency: {:.2f}.'.format(result.mean*1000, gops_per_sec, gops_per_sec/peak))
86
        #t_func.export_library("gemm_tensorize.o")
87 88 89 90 91

    verify()

if __name__ == "__main__":
    benchmark_fc_int8_acc16()