본문 바로가기

NVIDIA Jetson Xavier AGX

Secrets of cudaMemcpyAsync delay

Host To Device copy를 비동기식으로 사용하기 위해 cudaMemcpyAsync를 사용하고 있다.

Nsight system으로 분석하던 중 cudaMemcpyAsync가 delay 되는 것을 확인했다.

cudaMemcpyAsync가 delay되는 가정과 조건을 작성해본다.

 

Asynchronous memory copy란,

CPU의 cudaMemcpyAsync, kernel call (CUDA API) 등 과 GPU의 Memcpy HtoD, Kernel executions (CUDA HW) 등이 동기화 없이 동작하는 것이다.

만약 동기화 상태로 동작한다면 CPU가 kernel을 호출하고나서 GPU의 kernel 연산이 끝날때 까지 CPU는 계속 대기한다.

비동기식으로 동작하면 CPU는 호출만 하고 다시 할일을 한다.

 

실험은 NVIDIA Jetson AGX Xavier에서 Darknet과 yolo v4를 사용했고 demand layering 방식이 적용되어 있다.

 

*참고사항

65,536 bytes = 16384 * 4 = 1<<14 * sizeof(float)

1,048,576 bytes = 262144 * 4 = 1<<18 * sizeof(float)

2,097,152 bytes = 524288 * 4 = 1<<19 * sizeof(float)

 

<가정>

GPU의 copy engine은 크게 2가지의 Queue를 가지고 있고, Queue가 가득차서 delay가 발생하는 것이다.

1. Queue for Normal size 

2. Queue for Big size

 

<실험 및 고찰>

1. Queue for Normal size 

먼저 Queue for Nornal size는 65,536 bytes 이하의 memcpy를 위한 queue이다.

간단하게 실험했을때

65,536 bytes는 14개까지 asynchronously 동작했고 15번째 cpoy부터 delay가 발생했다.

32,784 bytes는 25개까지 asynchronously 동작했고 26번째 cpoy부터 delay가 발생했다.

 

이렇게 많은 수의 copy를 대기시키지 않고서는 65,536 bytes 이하의 size는 delay를 발생시키기 꽤 어렵다.

 

2. Queue for Big size

Big size를 위한 Queue는 2개의 1,048,576 bytes Q로 분할되지만 복사할 크기에 따라 1개 또는 2개를 대기시킬 수 있다. 

Case 1) 65,540~1,048,576 bytes

Case 2) 1,048,580~2,097,152 bytes

Case 3) 2,097,156 bytes 이상

 

결론부터 말하면 Asynchronously memcpy size가 Case 1이라면 2개를 각 Q에 대기시킬 수 있고, Case 2라면 2개의 Q를 사용해 1개만 대기시킬 수 있으며, 그 이상의 사이즈(Case 3)는 Asynchronously 동작시킬 수 없고 Q가 모두 비어있을 때까지 기다려야 한다.

 

 

아래 더보기를 보면 yolov4가 총 245MB의 parameter를 가지고 있음을 알 수 있고 각 레이어는 몇 bytes를 요구하는지 알 수 있다. 

더보기

yolov4.weights file이 가지고 있는 layer별 parameter bytes

 

                          누적 bytes (해당 레이어의 parameter byte) n-th layer

