LayoutTensor ๋ฒ์
๊ฐ์
2D LayoutTensor a์ ๊ฐ ์์น์ 10์ ๋ํด 2D LayoutTensor output์ ์ ์ฅํ๋ ์ปค๋์ ๊ตฌํํด ๋ณด์ธ์.
์ฐธ๊ณ : ๋ธ๋ก๋น ์ค๋ ๋ ์๊ฐ a์ ํ๊ณผ ์ด ํฌ๊ธฐ๋ณด๋ค ๋ชจ๋ ์์ต๋๋ค.
ํต์ฌ ๊ฐ๋
์ด ํผ์ฆ์์ ๋ฐฐ์ธ ๋ด์ฉ:
- ์ฌ๋ฌ ๋ธ๋ก๊ณผ ํจ๊ป
LayoutTensor์ฌ์ฉํ๊ธฐ - 2D ๋ธ๋ก ๊ตฌ์ฑ์ผ๋ก ํฐ ํ๋ ฌ ์ฒ๋ฆฌํ๊ธฐ
- ๋ธ๋ก ์ธ๋ฑ์ฑ๊ณผ
LayoutTensor์ ๊ทผ ๊ฒฐํฉํ๊ธฐ
ํต์ฌ์ LayoutTensor๊ฐ 2D ์ธ๋ฑ์ฑ์ ๋จ์ํํด ์ฃผ์ง๋ง, ํฐ ํ๋ ฌ์์๋ ์ฌ์ ํ ๋ธ๋ก ๊ฐ ์กฐ์จ์ด ํ์ํ๋ค๋ ์ ์
๋๋ค.
๊ตฌ์ฑ
- ํ๋ ฌ ํฌ๊ธฐ: \(5 \times 5\) ์์
- ๋ ์ด์์ ์ฒ๋ฆฌ:
LayoutTensor๊ฐ ํ ์ฐ์ ๊ตฌ์ฑ ๊ด๋ฆฌ - ๋ธ๋ก ์กฐ์จ: ์ฌ๋ฌ ๋ธ๋ก์ผ๋ก ์ ์ฒด ํ๋ ฌ ์ปค๋ฒ
- 2D ์ธ๋ฑ์ฑ: ๊ฒฝ๊ณ ๊ฒ์ฌ์ ํจ๊ป ์์ฐ์ค๋ฌ์ด \((i,j)\) ์ ๊ทผ
- ์ด ์ค๋ ๋ ์: \(25\)๊ฐ ์์์ ๋ํด \(36\)๊ฐ
- ์ค๋ ๋ ๋งคํ: ๊ฐ ์ค๋ ๋๊ฐ ํ๋ ฌ ์์ ํ๋์ฉ ์ฒ๋ฆฌ
์์ฑํ ์ฝ๋
comptime SIZE = 5
comptime BLOCKS_PER_GRID = (2, 2)
comptime THREADS_PER_BLOCK = (3, 3)
comptime dtype = DType.float32
comptime out_layout = Layout.row_major(SIZE, SIZE)
comptime a_layout = Layout.row_major(SIZE, SIZE)
fn add_10_blocks_2d[
out_layout: Layout,
a_layout: Layout,
](
output: LayoutTensor[dtype, out_layout, MutAnyOrigin],
a: LayoutTensor[dtype, a_layout, ImmutAnyOrigin],
size: UInt,
):
row = block_dim.y * block_idx.y + thread_idx.y
col = block_dim.x * block_idx.x + thread_idx.x
# FILL ME IN (roughly 2 lines)
์ ์ฒด ์ฝ๋ ๋ณด๊ธฐ: problems/p07/p07_layout_tensor.mojo
ํ
- ์ ์ญ ์ธ๋ฑ์ค ๊ณ์ฐ:
row = block_dim.y * block_idx.y + thread_idx.y,col = block_dim.x * block_idx.x + thread_idx.x - ๊ฐ๋ ์ถ๊ฐ:
if row < size and col < size - ๊ฐ๋ ๋ด๋ถ: 2D LayoutTensor์ 10์ ๋ํ๋ ๋ฐฉ๋ฒ์ ์๊ฐํด ๋ณด์ธ์
์ฝ๋ ์คํ
์๋ฃจ์ ์ ํ ์คํธํ๋ ค๋ฉด ํฐ๋ฏธ๋์์ ๋ค์ ๋ช ๋ น์ด๋ฅผ ์คํํ์ธ์:
pixi run p07_layout_tensor
pixi run -e amd p07_layout_tensor
pixi run -e apple p07_layout_tensor
uv run poe p07_layout_tensor
ํผ์ฆ์ ์์ง ํ์ง ์์๋ค๋ฉด ์ถ๋ ฅ์ด ๋ค์๊ณผ ๊ฐ์ด ๋ํ๋ฉ๋๋ค:
out: HostBuffer([0.0, 0.0, 0.0, ... , 0.0])
expected: HostBuffer([10.0, 11.0, 12.0, ... , 34.0])
์๋ฃจ์
fn add_10_blocks_2d[
out_layout: Layout,
a_layout: Layout,
](
output: LayoutTensor[dtype, out_layout, MutAnyOrigin],
a: LayoutTensor[dtype, a_layout, ImmutAnyOrigin],
size: UInt,
):
row = block_dim.y * block_idx.y + thread_idx.y
col = block_dim.x * block_idx.x + thread_idx.x
if row < size and col < size:
output[row, col] = a[row, col] + 10.0
LayoutTensor๊ฐ 2D ๋ธ๋ก ๊ธฐ๋ฐ ์ฒ๋ฆฌ๋ฅผ ์ผ๋ง๋ ๊ฐ์ํํ๋์ง ๋ณด์ฌ์ฃผ๋ ์๋ฃจ์ ์ ๋๋ค:
-
2D ์ค๋ ๋ ์ธ๋ฑ์ฑ
-
์ ์ญ ํ(row):
block_dim.y * block_idx.y + thread_idx.y -
์ ์ญ ์ด(col):
block_dim.x * block_idx.x + thread_idx.x -
์ค๋ ๋ ๊ทธ๋ฆฌ๋๋ฅผ ํ ์ ์์์ ๋งคํ:
3ร3 ๋ธ๋ก์ผ๋ก ๊ตฌ์ฑ๋ 5ร5 ํ ์: Block (0,0) Block (1,0) [(0,0) (0,1) (0,2)] [(0,3) (0,4) * ] [(1,0) (1,1) (1,2)] [(1,3) (1,4) * ] [(2,0) (2,1) (2,2)] [(2,3) (2,4) * ] Block (0,1) Block (1,1) [(3,0) (3,1) (3,2)] [(3,3) (3,4) * ] [(4,0) (4,1) (4,2)] [(4,3) (4,4) * ] [ * * * ] [ * * * ](* = ์ค๋ ๋๋ ์กด์ฌํ์ง๋ง ํ ์ ๊ฒฝ๊ณ ๋ฐ)
-
-
LayoutTensor์ ์ฅ์
-
์์ฐ์ค๋ฌ์ด 2D ์ธ๋ฑ์ฑ: ์๋ ์คํ์ ๊ณ์ฐ ๋์
tensor[row, col]์ฌ์ฉ -
์๋ ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์ ์ต์ ํ
-
์ ๊ทผ ํจํด ์์:
์์ ๋ฉ๋ชจ๋ฆฌ: LayoutTensor: row * size + col tensor[row, col] (2,1) -> 11 (2,1) -> ๊ฐ์ ์์
-
-
๊ฒฝ๊ณ ๊ฒ์ฌ
- ๊ฐ๋
row < size and col < size๊ฐ ์ฒ๋ฆฌํ๋ ์ํฉ:- ๋ถ๋ถ ๋ธ๋ก์์ ๋ฒ์๋ฅผ ๋ฒ์ด๋๋ ์ค๋ ๋
- ํ ์ ๊ฒฝ๊ณ์ ์ฃ์ง ์ผ์ด์ค
- ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์์ LayoutTensor๊ฐ ์๋์ผ๋ก ์ฒ๋ฆฌ
- 25๊ฐ ์์๋ฅผ 36๊ฐ ์ค๋ ๋๋ก ์ฒ๋ฆฌ (3ร3 ๋ธ๋ก์ 2ร2 ๊ทธ๋ฆฌ๋)
- ๊ฐ๋
-
๋ธ๋ก ์กฐ์จ
- ๊ฐ 3ร3 ๋ธ๋ก์ด 5ร5 ํ ์์ ์ผ๋ถ๋ถ์ ๋ด๋น
- LayoutTensor๊ฐ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ:
- ๋ฉ๋ชจ๋ฆฌ ๋ ์ด์์ ์ต์ ํ
- ํจ์จ์ ์ธ ์ ๊ทผ ํจํด
- ๋ธ๋ก ๊ฒฝ๊ณ ๊ฐ ์กฐ์จ
- ์บ์ ์นํ์ ๋ฐ์ดํฐ ์ ๊ทผ
์ด ํจํด์ LayoutTensor๊ฐ ์ต์ ์ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ ํจํด๊ณผ ์ค๋ ๋ ์กฐ์จ์ ์ ์งํ๋ฉด์๋ 2D ๋ธ๋ก ์ฒ๋ฆฌ๋ฅผ ์ผ๋ง๋ ๊ฐ์ํํ๋์ง ๋ณด์ฌ์ค๋๋ค.