Hugging Face 发布块稀疏矩阵优化方案

原标题:Block Sparse Matrices for Smaller and Faster Language Models
48AI 研判分

Hugging Face 推出基于 PyTorch 的块稀疏矩阵实现,旨在通过剪枝降低语言模型参数量并提升推理速度。该方案针对现有稀疏计算效率低下的痛点,利用 NVIDIA Ampere 架构特性优化稀疏模式,为构建更小更快的 LLM 提供底层支持。

报道正文

本文目录6 个章节

返回文章列表

用于更小、更快语言模型的块稀疏矩阵

发布于 2020 年 9 月 10 日 在 GitHub 上更新 点赞 - François Lagunas madlag

逐个零地节省空间和时间

在之前的 博客 文章 中,我们介绍了稀疏矩阵及其对神经网络可能带来的改进。

基本假设是,全密集层(dense layers)往往过于冗余,可以在不显著损失精度的情况下进行剪枝。在某些情况下,稀疏线性层甚至能 提高精度和/或泛化能力。

主要问题在于,目前支持稀疏代数计算的代码严重缺乏效率。我们也 仍在等待 PyTorch 的官方支持。

正因如此,我们不再耐心等候,并在今年夏天抽出时间来解决这一“空白”。今天,我们很高兴 发布扩展库 pytorch_block_sparse。

该库本身,或者更好地结合其他方法如 知识蒸馏 和 量化,能够构建既 更小又更快 的 网络。Hugging Face 认为这对于让任何人以 低成本 在生产环境中使用神经网络至关重要,并能 提升最终用户的体验。

使用方法

提供的 BlockSparseLinear 模块是 torch.nn.Linear 的直接替代品,在你的模型中使用它非常简单:

PYTHON

from pytorch_block_sparse import BlockSparseLinear

...

self.fc = BlockSparseLinear(1024, 256, density=0.1)

该扩展还提供了 BlockSparseModelPatcher,允许“即时”修改现有模型,这在此 示例 notebook 中有所展示。这样的模型随后可以像往常一样进行训练,无需更改你的模型源代码。

NVIDIA CUTLASS

This extension is based on the cutlass tilesparse proof of concept by Yulhwa Kim.

It is using C++ CUDA templates for block-sparse matrix multiplication based on CUTLASS.

CUTLASS is a collection of CUDA C++ templates for implementing high-performance CUDA kernels. With CUTLASS, approching cuBLAS performance on custom kernels is possible without resorting to assembly language code.

The latest versions include all the Ampere Tensor Core primitives, providing x10 or more speedups with a limited loss of precision. Next versions of pytorch_block_sparse will make use of these primitives, as block sparsity is 100% compatible with Tensor Cores requirements.

Performance

At the current stage of the library, the performances for sparse matrices are roughly two times slower than their cuBLAS optimized dense counterpart, and we are confident that we can improve this in the future.

This is a huge improvement on PyTorch sparse matrices: their current implementation is an order of magnitude slower than the dense one.

But the more important point is that the performance gain of using sparse matrices grows with the sparsity, so a 75% sparse matrix is roughly 2x faster than the dense equivalent.

The memory savings are even more significant: for 75% sparsity, memory consumption is reduced by 4x as you would expect.

Future work

Being able to efficiently train block-sparse linear layers was just the first step. The sparsity pattern is currenly fixed at initialization, and of course optimizing it during learning will yield large improvements.

So in future versions, you can expect tools to measure the "usefulness" of parameters to be able to optimize the sparsity pattern. NVIDIA Ampere 50% sparse pattern within blocks will probably yield another significant performance gain, just as upgrading to more recent versions of CUTLASS does.

So, stay tuned for more sparsity goodness in a near future!