| 
 | 1 | +# Licensed to the Apache Software Foundation (ASF) under one  | 
 | 2 | +# or more contributor license agreements.  See the NOTICE file  | 
 | 3 | +# distributed with this work for additional information  | 
 | 4 | +# regarding copyright ownership.  The ASF licenses this file  | 
 | 5 | +# to you under the Apache License, Version 2.0 (the  | 
 | 6 | +# "License"); you may not use this file except in compliance  | 
 | 7 | +# with the License.  You may obtain a copy of the License at  | 
 | 8 | +#  | 
 | 9 | +#   http://www.apache.org/licenses/LICENSE-2.0  | 
 | 10 | +#  | 
 | 11 | +# Unless required by applicable law or agreed to in writing,  | 
 | 12 | +# software distributed under the License is distributed on an  | 
 | 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  | 
 | 14 | +# KIND, either express or implied.  See the License for the  | 
 | 15 | +# specific language governing permissions and limitations  | 
 | 16 | +# under the License.  | 
 | 17 | + | 
 | 18 | +"""Sparse operators"""  | 
 | 19 | +from __future__ import absolute_import  | 
 | 20 | +import tvm  | 
 | 21 | + | 
 | 22 | +from ..util import get_const_tuple  | 
 | 23 | + | 
 | 24 | + | 
 | 25 | +@tvm.target.generic_func  | 
 | 26 | +def sparse_dense(data, weight_data, weight_indices, weight_indptr):  | 
 | 27 | +    """  | 
 | 28 | +    Computes sparse-dense matrix multiplication of `data` and  | 
 | 29 | +    `(weight_data, weight_indices, weight_indptr).T`  | 
 | 30 | +
  | 
 | 31 | +    Parameters  | 
 | 32 | +    ----------  | 
 | 33 | +    x : tvm.Tensor  | 
 | 34 | +        2-D with shape [M, K], float32  | 
 | 35 | +
  | 
 | 36 | +    weight_data : tvm.Tensor  | 
 | 37 | +        1-D with shape [nnz] (CSR) or  | 
 | 38 | +        3-D with shape [num_blocks, bs_r, bs_c] (BSR)  | 
 | 39 | +
  | 
 | 40 | +    weight_indices : tvm.Tensor  | 
 | 41 | +        1-D with shape [nnz] (CSR) or  | 
 | 42 | +        1-D with shape [num_blocks] (BSR)  | 
 | 43 | +
  | 
 | 44 | +    weight_indptr : tvm.Tensor  | 
 | 45 | +        1-D with shape [N + 1] (CSR) or  | 
 | 46 | +        1-D with shape [(N + 1) // bs_r] (BSR)  | 
 | 47 | +
  | 
 | 48 | +    Returns  | 
 | 49 | +    -------  | 
 | 50 | +    output : tvm.Tensor  | 
 | 51 | +        2-D with shape [M, N]  | 
 | 52 | +    """  | 
 | 53 | +    assert len(weight_data.shape) in (1, 3)  | 
 | 54 | +    if len(weight_data.shape) == 1:  | 
 | 55 | +        return _sparse_dense_csrmv(  | 
 | 56 | +            data, weight_data, weight_indices, weight_indptr)  | 
 | 57 | +    if len(weight_data.shape) == 3:  | 
 | 58 | +        return _sparse_dense_bsrmv(  | 
 | 59 | +            data, weight_data, weight_indices, weight_indptr)  | 
 | 60 | + | 
 | 61 | + | 
 | 62 | +def _sparse_dense_csrmv(data, weight_data, weight_indices, weight_indptr):  | 
 | 63 | +    oshape = (  | 
 | 64 | +        get_const_tuple(data.shape)[0],  | 
 | 65 | +        get_const_tuple(weight_indptr.shape)[0] - 1)  | 
 | 66 | +    assert weight_indices.dtype == "int32", weight_indices.dtype  | 
 | 67 | +    assert weight_indptr.dtype == "int32", weight_indptr.dtype  | 
 | 68 | + | 
 | 69 | +    def f(i, row):  | 
 | 70 | +        assert row.dtype == "int32"  | 
 | 71 | +        row_start = weight_indptr[row]  | 
 | 72 | +        row_end = weight_indptr[row + 1]  | 
 | 73 | +        row_elems = row_end - row_start  | 
 | 74 | +        elem_idx = tvm.reduce_axis((0, row_elems), name="elem_idx")  | 
 | 75 | +        elem = row_start + elem_idx  | 
 | 76 | +        a_val = weight_data[elem].astype("float32")  | 
 | 77 | +        weight_val = data[i, weight_indices[elem]]  | 
 | 78 | +        return tvm.sum(a_val * weight_val, axis=elem_idx)  | 
 | 79 | +    return tvm.compute(oshape, f, tag="sparse_dense_csrmv")  | 
 | 80 | + | 
 | 81 | + | 
 | 82 | +def _sparse_dense_bsrmv(data, weight_data, weight_indices, weight_indptr):  | 
 | 83 | +    (M, K) = get_const_tuple(data.shape)  | 
 | 84 | +    (_, BS_R, BS_C) = get_const_tuple(weight_data.shape)  | 
 | 85 | +    (NB_plus_1, ) = get_const_tuple(weight_indptr.shape)  | 
 | 86 | +    NB = NB_plus_1 - 1  | 
 | 87 | +    oshape = (M, NB, BS_R)  | 
 | 88 | + | 
 | 89 | +    def f(i, nb, r):  | 
 | 90 | +        row_start = weight_indptr[nb]  | 
 | 91 | +        row_end = weight_indptr[nb + 1]  | 
 | 92 | +        row_elems = row_end - row_start  | 
 | 93 | +        elem_idx = tvm.reduce_axis(  | 
 | 94 | +            (0, row_elems), name="elem_idx")  | 
 | 95 | +        jj = row_start + elem_idx  | 
 | 96 | +        c = tvm.reduce_axis((0, BS_C), name="c")  | 
 | 97 | +        j = weight_indices[jj]  | 
 | 98 | +        block_ij_val = weight_data[jj][r][c]  | 
 | 99 | +        assert weight_data.dtype == "float32"  | 
 | 100 | +        x_val = data[i, BS_C * j + c]  | 
 | 101 | +        return tvm.sum(block_ij_val * x_val, axis=[elem_idx, c])  | 
 | 102 | + | 
 | 103 | +    Y = tvm.compute(  | 
 | 104 | +        oshape, f, tag="sparse_dense_bsrmv_block")  | 
 | 105 | +    return tvm.compute(  | 
 | 106 | +        (M, NB * BS_R), lambda m, n: Y[m, n // BS_R, n % BS_R],  | 
 | 107 | +        tag="sparse_dense_bsrmv")  | 
0 commit comments