估计是裁剪完的网络通道数不是2^n,导致再GPU上并行加速效果下降,推理时间就增加了。CPU上应该会有速度提升。
估计是裁剪完的网络通道数不是2^n,导致再GPU上并行加速效果下降,推理时间就增加了。CPU上应该会有速度提升。
Originally posted by @Yaodada12 in https://github.com/midasklr/yolov5prune/issues/89#issuecomment-1197633797 您好,我测试了您说明的情况发现CPU上确实是更快的,但是还是想了解,在这种情况下有什么办法可以加快GPU上的推理速度吗?还是说只有调大剪枝率才可以呢?
估计是裁剪完的网络通道数不是2^n,导致再GPU上并行加速效果下降,推理时间就增加了。CPU上应该会有速度提升。Originally posted by @Yaodada12 in #89 (comment) 您好,我测试了您说明的情况发现CPU上确实是更快的,但是还是想了解,在这种情况下有什么办法可以加快GPU上的推理速度吗?还是说只有调大剪枝率才可以呢?
尝试控制剪枝后的通道数为2^n,速度不满意根据场景看能不能用TensorRT加速。
对于通道数非2的幂次方的情况,可补充通道数圆整逻辑,具体如下(prune_utils.py文件):
mod-1: 补充通道数圆整参数计算逻辑:
def closest_powers_of_pruned_chn(n): max_power = int(math.ceil(math.log2(n))) min_power = int(math.floor(math.log2(n))) min_val, max_val = 2min_power, 2max_power # 若参数n与min_val差值的绝对值,较min_val不足25%,则统一圆整至min_val round_channel_num = max_val if float((n-min_val)) / float(min_val) > 0.25 else min_val return round_channel_num
mod-2: 修改obtain_bn_mask函数逻辑
def obtain_bn_mask(bn_module, thre): thre = thre.cuda() # 针对通道数,补充2的幂次方圆整逻辑 bn_weight_ori_list = bn_module.weight.data.abs().tolist() bn_weight_sort_list = sorted(bn_weight_ori_list, reverse=True) filter_weights = [cur_weight for cur_weight in bn_weight_sort_list if cur_weight > thre] filter_weight_len = len(filter_weights) round_chn_num = closest_powers_of_pruned_chn(filter_weight_len) rounded_thres = bn_weight_sort_list[round_chn_num-1] mask = bn_module.weight.data.abs().ge(rounded_thres).float() return mask