teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

4

修正

2020/07/22 03:54

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -17,6 +17,12 @@
17
17
  * Core i7-6700K @ 4.00GHz (index が numpy): 27.5 ms
18
18
  * Core i7-6700K @ 4.00GHz (index が list): 442 ms
19
19
 
20
+ インデックスにリストを使うなど配列処理に Python のオブジェクトが混じってしまうと、Cで最適化された numpy のコードではなく、Python のコードを実行することになるので、かなり遅くなります。
21
+
22
+ おおよそ
23
+ * GPU は CPU より数十倍早い
24
+ * C言語は Python より100倍ぐらい早い
25
+
20
26
  [Python - コードの実行時間を計測する方法 - pystyle](https://pystyle.info/python-wall-time-measurement/)
21
27
 
22
28
  ### Pytorch の計測コード

3

修正

2020/07/22 03:54

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -14,8 +14,8 @@
14
14
 
15
15
  * GTX 1080: 1.23 ms
16
16
  * GTX 2080: 700 µs
17
- * CPU (index が numpy): 27.5 ms
17
+ * Core i7-6700K @ 4.00GHz (index が numpy): 27.5 ms
18
- * CPU (index が list): 442 ms
18
+ * Core i7-6700K @ 4.00GHz (index が list): 442 ms
19
19
 
20
20
  [Python - コードの実行時間を計測する方法 - pystyle](https://pystyle.info/python-wall-time-measurement/)
21
21
 

2

修正

2020/07/22 03:48

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  > pytorchはfancy indexをサポートしていないということでしたので
2
2
 
3
3
  サポートしています。
4
- 遅いのは、一旦 numpy に戻して処理しているからだと思います。
4
+ 遅いのは、一旦 numpy に戻した上に、インデックスが tolist() で Python のリストにして処理しているからだと思います。
5
5
 
6
6
  numpy でできることは、基本的に Pytorch でできるので、例えば arange() なども `numpy.arange()` ではなく、`torch.arange(..., device="cuda") のように最初から GPU 上に作りましょう。
7
7
 
@@ -14,9 +14,13 @@
14
14
 
15
15
  * GTX 1080: 1.23 ms
16
16
  * GTX 2080: 700 µs
17
+ * CPU (index が numpy): 27.5 ms
18
+ * CPU (index が list): 442 ms
17
19
 
18
20
  [Python - コードの実行時間を計測する方法 - pystyle](https://pystyle.info/python-wall-time-measurement/)
19
21
 
22
+ ### Pytorch の計測コード
23
+
20
24
  ```python
21
25
  import torch
22
26
 
@@ -33,4 +37,42 @@
33
37
  %timeit A[I, J]
34
38
  # GTX 1080: 1.23 ms ± 154 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
35
39
  # GTX 2080: 700 µs ± 68.1 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
40
+ ```
41
+
42
+ ### CPU (index が numpy) の計測コード
43
+
44
+ ```
45
+ import numpy as np
46
+
47
+ N = 7000
48
+ M = 1500000
49
+
50
+ A = np.random.randn(N, N)
51
+ I = np.random.randint(0, N, size=M)
52
+ J = np.random.randint(0, N, size=M)
53
+
54
+ print(A.shape, I.shape, J.shape)
55
+ # torch.Size([7000, 7000]) torch.Size([1500000]) torch.Size([1500000])
56
+
57
+ %timeit -n100 A[I, J]
58
+ # 27.5 ms ± 57.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
59
+ ```
60
+
61
+ ### CPU (index が list) の計測コード
62
+
63
+ ```python
64
+ import numpy as np
65
+
66
+ N = 7000
67
+ M = 1500000
68
+
69
+ A = np.random.randn(N, N)
70
+ I = np.random.randint(0, N, size=M).tolist()
71
+ J = np.random.randint(0, N, size=M).tolist()
72
+
73
+ print(A.shape, len(I), len(J))
74
+ # torch.Size([7000, 7000]) torch.Size([1500000]) torch.Size([1500000])
75
+
76
+ %timeit -n1 A[I, J]
77
+ # 442 ms ± 2.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
36
78
  ```

1

修正

2020/07/22 03:46

投稿

tiitoi
tiitoi

スコア21960

answer CHANGED
@@ -23,9 +23,9 @@
23
23
  N = 7000
24
24
  M = 1500000
25
25
 
26
- A = torch.randn(N, N, device="cuda:1")
26
+ A = torch.randn(N, N, device="cuda")
27
- I = torch.randint(high=N, size=(M,), device="cuda:1")
27
+ I = torch.randint(high=N, size=(M,), device="cuda")
28
- J = torch.randint(high=N, size=(M,), device="cuda:1")
28
+ J = torch.randint(high=N, size=(M,), device="cuda")
29
29
 
30
30
  print(A.shape, I.shape, J.shape)
31
31
  # torch.Size([7000, 7000]) torch.Size([1500000]) torch.Size([1500000])