#!/usr/bin/env bash
set -uo pipefail

IMAGE="lomelidev/llama.cpp:cu130-nccl-sse-faall-10974"
MODEL_DIR="/home/lab/models/ornith15-9b"

CTX=524288

RUN="$HOME/bench/ornith/phase2-512k-$(date -u +%Y%m%dT%H%M%SZ)"

mkdir -p "$RUN"/{responses,requests,logs,gpu}

echo "$RUN" > "$HOME/bench/ornith/LAST_MATRIX"

SUMMARY="$RUN/summary.tsv"
DETAIL="$RUN/detail.tsv"

printf "config\tctx_requested\tctx_slot\tK\tV\tDFlash\tdraftK\tdraftV\tVRAM_MiB\tFREE_MiB\tavg_prompt_tps\tavg_gen_tps\tavg_draft_acc\tFA_warn\tcapping\n" > "$SUMMARY"

printf "config\tprompt\tprompt_tps\tgen_tps\tdraft_n\tdraft_accepted\tdraft_acc\tcompletion_tokens\twall_ms\n" > "$DETAIL"


make_requests() {

cat > "$RUN/requests/01-go.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "Implement a production-ready concurrent LRU cache in Go with TTL expiration, bounded memory, context cancellation and graceful shutdown. Explain the important design decisions."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 111111,
  "max_tokens": 1024,
  "stream": false
}
JSON

cat > "$RUN/requests/02-debug.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "You are debugging a Go HTTP service. Requests occasionally hang during shutdown because workers remain blocked on channels. Show a robust implementation that fixes the lifecycle, cancellation and channel ownership issues, then explain the failure modes."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 222222,
  "max_tokens": 1024,
  "stream": false
}
JSON

cat > "$RUN/requests/03-refactor.json" <<'JSON'
{
  "model": "ornith-bench",
  "messages": [
    {
      "role": "user",
      "content": "Refactor a message-processing pipeline in Go so that deduplication, retries with exponential backoff, bounded concurrency and graceful shutdown are cleanly separated. Provide production-quality code and explain the architecture."
    }
  ],
  "temperature": 0.6,
  "top_p": 0.95,
  "top_k": 20,
  "seed": 333333,
  "max_tokens": 1024,
  "stream": false
}
JSON

}


