def conv_filter3d_delta_coding(conv4d_indices, args, max_distance, pruned_filters_indices, pruned_channels_indices, directory): first_filter = prev_filter = None delta_filters = list() for i, cur_filter in list(enumerate(conv4d_indices)): if i == 0: first_filter = cur_filter else: delta_filters.append( matrix_cycledistance(prev_filter, cur_filter, max_distance)) prev_filter = cur_filter delta_filters = np.array(delta_filters) # print quantized indices statistics # for i in range(2 ** int(args.bits['conv']) + 1): # print(f'{i}: {get_index_percentage(conv4d_indices, i) :.2f} % ' # f'| {get_index_percentage(delta_filters, i) :.2f} %') # Encode t0, d0 = huffman_encode(first_filter.astype('float32'), directory) t1, d1 = huffman_encode(delta_filters.astype('float32'), directory) t2, d2 = huffman_encode(pruned_filters_indices.astype('int32'), directory) t3, d3 = huffman_encode(pruned_channels_indices.astype('int32'), directory) # Get statistics original = first_filter.nbytes + delta_filters.nbytes + pruned_filters_indices.nbytes + pruned_channels_indices.nbytes compressed = t0 + t1 + t2 + t3 + d0 + d1 + d2 + d3 return original, compressed
def conv_width1d_delta_coding(conv4d_indices, args, max_distance, pruned_filters_indices, pruned_channels_indices, directory): indices2d = conv4d_indices.reshape( -1, conv4d_indices.shape[3]) # shape: (kn * cn * h, w) first_col = prev_col = None delta_cols = list() for i in range(indices2d.shape[1]): cur_col = indices2d[:, i] if i == 0: first_col = cur_col else: delta_cols.append( matrix_cycledistance(prev_col, cur_col, max_distance)) prev_col = cur_col delta_cols = np.array(delta_cols) # print quantized indices statistics # for i in range(2 ** int(args.bits['conv']) + 1): # print(f'{i}: {get_index_percentage(conv4d_indices, i) :.2f} % | {get_index_percentage(delta_cols, i) :.2f} %') # Encode t0, d0 = huffman_encode(first_col.astype('float32'), directory) t1, d1 = huffman_encode(delta_cols.astype('float32'), directory) t2, d2 = huffman_encode(pruned_filters_indices.astype('int32'), directory) t3, d3 = huffman_encode(pruned_channels_indices.astype('int32'), directory) # Get statistics original = first_col.nbytes + delta_cols.nbytes + pruned_filters_indices.nbytes + pruned_channels_indices.nbytes compressed = t0 + t1 + t2 + t3 + d0 + d1 + d2 + d3 return original, compressed
def conv_non_delta_coding(conv4d_indices, pruned_filters_indices, pruned_channels_indices, directory): # Encode t0, d0 = huffman_encode(conv4d_indices.astype('float32'), directory) t1, d1 = huffman_encode(pruned_filters_indices.astype('int32'), directory) t2, d2 = huffman_encode(pruned_channels_indices.astype('int32'), directory) # Get statistics original = conv4d_indices.nbytes + pruned_filters_indices.nbytes + pruned_channels_indices.nbytes compressed = t0 + t1 + t2 + d0 + d1 + d2 return original, compressed
def mesa2_huffman_encode_fc2d(fc2d_arr, partitionsize, max_distance, directory): first_block_arr, delta_blocks_arr = get_fc_delta_blocks( to_indices(fc2d_arr), fc2d_arr, partitionsize, max_distance) # Encode t0, d0 = huffman_encode(first_block_arr, directory) t1, d1 = huffman_encode(delta_blocks_arr, directory) # Get statistics original = first_block_arr.nbytes + delta_blocks_arr.nbytes compressed = t0 + t1 + d0 + d1 return original, compressed
def conv_part_filter3d_delta_coding(conv4d_indices, args, max_distance, sorted_filters_indices, pruned_filters_indices, pruned_channels_indices, directory): part_filters_num = 10 # hyper-parameter part_filters_indices, left_filters_indices = ( sorted_filters_indices[:part_filters_num], sorted_filters_indices[part_filters_num:]) part_conv4d_indices, left_conv4d_indices = ( conv4d_indices[part_filters_indices, :, :, :], conv4d_indices[left_filters_indices, :, :, :]) original = compressed = 0 for indices4d in [part_conv4d_indices, left_conv4d_indices]: first_filter = prev_filter = None delta_filters = list() for i, cur_filter in list(enumerate(indices4d)): if i == 0: first_filter = cur_filter else: delta_filters.append( matrix_cycledistance(prev_filter, cur_filter, max_distance)) prev_filter = cur_filter delta_filters = np.array(delta_filters) # print quantized indices statistics # for i in range(2 ** int(args.bits['conv']) + 1): # print(f'{i}: {get_index_percentage(indices4d, i) :.2f} % | ' # f'{get_index_percentage(delta_filters, i) :.2f} %') # Encode t0, d0 = huffman_encode(first_filter.astype('float32'), directory) t1, d1 = huffman_encode(delta_filters.astype('float32'), directory) original += (first_filter.nbytes + delta_filters.nbytes) compressed += (t0 + t1 + d0 + d1) # Encode t2, d2 = huffman_encode(pruned_filters_indices.astype('int32'), directory) t3, d3 = huffman_encode(pruned_channels_indices.astype('int32'), directory) # Get statistics original += (pruned_filters_indices.nbytes + pruned_channels_indices.nbytes) compressed += (t2 + t3 + d2 + d3) return original, compressed
def compute_bpp(code, batch_size, prefix, dir='./code/', save=False): # Huffman coding c = code.data.cpu().numpy().astype(np.int32).flatten() tree_size, data_size = huffman_encode(c, prefix, save_dir=dir, save=save) # bpp = (tree_size + data_size) / batch_size / 128 / 128 * 8 counter = Counter(list(c)) prob = np.array(list(counter.values())) / len(c) entropy = stats.entropy(prob, base=2) bpp = entropy * len(c) / batch_size / 128 / 128 return bpp
def conv_group_filter3d_delta_coding(conv4d_indices, args, max_distance, sorted_filters_indices, pruned_filters_indices, pruned_channels_indices, directory): filters_nums_per_group = 5 # hyper-parameter original = compressed = 0 for i in range(0, len(sorted_filters_indices), filters_nums_per_group): group_filters_indices = sorted_filters_indices[i:i + filters_nums_per_group] indices4d = conv4d_indices[group_filters_indices, :, :, :] first_filter = prev_filter = None delta_filters = list() for j, cur_filter in list(enumerate(indices4d)): if j == 0: first_filter = cur_filter else: delta_filters.append( matrix_cycledistance(prev_filter, cur_filter, max_distance)) prev_filter = cur_filter delta_filters = np.array(delta_filters) # print quantized indices statistics # for j in range(2 ** int(args.bits['conv']) + 1): # print(f'{j}: {get_index_percentage(indices4d, j) :.2f} % |' # f' {get_index_percentage(delta_filters, j) :.2f} %') # Encode t0, d0 = huffman_encode(first_filter.astype('float32'), directory) t1, d1 = huffman_encode(delta_filters.astype('float32'), directory) original += (first_filter.nbytes + delta_filters.nbytes) compressed += (t0 + t1 + d0 + d1) # Encode t2, d2 = huffman_encode(pruned_filters_indices.astype('int32'), directory) t3, d3 = huffman_encode(pruned_channels_indices.astype('int32'), directory) # Get statistics original += (pruned_filters_indices.nbytes + pruned_channels_indices.nbytes) compressed += (t2 + t3 + d2 + d3) return original, compressed