> EulerNPU > 튜토리얼 > 04. npu_sim — cycle / memory 시뮬레이션

04. npu_sim — cycle / memory 시뮬레이션

cpu_ref가 "결과가 맞나"를 답한다면, npu_sim은 "얼마나 걸리고, 얼마나 쓰나"를 답합니다. 보드에 올리기 전에 cycle / memory / bandwidth 를 예측해 두는 단계입니다.

silicon 측정 없이 PL offload를 결정하지 않는다는 정책 (Measured design)을 지키기 위한 핵심 도구입니다.

무엇이 들어 있나

cycle model은 P06 silicon 측정값과 잠겨 있습니다: test_matmul_systolic_matches_p06_silicon 가 ±10% 이상 drift시 CI 실패.

실행

eulernpu compile --spec ... --backend npu_sim --out /tmp/sim
eulernpu run     --artifact /tmp/sim --input ... --out /tmp/sim.out.npz \
                 --profile /tmp/sim.profile.json

--profile 로 timeline JSON이 떨어집니다.

profile 읽기

eulernpu profile show /tmp/sim.profile.json
Total cycles      : 2,164,512  (≈ 3.25 ms @ 666 MHz)
Bottleneck op     : conv2d_3 (980,224 cyc, 45.3%)
Bandwidth peak    : 412 MB/s @ depthwise_conv2d_5
Memory breakdown  :
  weights         : 132 KB / 140 KB BRAM (94%)
  activations     :  18 KB
  KV cache        :   0 KB
  scratch         :   4 KB

board profile 비교

eulernpu profile --all-boards /tmp/sim.profile.json

같은 그래프가 XC7Z020 / XC7Z045 / XCZU7EV / XCZU9EG 에서 어떻게 변하는지 한 표로 봅니다. 어떤 보드부터 fit 되는지, 어디서 BRAM/DSP가 깨지는지 명확해집니다.

sweep — 보드 × systolic_size Pareto

eulernpu sweep \
    --spec    examples/industrial_kws/spec.yaml \
    --boards  xc7z020,xc7z045,xczu7ev,xczu9eg \
    --systolic 8,16,32,64 \
    --out     /tmp/kws_sweep.json

각 (board, systolic) 조합의 latency / area / power 가 출력되고, Pareto frontier가 표시됩니다. 어떤 trade-off가 의미 있는지가 한눈에 보입니다.

trace — per-op latency 추적

eulernpu trace /tmp/sim.profile.json --top 10
op                  cycles    %     bandwidth     mem
─────────────────────────────────────────────────────
conv2d_3           980,224   45.3   312 MB/s     BRAM
depthwise_conv2d_5 412,800   19.1   189 MB/s     BRAM
linear_7           198,432    9.2    24 MB/s     BRAM
gru_cell_0         156,224    7.2    12 MB/s     BRAM
...

bottleneck이 어디인지, 어떤 op가 bandwidth-bound 인지 (=memory hierarchy) versus compute-bound 인지 (=DSP) 구분이 가능해집니다.

roofline — bandwidth-bound vs compute-bound

eulernpu profile --roofline /tmp/sim.profile.json --board xc7z020

각 op이 roofline 차트의 어디에 떨어지는지 (peak compute 대비 측정 throughput). ridge point 좌측 = bandwidth-bound, 우측 = compute-bound. 어떤 최적화가 의미 있는지 결정하는 기준입니다.

measured 측정과의 비교

P06 silicon 측정으로 cycle model이 calibration 된 상태:

kernel모델 예측silicon 측정오차
matmul_systolic (M=32 K=48 N=8)456 cyc460 cyc+0.9%
kws_npu_top (full graph)230 K cyc2.11 M cyc (DMA 포함 wall)bandwidth-bound 영역

tests/hls/test_cycle_model_hls.py::test_matmul_systolic_matches_p06_silicon 가 lock-test 입니다. cycle model 식이 ±10% 이상 drift 시 CI 실패.

다음 단계

INT8 양자화로 weight/activation 비용을 줄이려면 → 05. quantize.