콘텐츠로 이동

정적 멀티코어 스케줄링

top executor는 static.for_each로 컴파일 시점에 코어마다 4행짜리 load를 하나씩 생성합니다. 32개 코어는 각각 자기 tile의 값을 두 배로 만든 뒤, 런타임 코어 ID로 정해지는 서로 겹치지 않는 출력 slice에 저장합니다. 128개 출력 행 전부를 CPU 기준값과 비교합니다.

BF16 행 하나는 원소 256개(512바이트)이므로, 4행짜리 load는 모두 shared DRAM line 전체 단위로 맞아떨어집니다. 이 예제는 예전의 FP32 10 x 10 matmul 예시를 대체합니다. 예전 예시는 물리 폭과 MPU dtype 때문에 EVT0 바이너리로 lowering할 수 없었습니다.

static_multi_core.py를 내려받거나, 체크아웃한 파일을 실행하십시오. 환경 설정은 예제 실행하기를 참고하십시오.

1
HA_BACKEND=sim HA_TOOLCHAIN=1 python "$DOCS_ROOT/docs/legato-language/tutorials/code/static_multi_core.py"
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Statically issue a separate four-row load for each of the 32 cores."""

import os
from pathlib import Path

import torch

import legato
import legato.model as model
import legato.model.bertha as bertha
from legato.language import arith

# Store compiler artifacts and optional simulator logs in a configurable directory.
OUTPUT = Path(os.environ.get("LEGATO_EXAMPLE_OUTPUT", "legato_example_output"))


@legato.compile(backend=lambda ctx: bertha.Bertha(ctx, "bertha"))
def double_rows(
    x: legato.get_model().tensor_type(legato.types.float("bfloat16"), [128, 256], "shared_dram"),
    out: legato.get_model().tensor_type(legato.types.float("bfloat16"), [128, 256], "shared_dram"),
):
    """Unroll loads statically; select each output slice using the runtime core ID."""
    device = legato.get_context().get_device()
    with device.get_top():

        def load_rows(i):
            rows = legato.range(i * 4, i * 4 + 4)
            block = legato.tensor.view(x, (rows, legato.range(0, 256)))
            legato.tensor.request_load(block, device.get_core(i), "sram")

        legato.static.for_each(legato.range(0, device.get_num_cores()), load_rows)

    with device.get_all_cores():
        tile = legato.tensor.receive_type(
            legato.types.tensor(legato.types.float("bfloat16"), [4, 256]), 0, None, "sram"
        )
        core_id = arith.cast(model.Executor.get_id(), legato.types.integer("int64"))
        rows = legato.range(core_id * 4, core_id * 4 + 4)
        block = legato.tensor.view(out, (rows, legato.range(0, 256)))
        legato.tensor.memcpy(block, tile + tile)


def main():
    """Check all 32 output slices, rather than checking only core zero."""
    # Register the LPU PyTorch backend before allocating device tensors.
    import hart
    import torch_ha

    torch_ha._autoload()
    # HA_BACKEND=sim selects EVT0 Torch FSim; otherwise use the hardware runtime.
    if os.environ.get("HA_BACKEND") == "sim":
        OUTPUT.mkdir(parents=True, exist_ok=True)
        hart.set_fsim_env(
            hart.HaDeviceVersion.BERTHA_CLOUD_EVT0,
            hart.HaFSimComputeMode.TORCH,
            str(OUTPUT / "fsim.log"),
            "error",
        )
    hart.set_device(0)
    # Keep CPU inputs reproducible without initializing a device RNG.
    torch.random.default_generator.manual_seed(0)
    cpu = torch.randn(128, 256, dtype=torch.bfloat16)
    # Allocate the physical layout expected by the kernel and copy contiguous host data.
    x = torch.ops.lpu.empty_with_kind(list(cpu.shape), "dram_word_interleaved", dtype=cpu.dtype, device="lpu:0")
    x.copy_(cpu.contiguous())
    out_cpu = torch.zeros_like(cpu)
    # Allocate the physical layout expected by the kernel and copy contiguous host data.
    out = torch.ops.lpu.empty_with_kind(
        list(out_cpu.shape), "dram_word_interleaved", dtype=out_cpu.dtype, device="lpu:0"
    )
    out.copy_(out_cpu.contiguous())
    # Compile a binary and launch it on the selected device.
    with legato.session(output_type=legato.OutputType.BINARY, output_path=str(OUTPUT), launch=True):
        double_rows(x, out)
    # Surface asynchronous execution errors before reading the result.
    hart.stream_synchronize()
    # Read back the synchronized result and compare every element with the CPU reference.
    actual = out.cpu().float()
    assert torch.isfinite(actual).all(), "Non-finite device result"
    torch.testing.assert_close(actual, (cpu.float() * 2).float(), atol=0, rtol=0)


if __name__ == "__main__":
    main()