NumPy/SciPy中如何使用多线程整数矩阵乘法?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (82)

做类似的事情

import numpy as np
a = np.random.rand(10**4, 10**4)
b = np.dot(a, a)

使用多个核心,运行良好。

中的元素a不过,64位浮点数(或者32位平台中的32位浮点数?),我想乘8位整数数组。不过,尝试以下几点:

a = np.random.randint(2, size=(n, n)).astype(np.int8)

结果在网点产品中不使用多核,从而使我的PC机运行速度慢了1000倍。

array: np.random.randint(2, size=shape).astype(dtype)

dtype    shape          %time (average)

float32 (2000, 2000)    62.5 ms
float32 (3000, 3000)    219 ms
float32 (4000, 4000)    328 ms
float32 (10000, 10000)  4.09 s

int8    (2000, 2000)    13 seconds
int8    (3000, 3000)    3min 26s
int8    (4000, 4000)    12min 20s
int8    (10000, 10000)  It didn't finish in 6 hours

float16 (2000, 2000)    2min 25s
float16 (3000, 3000)    Not tested
float16 (4000, 4000)    Not tested
float16 (10000, 10000)  Not tested

我理解NumPy使用blas,它不支持整数,但是如果我使用SciPyblas包装,即:

import scipy.linalg.blas as blas
a = np.random.randint(2, size=(n, n)).astype(np.int8)
b = blas.sgemm(alpha=1.0, a=a, b=a)

计算多线程。现在,blas.sgemm运行的时间与np.dot对于浮动32,但对于非浮动,它将一切转换为float32和输出浮动,这是np.dot不管用。(此外,b现在在F_CONTIGUOUS秩序,这是一个较小的问题)。

因此,如果我想做整数矩阵乘法,我必须执行以下操作之一:

  1. 使用NumPy的痛苦缓慢np.dot很高兴我保留了8位整数。
  2. 使用SciPy的sgemm消耗掉4倍的内存。
  3. 使用Numpy的np.float16只消耗了2x内存,但请注意np.dot在浮动16数组上要比在浮动32数组上慢得多,比int 8慢得多。
  4. 找到一个优化的多线程整数矩阵乘法库(实际上,数学这样做,但我更喜欢Python解决方案),理想情况下支持1位数组,尽管8位数组也很好。

我可以按照选项4吗?有这样的库吗?

我实际上正在运行NumPy+MKL,但我已经尝试过类似的测试,在Vanilly NumPy上,结果相似。

提问于
用户回答回答于

执行情况:

import numpy as np
from numpy.testing import assert_array_equal
import threading
from time import time


def blockshaped(arr, nrows, ncols):
    """
    Return an array of shape (nrows, ncols, n, m) where
    n * nrows, m * ncols = arr.shape.
    This should be a view of the original array.
    """
    h, w = arr.shape
    n, m = h // nrows, w // ncols
    return arr.reshape(nrows, n, ncols, m).swapaxes(1, 2)


def do_dot(a, b, out):
    #np.dot(a, b, out)  # does not work. maybe because out is not C-contiguous?
    out[:] = np.dot(a, b)  # less efficient because the output is stored in a temporary array?


def pardot(a, b, nblocks, mblocks, dot_func=do_dot):
    """
    Return the matrix product a * b.
    The product is split into nblocks * mblocks partitions that are performed
    in parallel threads.
    """
    n_jobs = nblocks * mblocks
    print('running {} jobs in parallel'.format(n_jobs))

    out = np.empty((a.shape[0], b.shape[1]), dtype=a.dtype)

    out_blocks = blockshaped(out, nblocks, mblocks)
    a_blocks = blockshaped(a, nblocks, 1)
    b_blocks = blockshaped(b, 1, mblocks)

    threads = []
    for i in range(nblocks):
        for j in range(mblocks):
            th = threading.Thread(target=dot_func, 
                                  args=(a_blocks[i, 0, :, :], 
                                        b_blocks[0, j, :, :], 
                                        out_blocks[i, j, :, :]))
            th.start()
            threads.append(th)

    for th in threads:
        th.join()

    return out


if __name__ == '__main__':
    a = np.ones((4, 3), dtype=int)
    b = np.arange(18, dtype=int).reshape(3, 6)
    assert_array_equal(pardot(a, b, 2, 2), np.dot(a, b))

    a = np.random.randn(1500, 1500).astype(int)

    start = time()
    pardot(a, a, 2, 4)
    time_par = time() - start
    print('pardot: {:.2f} seconds taken'.format(time_par))

    start = time()
    np.dot(a, a)
    time_dot = time() - start
    print('np.dot: {:.2f} seconds taken'.format(time_dot))

通过这个实现,我得到了大约x4的加速比,这是我的机器中核心的物理数目:

running 8 jobs in parallel
pardot: 5.45 seconds taken
np.dot: 22.30 seconds taken

扫码关注云+社区