run_test() {

    NAME="$1"
    CTK="$2"
    CTV="$3"
    DFLASH="$4"
    DRAFT_K="$5"
    DRAFT_V="$6"

    echo
    echo "============================================================"
    echo " TEST: $NAME"
    echo " ctx=$CTX"
    echo " main KV=$CTK/$CTV"
    echo " DFlash=$DFLASH"
    echo " draft KV=$DRAFT_K/$DRAFT_V"
    echo " YaRN=ON"
    echo "============================================================"

    docker rm -f ornith >/dev/null 2>&1 || true

    ARGS=(
      docker run -d
      --name ornith
      --gpus "device=0"
      --ipc=host
      -p 8120:8080
      -v "$MODEL_DIR:/models:ro"
      -e GGML_CUDA_GRAPH_OPT=1

      "$IMAGE"

      -m /models/Ornith-1.5-9B-Q4_K_L.gguf
      -a ornith-bench
      -ngl 999

      -fa on
      -ctk "$CTK"
      -ctv "$CTV"

      -c "$CTX"
      -np 1
      --no-kv-unified

      --rope-scaling yarn
      --rope-scale 4
      --yarn-orig-ctx 262144

      --no-mmproj

      -b 4096
      -ub 2048

      --cache-ram 16384
      --ctx-checkpoints 4

      --jinja
      --metrics

      --host 0.0.0.0
      --port 8080

      --temp 0.6
      --top-p 0.95
      --top-k 20
      --min-p 0
    )

    if [[ "$DFLASH" == "on" ]]; then

        ARGS+=(
          -md /models/Ornith-1.5-9B-DFlash-Q4_K_M.gguf
          --spec-draft-ngl 999
          --spec-type draft-dflash
          --spec-draft-n-max 2

          --spec-draft-type-k "$DRAFT_K"
          --spec-draft-type-v "$DRAFT_V"
        )

    fi


    "${ARGS[@]}" > "$RUN/logs/$NAME.container-id.txt"


    READY=0

    for i in $(seq 1 180); do

        if curl -fsS http://127.0.0.1:8120/health >/dev/null 2>&1; then
            READY=1
            break
        fi

        if ! docker ps --format '{{.Names}}' | grep -qx ornith; then
            break
        fi

        sleep 1
    done


    docker logs ornith > "$RUN/logs/$NAME.start.log" 2>&1 || true


    if [[ "$READY" != "1" ]]; then

        echo
        echo "ERROR: $NAME no logró cargar"

        docker logs ornith 2>&1 | tail -120 || true

        printf "%s\t%s\tFAIL\t%s\t%s\t%s\t%s\t%s\tFAIL\tFAIL\t0\t0\t0\t0\t0\n" \
          "$NAME" "$CTX" "$CTK" "$CTV" "$DFLASH" "$DRAFT_K" "$DRAFT_V" \
          >> "$SUMMARY"

        return
    fi


    # ----------------------------------------------------------
    # Verificar contexto REAL del slot
    # ----------------------------------------------------------

    SLOT_CTX=$(
      grep -oE 'n_ctx_slot = [0-9]+' "$RUN/logs/$NAME.start.log" \
        | tail -1 \
        | awk '{print $3}'
    )

    SLOT_CTX="${SLOT_CTX:-0}"

    CAPPING=$(
      grep -ci "capping" "$RUN/logs/$NAME.start.log" || true
    )

    if [[ "$SLOT_CTX" != "$CTX" || "$CAPPING" != "0" ]]; then

        echo
        echo "ERROR: contexto incorrecto"
        echo "requested=$CTX slot=$SLOT_CTX capping=$CAPPING"

        printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\tFAIL\tFAIL\t0\t0\t0\t0\t%s\n" \
          "$NAME" "$CTX" "$SLOT_CTX" "$CTK" "$CTV" "$DFLASH" "$DRAFT_K" "$DRAFT_V" "$CAPPING" \
          >> "$SUMMARY"

        return
    fi


    # ----------------------------------------------------------
    # VRAM idle
    # ----------------------------------------------------------

    GPU_MEM=$(nvidia-smi \
      --query-gpu=memory.used,memory.free \
      --format=csv,noheader,nounits \
      -i 0)

    VRAM=$(echo "$GPU_MEM" | awk -F',' '{gsub(/ /,"",$1); print $1}')
    FREE=$(echo "$GPU_MEM" | awk -F',' '{gsub(/ /,"",$2); print $2}')

    nvidia-smi \
      --query-gpu=index,memory.used,memory.free,utilization.gpu,power.draw,temperature.gpu \
      --format=csv \
      > "$RUN/gpu/$NAME.before.txt"


    SUM_PROMPT=0
    SUM_GEN=0
    SUM_ACC=0

    REQUEST_COUNT=0
    ACC_COUNT=0


    for REQ in \
      01-go \
      02-debug \
      03-refactor
    do

        echo
        echo "  -> $NAME / $REQ"

        START_NS=$(date +%s%N)

        curl -s \
          http://127.0.0.1:8120/v1/chat/completions \
          -H 'Content-Type: application/json' \
          --data-binary @"$RUN/requests/$REQ.json" \
          > "$RUN/responses/$NAME-$REQ.json"

        END_NS=$(date +%s%N)

        WALL_MS=$(( (END_NS - START_NS) / 1000000 ))


        PROMPT_TPS=$(
          jq -r '.timings.prompt_per_second // 0' \
          "$RUN/responses/$NAME-$REQ.json"
        )

        GEN_TPS=$(
          jq -r '.timings.predicted_per_second // 0' \
          "$RUN/responses/$NAME-$REQ.json"
        )

        DRAFT_N=$(
          jq -r '.timings.draft_n // 0' \
          "$RUN/responses/$NAME-$REQ.json"
        )

        DRAFT_ACCEPTED=$(
          jq -r '.timings.draft_n_accepted // 0' \
          "$RUN/responses/$NAME-$REQ.json"
        )

        DRAFT_ACC=$(
          jq -r '
            if ((.timings.draft_n // 0) > 0)
            then (.timings.draft_n_accepted / .timings.draft_n)
            else 0
            end
          ' "$RUN/responses/$NAME-$REQ.json"
        )

        COMPLETION=$(
          jq -r '.usage.completion_tokens // 0' \
          "$RUN/responses/$NAME-$REQ.json"
        )


        printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" \
          "$NAME" \
          "$REQ" \
          "$PROMPT_TPS" \
          "$GEN_TPS" \
          "$DRAFT_N" \
          "$DRAFT_ACCEPTED" \
          "$DRAFT_ACC" \
          "$COMPLETION" \
          "$WALL_MS" \
          >> "$DETAIL"


        SUM_PROMPT=$(awk -v a="$SUM_PROMPT" -v b="$PROMPT_TPS" 'BEGIN {print a+b}')
        SUM_GEN=$(awk -v a="$SUM_GEN" -v b="$GEN_TPS" 'BEGIN {print a+b}')

        REQUEST_COUNT=$((REQUEST_COUNT + 1))

        if [[ "$DRAFT_N" != "0" ]]; then

            SUM_ACC=$(awk -v a="$SUM_ACC" -v b="$DRAFT_ACC" 'BEGIN {print a+b}')

            ACC_COUNT=$((ACC_COUNT + 1))

        fi

    done


    AVG_PROMPT=$(awk -v s="$SUM_PROMPT" -v n="$REQUEST_COUNT" 'BEGIN {printf "%.3f", s/n}')
    AVG_GEN=$(awk -v s="$SUM_GEN" -v n="$REQUEST_COUNT" 'BEGIN {printf "%.3f", s/n}')

    if [[ "$ACC_COUNT" -gt 0 ]]; then
        AVG_ACC=$(awk -v s="$SUM_ACC" -v n="$ACC_COUNT" 'BEGIN {printf "%.4f", s/n}')
    else
        AVG_ACC="0"
    fi


    docker logs ornith > "$RUN/logs/$NAME.final.log" 2>&1 || true


    FA_WARN=$(
      grep -c "no FlashAttention vector kernel" \
        "$RUN/logs/$NAME.final.log" || true
    )

    CAPPING=$(
      grep -ci "capping" \
        "$RUN/logs/$NAME.final.log" || true
    )


    nvidia-smi \
      --query-gpu=index,memory.used,memory.free,utilization.gpu,power.draw,temperature.gpu \
      --format=csv \
      > "$RUN/gpu/$NAME.after.txt"


    printf "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" \
      "$NAME" \
      "$CTX" \
      "$SLOT_CTX" \
      "$CTK" \
      "$CTV" \
      "$DFLASH" \
      "$DRAFT_K" \
      "$DRAFT_V" \
      "$VRAM" \
      "$FREE" \
      "$AVG_PROMPT" \
      "$AVG_GEN" \
      "$AVG_ACC" \
      "$FA_WARN" \
      "$CAPPING" \
      >> "$SUMMARY"


    echo
    echo "--- RESULTADO $NAME ---"
    echo "CTX solicitado: $CTX"
    echo "CTX slot real:  $SLOT_CTX"
    echo "Avg prompt:     $AVG_PROMPT t/s"
    echo "Avg gen:        $AVG_GEN t/s"
    echo "Avg draft:      $AVG_ACC"
    echo "VRAM:           $VRAM MiB"
    echo "VRAM free:      $FREE MiB"
    echo "FA warnings:    $FA_WARN"
    echo "Capping:        $CAPPING"

    echo
    column -t -s $'\t' "$SUMMARY"

}


make_requests


# ==========================================================
# FASE 2 — 512K REAL
# ==========================================================

# Perfil principal / equilibrio contexto-VRAM
run_test K-512-q4q4-dflash \
  q4_0 q4_0 on q4_0 q4_0

# Perfil más pesado: ver si Q8/Q8 vale la VRAM extra
run_test L-512-q8q8-dflash \
  q8_0 q8_0 on q4_0 q4_0

# Baseline sin DFlash
run_test M-512-q4q4-base \
  q4_0 q4_0 off none none


echo
echo "============================================================"
echo " FASE 2 — 512K TERMINADA"
echo "============================================================"

echo
echo "PROMEDIOS:"
column -t -s $'\t' "$SUMMARY"

echo
echo "DETALLE:"
column -t -s $'\t' "$DETAIL"

echo
echo "Resultados:"
echo "$RUN"
