gpt4 book ai didi

Why is torch.sum slower than torch.dot when accumulating elements?(为什么torch.sum在累积元素时比torch.dot慢?)

转载 作者:bug小助手 更新时间:2023-10-25 14:20:19 31 4
gpt4 key购买 nike



When I accumulate elements in a one-dimensional array arr with size n, I use torch.sum like below.

当我在大小为n的一维数组arr中累加元素时,我使用的torch.sum如下所示。


import torch

n = 100000
arr = torch.randn(n)
res = torch.sum(arr) # about 25.5μs

I also use torch.dot to accumulate elements of arr with the help of a helper array helper_arr of size n whose elements are all 1s, as shown in the following code.

我还在一个大小为n的助手数组helper_arr的帮助下使用torch.dot累加arr的元素,它的元素都是1,如下面的代码所示。


import torch

n = 100000
arr = torch.randn(n)

helper_arr = torch.ones(n)
res = torch.dot(arr, helper_arr) # about 3.88μs

But I find that when n is 100000, torch.sum takes about 7 times longer than torch.dot. But when I increase n to 100000000, torch.sum takes less time than torch.dot. I don't know why this is happening, this question makes me very confused.

但我发现当n为100000时,torch.sum花费的时间大约是torch.dot的7倍。但是当我将n增加到100000000时,torch.sum所需的时间比torch.dot要少。我不知道为什么会发生这样的事情,这个问题让我很困惑。


更多回答

Please do not post images of code or errors. Text is preferred so that we can try to reproduce the problem without having to re-type everything and your question can be properly indexed or read by screen readers.

请不要发布代码或错误的图像。文本是首选的,这样我们就可以尝试重现问题,而不必重新键入所有内容,并且您的问题可以被屏幕阅读器正确索引或阅读。

not taking into account the time of initializing the tensors, torch.dot is always faster in my machine, which might be because dot method is optimized in pytorch.

不考虑初始化张量的时间,torch.dot在我的机器上总是更快,这可能是因为点方法在pytorch中进行了优化。

优秀答案推荐
更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com