xtensor 类型的性能与 NumPy 的简单归约

Performance of xtensor types vs. NumPy for simple reduction

我正在尝试 xtensor-python and started by writing a very simple sum function, after using the cookiecutter setup and enabling SIMD intrinsics with xsimd

inline double sum_pytensor(xt::pytensor<double, 1> &m)
{
  return xt::sum(m)();
}
inline double sum_pyarray(xt::pyarray<double> &m)
{
  return xt::sum(m)();
}

使用 setup.py 构建了我的 Python 模块,然后测试了由不同大小的 np.random.randn 构建的 NumPy 数组的求和函数,与 np.sum 进行比较。

import timeit

def time_each(func_names, sizes):
    setup = f'''
import numpy; import xtensor_basics
arr = numpy.random.randn({sizes})
    '''
    tim = lambda func: min(timeit.Timer(f'{func}(arr)',
                                        setup=setup).repeat(7, 100))
    return [tim(func) for func in func_names]

from functools import partial

sizes = [10 ** i for i in range(9)]
funcs = ['numpy.sum',
         'xtensor_basics.sum_pyarray',
         'xtensor_basics.sum_pytensor']
sum_timer = partial(time_each, funcs)
times = list(map(sum_timer, sizes))

这个(可能有缺陷的)基准测试似乎表明,与 NumPy 相比,对于更大的数组,xtensor 对于这个基本函数的性能有所下降。

           numpy.sum  xtensor_basics.sum_pyarray  xtensor_basics.sum_pytensor
1           0.000268                    0.000039                     0.000039
10          0.000258                    0.000040                     0.000039
100         0.000247                    0.000048                     0.000049
1000        0.000288                    0.000167                     0.000164
10000       0.000568                    0.001353                     0.001341
100000      0.003087                    0.013033                     0.013038
1000000     0.045171                    0.132150                     0.132174
10000000    0.434112                    1.313274                     1.313434
100000000   4.180580                   13.129517                    13.129058

知道我为什么会看到这个吗?我猜这是 NumPy 利用的东西,而 xtensor(目前)还没有,但我不确定像这样简单的减少会是什么。我仔细研究了 xmath.hpp 但没有看到任何明显的东西,文档中也没有引用任何类似内容。


版本

numpy                          1.13.3
openblas                       0.2.20
python                         3.6.3
xtensor                        0.12.1
xtensor-python                 0.14.0 

哇,这真是巧合!我正在研究这个加速!

xtensor 的求和是一个惰性操作——它不使用(自动)矢量化的最高性能迭代顺序。但是,我们刚刚向缩减(以及即将到来的累积)添加了一个 evaluation_strategy 参数,它允许您在 immediatelazy 之间进行 select 缩减。

立即缩减立即执行缩减(而不是懒惰)并且可以使用针对矢量化缩减优化的迭代顺序。

您可以在此 PR 中找到此功能:https://github.com/QuantStack/xtensor/pull/550

在我的基准测试中,这应该至少与 numpy 一样快或更快。 我希望今天能把它合并。

顺便说一句。请不要犹豫,访问我们的 gitter 频道和 post 一个 link 问题,我们需要更好地监控 Whosebug:https://gitter.im/QuantStack/Lobby