콘텐츠로 이동

Broadcasting

shape broadcast와 rank 분해를 다루는 커널 예제입니다. 이론은 Broadcasting에서 설명합니다. Bertha lowering(VPU / MPU, SRAM과 mpu_buffer)은 Bertha에서의 Broadcasting에서 설명합니다.

각 데코레이터는 lambda ctx: bertha.Bertha(ctx, "bertha")로 Bertha 백엔드를 직접 만들고 현재 EVT0 기본값을 사용합니다. 백엔드 팩토리 인터페이스는 전형적인 백엔드 설정을 참고하십시오.

Add — 앞쪽 1

[1, 256] + [256, 256] → [256, 256]. a의 첫 행이 b의 모든 행에 맞춰 늘어납니다.
앞쪽 차원이 1인 add, Leaf에 대한 정보는 Broadcasting, VPU leaf를 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"a": "universal", "b": "universal", "out": "universal"},
)
def add_leading_one(
    a: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [1, 256], "shared_memory"
    ),
    b: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [256, 256], "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [256, 256], "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(a, device.get_core(0), "sram")
        legato.tensor.request_load(b, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(a, 0, "sram")
        y = legato.tensor.receive(b, 0, "sram")
        legato.tensor.memcpy(out, x + y)

Conv — batch가 있는 N*

[2, 8, 64]는 batch 없는 [8, 64] MPU conv 두 개로 분해됩니다. weight에는 batch prefix가 없습니다. Spatial N*와 Batch가 있는 conv_1d를 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"inp": "universal", "weight": "universal", "out": "universal"},
)
def conv1d_nstar(
    inp: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [2, 8, 64], "shared_memory"
    ),
    weight: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [3, 64, 64], "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [2, 8, 64], "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(inp, device.get_core(0), "sram")
        legato.tensor.request_load(weight, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(inp, 0, "sram")
        w = legato.tensor.receive(weight, 0, "sram")
        y = legato.math.conv_1d(x, w, None, (1,), (1,), (1, 1), "zeros")
        legato.tensor.memcpy(out, y)

Dwconv — batch가 있는 N*

입력 [2, 19, 256]는 같은 weight [4, 256]를 쓰는 [19, 256] depthwise conv 두 개로 분해됩니다. batch가 반복 실행되는 경우에는 weight를 SRAM에 두십시오. Batch가 있는 dwconv_1d를 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"inp": "universal", "weight": "universal", "out": "universal"},
)
def dwconv1d_nstar(
    inp: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [2, 19, 256], "shared_memory"
    ),
    weight: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [4, 256], "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [2, 16, 256], "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(inp, device.get_core(0), "sram")
        legato.tensor.request_load(weight, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(inp, 0, "sram")
        w = legato.tensor.receive(weight, 0, "sram")
        y = legato.math.dwconv_1d(x, w)
        legato.tensor.memcpy(out, y)

Pool — batch 없는 leaf

[8, 64] average pool은 이미 leaf입니다(k=2, s=2 → [4, 64]).
Leaf가 무엇인지는 Broadcasting를 참고하십시오.
Average pool이 무엇인지는 avg_pool_1d를 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"inp": "universal", "out": "universal"},
)
def avg_pool1d_leaf(
    inp: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [8, 64], "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [4, 64], "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(inp, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(inp, 0, "sram")
        y = legato.math.avg_pool_1d(x, (2,), (2,))
        legato.tensor.memcpy(out, y)

LayerNorm — 마지막 축 행

[4, 128]은 rank 1 [128] 노드의 가장 최소 단위인 leaf 네 개로 분해됩니다. affine 행(정규화 뒤 weight를 곱하고 bias를 더하는 것)은 현재 load 조건에 맞춰 (1, (128, 256))으로 두지만, 연산에는 길이 128을 씁니다.

Leaf가 무엇인지는 Broadcasting를 참고하십시오. 마지막 축의 행 단위 연산과 layer_norm을 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"inp": "universal", "weight": "universal", "bias": "universal", "out": "universal"},
)
def layernorm_rows(
    inp: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [4, 128], "shared_memory"
    ),
    weight: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), (1, (128, 256)), "shared_memory"
    ),
    bias: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), (1, (128, 256)), "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), [4, 128], "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(inp, device.get_core(0), "sram")
        legato.tensor.request_load(weight, device.get_core(0), "sram")
        legato.tensor.request_load(bias, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(inp, 0, "sram")
        w = legato.tensor.receive(weight, 0, "sram")
        b = legato.tensor.receive(bias, 0, "sram")
        y = legato.math.layer_norm(x, w, b)
        legato.tensor.memcpy(out, y)

RMSNorm — padding된 마지막 차원

(4, (70, 256))에서는 4,256칸짜리 물리적 텐서 공간에서 70개의 원소만 사용합니다. (D=70). 나머지 뒤는 padding 처리합니다. (RMSNorm 같은 평균을 구하는 식은 전체 공간이 아닌 원소가 실제 존재하는 개수로 나누어야 하기 때문입니다.) padding된 rms_norm을 참고하십시오.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@legato.compile(
    backend=lambda ctx: bertha.Bertha(ctx, "bertha"),
    param_kinds={"inp": "universal", "weight": "universal", "out": "universal"},
)
def rmsnorm_padded(
    inp: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), (4, (70, 256)), "shared_memory"
    ),
    weight: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), (1, (70, 256)), "shared_memory"
    ),
    out: legato.get_model().tensor_type(
        legato.types.float("bfloat16"), (4, (70, 256)), "shared_memory"
    ),
):
    device = legato.get_context().get_device()
    with device.get_top():
        legato.tensor.request_load(inp, device.get_core(0), "sram")
        legato.tensor.request_load(weight, device.get_core(0), "sram")
    with device.get_core(0):
        x = legato.tensor.receive(inp, 0, "sram")
        w = legato.tensor.receive(weight, 0, "sram")
        y = legato.math.rms_norm(x, w)
        legato.tensor.memcpy(out, y)