为什么`numpy.einsum`在`float32`中比在`float16`或`uint16`中工作更快? [英] Why does `numpy.einsum` work faster with `float32` than `float16` or `uint16`?

查看:179
本文介绍了为什么`numpy.einsum`在`float32`中比在`float16`或`uint16`中工作更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用numpy 1.12.0的基准测试中,使用float32 ndarrays计算点积比其他数据类型要快得多:

In my benchmark using numpy 1.12.0, calculating dot products with float32 ndarrays is much faster than the other data types:

In [3]: f16 = np.random.random((500000, 128)).astype('float16')
In [4]: f32 = np.random.random((500000, 128)).astype('float32')
In [5]: uint = np.random.randint(1, 60000, (500000, 128)).astype('uint16')

In [7]: %timeit np.einsum('ij,ij->i', f16, f16)
1 loop, best of 3: 320 ms per loop

In [8]: %timeit np.einsum('ij,ij->i', f32, f32)
The slowest run took 4.88 times longer than the fastest. This could mean that an intermediate result is being cached.
10 loops, best of 3: 19 ms per loop

In [9]: %timeit np.einsum('ij,ij->i', uint, uint)
10 loops, best of 3: 43.5 ms per loop

我尝试分析einsum,但是它只是将所有计算委托给C函数,所以我不知道造成这种性能差异的主要原因是什么.

I've tried profiling einsum, but it just delegates all the computing to a C function, so I don't know what's the main reason for this performance difference.

推荐答案

我对f16f32数组进行的测试显示,对于所有计算,f16的速度要慢5-10倍.仅当执行数组copy之类的字节级操作时,float16的更紧凑的性质才会显示出任何速度优势.

My tests with your f16 and f32 arrays shows that f16 is 5-10x slower for all calculations. It's only when doing byte level operations like array copy does more compact nature of float16 show any speed advantage.

https://gcc.gnu.org/onlinedocs/gcc/Half -Precision.html

gcc文档中有关半浮点数fp16的部分.使用正确的处理器和正确的编译器开关,可能会以加速这些计算的方式安装numpy.我们还必须检查numpy .h文件是否有任何特殊规定来处理半浮点数.

Is the section in the gcc docs about half floats, fp16. With the right processor and right compiler switches, it may possible to install numpy in way that speeds up these calculations. We'd also have to check if numpy .h files have any provision for special handling of half floats.

较早的问题,可能足以重复引用

Earlier questions, may be good enough to be duplicate references

Python Numpy数据类型性能

Python numpy float16数据类型操作和float8吗?

这篇关于为什么`numpy.einsum`在`float32`中比在`float16`或`uint16`中工作更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