File start :             20bytes
l.weights :             3988 (3968) 0th layer
l.weights :             78740 (74752) 1th layer
l.weights :             96148 (17408) 2th layer
l.weights :             113556 (17408) 4th layer
l.weights :             122260 (8704) 5th layer
l.weights :             197012 (74752) 6th layer
l.weights :             214420 (17408) 8th layer
l.weights :             248212 (33792) 10th layer
l.weights :             545172 (296960) 11th layer
l.weights :             578964 (33792) 12th layer
l.weights :             612756 (33792) 14th layer
l.weights :             630164 (17408) 15th layer
l.weights :             778644 (148480) 16th layer
l.weights :             796052 (17408) 18th layer
l.weights :             944532 (148480) 19th layer
l.weights :             961940 (17408) 21th layer
l.weights :             1029524 (67584) 23th layer
l.weights :             2213268 (1183744) 24th layer
l.weights :             2346388 (133120) 25th layer
l.weights :             2479508 (133120) 27th layer
l.weights :             2547092 (67584) 28th layer
l.weights :             3138964 (591872) 29th layer
l.weights :             3206548 (67584) 31th layer
l.weights :             3798420 (591872) 32th layer
l.weights :             3866004 (67584) 34th layer
l.weights :             4457876 (591872) 35th layer
l.weights :             4525460 (67584) 37th layer
l.weights :             5117332 (591872) 38th layer
l.weights :             5184916 (67584) 40th layer
l.weights :             5776788 (591872) 41th layer
l.weights :             5844372 (67584) 43th layer
l.weights :             6436244 (591872) 44th layer
l.weights :             6503828 (67584) 46th layer
l.weights :             7095700 (591872) 47th layer
l.weights :             7163284 (67584) 49th layer
l.weights :             7755156 (591872) 50th layer
l.weights :             7822740 (67584) 52th layer
l.weights :             8088980 (266240) 54th layer
l.weights :             12815764 (4726784) 55th layer
l.weights :             13344148 (528384) 56th layer
l.weights :             13872532 (528384) 58th layer
l.weights :             14138772 (266240) 59th layer
l.weights :             16502164 (2363392) 60th layer
l.weights :             16768404 (266240) 62th layer
l.weights :             19131796 (2363392) 63th layer
l.weights :             19398036 (266240) 65th layer
l.weights :             21761428 (2363392) 66th layer
l.weights :             22027668 (266240) 68th layer
l.weights :             24391060 (2363392) 69th layer
l.weights :             24657300 (266240) 71th layer
l.weights :             27020692 (2363392) 72th layer
l.weights :             27286932 (266240) 74th layer
l.weights :             29650324 (2363392) 75th layer
l.weights :             29916564 (266240) 77th layer
l.weights :             32279956 (2363392) 78th layer
l.weights :             32546196 (266240) 80th layer
l.weights :             34909588 (2363392) 81th layer
l.weights :             35175828 (266240) 83th layer
l.weights :             36232596 (1056768) 85th layer
l.weights :             55123348 (18890752) 86th layer
l.weights :             57228692 (2105344) 87th layer
l.weights :             59334036 (2105344) 89th layer
l.weights :             60390804 (1056768) 90th layer
l.weights :             69836180 (9445376) 91th layer
l.weights :             70892948 (1056768) 93th layer
l.weights :             80338324 (9445376) 94th layer
l.weights :             81395092 (1056768) 96th layer
l.weights :             90840468 (9445376) 97th layer
l.weights :             91897236 (1056768) 99th layer
l.weights :             101342612 (9445376) 100th layer
l.weights :             102399380 (1056768) 102th layer
l.weights :             106610068 (4210688) 104th layer
l.weights :             108715412 (2105344) 105th layer
l.weights :             127606164 (18890752) 106th layer
l.weights :             129711508 (2105344) 107th layer
l.weights :             133914004 (4202496) 114th layer
l.weights :             152804756 (18890752) 115th layer
l.weights :             154910100 (2105344) 116th layer
l.weights :             155438484 (528384) 117th layer
l.weights :             155966868 (528384) 120th layer
l.weights :             156495252 (528384) 122th layer
l.weights :             161222036 (4726784) 123th layer
l.weights :             161750420 (528384) 124th layer
l.weights :             166477204 (4726784) 125th layer
l.weights :             167005588 (528384) 126th layer
l.weights :             167138708 (133120) 127th layer
l.weights :             167271828 (133120) 130th layer
l.weights :             167404948 (133120) 132th layer
l.weights :             168588692 (1183744) 133th layer
l.weights :             168721812 (133120) 134th layer
l.weights :             169905556 (1183744) 135th layer
l.weights :             170038676 (133120) 136th layer
l.weights :             171222420 (1183744) 137th layer
l.weights :             171484560 (262140) 138th layer
l.weights :             172668304 (1183744) 141th layer
l.weights :             173196688 (528384) 143th layer
l.weights :             177923472 (4726784) 144th layer
l.weights :             178451856 (528384) 145th layer
l.weights :             183178640 (4726784) 146th layer
l.weights :             183707024 (528384) 147th layer
l.weights :             188433808 (4726784) 148th layer
l.weights :             188957068 (523260) 149th layer
l.weights :             193683852 (4726784) 152th layer
l.weights :             195789196 (2105344) 154th layer
l.weights :             214679948 (18890752) 155th layer
l.weights :             216785292 (2105344) 156th layer
l.weights :             235676044 (18890752) 157th layer
l.weights :             237781388 (2105344) 158th layer
l.weights :             256672140 (18890752) 159th layer
l.weights :             257717640 (1045500) 160th layer
Done! Loaded 162 layers from weights-file

 

위 parameter들은 모두 cudaMemcpyAsync로 복사된다.

이때 1,6,11,16,19,24,25 ... 등의 레이어는 65,536 bytes를 넘기때문에 big size queue로 들어가게 된다. 

big size queue는 Case 1이 최대 2개 들어갈 수 있어서 1번, 6번 레이어는 async가 가능하다.

 

CUDA API의 빨간색은 cudaMemcpyAsync call이고 CUDA HW의 초록색이 Memcpy HtoD이다.

11번째 레이어에서 async delay가 발생하게 되는데 delay가 풀리는 시점을 보면 queue에 들어간 1번 레이어의 memcpy가 동작한 직후 aysnc delay가 해제된다.

 

24번 레이어는 Case 1을 넘기는 매우 큰 사이즈를 갖는데 이는 2개의 queue를 필요로 하기 때문에 이전에 사용중인 16번 레이어와 19번 레이어가 memcpy 완료되어야 queue에 할당할 수 있다.

 

가정이지만 거의 들어맞고 더 정확한 자료나 오피셜 정보가 보이면 공유하도록 하겠다.

 

 

'NVIDIA Jetson Xavier AGX' 카테고리의 다른 글

Memory address 0x55 & 0x7f  (0) 2021.06.29
LaneNet Install in NVIDIA Jetson Xavier AGX  (0) 2020.10.07