|
4 | 4 |
|
5 | 5 | ## ๊ฐ์ |
6 | 6 |
|
7 | | -๋ฒกํฐ `a`์ `b`๋ฅผ ๋ธ๋ก๋์บ์คํธ(broadcast)๋ก ๋ํด 2D ํ๋ ฌ `output`์ ์ ์ฅํ๋ ์ปค๋์ ๊ตฌํํด ๋ณด์ธ์. |
8 | | - |
9 | | -๋ณ๋ ฌ ํ๋ก๊ทธ๋๋ฐ์์ **๋ธ๋ก๋์บ์คํธ**๋ ์์๋ณ ์ฐ์ฐ์ ํ ๋ ์ ์ฐจ์ ๋ฐฐ์ด์ ๊ณ ์ฐจ์ ๋ฐฐ์ด์ ํ์์ ๋ง๊ฒ ์๋์ผ๋ก ํ์ฅํ๋ ๊ฒ์ ๋งํฉ๋๋ค. ์ค์ ๋ก ๋ฉ๋ชจ๋ฆฌ์ ๋ฐ์ดํฐ๋ฅผ ๋ณต์ ํ์ง ์๊ณ , ์ถ๊ฐ ์ฐจ์์ ๊ฑธ์ณ ๊ฐ์ ๋
ผ๋ฆฌ์ ์ผ๋ก ๋ฐ๋ณตํ๋ ๋ฐฉ์์
๋๋ค. ์๋ฅผ ๋ค์ด, 2D ํ๋ ฌ์ ๊ฐ ํ(๋๋ ์ด)์ 1D ๋ฒกํฐ๋ฅผ ๋ํ ๋ ๋ฒกํฐ๋ฅผ ์ฌ๋ฌ ๋ฒ ๋ณต์ฌํ์ง ์์๋ ๊ฐ์ ์์๊ฐ ์๋์ผ๋ก ๋ฐ๋ณต ์ ์ฉ๋ฉ๋๋ค. |
| 7 | +1D TileTensor `a`์ `b`๋ฅผ ๋ธ๋ก๋์บ์คํธ๋ก ๋ํด 2D TileTensor `output`์ ์ ์ฅํ๋ ์ปค๋์ ๊ตฌํํด ๋ณด์ธ์. |
10 | 8 |
|
11 | 9 | **์ฐธ๊ณ **: _์ค๋ ๋ ์๊ฐ ํ๋ ฌ์ ์์น ์๋ณด๋ค ๋ง์ต๋๋ค._ |
12 | 10 |
|
|
15 | 13 |
|
16 | 14 | ## ํต์ฌ ๊ฐ๋
|
17 | 15 |
|
18 | | -- ๋ฒกํฐ๋ฅผ ํ๋ ฌ๋ก ๋ธ๋ก๋์บ์คํธํ๊ธฐ |
19 | | -- 2D ์ค๋ ๋ ๊ด๋ฆฌ |
20 | | -- ์๋ก ๋ค๋ฅธ ์ฐจ์ ๊ฐ ์ฐ์ฐ |
21 | | -- ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์ ํจํด |
| 16 | +์ด ํผ์ฆ์์ ๋ฐฐ์ธ ๋ด์ฉ: |
| 17 | + |
| 18 | +- ๋ธ๋ก๋์บ์คํธ ์ฐ์ฐ์ `TileTensor` ์ฌ์ฉํ๊ธฐ |
| 19 | +- ์๋ก ๋ค๋ฅธ ํ
์ ํฌ๊ธฐ ๋ค๋ฃจ๊ธฐ |
| 20 | +- `TileTensor`๋ก 2D ์ธ๋ฑ์ฑ ์ฒ๋ฆฌํ๊ธฐ |
| 21 | + |
| 22 | +ํต์ฌ์ `TileTensor`๊ฐ ์๋ก ๋ค๋ฅธ ํ
์ ํฌ๊ธฐ \\((1, n)\\)์ \\((n, 1)\\)์ \\((n,n)\\)์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๋ธ๋ก๋์บ์คํธํ ์ ์๋ค๋ ์ ์
๋๋ค. ๊ทธ๋ฌ๋ฉด์๋ ๊ฒฝ๊ณ ๊ฒ์ฌ๋ ์ฌ์ ํ ํ์ํฉ๋๋ค. |
| 23 | + |
| 24 | +- **ํ
์ ํฌ๊ธฐ**: ์
๋ ฅ ๋ฒกํฐ์ ํฌ๊ธฐ๋ \\((1, n)\\)๊ณผ \\((n, 1)\\) |
| 25 | +- **๋ธ๋ก๋์บ์คํธ**: ๋ ์ฐจ์์ ๊ฒฐํฉํด \\((n,n)\\) ์ถ๋ ฅ ์์ฑ |
| 26 | +- **๊ฐ๋ ์กฐ๊ฑด**: ์ถ๋ ฅ ํฌ๊ธฐ์ ๋ํ ๊ฒฝ๊ณ ๊ฒ์ฌ๋ ์ฌ์ ํ ํ์ |
| 27 | +- **์ค๋ ๋ ๋ฒ์**: ํ
์ ์์ \\((2 \times 2)\\)๋ณด๋ค ์ค๋ ๋ \\((3 \times 3)\\)๊ฐ ๋ง์ |
| 28 | + |
| 29 | +## ์์ฑํ ์ฝ๋ |
| 30 | + |
| 31 | +```mojo |
| 32 | +{{#include ../../../../../problems/p05/p05_tile_tensor.mojo:broadcast_add_tile_tensor}} |
| 33 | +``` |
| 34 | + |
| 35 | +<a href="{{#include ../_includes/repo_url.md}}/blob/main/problems/p05/p05_tile_tensor.mojo" class="filename">์ ์ฒด ์ฝ๋ ๋ณด๊ธฐ: problems/p05/p05_tile_tensor.mojo</a> |
| 36 | + |
| 37 | +<details> |
| 38 | +<summary><strong>ํ</strong></summary> |
| 39 | + |
| 40 | +<div class="solution-tips"> |
| 41 | + |
| 42 | +1. 2D ์ธ๋ฑ์ค ๊ฐ์ ธ์ค๊ธฐ: `row = thread_idx.y`, `col = thread_idx.x` |
| 43 | +2. ๊ฐ๋ ์ถ๊ฐ: `if row < size and col < size` |
| 44 | +3. ๊ฐ๋ ๋ด๋ถ: TileTensor๋ก `a`์ `b` ๊ฐ์ ์ด๋ป๊ฒ ๋ธ๋ก๋์บ์คํธํ ์ง ์๊ฐํด ๋ณด์ธ์ |
| 45 | + |
| 46 | +</div> |
| 47 | +</details> |
| 48 | + |
| 49 | +## ์ฝ๋ ์คํ |
| 50 | + |
| 51 | +์๋ฃจ์
์ ํ
์คํธํ๋ ค๋ฉด ํฐ๋ฏธ๋์์ ๋ค์ ๋ช
๋ น์ด๋ฅผ ์คํํ์ธ์: |
| 52 | + |
| 53 | +<div class="code-tabs" data-tab-group="package-manager"> |
| 54 | + <div class="tab-buttons"> |
| 55 | + <button class="tab-button">pixi NVIDIA (default)</button> |
| 56 | + <button class="tab-button">pixi AMD</button> |
| 57 | + <button class="tab-button">pixi Apple</button> |
| 58 | + <button class="tab-button">uv</button> |
| 59 | + </div> |
| 60 | + <div class="tab-content"> |
| 61 | + |
| 62 | +```bash |
| 63 | +pixi run p05_tile_tensor |
| 64 | +``` |
| 65 | + |
| 66 | + </div> |
| 67 | + <div class="tab-content"> |
| 68 | + |
| 69 | +```bash |
| 70 | +pixi run -e amd p05_tile_tensor |
| 71 | +``` |
| 72 | + |
| 73 | + </div> |
| 74 | + <div class="tab-content"> |
| 75 | + |
| 76 | +```bash |
| 77 | +pixi run -e apple p05_tile_tensor |
| 78 | +``` |
| 79 | + |
| 80 | + </div> |
| 81 | + <div class="tab-content"> |
| 82 | + |
| 83 | +```bash |
| 84 | +uv run poe p05_tile_tensor |
| 85 | +``` |
| 86 | + |
| 87 | + </div> |
| 88 | +</div> |
| 89 | + |
| 90 | +ํผ์ฆ์ ์์ง ํ์ง ์์๋ค๋ฉด ์ถ๋ ฅ์ด ๋ค์๊ณผ ๊ฐ์ด ๋ํ๋ฉ๋๋ค: |
| 91 | + |
| 92 | +```txt |
| 93 | +out: HostBuffer([0.0, 0.0, 0.0, 0.0]) |
| 94 | +expected: HostBuffer([1.0, 2.0, 11.0, 12.0]) |
| 95 | +``` |
| 96 | + |
| 97 | +## ์๋ฃจ์
|
| 98 | + |
| 99 | +<details class="solution-details"> |
| 100 | +<summary></summary> |
| 101 | + |
| 102 | +```mojo |
| 103 | +{{#include ../../../../../solutions/p05/p05_tile_tensor.mojo:broadcast_add_tile_tensor_solution}} |
| 104 | +``` |
| 105 | + |
| 106 | +<div class="solution-explanation"> |
| 107 | + |
| 108 | +TileTensor ๋ธ๋ก๋์บ์คํธ์ GPU ์ค๋ ๋ ๋งคํ์ ํต์ฌ ๊ฐ๋
์ ๋ณด์ฌ์ฃผ๋ ์๋ฃจ์
์
๋๋ค: |
22 | 109 |
|
23 | | -## ๊ตฌํ ๋ฐฉ์ |
| 110 | +1. **์ค๋ ๋์์ ํ๋ ฌ๋ก ๋งคํ** |
24 | 111 |
|
25 | | -### [๐ฐ ์์ ๋ฉ๋ชจ๋ฆฌ ๋ฐฉ์](./raw.md) |
| 112 | + - `thread_idx.y`๋ก ํ, `thread_idx.x`๋ก ์ด์ ์ ๊ทผ |
| 113 | + - ์์ฐ์ค๋ฌ์ด 2D ์ธ๋ฑ์ฑ์ด ์ถ๋ ฅ ํ๋ ฌ ๊ตฌ์กฐ์ ์ผ์น |
| 114 | + - ์ด๊ณผ ์ค๋ ๋(3ร3 ๊ทธ๋ฆฌ๋)๋ ๊ฒฝ๊ณ ๊ฒ์ฌ๋ก ์ฒ๋ฆฌ |
26 | 115 |
|
27 | | -์๋ ๋ฉ๋ชจ๋ฆฌ ์ธ๋ฑ์ฑ์ผ๋ก ๋ธ๋ก๋์บ์คํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ ์์๋ด
๋๋ค. |
| 116 | +2. **๋ธ๋ก๋์บ์คํธ ์๋ ๋ฐฉ์** |
| 117 | + - ์
๋ ฅ `a`์ ํฌ๊ธฐ๋ `(1,n)`: `a[0,col]`์ด ํ์ ๊ฐ๋ก์ง๋ฌ ๋ธ๋ก๋์บ์คํธ |
| 118 | + - ์
๋ ฅ `b`์ ํฌ๊ธฐ๋ `(n,1)`: `b[row,0]`์ด ์ด์ ๊ฐ๋ก์ง๋ฌ ๋ธ๋ก๋์บ์คํธ |
| 119 | + - ์ถ๋ ฅ์ ํฌ๊ธฐ๋ `(n,n)`: ๊ฐ ์์๋ ํด๋น ๋ธ๋ก๋์บ์คํธ ๊ฐ๋ค์ ํฉ |
28 | 120 |
|
29 | | -### [๐ TileTensor ๋ฒ์ ](./tile_tensor.md) |
| 121 | + ```txt |
| 122 | + [ a0 a1 ] + [ b0 ] = [ a0+b0 a1+b0 ] |
| 123 | + [ b1 ] [ a0+b1 a1+b1 ] |
| 124 | + ``` |
30 | 125 |
|
31 | | -์๋ก ๋ค๋ฅธ ์ฐจ์ ๊ฐ ์ฐ์ฐ์ TileTensor๋ก ์ฒ๋ฆฌํฉ๋๋ค. |
| 126 | +3. **๊ฒฝ๊ณ ๊ฒ์ฌ** |
| 127 | + - ๊ฐ๋ ์กฐ๊ฑด `row < size and col < size`๋ก ๋ฒ์ ์ด๊ณผ ์ ๊ทผ ๋ฐฉ์ง |
| 128 | + - ํ๋ ฌ ๋ฒ์์ ์ด๊ณผ ์ค๋ ๋๋ฅผ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌ |
| 129 | + - ๋ธ๋ก๋์บ์คํธ ๋๋ถ์ `a`์ `b`์ ๋ํ ๋ณ๋ ๊ฒ์ฌ ๋ถํ์ |
32 | 130 |
|
33 | | -๐ก **์ฐธ๊ณ **: ์๋ ์ธ๋ฑ์ฑ๊ณผ ๋น๊ตํ์ ๋ TileTensor๊ฐ ๋ธ๋ก๋์บ์คํธ๋ฅผ ์ผ๋ง๋ ๊ฐ๋จํ๊ฒ ๋ง๋ค์ด์ฃผ๋์ง ํ์ธํด ๋ณด์ธ์. |
| 131 | +์ด ํจํด์ ์ดํ ํผ์ฆ์์ ๋ค๋ฃฐ ๋ ๋ณต์กํ ํ
์ ์ฐ์ฐ์ ๊ธฐ์ด๊ฐ ๋ฉ๋๋ค. |
| 132 | +</div> |
| 133 | +</details> |
0 commit comments