diff --git a/docs/architecture.md b/docs/architecture.md
index 66bf0b7b80..c83c83a3b3 100644
--- a/docs/architecture.md
+++ b/docs/architecture.md
@@ -55,4 +55,4 @@ Pinned versions of the third-party runtimes rocm-cli manages (from workspace-roo
### `engines/lemonade`, `engines/vllm` — inference engine adapters
-`engines/vllm` follows full domain extraction: `runtime.rs` (TheRock/managed-runtime resolution), `install.rs` (ROCm/vLLM build-variant discovery, install execution, runtime-repair assessment), `process.rs` (spawn/env/path setup, readiness polling), `state.rs` (service state file I/O). `lib.rs` keeps only CLI/envelope dispatch and the few items shared across those modules. `engines/lemonade`'s `lib.rs` is **not yet modularized** — see EAI-7768.
+`engines/vllm` follows full domain extraction: `runtime.rs` (TheRock/managed-runtime resolution), `install.rs` (ROCm/vLLM build-variant discovery, install execution, runtime-repair assessment), `process.rs` (spawn/env/path setup, readiness polling), `state.rs` (service state file I/O). `lib.rs` keeps only CLI/envelope dispatch and the few items shared across those modules. `engines/lemonade` follows the same pattern across six modules: `install.rs` (embeddable-runtime archive download/verify/extract, cache locking, manifest persistence), `backend_alignment.rs` (ROCm-SDK-to-llama.cpp-backend Tier 1 / Tier 2 / revert alignment, backend discovery/install/selection), `process.rs` (`lemond`/llama-server spawn, env/path setup, readiness and health polling), `runtime_dir.rs` (`XDG_RUNTIME_DIR` tier1/2/3 hardening for the spawned child), `direct_llama.rs` (Hugging Face checkpoint/GGUF resolution and the direct-llama-server serve path), `state.rs` (service state file I/O, device-policy/GPU-selection parsing, port/endpoint helpers). `lib.rs` keeps only CLI/envelope dispatch and the service-lifecycle orchestration that calls across those modules.
diff --git a/engines/lemonade/src/backend_alignment.rs b/engines/lemonade/src/backend_alignment.rs
new file mode 100644
index 0000000000..16f2d63eaa
--- /dev/null
+++ b/engines/lemonade/src/backend_alignment.rs
@@ -0,0 +1,1785 @@
+// Copyright © Advanced Micro Devices, Inc., or its affiliates.
+//
+// SPDX-License-Identifier: MIT
+
+use anyhow::{Context, Result, bail};
+use rocm_core::{AppPaths, RocmCliConfig, active_managed_therock_version, runtime_is_linux};
+use serde_json::Value;
+use std::fs;
+use std::path::{Path, PathBuf};
+use std::process::{Command as ProcessCommand, Stdio};
+use std::time::Duration;
+
+use crate::direct_llama::find_llama_server_binary_for_backend;
+use crate::install::{LemonadeInstallManifest, read_manifest};
+use crate::process::{
+ LemonadeProcessEnvironment, apply_lemonade_process_environment, hide_child_console_window,
+ lemonade_process_environment, run_lemonade_backend_install, spawn_lemond,
+ wait_for_lemonade_cli_status,
+};
+use crate::state::{free_local_port, terminate_pid};
+use crate::{DEFAULT_HOST, LLAMACPP_RECIPE, ROCM_BACKEND_NAME};
+
+/// Resource file inside the extracted Lemonade embeddable pinning which
+/// llama.cpp build (and paired ROCm version) `llamacpp:rocm` downloads. Its own
+/// header comment invites hand-editing: "You can modify these values to pin
+/// specific versions without rebuilding the application."
+const BACKEND_VERSIONS_RESOURCE: &str = "resources/backend_versions.json";
+
+/// Preferred llama.cpp backends, best first. Lemonade reports per-GPU support;
+/// we pick the highest-priority backend it considers supported on this host.
+/// GPU backends only — `cpu` is intentionally excluded so the router path never
+/// selects CPU under the GPU-required policy (AGENTS.md §6, matching
+/// `DIRECT_LLAMA_SERVER_BACKENDS`). When only CPU is usable, selection returns
+/// `None` and the caller fails with actionable guidance.
+const LLAMACPP_BACKEND_PRIORITY: [&str; 2] = ["rocm", "vulkan"];
+
+#[derive(Debug, Clone)]
+pub(crate) struct LemonadeRuntime {
+ pub(crate) manifest: LemonadeInstallManifest,
+}
+
+/// The variable that opts a machine out of rocm-cli aligning Lemonade's
+/// `llamacpp:rocm` backend to the active ROCm SDK.
+///
+/// `resources/backend_versions.json` is a documented, first-party
+/// customization point — its own header comment invites users to hand-edit it
+/// to pin specific versions without rebuilding — so alignment running
+/// unconditionally on every plain install would silently overwrite a user's
+/// manual pin. Mirrors vLLM's `ROCM_CLI_DISABLE_TORCH_ALIGNMENT`
+/// (`rocm_core::TORCH_ALIGNMENT_DISABLED_ENV`): presence is the signal, so any
+/// value — including the empty string — disables the alignment.
+pub const LEMONADE_BACKEND_ALIGNMENT_DISABLED_ENV: &str =
+ "ROCM_CLI_DISABLE_LEMONADE_BACKEND_ALIGNMENT";
+
+/// Whether the user has opted out of rocm-cli aligning Lemonade's backend.
+fn lemonade_backend_alignment_disabled() -> bool {
+ std::env::var_os(LEMONADE_BACKEND_ALIGNMENT_DISABLED_ENV).is_some()
+}
+
+/// Point Lemonade's `llamacpp:rocm` backend at the ROCm version rocm-cli already has
+/// installed and active, instead of Lemonade's hardcoded pin (which does not track
+/// whatever the user separately installed via `rocm install sdk`).
+///
+/// Best-effort and defensive at every step: if there is no managed rocm-cli SDK, the
+/// pinned version already matches, or an aligned attempt cannot be verified to actually
+/// resolve its GPU library, this falls back to (or reverts to) today's unmodified
+/// pinned-version install — never a regression, and never a silent CPU fallback
+/// (AGENTS.md §6): an aligned backend is only kept once [`rocm_backend_resolves`]
+/// confirms it.
+///
+/// Returns the aligned version string when one was applied and verified, so the caller
+/// can surface it in the install response.
+pub(crate) fn prepare_llamacpp_backend_for_active_rocm(
+ paths: &AppPaths,
+ manifest: &mut LemonadeInstallManifest,
+) -> Result