#!/usr/bin/env bash
# Purpose: load the lab's model into the running Lemonade Server with its llama.cpp backend and
#          context length chosen explicitly, so that Lemonade can be measured against the
#          llama-server builds from Part 6 and the AMD lesson through the same API
# Platform: strix (Lemonade runs elsewhere too; the backend choice below is the AMD one)
# Minimum memory: 12 GB
# Assumes: Lemonade Server installed from AMD's playbook and running in the background (it
#          starts as a service after installation), the model registered with "lemonade pull"
#          as the lab page shows, and no other model server holding the memory
#
# Usage: MODEL=user.Qwen3-8B-Q4_K_M BACKEND=rocm bash serve-lemonade.sh
#        MODEL=user.Qwen3-8B-Q4_K_M BACKEND=vulkan bash serve-lemonade.sh
#
# Environment:
#   MODEL    registered Lemonade model name            (required)
#   BACKEND  llama.cpp backend: rocm or vulkan         (default: rocm)
#   CTX      context size, in tokens                   (default: 8192)
#   HOST     server address, exported as LEMONADE_HOST (default: 127.0.0.1)
#   PORT     server port, exported as LEMONADE_PORT    (default: 13305)
#   LLAMACPP_ARGS  arguments passed to llama-server    (default: --flash-attn on --cache-ram 0)
#
# Lemonade is a long-running server, not a foreground process: "lemonade load" asks it to
# start a llama.cpp backend with the given options and returns. There is no pid to record;
# the memory for this engine comes from the idle-snapshot delta. Stop the model afterwards
# with "lemonade unload". The Lemonade CLI documentation read on 2026-09-13 lists load with
# --ctx-size, --llamacpp and --llamacpp-args for the llamacpp recipe. --cache-ram 0 in the
# llama.cpp arguments turns off llama-server's host-RAM prompt cache, as serve-llama-cpp.sh does,
# so that the Lemonade rows and the llama-server rows allocate the same memory.

set -euo pipefail

MODEL="${MODEL:-}"
BACKEND="${BACKEND:-rocm}"
CTX="${CTX:-8192}"
HOST="${HOST:-127.0.0.1}"
PORT="${PORT:-13305}"
LLAMACPP_ARGS="${LLAMACPP_ARGS:---flash-attn on --cache-ram 0}"

die() { echo "serve-lemonade: $*" >&2; exit 1; }

[ -n "$MODEL" ] || die "set MODEL to a registered Lemonade model name; see 'lemonade list'"
command -v lemonade >/dev/null || die "lemonade not found; install it as the AMD lesson describes"
case "$BACKEND" in
  rocm|vulkan) ;;
  *) die "BACKEND must be rocm or vulkan for this lab, not '$BACKEND'" ;;
esac

export LEMONADE_HOST="$HOST"
export LEMONADE_PORT="$PORT"

lemonade status || die "Lemonade Server is not reachable on $HOST:$PORT; start or restart its service"

echo "==> Lemonade Server"
echo "    CLI      $(lemonade --version 2>/dev/null || echo 'version not reported')"
echo "    model    $MODEL"
echo "    backend  llama.cpp / $BACKEND, context $CTX tokens"
echo "    args     $LLAMACPP_ARGS"
echo "    API      http://$HOST:$PORT/v1"

lemonade unload >/dev/null 2>&1 || true
lemonade load "$MODEL" \
    --ctx-size "$CTX" \
    --llamacpp "$BACKEND" \
    --llamacpp-args "$LLAMACPP_ARGS"

echo "    loaded; measure it now, then run: lemonade unload"
