|
| 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 | +# pylint: disable=no-else-return |
| 18 | +# pylint: disable=unidiomatic-typecheck |
| 19 | +""" |
| 20 | +This file contains helper functions for convert dense model |
| 21 | +to block sparse model |
| 22 | +""" |
| 23 | +from collections import namedtuple |
| 24 | +import numpy as np |
| 25 | +import scipy.sparse as sp |
| 26 | +import tvm |
| 27 | +from . import _ffi_api |
| 28 | + |
| 29 | + |
| 30 | +SparseAnalysisResult = namedtuple("SparseAnalysisResult", [ |
| 31 | + "weight_name", |
| 32 | + "weight_shape", |
| 33 | +]) |
| 34 | + |
| 35 | +def _search_dense_op_weight(expr): |
| 36 | + """Search name of weight in all ```nn.dense``` operator |
| 37 | + This is a helpful function to determine which param need |
| 38 | + to be converted to sparse |
| 39 | +
|
| 40 | + Parameters |
| 41 | + ---------- |
| 42 | + expr : relay.Expr |
| 43 | + Expr will be searched |
| 44 | +
|
| 45 | + Returns |
| 46 | + ------- |
| 47 | + ret : Array[String] |
| 48 | + name of weight in all ``nn.dense``` operator |
| 49 | + """ |
| 50 | + return _ffi_api.search_dense_op_weight(expr) |
| 51 | + |
| 52 | + |
| 53 | +def process_params(expr, params, block_size, sparsity_threshold): |
| 54 | + """[summary] |
| 55 | +
|
| 56 | + Parameters |
| 57 | + ---------- |
| 58 | + expr : Relay.Expr |
| 59 | + Expr of the network |
| 60 | + params : Dict[String, tvm.nd.array] |
| 61 | + parameters of the network |
| 62 | + block_size : Tuple(int, int) |
| 63 | + Blocksize in BSR matrix |
| 64 | + sparsity_threshold : float |
| 65 | + Minimal sparsity requirement for converting to sparse operation |
| 66 | +
|
| 67 | + Returns |
| 68 | + ------- |
| 69 | + ret : Namedtuple[weight_name: Array[String], weight_shape: Array[Array[IntImm]]] |
| 70 | + return names of qualified dense weight and the shape in BSR format |
| 71 | + """ |
| 72 | + memo = SparseAnalysisResult(weight_name=[], weight_shape=[]) |
| 73 | + weight_names = _search_dense_op_weight(expr) |
| 74 | + for name in weight_names: |
| 75 | + name = str(name) |
| 76 | + w_np = params[name].asnumpy() |
| 77 | + sparsity = 1.0 - (np.count_nonzero(w_np) / w_np.size) |
| 78 | + if sparsity >= sparsity_threshold: |
| 79 | + sparse_weight = sp.bsr_matrix(w_np, blocksize=block_size) |
| 80 | + # remove dense weight |
| 81 | + del params[name] |
| 82 | + memo.weight_name.append(name) |
| 83 | + memo.weight_shape.append(list(sparse_weight.data.shape) + |
| 84 | + list(sparse_weight.indices.shape) + |
| 85 | + list(sparse_weight.indptr.shape)) |
| 86 | + params[name + ".data"] = tvm.nd.array(sparse_weight.data) |
| 87 | + params[name + ".indices"] = tvm.nd.array(sparse_weight.indices) |
| 88 | + params[name + ".indptr"] = tvm.nd.array(sparse_weight.indptr) |
| 89 | + ret = SparseAnalysisResult( |
| 90 | + weight_name=tvm.runtime.convert(memo.weight_name), |
| 91 | + weight_shape=tvm.runtime.convert(memo.weight_shape) |
| 92 | + ) |
| 93 | + return ret |
0 commit comments