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

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

RUN="$HOME/bench/ornith/faall-matrix-$(date -u +%Y%m%dT%H%M%SZ)"
mkdir -p "$RUN"/{responses,logs,gpu}

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

SUMMARY="$RUN/summary.tsv"

printf "config\tctx\tK\tV\tDFlash\tFA\tYaRN\tVRAM_MiB\tFREE_MiB\tprompt_tps\tgen_tps\tdraft_acc\tFA_warn\tcapping\n" > "$SUMMARY"


run_test() {
    NAME="$1"
    CTX="$2"
    CTK="$3"
    CTV="$4"
    DFLASH="$5"
    FA="$6"
    YARN="$7"

    echo
    echo "============================================================"
    echo " TEST: $NAME"
    echo " ctx=$CTX K=$CTK V=$CTV DFlash=$DFLASH FA=$FA YaRN=$YARN"
    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 "$FA"
      -ctk "$CTK"
      -ctv "$CTV"

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

      --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
        )
    fi

    if [[ "$YARN" == "on" ]]; then
        ARGS+=(
          --rope-scaling yarn
          --rope-scale 4
          --yarn-orig-ctx 262144
        )
    fi

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

    READY=0

    for i in $(seq 1 120); 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 "ERROR: $NAME no logró cargar"

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

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

        return
    fi


    # ----------------------------------------------------------
    # VRAM en reposo
    # ----------------------------------------------------------

    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"


    # ----------------------------------------------------------
    # Request idéntico para todas las configuraciones
    # ----------------------------------------------------------

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


    START_NS=$(date +%s%N)

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

    END_NS=$(date +%s%N)

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

    echo "$WALL_MS" > "$RUN/logs/$NAME.wall-ms.txt"

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

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


    # ----------------------------------------------------------
    # Validaciones
    # ----------------------------------------------------------

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

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

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

    GEN_TPS=$(
      jq -r '.timings.predicted_per_second // 0' \
        "$RUN/responses/$NAME.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.json"
    )


    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\n" \
      "$NAME" \
      "$CTX" \
      "$CTK" \
      "$CTV" \
      "$DFLASH" \
      "$FA" \
      "$YARN" \
      "$VRAM" \
      "$FREE" \
      "$PROMPT_TPS" \
      "$GEN_TPS" \
      "$DRAFT_ACC" \
      "$FA_WARN" \
      "$CAPPING" \
      >> "$SUMMARY"


    echo
    echo "--- RESULTADO $NAME ---"

    jq '{
      prompt_tps: .timings.prompt_per_second,
      generation_tps: .timings.predicted_per_second,
      draft_generated: .timings.draft_n,
      draft_accepted: .timings.draft_n_accepted,
      acceptance:
        (if ((.timings.draft_n // 0) > 0)
         then (.timings.draft_n_accepted / .timings.draft_n)
         else null end),
      completion_tokens: .usage.completion_tokens,
      cached_tokens: .usage.prompt_tokens_details.cached_tokens
    }' "$RUN/responses/$NAME.json"

    echo "Wall: ${WALL_MS} ms"
    echo "VRAM: ${VRAM} MiB used / ${FREE} MiB free"
    echo "FA warnings: $FA_WARN"
    echo "Capping: $CAPPING"

    echo
    echo "Tabla actual:"
    column -t -s $'\t' "$SUMMARY"
}


# ============================================================
# FASE 1 — 262K NATIVO
# ============================================================

run_test A-q4q4-dflash 262144 q4_0 q4_0 on  on  off
run_test B-q8q4-dflash 262144 q8_0 q4_0 on  on  off
run_test C-q8q8-dflash 262144 q8_0 q8_0 on  on  off

run_test D-q4q4-base   262144 q4_0 q4_0 off on  off
run_test E-q8q4-base   262144 q8_0 q4_0 off on  off

# Control para medir impacto real de FlashAttention
run_test F-q4q4-no-fa  262144 q4_0 q4_0 off off off


echo
echo "============================================================"
echo " FASE 1 TERMINADA"
echo "============================================================"
echo

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

echo
echo "Resultados:"
echo "$RUN"
