Closed
Conversation
rsub_ offers a much better performance when one wants to perform `a = b
- a`. Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.25126500800252
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.967831622809172
a = 1 - a (a.numel() == 100000) for 4000 times
8.675457138568163
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.616496190428734
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.577398162335157
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.886684477329254
a = 1 - a (a.numel() == 100000) for 4000 times
0.5570489168167114
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3887205272912979
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Collaborator
|
This is a bit unintuitive... Is |
rsub_ offers a much better performance when one wants to perform `a = b - a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.25126500800252
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.967831622809172
a = 1 - a (a.numel() == 100000) for 4000 times
8.675457138568163
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.616496190428734
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.577398162335157
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.886684477329254
a = 1 - a (a.numel() == 100000) for 4000 times
0.5570489168167114
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3887205272912979
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
xuhdev
added a commit
that referenced
this pull request
Aug 23, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`. Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.25126500800252
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.967831622809172
a = 1 - a (a.numel() == 100000) for 4000 times
8.675457138568163
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.616496190428734
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.577398162335157
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.886684477329254
a = 1 - a (a.numel() == 100000) for 4000 times
0.5570489168167114
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3887205272912979
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 2f283dd
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b - a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.25126500800252
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.967831622809172
a = 1 - a (a.numel() == 100000) for 4000 times
8.675457138568163
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.616496190428734
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.577398162335157
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.886684477329254
a = 1 - a (a.numel() == 100000) for 4000 times
0.5570489168167114
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3887205272912979
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
xuhdev
added a commit
that referenced
this pull request
Aug 26, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`. Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.25126500800252
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.967831622809172
a = 1 - a (a.numel() == 100000) for 4000 times
8.675457138568163
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.616496190428734
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.577398162335157
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.886684477329254
a = 1 - a (a.numel() == 100000) for 4000 times
0.5570489168167114
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3887205272912979
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 8952934
Pull Request resolved: #25115
Collaborator
Author
@ssnl I updated the benchmark to include your suggestion -- yes, it is often slower. But I'm also puzzled by this. |
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 27, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 20a7704
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 27, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 3c7312f
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 28, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 0260645
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 29, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 8fcc80c
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 29, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: 225a258
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
xuhdev
added a commit
that referenced
this pull request
Aug 31, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: a6b5f29
Pull Request resolved: #25115
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
Pull Request resolved: #25115
[ghstack-poisoned]
xuhdev
added a commit
that referenced
this pull request
Oct 7, 2019
rsub_ offers a much better performance when one wants to perform `a = b
- a`.
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
```python
import timeit
for device in ('cpu', 'cuda'):
print(device)
for n, t in [(1000, 40_000),
(100_000, 4000)]:
print(f'a = 1 - a (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a=1-a; torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'a.rsub_(1) (a.numel() == {n}) for {t} times')
print(timeit.timeit(f'a.rsub_(1);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
print(f'torch.sub(1, a, out=a) (a.numel() == {n}) for
{t} times')
print(timeit.timeit(f'torch.sub(1, a, out=a);
torch.cuda.synchronize()',
setup=f'import torch; a =
torch.rand({n}, device="{device}");',
number=t))
```
Output:
```
cpu
a = 1 - a (a.numel() == 1000) for 40000 times
9.482540775090456
a.rsub_(1) (a.numel() == 1000) for 40000 times
4.980003070086241
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
6.157954938709736
a = 1 - a (a.numel() == 100000) for 4000 times
8.676277630031109
a.rsub_(1) (a.numel() == 100000) for 4000 times
8.622812658548355
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
8.741503324359655
cuda
a = 1 - a (a.numel() == 1000) for 40000 times
5.594695191830397
a.rsub_(1) (a.numel() == 1000) for 40000 times
3.868969976902008
torch.sub(1, a, out=a) (a.numel() == 1000) for 40000 times
4.848179902881384
a = 1 - a (a.numel() == 100000) for 4000 times
0.5615557432174683
a.rsub_(1) (a.numel() == 100000) for 4000 times
0.3879886604845524
torch.sub(1, a, out=a) (a.numel() == 100000) for 4000 times
0.48638539388775826
```
Additionally:
- rsub.out is kept out, because sub.out should be sufficient.
- Also added torch.rsub (function variant) to match torch.sub.
ghstack-source-id: ce0fbb5
Pull Request resolved: #25115
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stack from ghstack:
rsub_ offers a much better performance when one wants to perform `a = b
Benchmark shows a significant runtime improvement for this
scenario when rsub_ is available:
Script:
Output:
Additionally:
Pull Request resolved: #25115