콘텐츠로 이동

컨텍스트 관리

Legato 연산은 executor 컨텍스트 아래에서 생성됩니다.
컨텍스트는 뒤따르는 연산이 어디에서 실행되는지를 기술합니다.
top executor, 코어 하나, 또는 모든 코어입니다.

현재 컨텍스트 얻기

컴파일된 함수 안에서 활성 컴파일러 컨텍스트와 그 디바이스에 접근하려면 legato.get_context()를 사용하십시오.

1
device = legato.get_context().get_device()

대부분의 사용자 코드는 곧바로 Python with 문으로 executor를 선택합니다.

1
2
3
4
5
6
7
8
device = legato.get_context().get_device()

with device.get_top():
    legato.tensor.request_load(input_data, device.get_core(0), "sram")

with device.get_core(0):
    loaded = legato.tensor.receive(input_data, 0, "sram")
    result = loaded + loaded

Executor 범위

  • device.get_top()은 orchestration과 top 쪽 load 요청에 사용합니다.
  • device.get_core(index)는 코어 하나에서 영역을 실행합니다.
  • device.get_all_cores()는 모든 코어에서 영역을 실행합니다.

with ... as name이 넘겨주는 객체는 그 컨텍스트가 선택한 executor입니다.

1
2
with device.get_core(0) as core0:
    copied = legato.tensor.copy(input_tensor, core0)

영역 사이에 값 yield하기

Legato의 범위(Scope) 규칙은 Python보다 엄격합니다.
한 영역 안에서 만든 값을 뒤의 영역에서 사용하려면 그 영역에서 yield로 넘기십시오.

1
2
3
4
5
6
7
with device.get_core(0):
    loaded = legato.tensor.receive(input_data, 0, "sram")
    q = loaded @ weight
    yield q

with device.get_core(0):
    legato.tensor.memcpy(output, q)

조건부 영역에서 바깥 범위가 그 값을 사용한다면, 두 분기 모두 호환되는 값을 yield해야 합니다.

세션과 컨텍스트

@legato.compile 함수를 호출하려면 활성 legato.session이 있어야 합니다.

1
2
with legato.session(output_type=legato.OutputType.MLIR, output_path="test-output"):
    compiled_kernel(*args)

세션은 컴파일러 출력과 로깅을 제어합니다. executor 컨텍스트는 컴파일된 커널 안에서 생성되는 IR 영역을 제어합니다.