Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

App: dashboard on the TV, model library, presets and USB import

Abhiram266fb74f87ca
diff --git a/.gitmodules b/.gitmodules
index 067c173..a3e2c95 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -1,3 +1,6 @@
[submodule "third_party/llama.cpp"]
path = third_party/llama.cpp
url = https://github.com/ggml-org/llama.cpp
+[submodule "third_party/ps5-homebrew-ui"]
+ path = third_party/ps5-homebrew-ui
+ url = https://github.com/blackbearreloaded/ps5-homebrew-ui
diff --git a/README.md b/README.md
index 24e49c2..4a3176d 100644
--- a/README.md
+++ b/README.md
@@ -97,11 +97,25 @@ The app links llama.cpp with ggml-vulkan against [PS5_Vulkan](https://github.com
scripts/build-app.sh # build/app-ps5/title/PPSA99581
```
-Copy `build/app-ps5/title/PPSA99581` to `/data/homebrew/PPSA99581` and the model to `/data/PS5LM/models`, then start **PS5LM** from the home screen. The screen stays black; after about 90 s the chat is at `http://<console IP>:8081`. Its arguments come from `/data/PS5LM/app-args.txt` (one per line) and its log goes to `/data/PS5LM/app.log` and `llama.log`.
+Copy `build/app-ps5/title/PPSA99581` to `/data/homebrew/PPSA99581` and a model to `/data/PS5LM/models`, then start **PS5LM** from the home screen. The chat is at `http://<console IP>:8081` once the model is on the GPU (about 90 s).
+
+**On the TV** the app shows a dashboard drawn with [ps5-homebrew-ui](https://github.com/blackbearreloaded/ps5-homebrew-ui) (a submodule in `third_party/`): generation speed, how busy the GPU is, CPU use, the memory pool the GPU and CPU share and how the model, its KV cache and the rest split it, context in use, and the model. Cross on the Model tile opens the **model library**: every `.gguf` in `/data/PS5LM/models` and on USB drives (`/mnt/usbN` or `/mnt/usbN/PS5LM/models`), each with the context and KV cache type the planner (`ps5/app/model_plan.cpp`) picks for the memory free, and the sampling settings of its family when it is a model we know (Qwen 3.5/3.6/3.8, Gemma 4, gpt-oss, Granite, Nemotron, Mistral, Llama). Cross loads one, or imports one from USB into internal storage.
+
+`/data/PS5LM/app-args.txt` (one argument per line) overrides the planner. The app logs to `/data/PS5LM/app.log` and `llama.log`.
+
+From the PC:
+
+```sh
+scripts/preview-app-ui.sh # render the dashboard to build/app-preview/*.png (Mesa, no console)
+PS5_HOST=<console IP> scripts/ps5lm-app.sh cycle # close, deploy, launch; also close|deploy|launch
+```
+
+The app closes itself on request (`/data/PS5LM/quit`), so nothing is killed while it draws; launching uses [ps5-homebrew-dev-protocol](https://github.com/blackbearreloaded/ps5-homebrew-dev-protocol)'s launch payload, cloned into `.deps/src`.
## Credits
- [llama.cpp / ggml](https://github.com/ggml-org/llama.cpp), which does the actual work.
+- [PS5_Vulkan](https://github.com/mihawk-99/PS5_Vulkan) (Mihawk-99) for RADV on the PS5, and [ps5-homebrew-ui](https://github.com/blackbearreloaded/ps5-homebrew-ui), [ps5-opengl](https://github.com/blackbearreloaded/ps5-opengl) and ps5-homebrew-dev-protocol (BlackBearReloaded) for the app's screen and tooling.
- [ps5-payload-dev](https://github.com/ps5-payload-dev) (John Törnblom) for the SDK and the ELF loader.
- [PS5SX2](https://github.com/Swordpdf/PS5SX2), [ProsperoAI](https://github.com/blackbearreloaded/ProsperoAI) and [PS5_Vulkan](https://github.com/mihawk-99/PS5_Vulkan), whose notes on the console's memory and GPU this project leans on.
diff --git a/docs/CONSOLE.md b/docs/CONSOLE.md
index 3f3bbb6..79fecfc 100644
--- a/docs/CONSOLE.md
+++ b/docs/CONSOLE.md
@@ -95,3 +95,13 @@ What a title has to do differently from a payload, each found by a crash:
- **The SDK fork's `getaddrinfo` always fails**, and cpp-httplib resolves even the address it binds; `compat_app.c` has a numeric one. Its `accept4` is the title's own too, and logs `errno` when `accept` fails: with the payload's version the listener stopped 20 s into the first run.
- **No `HOME`**: llama.cpp throws looking for its cache directory without one.
- C++ exceptions and RTTI are off by default for the PS5 target and must be turned on for llama.cpp.
+
+### Switching models
+
+Unloading a model in the running app leaves part of its GPU memory held: with Qwen3.8-27B UD-IQ2_XXS at 64k, the pool went from 11.01 GiB free before loading to 5.77 GiB free after `llama_server` returned, and ggml's Vulkan figure fell the same way, so the next model could not get its KV cache. The app therefore changes model by restarting itself: it writes the choice to `/data/PS5LM/model.txt` and calls `sceSystemServiceLoadExec("/app0/eboot.bin", NULL)`. The system replaces the process (`Kill for LoadExec(0x11d) => 0`, then a new `EXEC`) without the crash reporter, and the new process starts from the whole pool (11.01 GiB free). Where the memory is held is still open.
+
+### The dashboard on the console
+
+ps5-homebrew-ui draws through ps5-opengl in the same title as RADV: the GL stack is linked as one object with only its GL/EGL API global (4,399 symbol names collide with RADV's Mesa otherwise), `sceAgcInit` runs once for both drivers (`--wrap`), and the GL runtime's thread-local initialiser `_ZTH23_mesa_glapi_tls_Context` is an empty function, as ProsperoAI has it. The screen costs about 0.35 GiB of the pool (11.38 to 11.03 GiB free). The app can be driven from a PC without a controller: `scripts/ps5lm-app.sh close|deploy|launch|screenshot|load|unload|import`.
+
+ShadowMountPlus registers a title once and copies its icon and backgrounds then; replacing them later needs a re-registration. Uninstalling to force one hit "Register failed: TitleDir bridge unavailable" after ShadowMountPlus had logged `stale cave bridge detached`; reloading ShadowMountPlus fixed it.
diff --git a/ps5/app/compat_app.c b/ps5/app/compat_app.c
index 0ecd779..32da530 100644
--- a/ps5/app/compat_app.c
+++ b/ps5/app/compat_app.c
@@ -259,3 +259,7 @@ int accept4(int s, struct sockaddr *restrict addr, socklen_t *restrict addrlen,
}
return fd;
}
+
+/* ps5-homebrew-ui's system.cpp calls this before hiding the splash, for apps
+ * whose runtime holds the request back; PS5LM's does not. */
+void hui_release_splash(void) {}
diff --git a/ps5/app/cpp_runtime.cpp b/ps5/app/cpp_runtime.cpp
new file mode 100644
index 0000000..55d4f3e
--- /dev/null
+++ b/ps5/app/cpp_runtime.cpp
@@ -0,0 +1,157 @@
+// Copied from PS5_Vulkan tooling/native/app_cpp_runtime.cpp; changed for PS5LM
+// where marked.
+/*
+ * ps5-native-app-boilerplate - Minimal target C++ allocation runtime.
+ * Copyright (C) 2026 BlackBearReloaded
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ * Bridges standard C++ allocation operators to the clean-room libc module
+ * without introducing exceptions, RTTI, or the complete libc++ runtime.
+ */
+
+#include <cstddef>
+#include <new>
+
+extern "C"
+{
+ void *malloc(std::size_t size);
+ void free(void *address);
+ int posix_memalign(void **address, std::size_t alignment, std::size_t size);
+}
+
+namespace
+{
+[[nodiscard]] void *allocate(std::size_t size) noexcept
+{
+ return malloc(size == 0 ? 1 : size);
+}
+
+[[nodiscard]] void *allocate_aligned(std::size_t size, std::size_t alignment) noexcept
+{
+ void *address = nullptr;
+ if (alignment < sizeof(void *))
+ alignment = sizeof(void *);
+ if ((alignment & (alignment - 1)) != 0)
+ return nullptr;
+ return posix_memalign(&address, alignment, size == 0 ? 1 : size) == 0 ? address : nullptr;
+}
+
+// PS5LM: a failed allocation throws, as the standard says, so llama-server
+// turns a request that does not fit into an error reply instead of the
+// process ending on a trap (a prompt checkpoint did, at 64k context).
+[[noreturn]] void allocation_failure()
+{
+ throw std::bad_alloc();
+}
+} // namespace
+
+extern "C" __attribute__((noinline, visibility("hidden"))) bool
+ps5ObserveOwnedAllocation(const void *address) noexcept
+{
+ __asm__ volatile("" : : "r"(address) : "memory");
+ return address != nullptr;
+}
+
+void *operator new(std::size_t size)
+{
+ if (void *address = allocate(size))
+ return address;
+ allocation_failure();
+}
+
+void *operator new[](std::size_t size)
+{
+ return ::operator new(size);
+}
+
+void *operator new(std::size_t size, const std::nothrow_t &) noexcept
+{
+ return allocate(size);
+}
+
+void *operator new[](std::size_t size, const std::nothrow_t &) noexcept
+{
+ return allocate(size);
+}
+
+void *operator new(std::size_t size, std::align_val_t alignment)
+{
+ if (void *address = allocate_aligned(size, static_cast<std::size_t>(alignment)))
+ return address;
+ allocation_failure();
+}
+
+void *operator new[](std::size_t size, std::align_val_t alignment)
+{
+ return ::operator new(size, alignment);
+}
+
+void *operator new(std::size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept
+{
+ return allocate_aligned(size, static_cast<std::size_t>(alignment));
+}
+
+void *operator new[](std::size_t size, std::align_val_t alignment, const std::nothrow_t &) noexcept
+{
+ return allocate_aligned(size, static_cast<std::size_t>(alignment));
+}
+
+void operator delete(void *address) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address) noexcept
+{
+ free(address);
+}
+
+void operator delete(void *address, std::size_t) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address, std::size_t) noexcept
+{
+ free(address);
+}
+
+void operator delete(void *address, std::align_val_t) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address, std::align_val_t) noexcept
+{
+ free(address);
+}
+
+void operator delete(void *address, std::size_t, std::align_val_t) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address, std::size_t, std::align_val_t) noexcept
+{
+ free(address);
+}
+
+void operator delete(void *address, const std::nothrow_t &) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address, const std::nothrow_t &) noexcept
+{
+ free(address);
+}
+
+void operator delete(void *address, std::align_val_t, const std::nothrow_t &) noexcept
+{
+ free(address);
+}
+
+void operator delete[](void *address, std::align_val_t, const std::nothrow_t &) noexcept
+{
+ free(address);
+}
diff --git a/ps5/app/dashboard.cpp b/ps5/app/dashboard.cpp
new file mode 100644
index 0000000..3cf1df6
--- /dev/null
+++ b/ps5/app/dashboard.cpp
@@ -0,0 +1,673 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// Layout, palette, panels, the focus ring and spatial navigation follow
+// ps5-homebrew-ui's src/concepts/dashboard.cpp ("Pulse Dashboard",
+// BlackBearReloaded, GPL-3.0-or-later); the tiles and their data are PS5LM's.
+#include "dashboard.hpp"
+
+#include "ui/glyphs.hpp"
+
+#include <algorithm>
+#include <array>
+#include <cmath>
+#include <cstdio>
+#include <span>
+
+namespace ps5lm {
+
+using hui::Action;
+using hui::Direction;
+using hui::gfx::Color;
+using hui::gfx::Rect;
+namespace gfx = hui::gfx;
+namespace ui = hui::ui;
+namespace tween = hui::tween;
+
+namespace {
+
+// ---- the design language (Pulse Dashboard's) --------------------------------
+
+const Color kInk = Color::rgb(0xe9eefc);
+const Color kClear = Color::rgb(0x000000, 0.0f);
+const Color kBlack = Color::rgb(0x000000);
+const Color kPanelTop = Color::rgb(0x171f33);
+const Color kPanelBottom = Color::rgb(0x0e1424);
+const Color kCyan = Color::rgb(0x4fe0ff); // speed and the GPU
+const Color kLime = Color::rgb(0xb8f26b); // the CPU and the model
+const Color kAmber = Color::rgb(0xffc24a); // memory
+const Color kRose = Color::rgb(0xff6f9c); // GPU memory
+const Color kBrand = Color::rgb(0x8288f0); // PS5LM's periwinkle
+
+constexpr float kMuted = 0.62f;
+constexpr float kFaint = 0.38f;
+
+constexpr float kGridX = 96.0f, kGridY = 164.0f, kGridW = 1728.0f, kGridH = 800.0f;
+constexpr float kGap = 20.0f, kRadius = 28.0f, kPad = 28.0f;
+constexpr float kLabelY = 46.0f, kLabelSize = 17.0f, kLift = 0.02f;
+constexpr float kTau = 6.2831853f;
+constexpr const char * kDash = "\xE2\x80\x94";
+
+enum Tile : int { kSpeed, kGpu, kCpu, kMemory, kVram, kSession, kContext, kModel };
+
+struct TileSpec {
+ const char * label;
+ int column, row, columns, rows;
+ std::uint32_t accent;
+};
+
+// In reading order: ties in the navigation go to the earlier tile.
+constexpr TileSpec kSpec[Dashboard::kTiles] = {
+ { "GENERATION", 0, 0, 6, 2, 0x4fe0ff },
+ { "GPU", 6, 0, 3, 2, 0x4fe0ff },
+ { "CPU", 9, 0, 3, 2, 0xb8f26b },
+ { "MEMORY", 0, 2, 3, 2, 0xffc24a },
+ { "GPU MEMORY", 3, 2, 6, 1, 0xff6f9c },
+ { "SESSION", 3, 3, 3, 1, 0x4fe0ff },
+ { "CONTEXT", 6, 3, 3, 1, 0xffc24a },
+ { "MODEL", 9, 2, 3, 2, 0xb8f26b },
+};
+
+Color accent_of(int tile) { return Color::rgb(kSpec[tile].accent); }
+
+Rect tile_rect(int tile) {
+ const TileSpec & s = kSpec[tile];
+ const float column = (kGridW - 11.0f * kGap) / 12.0f;
+ const float row = (kGridH - 3.0f * kGap) / 4.0f;
+ return { kGridX + s.column * (column + kGap), kGridY + s.row * (row + kGap),
+ s.columns * (column + kGap) - kGap, s.rows * (row + kGap) - kGap };
+}
+
+Rect scaled(const Rect & r, float scale) {
+ return { r.cx() - r.w * scale * 0.5f, r.cy() - r.h * scale * 0.5f, r.w * scale, r.h * scale };
+}
+
+// Pulse Dashboard's spatial navigation: the candidate beyond the leading
+// edge with the least travel, a penalty for missing sideways, and a light
+// pull toward the aligned one.
+int spatial_next(std::span<const Rect> rects, int from, Direction direction) {
+ if (direction == Direction::none) {
+ return -1;
+ }
+ const bool horizontal = direction == Direction::left || direction == Direction::right;
+ const float sign = direction == Direction::right || direction == Direction::down ? 1.0f : -1.0f;
+ auto main_low = [=](const Rect & r) { return horizontal ? r.x : r.y; };
+ auto main_size = [=](const Rect & r) { return horizontal ? r.w : r.h; };
+ auto cross_low = [=](const Rect & r) { return horizontal ? r.y : r.x; };
+ auto cross_size = [=](const Rect & r) { return horizontal ? r.h : r.w; };
+ const Rect & o = rects[(size_t) from];
+ const float leading = sign > 0 ? main_low(o) + main_size(o) : main_low(o);
+ int best = -1;
+ float best_score = 0;
+ for (int i = 0; i < (int) rects.size(); ++i) {
+ if (i == from) {
+ continue;
+ }
+ const Rect & c = rects[(size_t) i];
+ if ((main_low(c) + main_size(c) * 0.5f - leading) * sign <= 0) {
+ continue;
+ }
+ const float facing = sign > 0 ? main_low(c) : main_low(c) + main_size(c);
+ const float along = std::max(0.0f, (facing - leading) * sign);
+ const float miss = std::max({ 0.0f, cross_low(c) - (cross_low(o) + cross_size(o)),
+ cross_low(o) - (cross_low(c) + cross_size(c)) });
+ const float offset = std::fabs((cross_low(c) + cross_size(c) * 0.5f) - (cross_low(o) + cross_size(o) * 0.5f));
+ const float score = along + 2.0f * miss + 0.25f * offset;
+ if (best < 0 || score < best_score - 0.01f) {
+ best = i;
+ best_score = score;
+ }
+ }
+ return best;
+}
+
+Direction opposite(Direction d) {
+ switch (d) {
+ case Direction::left: return Direction::right;
+ case Direction::right: return Direction::left;
+ case Direction::up: return Direction::down;
+ case Direction::down: return Direction::up;
+ default: return Direction::none;
+ }
+}
+
+float rise(float time, int index = 0, float step = 0.05f, float duration = 0.7f) {
+ return tween::cubic_out(tween::clamp01((time - step * (float) index) / duration));
+}
+
+const char * state_word(ServerState s) {
+ switch (s) {
+ case ServerState::loading: return "LOADING";
+ case ServerState::ready: return "READY";
+ case ServerState::generating: return "GENERATING";
+ case ServerState::failed: return "FAILED";
+ default: return "NO MODEL";
+ }
+}
+
+Color state_color(ServerState s) {
+ switch (s) {
+ case ServerState::ready: return kLime;
+ case ServerState::generating: return kCyan;
+ case ServerState::failed: return kRose;
+ default: return kAmber;
+ }
+}
+
+constexpr float kRowH = 78.0f;
+constexpr Rect kLibrary{ 360.0f, 150.0f, 1200.0f, 800.0f };
+
+} // namespace
+
+// ---- update -------------------------------------------------------------------
+
+Rect Dashboard::ring_rect() const {
+ return scaled(tile_rect(focus_), 1.0f + kLift).inset(-5.0f);
+}
+
+void Dashboard::update(const hui::InputFrame & input, float dt, ui::Feedback & feedback) {
+ age_ += dt;
+ clock_ += dt;
+ const int rows = (int) live_.models.size();
+
+ if (library_open_) {
+ if (input.nav == Direction::up || input.nav == Direction::down) {
+ const int next = library_cursor_ + (input.nav == Direction::down ? 1 : -1);
+ if (next >= 0 && next < rows) {
+ library_cursor_ = next;
+ feedback.play(hui::audio::Cue::focus, 1.0f + 0.03f * (float) next);
+ } else if (!input.nav_repeat) {
+ feedback.play(hui::audio::Cue::error, 1.0f, 0.0f, 0.6f);
+ refusal_.trigger();
+ }
+ }
+ if (input.is_pressed(Action::confirm) && rows > 0) {
+ const ModelRow & m = live_.models[(size_t) library_cursor_];
+ if (m.on_usb && m.import_progress < 0) {
+ request_ = { Request::import, m.file };
+ feedback.play(hui::audio::Cue::select);
+ } else if (m.current) {
+ request_ = { Request::unload, m.file };
+ library_open_ = false;
+ feedback.play(hui::audio::Cue::back);
+ } else if (!m.on_usb && m.fits) {
+ request_ = { Request::load, m.file };
+ library_open_ = false;
+ feedback.play(hui::audio::Cue::complete);
+ } else {
+ feedback.play(hui::audio::Cue::error);
+ refusal_.trigger();
+ }
+ }
+ if (input.is_pressed(Action::back)) {
+ library_open_ = false;
+ feedback.play(hui::audio::Cue::back);
+ }
+ } else {
+ if (input.nav != Direction::none) {
+ std::array<Rect, kTiles> rects;
+ for (int i = 0; i < kTiles; ++i) {
+ rects[(size_t) i] = tile_rect(i);
+ }
+ int next = input.nav == opposite(came_by_) ? came_from_ : spatial_next(rects, focus_, input.nav);
+ if (next < 0) {
+ next = spatial_next(rects, focus_, input.nav);
+ }
+ if (next >= 0) {
+ came_from_ = focus_;
+ came_by_ = input.nav;
+ focus_ = next;
+ const Rect r = tile_rect(focus_);
+ feedback.play(hui::audio::Cue::focus, 1.08f - 0.16f * (r.cy() - kGridY) / kGridH, ui::pan_for_x(r.cx()));
+ } else if (!input.nav_repeat) {
+ feedback.play(hui::audio::Cue::error, 1.0f, 0.0f, 0.6f);
+ feedback.rumble(0.25f, 0.05f);
+ refusal_.trigger();
+ refusal_x_ = input.nav == Direction::right ? 1.0f : input.nav == Direction::left ? -1.0f : 0.0f;
+ refusal_y_ = input.nav == Direction::down ? 1.0f : input.nav == Direction::up ? -1.0f : 0.0f;
+ }
+ }
+ if (input.is_pressed(Action::confirm) && focus_ == kModel) {
+ library_open_ = true;
+ library_cursor_ = 0;
+ for (int i = 0; i < rows; ++i) {
+ if (live_.models[(size_t) i].current) {
+ library_cursor_ = i;
+ }
+ }
+ cursor_y_.snap((float) library_cursor_);
+ feedback.play(hui::audio::Cue::open);
+ }
+ }
+
+ for (int i = 0; i < kTiles; ++i) {
+ lift_[i].target = i == focus_ ? 1.0f : 0.0f;
+ lift_[i].update(dt, 16.0f);
+ }
+ ring_.target(ring_rect());
+ ring_.update(dt, 20.0f);
+ refusal_.update(dt, 9.0f);
+ library_.target = library_open_ ? 1.0f : 0.0f;
+ library_.update(dt, 13.0f);
+ cursor_y_.target = (float) library_cursor_;
+ cursor_y_.update(dt, 20.0f);
+}
+
+// ---- drawing helpers -----------------------------------------------------------
+
+namespace {
+
+float caps(gfx::DrawList & list, const ui::Fonts & f, std::string_view v, float x, float y, float size, Color c,
+ gfx::Align a = gfx::Align::left) {
+ return ui::text(list, f.semibold, v, x, y, size, c, a, 3.0f);
+}
+
+// A value, or a dash while there is nothing measured.
+float number(gfx::DrawList & list, const ui::Fonts & f, bool known, const char * format, double v, float x, float y,
+ float size, Color c, gfx::Align a = gfx::Align::left) {
+ char text[32];
+ if (known) {
+ std::snprintf(text, sizeof(text), format, v);
+ } else {
+ std::snprintf(text, sizeof(text), "%s", kDash);
+ }
+ return ui::text(list, f.mono, text, x, y, size, known ? c : c.with_alpha(kFaint), a);
+}
+
+// A filled history chart, newest on the right, scaled to `top`.
+void history(gfx::DrawList & list, const std::vector<float> & v, const Rect & g, float top, Color c, float t) {
+ const float base = g.y + g.h;
+ list.rounded_rect({ g.x, base - 1.0f, g.w, 2.0f }, 1, kInk.with_alpha(0.22f));
+ if (v.size() < 2) {
+ list.rounded_rect({ g.x, base - 1.5f, g.w, 3.0f }, 1.5f, c.with_alpha(0.45f));
+ return;
+ }
+ const float grow = rise(t, 0, 0.0f, 0.9f);
+ const float step = g.w / 119.0f;
+ float lx = 0, ly = 0;
+ for (size_t age = 0; age < v.size(); ++age) {
+ const float value = v[v.size() - 1 - age];
+ const float x = g.x + g.w - (float) age * step;
+ const float y = base - tween::clamp01(value / top) * g.h * grow;
+ list.gradient_rect({ x - step * 0.5f, y, step, base - y }, 0, c.with_alpha(0.5f * (base - y) / g.h),
+ c.with_alpha(0.0f));
+ if (age > 0) {
+ list.line(lx, ly, x, y, 2.5f, c);
+ }
+ lx = x;
+ ly = y;
+ }
+ const float newest = base - tween::clamp01(v.back() / top) * g.h * grow;
+ list.circle(g.x + g.w, newest, 6, c);
+ list.circle(g.x + g.w, newest, 3, kPanelBottom);
+}
+
+void bar(gfx::DrawList & list, const Rect & r, float share, Color c) {
+ list.rounded_rect(r, r.h * 0.5f, kInk.with_alpha(0.1f));
+ list.rounded_rect({ r.x, r.y, std::max(r.h, r.w * tween::clamp01(share)), r.h }, r.h * 0.5f, c);
+}
+
+void draw_panel(gfx::DrawList & list, const Rect & r, float radius, float lit, Color accent) {
+ list.gradient_rect(r, radius, gfx::mix(kPanelTop, accent, 0.05f * lit).with_alpha(0.9f),
+ gfx::mix(kPanelBottom, accent, 0.02f * lit).with_alpha(0.92f));
+ list.bordered_rect(r, radius, kClear, 1.5f, gfx::mix(kInk.with_alpha(0.09f), accent.with_alpha(0.5f), lit));
+}
+
+std::string gib(double v) {
+ char t[24];
+ std::snprintf(t, sizeof(t), "%.1f", v);
+ return t;
+}
+
+} // namespace
+
+// ---- tiles ------------------------------------------------------------------------
+
+void Dashboard::draw_content(gfx::DrawList & list, int tile, const Rect & r, float t) const {
+ const ui::Fonts & f = fonts_;
+ const Live & L = live_;
+ const bool serving = L.state == ServerState::ready || L.state == ServerState::generating;
+ const float up = rise(t, 0, 0.0f, 0.9f);
+ char text[96];
+
+ switch (tile) {
+ case kSpeed: {
+ float peak = 1, sum = 0, mn = 1e9f;
+ int n = 0;
+ for (float v : L.gen_history) {
+ if (v > 0) {
+ peak = std::max(peak, v);
+ mn = std::min(mn, v);
+ sum += v;
+ n++;
+ }
+ }
+ // Idle, the big figure keeps the last generation's speed.
+ float shown = L.gen_tps;
+ for (auto it = L.gen_history.rbegin(); shown <= 0 && it != L.gen_history.rend(); ++it) {
+ shown = *it;
+ }
+ caps(list, f, "TOK/S", r.x + r.w - kPad, r.y + 84, 17, kInk.with_alpha(kMuted), gfx::Align::right);
+ number(list, f, serving && shown > 0, "%.1f", shown * up, r.x + r.w - kPad - 76, r.y + 84, 64,
+ L.state == ServerState::generating ? kInk : kInk.with_alpha(0.8f), gfx::Align::right);
+ if (L.state == ServerState::loading) {
+ const float p = std::max(0.0f, L.load_progress);
+ std::snprintf(text, sizeof(text), "Loading the model onto the GPU \xC2\xB7 %.0f%%", p * 100.0f);
+ const Rect lb{ r.x + kPad, r.y + 196, r.w - 2 * kPad, 14 };
+ list.rounded_rect(lb, 7, kInk.with_alpha(0.08f));
+ // A bright sheen runs along the filled part while it grows.
+ const float w = std::max(14.0f, lb.w * p);
+ list.rounded_rect({ lb.x, lb.y, w, lb.h }, 7, kCyan);
+ const float sheen = std::fmod(clock_ * 0.6f, 1.0f);
+ list.glow({ lb.x + w * sheen - 20, lb.y, 40, lb.h }, 7, 16, kInk.with_alpha(0.35f));
+ std::snprintf(size_text_, sizeof(size_text_), "%.1f of %.1f GiB on the GPU", p * (L.model_gib + L.kv_gib),
+ L.model_gib + L.kv_gib);
+ ui::text(list, f.regular, size_text_, r.x + kPad, r.y + 250, 20, kInk.with_alpha(kFaint));
+ } else if (!serving) {
+ std::snprintf(text, sizeof(text), "%s", "Not serving");
+ } else if (L.state == ServerState::generating) {
+ std::snprintf(text, sizeof(text), "Generating now");
+ } else if (n == 0) {
+ std::snprintf(text, sizeof(text), "Waiting for the first prompt");
+ } else {
+ std::snprintf(text, sizeof(text), "Idle, %llu tokens so far", (unsigned long long) L.tokens);
+ }
+ ui::text(list, f.regular, text, r.x + kPad, r.y + 84, 22, kInk.with_alpha(kMuted));
+ if (L.state != ServerState::loading) {
+ history(list, L.gen_history, { r.x + kPad, r.y + 116, r.w - 2 * kPad, 186 }, peak * 1.2f, kCyan, t);
+ }
+ const char * names[] = { "MIN", "AVG", "MAX" };
+ const float vals[] = { n ? mn : 0, n ? sum / n : 0, n ? peak : 0 };
+ for (int k = 0; k < 3; ++k) {
+ const float x = r.x + kPad + k * 170.0f;
+ caps(list, f, names[k], x, r.y + 352, 15, kInk.with_alpha(kMuted));
+ number(list, f, n > 0, "%.1f", vals[k], x + 56, r.y + 354, 26, kInk);
+ }
+ const float uw = ui::text(list, f.regular, "tok/s", r.x + r.w - kPad, r.y + 353, 20, kInk.with_alpha(kFaint),
+ gfx::Align::right);
+ const float pw = number(list, f, L.prompt_tps > 0, "%.0f", L.prompt_tps, r.x + r.w - kPad - uw - 10, r.y + 354, 26,
+ kInk, gfx::Align::right);
+ caps(list, f, "PROMPT", r.x + r.w - kPad - uw - pw - 26, r.y + 352, 15, kInk.with_alpha(kMuted), gfx::Align::right);
+ break;
+ }
+ case kGpu: {
+ const float gw = number(list, f, serving, "%.0f", L.gpu_busy * 100.0f * up, r.x + kPad, r.y + 118, 60, kInk);
+ ui::text(list, f.regular, "% busy computing", r.x + kPad + gw + 10, r.y + 118, 22, kInk.with_alpha(kMuted));
+ ui::text(list, f.regular, "PlayStation 5 GPU, Vulkan (RADV)", r.x + kPad, r.y + 152, 20, kInk.with_alpha(kFaint));
+ history(list, L.gpu_history, { r.x + kPad, r.y + 186, r.w - 2 * kPad, 90 }, 1.0f, kCyan, t);
+ list.rounded_rect({ r.x + kPad, r.y + 300, r.w - 2 * kPad, 1 }, 0, kInk.with_alpha(0.1f));
+ caps(list, f, "MODEL ON THE GPU", r.x + kPad, r.y + 338, 15, kInk.with_alpha(kMuted));
+ std::snprintf(text, sizeof(text), "%s GiB", gib(L.model_gib + L.kv_gib).c_str());
+ ui::text(list, f.mono, text, r.x + r.w - kPad, r.y + 340, 24, kInk, gfx::Align::right);
+ break;
+ }
+ case kCpu: {
+ const float cw = number(list, f, true, "%.0f", L.cpu_use * 100.0f * up, r.x + kPad, r.y + 118, 60, kInk);
+ std::snprintf(text, sizeof(text), "%% of %d CPUs", L.cpus);
+ ui::text(list, f.regular, text, r.x + kPad + cw + 10, r.y + 118, 22, kInk.with_alpha(kMuted));
+ ui::text(list, f.regular, "Zen 2, the app's share", r.x + kPad, r.y + 152, 20, kInk.with_alpha(kFaint));
+ history(list, L.cpu_history, { r.x + kPad, r.y + 186, r.w - 2 * kPad, 90 }, 1.0f, kLime, t);
+ list.rounded_rect({ r.x + kPad, r.y + 300, r.w - 2 * kPad, 1 }, 0, kInk.with_alpha(0.1f));
+ caps(list, f, "CPU HEAP", r.x + kPad, r.y + 338, 15, kInk.with_alpha(kMuted));
+ std::snprintf(text, sizeof(text), "%s GiB", gib(L.heap_gib).c_str());
+ ui::text(list, f.mono, text, r.x + r.w - kPad, r.y + 340, 24, kInk, gfx::Align::right);
+ break;
+ }
+ case kMemory: {
+ const bool known = L.pool_gib > 0;
+ const float used = known ? (float) ((L.pool_gib - L.free_gib) / L.pool_gib) : 0.0f;
+ const float cx = r.cx(), cy = r.y + 178;
+ list.arc(cx, cy, 98, 16, 0.0f, kTau, kInk.with_alpha(0.08f));
+ list.arc(cx, cy, 98, 16, 0.0f, kTau * used * up, kAmber);
+ number(list, f, known, "%.0f%%", used * 100.0f * up, cx, cy + 12, 50, kInk, gfx::Align::center);
+ ui::text(list, f.regular, "in use", cx, cy + 42, 20, kInk.with_alpha(kMuted), gfx::Align::center);
+ std::snprintf(text, sizeof(text), "%s of %s GiB", gib(L.pool_gib - L.free_gib).c_str(), gib(L.pool_gib).c_str());
+ ui::text(list, f.mono, known ? text : kDash, cx, r.y + 330, 24, kInk, gfx::Align::center);
+ ui::text(list, f.regular, "GPU and CPU share this pool", cx, r.y + 360, 18, kInk.with_alpha(kFaint),
+ gfx::Align::center);
+ break;
+ }
+ case kVram: {
+ // Each part capped at what is in use, so a model still loading
+ // never pushes the bar past the pool.
+ const double used = std::max(0.0, L.pool_gib - L.free_gib);
+ const double model = std::min(L.model_gib, used);
+ const double kv = std::min(L.kv_gib, used - model);
+ const double other = used - model - kv;
+ const double parts[4] = { model, kv, other, L.free_gib };
+ const char * names[4] = { "Model", "KV cache", "Other", "Free" };
+ const Color colors[4] = { kRose, Color::rgb(0xffa9c4), Color::rgb(0xb48cff), kInk.with_alpha(0.16f) };
+ std::snprintf(text, sizeof(text), "of %s GiB", gib(L.pool_gib).c_str());
+ const float w = f.regular.measure(text, 22);
+ ui::text(list, f.regular, text, r.x + r.w - kPad, r.y + 50, 22, kInk.with_alpha(kMuted), gfx::Align::right);
+ number(list, f, L.pool_gib > 0, "%.1f", (L.pool_gib - L.free_gib) * up, r.x + r.w - kPad - w - 12, r.y + 50,
+ 34, kInk, gfx::Align::right);
+ const Rect b{ r.x + kPad, r.y + 76, r.w - 2 * kPad, 28 };
+ list.rounded_rect(b, 10, kInk.with_alpha(0.05f));
+ float x = b.x;
+ for (int k = 0; k < 4 && L.pool_gib > 0; ++k) {
+ const float full = b.w * (float) (parts[k] / L.pool_gib);
+ const float seg = std::max(0.0f, full * rise(t, k, 0.16f, 0.5f) - 4.0f);
+ if (seg > 1.0f) {
+ list.rounded_rect({ x, b.y, seg, b.h }, 10, colors[k]);
+ }
+ x += full * rise(t, k, 0.16f, 0.5f);
+ }
+ x = r.x + kPad;
+ for (int k = 0; k < 4; ++k) {
+ list.circle(x + 7, r.y + 143, 7, k == 3 ? kInk.with_alpha(0.45f) : colors[k]);
+ x += 24;
+ x += ui::text(list, f.regular, names[k], x, r.y + 150, 20, kInk.with_alpha(0.85f));
+ x += 10 + ui::text(list, f.mono, gib(parts[k]), x + 10, r.y + 150, 20, kInk.with_alpha(kMuted));
+ x += 36;
+ }
+ break;
+ }
+ case kSession: {
+ const int s = (int) L.uptime_s;
+ std::snprintf(text, sizeof(text), "%02d:%02d:%02d", s / 3600 % 100, s / 60 % 60, s % 60);
+ ui::text(list, f.mono, text, r.x + kPad, r.y + 104, 40, kInk);
+ std::snprintf(text, sizeof(text), "%llu tokens out, %llu in, %llu replies", (unsigned long long) L.tokens,
+ (unsigned long long) L.prompt_tokens, (unsigned long long) L.requests);
+ ui::text(list, f.regular, text, r.x + kPad, r.y + 150, 20, kInk.with_alpha(kMuted));
+ break;
+ }
+ case kContext: {
+ if (L.ctx == 0) {
+ ui::text(list, f.regular, "No model loaded", r.x + kPad, r.y + 104, 26, kInk.with_alpha(kFaint));
+ break;
+ }
+ const float share = L.ctx ? (float) L.ctx_used / (float) L.ctx : 0.0f;
+ const float w = number(list, f, L.ctx > 0, "%.0f", (double) L.ctx_used, r.x + kPad, r.y + 104, 40, kInk);
+ std::snprintf(text, sizeof(text), "of %uk tokens", L.ctx / 1024);
+ ui::text(list, f.regular, text, r.x + kPad + w + 12, r.y + 104, 22, kInk.with_alpha(kMuted));
+ bar(list, { r.x + kPad, r.y + 126, r.w - 2 * kPad, 10 }, share * up, kAmber);
+ std::snprintf(text, sizeof(text), "%s cache, %s GiB", L.kv_type.empty() ? kDash : L.kv_type.c_str(),
+ gib(L.kv_gib).c_str());
+ ui::text(list, f.regular, text, r.x + kPad, r.y + 164, 20, kInk.with_alpha(kMuted));
+ break;
+ }
+ default: { // kModel
+ const float width = r.w - 2 * kPad;
+ if (L.model_label.empty()) {
+ ui::text(list, f.semibold, "No model loaded", r.x + kPad, r.y + 104, 34, kInk);
+ ui::text(list, f.regular, "Cross opens the library", r.x + kPad, r.y + 138, 20, kInk.with_alpha(kMuted));
+ std::snprintf(text, sizeof(text), "%zu installed", L.models.size());
+ ui::text(list, f.regular, text, r.x + kPad, r.y + 352, 20, kInk.with_alpha(kFaint));
+ break;
+ }
+ const std::string name = L.model_label.empty() ? std::string("No model") : L.model_label;
+ ui::text(list, f.semibold, f.semibold.font->fit(L.preset.empty() ? name : L.preset, 34, width), r.x + kPad,
+ r.y + 104, 34, kInk);
+ ui::text(list, f.regular, f.regular.font->fit(name, 20, width), r.x + kPad, r.y + 138, 20,
+ kInk.with_alpha(kMuted));
+ const std::string rows[3][2] = {
+ { "SIZE", gib(L.model_gib) + " GiB" },
+ { "CONTEXT", L.ctx ? std::to_string(L.ctx / 1024) + "k, " + L.kv_type : std::string(kDash) },
+ { "ARCH", L.model_arch.empty() ? std::string(kDash) : L.model_arch },
+ };
+ for (int k = 0; k < 3; ++k) {
+ const float y = r.y + 196 + k * 46.0f;
+ caps(list, f, rows[k][0], r.x + kPad, y, 15, kInk.with_alpha(kMuted));
+ ui::text(list, f.mono, rows[k][1], r.x + r.w - kPad, y + 2, 22, kInk, gfx::Align::right);
+ }
+ std::snprintf(text, sizeof(text), "%zu installed", L.models.size());
+ ui::text(list, f.regular, text, r.x + kPad, r.y + 352, 20, kInk.with_alpha(kFaint));
+ break;
+ }
+ }
+}
+
+void Dashboard::draw_tile(gfx::DrawList & list, int tile) const {
+ const float in = tween::stagger(age_, tile, 0.06f, 0.5f);
+ const float lift = lift_[tile].value;
+ const Color accent = accent_of(tile);
+ Rect r = tile_rect(tile);
+ r.y += 28.0f * (1.0f - in);
+ float dx = 0, dy = 0;
+ if (tile == focus_ && !library_open_) {
+ const float nudge = ui::shake(refusal_.value, clock_, 10.0f, 9.0f);
+ dx = nudge * refusal_x_;
+ dy = nudge * refusal_y_;
+ }
+ list.push_opacity(in);
+ list.push_transform(1.0f + kLift * lift, r.cx(), r.cy(), dx, dy);
+ if (lift > 0.01f) {
+ list.shadow({ r.x, r.y + 16, r.w, r.h }, kRadius, 38, kBlack.with_alpha(0.5f * lift));
+ list.glow(r, kRadius, 26, accent.with_alpha((0.16f + 0.1f * ui::breathe(clock_)) * lift));
+ }
+ draw_panel(list, r, kRadius, lift, accent);
+ caps(list, fonts_, kSpec[tile].label, r.x + kPad, r.y + kLabelY, kLabelSize, accent);
+ draw_content(list, tile, r, age_ - 0.25f - 0.06f * tile);
+ list.pop_transform();
+ list.pop_opacity();
+}
+
+void Dashboard::draw_header(gfx::DrawList & list) const {
+ const float in = tween::stagger(age_, 0, 0.05f, 0.5f);
+ list.push_opacity(in);
+ const float y = 124 - 10.0f * (1.0f - in);
+ const float w = ui::text(list, fonts_.display, "PS5LM", kGridX - 3, y, 56, kInk);
+ std::string sub = "Chat at " + live_.address;
+ ui::text(list, fonts_.regular, sub, kGridX + w + 24, y, 24, kInk.with_alpha(kMuted));
+
+ const char * word = state_word(live_.state);
+ const Color dot = state_color(live_.state);
+ const float chip_w = 70.0f + fonts_.semibold.measure(word, 17) + 3.0f * (float) std::char_traits<char>::length(word);
+ const Rect chip{ kGridX + kGridW - chip_w, 82, chip_w, 48 };
+ list.bordered_rect(chip, 24, kPanelTop.with_alpha(0.9f), 1.5f, dot.with_alpha(0.4f));
+ const float beat = live_.state == ServerState::loading || live_.state == ServerState::generating ? ui::breathe(clock_, 1.2f) : 0.6f;
+ list.glow({ chip.x + 20, chip.cy() - 7, 14, 14 }, 7, 12, dot.with_alpha(0.7f * beat));
+ list.circle(chip.x + 27, chip.cy(), 7, dot);
+ caps(list, fonts_, word, chip.x + 48, chip.cy() + 6, 17, kInk);
+ list.pop_opacity();
+}
+
+void Dashboard::draw_library(gfx::DrawList & list, std::uint32_t glass) const {
+ const float t = library_.value;
+ if (t <= 0.01f) {
+ return;
+ }
+ const ui::Fonts & f = fonts_;
+ Rect panel = kLibrary;
+ panel.y += 30.0f * (1.0f - t);
+ list.push_opacity(t);
+ list.shadow({ panel.x, panel.y + 20, panel.w, panel.h }, 40, 50, kBlack.with_alpha(0.55f));
+ if (glass) {
+ list.glass(glass, panel, 40, Color::rgb(0xffffff));
+ }
+ draw_panel(list, panel, 40, 1.0f, kLime);
+ caps(list, f, "MODEL LIBRARY", panel.x + 56, panel.y + 60, 20, kLime);
+ ui::text(list, f.regular,
+ f.regular.font->fit("Settings fit the memory free now. Add models over FTP to /data/PS5LM/models, or import them from USB.",
+ 20, panel.w - 112),
+ panel.x + 56, panel.y + 98, 20, kInk.with_alpha(kMuted));
+
+ const float top = panel.y + 132;
+ const int rows = (int) live_.models.size();
+ if (rows == 0) {
+ ui::text(list, f.regular, "No models found yet", panel.cx(), panel.cy(), 26, kInk.with_alpha(kFaint), gfx::Align::center);
+ }
+ // Eight rows show; the list scrolls to keep the cursor among them.
+ const int first = std::max(0, library_cursor_ - 7);
+ const Rect hl{ panel.x + 36, top + (cursor_y_.value - (float) first) * kRowH, panel.w - 72, kRowH - 8 };
+ if (rows > 0) {
+ list.glow(hl, 18, 18, kLime.with_alpha(0.25f));
+ list.bordered_rect(hl, 18, kLime.with_alpha(0.1f), 2.0f, kLime.with_alpha(0.7f));
+ }
+ for (int i = first; i < rows && i < first + 8; ++i) {
+ const ModelRow & m = live_.models[(size_t) i];
+ const float y = top + (float) (i - first) * kRowH;
+ const float ink = m.fits ? 1.0f : 0.45f;
+ ui::text(list, f.semibold, f.semibold.font->fit(m.label, 24, 620), panel.x + 60, y + 32, 24, kInk.with_alpha(ink));
+ std::string sub = m.plan;
+ if (!m.preset.empty()) {
+ sub = m.preset + " preset \xC2\xB7 " + sub;
+ }
+ if (m.on_usb) {
+ sub = "On USB \xC2\xB7 " + sub;
+ }
+ ui::text(list, f.regular, f.regular.font->fit(sub, 18, 700), panel.x + 60, y + 58, 18, kInk.with_alpha(kMuted * ink));
+ char size[24];
+ std::snprintf(size, sizeof(size), "%.1f GiB", m.size_gib);
+ ui::text(list, f.mono, size, panel.x + panel.w - 260, y + 44, 22, kInk.with_alpha(ink), gfx::Align::right);
+ if (m.import_progress >= 0) {
+ bar(list, { panel.x + panel.w - 200, y + 34, 140, 10 }, m.import_progress, kAmber);
+ } else {
+ const char * tag = m.current ? "LOADED" : m.on_usb ? "IMPORT" : m.fits ? "LOAD" : "TOO BIG";
+ const Color tc = m.current ? kCyan : m.on_usb ? kAmber : m.fits ? kLime : kRose;
+ caps(list, f, tag, panel.x + panel.w - 60, y + 44, 15, tc, gfx::Align::right);
+ }
+ }
+ list.pop_opacity();
+}
+
+void Dashboard::draw_hints(DashboardFrame & frame) const {
+ const ui::GlyphStyle style = ui::GlyphStyle::dark();
+ if (library_.value > 0.5f) {
+ const bool on = library_cursor_ < (int) live_.models.size();
+ const ModelRow * m = on ? &live_.models[(size_t) library_cursor_] : nullptr;
+ const char * act = !m ? "Load" : m->current ? "Unload" : m->on_usb ? "Import" : "Load";
+ const ui::Hint hints[] = { { ui::Button::dpad, "Choose" }, { ui::Button::cross, act }, { ui::Button::circle, "Back" } };
+ ui::draw_hints(frame.overlay, fonts_, style, hints, 3, kGridX + kGridW, true);
+ } else {
+ const ui::Hint hints[] = { { ui::Button::dpad, "Move" }, { ui::Button::cross, "Models" } };
+ ui::draw_hints(frame.scene, fonts_, style, hints, focus_ == kModel ? 2 : 1, kGridX + kGridW, true);
+ }
+}
+
+void Dashboard::draw(DashboardFrame & frame) const {
+ frame.backdrop.mode = gfx::BackdropMode::dots;
+ frame.backdrop.colors[0] = Color::rgb(0x05070f);
+ frame.backdrop.colors[1] = Color::rgb(0x0b1122);
+ frame.backdrop.colors[2] = gfx::mix(Color::rgb(0x2b3d66), kBrand, 0.35f);
+ frame.backdrop.time = clock_;
+
+ gfx::DrawList & scene = frame.scene;
+ const float open = library_.value;
+ scene.push_transform(1.0f - 0.04f * open, 960, 564, 0, 0);
+ scene.push_opacity(1.0f - 0.6f * open);
+ for (int i = 0; i < kTiles; ++i) {
+ if (i != focus_) {
+ draw_tile(scene, i);
+ }
+ }
+ draw_tile(scene, focus_); // last: its glow and shadow sit on its neighbours
+ const float alpha = tween::stagger(age_, focus_, 0.06f, 0.5f) * (1.0f - tween::clamp01(open * 4.0f));
+ if (alpha > 0.01f) {
+ Rect ring = ring_.value();
+ const float nudge = ui::shake(refusal_.value, clock_, 10.0f, 9.0f);
+ ring.x += nudge * refusal_x_;
+ ring.y += nudge * refusal_y_;
+ scene.bordered_rect(ring, kRadius + 5.0f, kClear, 3.0f, accent_of(focus_).with_alpha(alpha));
+ }
+ scene.pop_opacity();
+ scene.pop_transform();
+ draw_header(scene);
+ if (open > 0.01f) {
+ frame.glass = true;
+ draw_library(frame.overlay, frame.glass_texture);
+ }
+ draw_hints(frame);
+}
+
+} // namespace ps5lm
diff --git a/ps5/app/dashboard.hpp b/ps5/app/dashboard.hpp
new file mode 100644
index 0000000..68a7ed0
--- /dev/null
+++ b/ps5/app/dashboard.hpp
@@ -0,0 +1,83 @@
+// PS5LM's screen: a bento grid of live tiles in the language of
+// ps5-homebrew-ui's "Pulse Dashboard" design (its palette, grid, panels,
+// focus ring and spatial navigation), showing the model, its speed, the GPU,
+// the CPU and the memory they share. Cross on the Model tile opens the model
+// library; picking one asks the app to load it.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include "stats.hpp"
+
+#include "core/input.hpp"
+#include "core/tween.hpp"
+#include "gfx/backdrop_spec.hpp"
+#include "gfx/draw_list.hpp"
+#include "ui/feedback.hpp"
+#include "ui/fonts.hpp"
+#include "ui/motion.hpp"
+
+#include <string>
+
+namespace ps5lm {
+
+struct DashboardFrame {
+ hui::gfx::BackdropSpec backdrop;
+ hui::gfx::DrawList scene;
+ hui::gfx::DrawList overlay;
+ bool glass = false;
+ std::uint32_t glass_texture = 0;
+};
+
+class Dashboard {
+ public:
+ static constexpr int kTiles = 8;
+
+ explicit Dashboard(const hui::ui::Fonts & fonts) : fonts_(fonts) { ring_.snap(ring_rect()); }
+
+ void set_live(const Live & live) { live_ = live; }
+ void update(const hui::InputFrame & input, float dt, hui::ui::Feedback & feedback);
+ void draw(DashboardFrame & frame) const;
+
+ // What the user asked for in the library, once: load a model, or import
+ // one from a USB drive. Empty path: nothing.
+ struct Request {
+ enum Kind { none, load, unload, import } kind = none;
+ std::string path;
+ };
+ Request take_request() {
+ Request r = request_;
+ request_ = {};
+ return r;
+ }
+
+ private:
+ hui::gfx::Rect ring_rect() const;
+ void draw_tile(hui::gfx::DrawList & list, int tile) const;
+ void draw_content(hui::gfx::DrawList & list, int tile, const hui::gfx::Rect & r, float t) const;
+ void draw_header(hui::gfx::DrawList & list) const;
+ void draw_library(hui::gfx::DrawList & list, std::uint32_t glass) const;
+ void draw_hints(DashboardFrame & frame) const;
+
+ const hui::ui::Fonts & fonts_;
+ Live live_;
+ Request request_;
+
+ float age_ = 0; // seconds since start: the entrance
+ float clock_ = 0; // idle motion
+ int focus_ = 0;
+ int came_from_ = -1;
+ hui::Direction came_by_ = hui::Direction::none;
+ hui::ui::SpringRect ring_;
+ hui::tween::Spring lift_[kTiles];
+ hui::ui::Pulse refusal_;
+ float refusal_x_ = 0, refusal_y_ = 0;
+
+ bool library_open_ = false;
+ int library_cursor_ = 0;
+ hui::tween::Spring library_; // 0 closed, 1 open
+ hui::tween::Spring cursor_y_; // the highlight's row
+ mutable char size_text_[64] = {};
+};
+
+} // namespace ps5lm
diff --git a/ps5/app/display.cpp b/ps5/app/display.cpp
new file mode 100644
index 0000000..bdbd85d
--- /dev/null
+++ b/ps5/app/display.cpp
@@ -0,0 +1,131 @@
+// The app's screen: an OpenGL 4.6 context on the TV through ps5-opengl's EGL,
+// opened in the order ps5-homebrew-ui's display_egl.cpp uses. The GL stack is
+// linked in as one object with only its API global (scripts/build-app.sh), so
+// its Mesa does not meet RADV's.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "display.hpp"
+
+#include "gfx/renderer.hpp"
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#define GL_GLEXT_PROTOTYPES 1
+#include <GL/glcorearb.h>
+#include <ps5_opengl_display.h>
+#include <ps5_opengl_display_modes.h>
+
+#include <atomic>
+#include <cstdint>
+#include <cstdio>
+
+extern "C" {
+// Both GPU drivers in the title initialise AGC; the first call does it and
+// every later one waits for it and gets the same answer (as ProsperoAI does
+// for its OpenGL UI and its own compute). Linked with --wrap=sceAgcInit.
+int __real_sceAgcInit(uint32_t flags);
+int sceKernelUsleep(uint32_t microseconds);
+
+int __wrap_sceAgcInit(uint32_t flags) {
+ static std::atomic<int> state{ 0 };
+ static int result = 0;
+ int expected = 0;
+ if (state.compare_exchange_strong(expected, 1, std::memory_order_acq_rel)) {
+ result = __real_sceAgcInit(flags);
+ state.store(2, std::memory_order_release);
+ } else {
+ while (state.load(std::memory_order_acquire) != 2) {
+ sceKernelUsleep(100);
+ }
+ }
+ return result;
+}
+
+// The GL runtime's thread_local context has an initialiser function the SDK
+// does not define; it has nothing to do (ProsperoAI's runtime_shims.c).
+void ps5lm_glapi_tls_context_init(void) __asm__("_ZTH23_mesa_glapi_tls_Context");
+void ps5lm_glapi_tls_context_init(void) {}
+}
+
+namespace ps5lm {
+
+namespace {
+
+constexpr EGLint kConfig[] = {
+ EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
+ EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8,
+ EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, EGL_NONE,
+};
+constexpr EGLint kContext[] = {
+ EGL_CONTEXT_MAJOR_VERSION_KHR, 4, EGL_CONTEXT_MINOR_VERSION_KHR, 6,
+ EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR, EGL_NONE,
+};
+
+EGLDisplay g_display = EGL_NO_DISPLAY;
+EGLSurface g_surface = EGL_NO_SURFACE;
+EGLContext g_context = EGL_NO_CONTEXT;
+
+bool fail(const char * what) {
+ std::printf("ps5lm-app: display: %s failed (egl 0x%04x)\n", what, (unsigned) eglGetError());
+ return false;
+}
+
+} // namespace
+
+bool Display::open(int width, int height) {
+ g_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ if (g_display == EGL_NO_DISPLAY) {
+ return fail("eglGetDisplay");
+ }
+ if (!eglSetDisplayModePS5(g_display, width, height)) {
+ fail("eglSetDisplayModePS5");
+ }
+ EGLint major = 0, minor = 0, count = 0;
+ EGLConfig config = nullptr;
+ if (!eglInitialize(g_display, &major, &minor)) {
+ return fail("eglInitialize");
+ }
+ if (!eglBindAPI(EGL_OPENGL_API)) {
+ return fail("eglBindAPI");
+ }
+ if (!eglChooseConfig(g_display, kConfig, &config, 1, &count) || count != 1) {
+ return fail("eglChooseConfig");
+ }
+ // The PS5 backend owns the window: native handle 0.
+ g_surface = eglCreateWindowSurface(g_display, config, (EGLNativeWindowType) 0, nullptr);
+ if (g_surface == EGL_NO_SURFACE) {
+ return fail("eglCreateWindowSurface");
+ }
+ g_context = eglCreateContext(g_display, config, EGL_NO_CONTEXT, kContext);
+ if (g_context == EGL_NO_CONTEXT) {
+ return fail("eglCreateContext");
+ }
+ if (!eglMakeCurrent(g_display, g_surface, g_surface, g_context)) {
+ return fail("eglMakeCurrent");
+ }
+ eglQuerySurface(g_display, g_surface, EGL_WIDTH, &width_);
+ eglQuerySurface(g_display, g_surface, EGL_HEIGHT, &height_);
+ eglSwapInterval(g_display, 1);
+ std::printf("ps5lm-app: display: egl %d.%d, %dx%d, %s\n", major, minor, width_, height_,
+ (const char *) glGetString(GL_RENDERER));
+ return true;
+}
+
+void Display::clear(float r, float g, float b) {
+ glViewport(0, 0, width_, height_);
+ glClearColor(r, g, b, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT);
+}
+
+void Display::present(hui::gfx::Renderer & renderer) {
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
+ renderer.present(0, width_, height_);
+}
+
+bool Display::swap() {
+ return eglSwapBuffers(g_display, g_surface) == EGL_TRUE;
+}
+
+} // namespace ps5lm
diff --git a/ps5/app/display.hpp b/ps5/app/display.hpp
new file mode 100644
index 0000000..c16cbf0
--- /dev/null
+++ b/ps5/app/display.hpp
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+namespace hui::gfx {
+class Renderer;
+}
+
+namespace ps5lm {
+
+// The TV, through ps5-opengl: one OpenGL 4.6 context, presented at vsync.
+class Display {
+ public:
+ bool open(int width, int height);
+ void clear(float r, float g, float b);
+ // Draws the renderer's frame into the TV's framebuffer.
+ void present(hui::gfx::Renderer & renderer);
+ bool swap();
+ int width() const { return width_; }
+ int height() const { return height_; }
+
+ private:
+ int width_ = 0;
+ int height_ = 0;
+};
+
+} // namespace ps5lm
diff --git a/ps5/app/main.cpp b/ps5/app/main.cpp
index 2cd2eae..764566c 100644
--- a/ps5/app/main.cpp
+++ b/ps5/app/main.cpp
@@ -1,38 +1,69 @@
-// PS5LM native app: llama-server on the PS5's GPU through RADV (ggml-vulkan).
+// PS5LM native app: llama-server on the PS5's GPU through RADV (ggml-vulkan),
+// with a dashboard on the TV drawn by ps5-homebrew-ui through ps5-opengl.
//
// A title gets the console's direct memory (12 GiB) where a payload gets none,
// and RADV turns it into Vulkan device memory, so the whole model sits on the
-// GPU. The app runs llama-server on port 8081, logs to /data/PS5LM/app.log and
-// takes its arguments from /data/PS5LM/app-args.txt, one per line (FTP lets
-// them change without a rebuild).
+// GPU. llama-server runs on its own thread on port 8081 (chat page and OpenAI
+// API); the main thread draws the dashboard, reads the controller and plays
+// the interface sounds. Models come from /data/PS5LM/models and USB drives; the
+// planner (model_plan.cpp) picks each one's context and cache for the memory
+// free. /data/PS5LM/app-args.txt, one argument per line, overrides all of it.
+// The app logs to /data/PS5LM/app.log and llama.log.
//
// SPDX-License-Identifier: GPL-3.0-or-later
+#include <pthread.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
+#include <cstring>
#include <exception>
#include <fstream>
+#include <mutex>
+#include <span>
#include <string>
+#include <thread>
#include <vector>
#include <vulkan/vulkan_core.h>
+#include <GL/glcorearb.h>
+
+#include "dashboard.hpp"
+#include "display.hpp"
#include "ggml-backend.h"
+#include "model_plan.hpp"
+#include "stats.hpp"
+
+#include <ps5platform/heap.h>
+#include <ps5platform/kernel.h>
+
+#include "audio/mixer.hpp"
+#include "core/input.hpp"
+#include "core/save_file.hpp"
+#include "gfx/renderer.hpp"
+#include "platform/ps5/audio_out.hpp"
+#include "platform/ps5/pad.hpp"
+#include "platform/ps5/system.hpp"
+#include "ui/fonts.hpp"
-int llama_server(int argc, char ** argv);
+int llama_server(int argc, char ** argv);
+void llama_server_terminate();
-// No Vulkan loader on the console: RADV is linked in whole, and ggml-vulkan's
-// dispatcher starts from vkGetInstanceProcAddr, which the ICD entry point
-// answers for every command.
+// ---- Vulkan without a loader ----------------------------------------------------
+// RADV is linked in whole, and ggml-vulkan's dispatcher starts from
+// vkGetInstanceProcAddr, which the ICD entry point answers for every command.
+// The few commands ggml calls by name go to Mesa's common implementations,
+// which dispatch through the object's own table.
extern "C" PFN_vkVoidFunction vk_icdGetInstanceProcAddr(VkInstance instance, const char * name);
extern "C" PFN_vkVoidFunction vkGetInstanceProcAddr(VkInstance instance, const char * name) {
return vk_icdGetInstanceProcAddr(instance, name);
}
-
-// The few commands ggml-vulkan calls by name rather than through its
-// dispatcher go to Mesa's common implementations, which dispatch through the
-// object's own table.
extern "C" PFN_vkVoidFunction vk_common_GetDeviceProcAddr(VkDevice device, const char * name);
extern "C" void vk_common_GetPhysicalDeviceFeatures2(VkPhysicalDevice dev, VkPhysicalDeviceFeatures2 * features);
extern "C" void vk_common_CmdCopyBuffer(VkCommandBuffer cmd, VkBuffer src, VkBuffer dst, uint32_t n,
@@ -52,10 +83,25 @@ extern "C" {
int sceSystemServiceHideSplashScreen(void);
int sceKernelSendNotificationRequest(uint32_t device, void * request, size_t size, int blocking);
int sceKernelUsleep(uint32_t microseconds);
+int sceSystemServiceLoadExec(const char * path, const char * const * args);
}
-// A system notification on the TV: the app draws nothing itself.
-static void notify(const char * text) {
+namespace {
+
+using namespace ps5lm;
+
+constexpr const char * kLog = "/data/PS5LM/app.log";
+constexpr const char * kArgs = "/data/PS5LM/app-args.txt";
+constexpr const char * kChoice = "/data/PS5LM/model.txt";
+constexpr const char * kQuit = "/data/PS5LM/quit";
+constexpr const char * kModels = "/data/PS5LM/models";
+constexpr const char * kAssets = "/app0/assets";
+constexpr int kPort = 8081;
+const std::vector<std::string> kModelDirs = { kModels, "/mnt/usb0/PS5LM/models", "/mnt/usb1/PS5LM/models",
+ "/mnt/usb0", "/mnt/usb1" };
+
+// A system notification on the TV.
+void notify(const char * text) {
struct {
uint8_t reserved[45];
char message[3075];
@@ -64,86 +110,502 @@ static void notify(const char * text) {
sceKernelSendNotificationRequest(0, &request, sizeof(request), 0);
}
-static const char * k_log = "/data/PS5LM/app.log";
-static const char * k_args = "/data/PS5LM/app-args.txt";
+// A quit request from the PC (scripts/ps5lm-app.sh close): the app ends
+// itself the way the system expects; killing a title that is rendering has
+// been followed by lost consoles (ps5-homebrew-ui's AGENTS.md).
+void quit_if_asked() {
+ if (access(kQuit, F_OK) != 0) {
+ return;
+ }
+ unlink(kQuit);
+ std::printf("ps5lm-app: quit requested\n");
+ std::fflush(nullptr);
+ sceSystemServiceLoadExec("exit", nullptr);
+}
+
+// The screen as a 24-bit BMP (glReadPixels of the TV's framebuffer), for
+// checking the dashboard from the PC.
+bool save_screen(const char * path, int width, int height) {
+ std::vector<unsigned char> pixels((size_t) width * height * 4);
+ glBindFramebuffer(GL_FRAMEBUFFER, 0);
+ glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
+ const uint32_t row = ((uint32_t) width * 3 + 3) & ~3u;
+ const uint32_t size = 54 + row * (uint32_t) height;
+ unsigned char header[54] = { 'B', 'M' };
+ auto put = [&](int at, uint32_t v) {
+ for (int i = 0; i < 4; ++i) {
+ header[at + i] = (unsigned char) (v >> (8 * i));
+ }
+ };
+ put(2, size);
+ put(10, 54);
+ put(14, 40);
+ put(18, (uint32_t) width);
+ put(22, (uint32_t) height);
+ header[26] = 1;
+ header[28] = 24;
+ put(34, size - 54);
+ FILE * f = std::fopen(path, "wb");
+ if (!f) {
+ return false;
+ }
+ bool ok = std::fwrite(header, 1, sizeof(header), f) == sizeof(header);
+ std::vector<unsigned char> line(row);
+ for (int y = 0; ok && y < height; ++y) {
+ const unsigned char * in = pixels.data() + (size_t) y * width * 4;
+ for (int x = 0; x < width; ++x) {
+ line[(size_t) x * 3 + 0] = in[x * 4 + 2];
+ line[(size_t) x * 3 + 1] = in[x * 4 + 1];
+ line[(size_t) x * 3 + 2] = in[x * 4 + 0];
+ }
+ ok = std::fwrite(line.data(), 1, row, f) == row;
+ }
+ std::fclose(f);
+ return ok;
+}
+
+// A request file from the PC: its first line, and the file is consumed.
+std::string take_request_file(const char * path) {
+ if (access(path, F_OK) != 0) {
+ return {};
+ }
+ std::string line;
+ std::ifstream(path) >> line;
+ unlink(path);
+ return line.empty() ? std::string("-") : line;
+}
+
+// Where the memory is: the pool's free part, the CPU heap, and what ggml
+// says about each device (its GPU figure wraps once the unified heap is
+// over-committed; the raw numbers still show a change).
+void log_memory(const char * when) {
+ int64_t at = 0;
+ size_t avail = 0;
+ const int64_t pool = sceKernelGetDirectMemorySize();
+ sceKernelAvailableDirectMemorySize(0, pool, 2 * 1024 * 1024, &at, &avail);
+ struct ps5_heap_stats heap = {};
+ ps5_heap_stats(&heap);
+ std::printf("ps5lm-app: memory %s: pool free %.2f GiB, cpu heap %.2f GiB (peak %.2f)", when, avail / 1073741824.0,
+ heap.mapped_bytes / 1073741824.0, heap.peak_bytes / 1073741824.0);
+ for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
+ size_t free = 0, total = 0;
+ ggml_backend_dev_memory(ggml_backend_dev_get(i), &free, &total);
+ std::printf(", %s free %zu total %zu", ggml_backend_dev_name(ggml_backend_dev_get(i)), free, total);
+ }
+ std::printf("\n");
+}
-static std::vector<std::string> read_args() {
- std::vector<std::string> args;
- std::ifstream in(k_args);
+std::vector<std::string> read_lines(const char * path) {
+ std::vector<std::string> out;
+ std::ifstream in(path);
for (std::string line; std::getline(in, line);) {
while (!line.empty() && (line.back() == '\r' || line.back() == ' ')) {
line.pop_back();
}
if (!line.empty() && line[0] != '#') {
- args.push_back(line);
+ out.push_back(line);
}
}
- if (args.empty()) {
- args = { "-m", "/data/PS5LM/models/Qwen3.8-27B-UD-IQ2_XXS.gguf", "-ngl", "999", "-fit", "off",
- "-lm", "none", "-c", "4096", "-b", "256", "-ub", "64", "-np", "1",
- "--host", "0.0.0.0", "--port", "8081" };
+ return out;
+}
+
+bool is_usb(const std::string & path) { return path.rfind("/mnt/usb", 0) == 0; }
+
+std::string base_name(const std::string & path) {
+ std::string n = path.substr(path.find_last_of('/') + 1);
+ if (n.size() > 5 && n.compare(n.size() - 5, 5, ".gguf") == 0) {
+ n.resize(n.size() - 5);
+ }
+ return n;
+}
+
+// ---- the server thread ------------------------------------------------------------
+
+std::atomic<int> g_server_rc{ -1 }; // llama-server's exit code once it returns; -1 while it runs
+std::vector<std::string> g_server_args;
+
+void * run_server(void *) {
+ std::vector<char *> argv;
+ argv.push_back(const_cast<char *>("llama-server"));
+ for (auto & a : g_server_args) {
+ argv.push_back(a.data());
+ }
+ argv.push_back(nullptr);
+ int rc = 1;
+ try {
+ rc = llama_server((int) argv.size() - 1, argv.data());
+ } catch (const std::exception & e) {
+ std::printf("ps5lm-app: exception: %s\n", e.what());
+ } catch (...) {
+ std::printf("ps5lm-app: unknown exception\n");
}
- return args;
+ std::printf("ps5lm-app: llama_server returned %d\n", rc);
+ std::fflush(nullptr);
+ g_server_rc = rc == 0 ? 0 : 1;
+ return nullptr;
}
-int main(int, char **) {
- // The splash stays up until a title hides it; this one draws nothing, so
- // it hides it at once (as the boilerplate's headless examples do).
- (void)sceSystemServiceHideSplashScreen();
+// ---- models -------------------------------------------------------------------------
+
+struct Models {
+ double budget_gib = 0; // GPU memory free before any model
+ std::vector<ModelRow> rows;
+ std::string running; // path of the model llama-server has
+ pthread_t server{};
+ bool server_started = false;
+
+ // Import from USB, one at a time.
+ std::mutex import_mutex;
+ std::string importing; // source path
+ std::atomic<float> import_progress{ -1 };
+ std::atomic<bool> import_done{ false };
+
+ void scan(StatsCollector & stats) {
+ rows.clear();
+ for (const auto & path : find_models(kModelDirs)) {
+ const ModelInfo m = read_model_info(path);
+ ModelRow row;
+ row.file = path;
+ row.label = base_name(path);
+ row.on_usb = is_usb(path);
+ if (!m.ok) {
+ row.plan = m.error;
+ rows.push_back(row);
+ continue;
+ }
+ const Plan p = plan_model(m, budget_gib);
+ const Preset * pre = find_preset(m);
+ row.arch = m.arch;
+ row.plan = p.why;
+ row.fits = p.fits;
+ row.size_gib = m.file_bytes / 1073741824.0;
+ row.preset = pre ? pre->label : "";
+ row.current = path == running;
+ rows.push_back(row);
+ std::printf("ps5lm-app: model %s (%s): %s\n", m.file_name.c_str(), m.arch.c_str(), p.why.c_str());
+ }
+ publish(stats);
+ }
+
+ void publish(StatsCollector & stats) {
+ std::vector<ModelRow> out = rows;
+ std::lock_guard<std::mutex> lock(import_mutex);
+ for (auto & r : out) {
+ r.current = r.file == running;
+ if (r.file == importing) {
+ r.import_progress = import_progress;
+ }
+ }
+ stats.set_models(out);
+ }
+ // The arguments for a model: the planner's, or app-args.txt's verbatim.
+ std::vector<std::string> args_for(const std::string & path, Live & model_part) {
+ const ModelInfo m = read_model_info(path);
+ const Plan p = plan_model(m, budget_gib);
+ const Preset * pre = find_preset(m);
+ model_part.model_label = base_name(path);
+ model_part.model_arch = m.arch;
+ model_part.preset = pre ? pre->label : "";
+ model_part.model_gib = m.file_bytes / 1073741824.0;
+ model_part.ctx = p.ctx;
+ model_part.kv_type = p.kv_type;
+ model_part.kv_gib = p.kv_gib;
+ return p.fits ? p.args : std::vector<std::string>{};
+ }
+
+ void start(const std::string & path, std::vector<std::string> args, StatsCollector & stats, const Live & model_part) {
+ // llama.cpp's own log, written as it goes (it survives a crash), and
+ // the metrics the dashboard reads.
+ args.insert(args.begin(), { "--log-file", "/data/PS5LM/llama.log" });
+ if (std::find(args.begin(), args.end(), "--metrics") == args.end()) {
+ args.push_back("--metrics");
+ }
+ for (const auto & a : args) {
+ std::printf("ps5lm-app: arg %s\n", a.c_str());
+ }
+ g_server_args = args;
+ g_server_rc = -1;
+ running = path;
+ stats.set_model(model_part);
+ stats.set_state(ServerState::loading);
+ publish(stats);
+ pthread_attr_t attr;
+ pthread_attr_init(&attr);
+ pthread_attr_setstacksize(&attr, 16u << 20); // the console's default stack is small
+ pthread_create(&server, &attr, run_server, nullptr);
+ server_started = true;
+ notify("PS5LM: loading the model onto the GPU");
+ }
+
+ void stop() {
+ if (!server_started) {
+ return;
+ }
+ llama_server_terminate();
+ pthread_join(server, nullptr);
+ server_started = false;
+ running.clear();
+ log_memory("after unloading");
+ }
+
+ void begin_import(const std::string & from) {
+ {
+ std::lock_guard<std::mutex> lock(import_mutex);
+ if (!importing.empty()) {
+ return;
+ }
+ importing = from;
+ }
+ import_progress = 0;
+ import_done = false;
+ std::thread([this, from] {
+ const std::string to = std::string(kModels) + "/" + base_name(from) + ".gguf";
+ const std::string part = to + ".part";
+ FILE * in = std::fopen(from.c_str(), "rb");
+ FILE * out = in ? std::fopen(part.c_str(), "wb") : nullptr;
+ bool ok = in && out;
+ if (ok) {
+ std::fseek(in, 0, SEEK_END);
+ const double total = (double) std::ftell(in);
+ std::fseek(in, 0, SEEK_SET);
+ std::vector<char> buf(8u << 20);
+ double done = 0;
+ for (size_t n; ok && (n = std::fread(buf.data(), 1, buf.size(), in)) > 0;) {
+ ok = std::fwrite(buf.data(), 1, n, out) == n;
+ done += (double) n;
+ import_progress = (float) (done / total);
+ }
+ ok = ok && std::fflush(out) == 0 && fsync(fileno(out)) == 0;
+ }
+ if (in) std::fclose(in);
+ if (out) std::fclose(out);
+ ok = ok && std::rename(part.c_str(), to.c_str()) == 0;
+ if (!ok) {
+ std::remove(part.c_str());
+ }
+ std::printf("ps5lm-app: import %s: %s\n", from.c_str(), ok ? "done" : "failed");
+ notify(ok ? "PS5LM: model imported" : "PS5LM: import failed (space on /data?)");
+ std::lock_guard<std::mutex> lock(import_mutex);
+ importing.clear();
+ import_progress = -1;
+ import_done = true;
+ }).detach();
+ }
+};
+
+bool load_font(hui::gfx::Renderer & renderer, const char * name, hui::gfx::Font * font, hui::ui::FontRef * ref) {
+ std::string data;
+ const std::string path = std::string(kAssets) + "/fonts/" + name;
+ if (!hui::save::read_file(path, &data) || !font->load(data)) {
+ std::printf("ps5lm-app: font %s failed\n", name);
+ return false;
+ }
+ ref->font = font;
+ ref->texture = renderer.batch().create_font_texture(*font);
+ return true;
+}
+
+} // namespace
+
+int main(int, char **) {
// stdout and stderr to a file the PC can fetch over FTP.
- if (std::freopen(k_log, "w", stdout) != nullptr) {
+ if (std::freopen(kLog, "w", stdout) != nullptr) {
setvbuf(stdout, nullptr, _IOLBF, 0);
}
- if (std::freopen(k_log, "a", stderr) != nullptr) {
+ if (std::freopen(kLog, "a", stderr) != nullptr) {
setvbuf(stderr, nullptr, _IONBF, 0);
}
-
- // RADV splits an APU's memory into 2/3 device-local and 1/3 host heaps;
- // one device-local heap over the whole pool lets the model take nearly all
- // of the 12 GiB.
+ // One device-local heap over the whole pool (RADV splits an APU's 2/3 to
+ // 1/3), and a HOME for llama.cpp, which throws without one.
setenv("radv_enable_unified_heap_on_apu", "true", 1);
- // A title has no HOME, and llama.cpp throws when it looks for its cache
- // directory without one.
setenv("HOME", "/data/PS5LM", 0);
setenv("LLAMA_CACHE", "/data/PS5LM/cache", 0);
-
+ mkdir(kModels, 0777);
std::printf("ps5lm-app: start\n");
+
+ // The screen first: it takes from the same memory as the model.
+ Display display;
+ if (!display.open(1920, 1080)) {
+ std::printf("ps5lm-app: no display\n");
+ }
+ hui::gfx::Renderer renderer;
+ hui::gfx::Font regular, semibold, headline, mono;
+ hui::ui::Fonts fonts;
+ const bool ui_ok = renderer.init() && load_font(renderer, "inter-regular.huifont", &regular, &fonts.regular) &&
+ load_font(renderer, "inter-semibold.huifont", &semibold, &fonts.semibold) &&
+ load_font(renderer, "montserrat-medium.huifont", &headline, &fonts.display) &&
+ load_font(renderer, "dejavu-sans-mono.huifont", &mono, &fonts.mono);
+ fonts.pixel = fonts.mono;
+ fonts.hand = fonts.regular;
+ std::printf("ps5lm-app: dashboard %s\n", ui_ok ? "ready" : "failed");
+
+ Models models;
for (size_t i = 0; i < ggml_backend_dev_count(); i++) {
ggml_backend_dev_t dev = ggml_backend_dev_get(i);
size_t free = 0, total = 0;
ggml_backend_dev_memory(dev, &free, &total);
std::printf("ps5lm-app: device %zu %s (%s): %.2f / %.2f GiB free\n", i, ggml_backend_dev_name(dev),
ggml_backend_dev_description(dev), free / 1073741824.0, total / 1073741824.0);
+ // The PS5 is an APU: ggml calls its GPU integrated.
+ const auto type = ggml_backend_dev_type(dev);
+ if (type == GGML_BACKEND_DEVICE_TYPE_GPU || type == GGML_BACKEND_DEVICE_TYPE_IGPU) {
+ models.budget_gib = free / 1073741824.0;
+ }
}
- std::vector<std::string> args = read_args();
- // llama.cpp's own log, written as it goes: it survives a crash, where
- // its log thread's buffer would not.
- args.insert(args.begin(), { "--log-file", "/data/PS5LM/llama.log" });
- std::vector<char *> argv;
- argv.push_back(const_cast<char *>("llama-server"));
- for (auto & a : args) {
- std::printf("ps5lm-app: arg %s\n", a.c_str());
- argv.push_back(a.data());
- }
- argv.push_back(nullptr);
+ StatsCollector stats;
+ stats.start(kPort);
+ models.scan(stats);
- notify("PS5LM: loading the model onto the GPU, chat on port 8081 when ready");
- int rc = 1;
- try {
- rc = llama_server((int) argv.size() - 1, argv.data());
- } catch (const std::exception & e) {
- std::printf("ps5lm-app: exception: %s\n", e.what());
- } catch (...) {
- std::printf("ps5lm-app: unknown exception\n");
+ // Which model: app-args.txt verbatim, else model.txt (the library's last
+ // choice), else the largest that fits with at least 8k of context.
+ Live model_part;
+ std::vector<std::string> args = read_lines(kArgs);
+ std::string path;
+ if (!args.empty()) {
+ const auto m = std::find(args.begin(), args.end(), "-m");
+ path = m != args.end() && m + 1 != args.end() ? *(m + 1) : "";
+ model_part.model_label = base_name(path);
+ const ModelInfo info = read_model_info(path);
+ model_part.model_arch = info.arch;
+ model_part.model_gib = info.file_bytes / 1073741824.0;
+ std::printf("ps5lm-app: app-args.txt overrides the planner\n");
+ } else {
+ const auto chosen = read_lines(kChoice);
+ double best = -1;
+ // "none": the user unloaded the model; start without one.
+ for (const auto & r : chosen.empty() || chosen[0] != "none" ? models.rows : std::vector<ModelRow>{}) {
+ if (!r.fits || r.on_usb) {
+ continue;
+ }
+ if (!chosen.empty() && r.file == chosen[0]) {
+ path = r.file;
+ break;
+ }
+ if (r.size_gib > best) {
+ best = r.size_gib;
+ path = r.file;
+ }
+ }
+ if (!path.empty()) {
+ args = models.args_for(path, model_part);
+ }
}
- std::printf("ps5lm-app: llama_server returned %d\n", rc);
- std::fflush(nullptr);
- notify(rc == 0 ? "PS5LM: server stopped" : "PS5LM: server failed, see /data/PS5LM/llama.log");
- // A title must not exit on its own (exit() and _Exit end in SIGSYS); it
- // waits for the shell to close it.
- for (;;) {
- sceKernelUsleep(2000000);
+ log_memory("before loading");
+ if (!args.empty()) {
+ models.start(path, args, stats, model_part);
+ } else {
+ stats.set_state(ServerState::no_model);
+ notify("PS5LM: no model fits; copy a .gguf to /data/PS5LM/models or import one from USB");
+ }
+
+ // Input and sound, as ps5-homebrew-ui's own app does.
+ hui::ps5::Pad pad;
+ pad.open();
+ hui::InputTracker tracker;
+ hui::audio::Mixer mixer;
+ hui::ps5::AudioOut audio_out;
+ audio_out.start(mixer);
+ hui::audio::SoundBank sounds;
+ sounds.load(std::string(kAssets) + "/audio/sfx");
+
+ Dashboard dash(fonts);
+ DashboardFrame frame;
+ hui::ui::Feedback feedback;
+ hui::PadSample samples[64];
+ int64_t last = hui::sys::monotonic_us();
+ bool was_serving = false;
+ for (uint64_t n = 0;; ++n) {
+ const int64_t now = hui::sys::monotonic_us();
+ const float dt = n == 0 ? 1.0f / 60.0f : std::min(0.05f, (float) (now - last) / 1e6f);
+ last = now;
+
+ const size_t count = pad.read(samples);
+ const hui::InputFrame input = tracker.update(std::span<const hui::PadSample>(samples, count), (uint64_t) now);
+ Live live = stats.snapshot();
+ if (g_server_rc >= 0 && live.state != ServerState::failed && live.state != ServerState::no_model) {
+ stats.set_state(ServerState::failed);
+ notify("PS5LM: the server stopped, see /data/PS5LM/llama.log");
+ }
+ const bool serving = live.state == ServerState::ready || live.state == ServerState::generating;
+ if (serving && !was_serving) {
+ notify("PS5LM: ready, chat in a browser on this network");
+ }
+ was_serving = serving;
+ if (n % 30 == 0) {
+ models.publish(stats);
+ }
+ if (models.import_done.exchange(false)) {
+ models.scan(stats);
+ }
+ dash.set_live(live);
+ feedback.clear();
+ dash.update(input, dt, feedback);
+ for (const auto & cue : feedback.cues) {
+ sounds.play(mixer, cue.set == hui::audio::SoundSet::count ? hui::audio::SoundSet::glass : cue.set, cue);
+ }
+ if (feedback.rumble_strength > 0.0f) {
+ pad.rumble(feedback.rumble_strength, feedback.rumble_seconds);
+ }
+ pad.tick(dt);
+
+ Dashboard::Request req = dash.take_request();
+ // The same requests from the PC (scripts/ps5lm-app.sh load|import).
+ if (n % 60 == 30 && req.kind == Dashboard::Request::none) {
+ if (std::string p = take_request_file("/data/PS5LM/load"); !p.empty()) {
+ req = { Dashboard::Request::load, p };
+ } else if (!take_request_file("/data/PS5LM/unload").empty()) {
+ req = { Dashboard::Request::unload, "" };
+ } else if (std::string q = take_request_file("/data/PS5LM/import"); !q.empty()) {
+ req = { Dashboard::Request::import, q };
+ }
+ }
+ if (req.kind == Dashboard::Request::import) {
+ models.begin_import(req.path);
+ } else if (req.kind == Dashboard::Request::unload || req.kind == Dashboard::Request::load) {
+ // Unloading in place leaves part of the model's GPU memory held
+ // (about 5 GiB of a 6.8 GiB model, measured), so a model change
+ // restarts the app: the system replaces the process (LoadExec on
+ // our own eboot.bin, "Kill for LoadExec" in the log) and the new
+ // one starts from the whole pool, with model.txt saying what to load.
+ std::ofstream(kChoice) << (req.kind == Dashboard::Request::load ? req.path : std::string("none")) << "\n";
+ if (req.kind == Dashboard::Request::load) {
+ unlink(kArgs); // the library's choice replaces a manual override
+ }
+ notify(req.kind == Dashboard::Request::load ? "PS5LM: loading the new model" : "PS5LM: model unloaded");
+ std::printf("ps5lm-app: restarting for %s\n", req.kind == Dashboard::Request::load ? req.path.c_str() : "no model");
+ std::fflush(nullptr);
+ models.stop();
+ sceSystemServiceLoadExec("/app0/eboot.bin", nullptr);
+ }
+
+ if (ui_ok) {
+ frame.backdrop = {};
+ frame.scene.clear();
+ frame.overlay.clear();
+ frame.glass = false;
+ frame.glass_texture = renderer.glass_texture();
+ dash.draw(frame);
+ renderer.begin();
+ renderer.backdrop(frame.backdrop);
+ renderer.draw(frame.scene);
+ if (frame.glass) {
+ renderer.glass();
+ }
+ renderer.draw(frame.overlay);
+ display.present(renderer);
+ if (n % 60 == 45 && !take_request_file("/data/PS5LM/screenshot").empty()) {
+ const bool ok = save_screen("/data/PS5LM/screen.bmp", display.width(), display.height());
+ std::printf("ps5lm-app: screenshot %s\n", ok ? "saved" : "failed");
+ }
+ }
+ display.swap();
+ if (n == 0) {
+ sceSystemServiceHideSplashScreen(); // the first picture is up
+ }
+ if (n % 60 == 0) {
+ quit_if_asked();
+ }
}
}
diff --git a/ps5/app/model_plan.cpp b/ps5/app/model_plan.cpp
new file mode 100644
index 0000000..51b60e7
--- /dev/null
+++ b/ps5/app/model_plan.cpp
@@ -0,0 +1,321 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "model_plan.hpp"
+
+#include <dirent.h>
+
+#include <algorithm>
+#include <cctype>
+#include <cstdio>
+#include <cstring>
+#include <map>
+
+namespace ps5lm {
+
+namespace {
+
+constexpr double kGiB = 1073741824.0;
+
+// GGUF metadata value types.
+enum : uint32_t { U8, I8, U16, I16, U32, I32, F32, BOOL, STRING, ARRAY, U64, I64, F64 };
+
+struct Reader {
+ FILE * f;
+ bool bad = false;
+
+ template <typename T> T get() {
+ T v{};
+ if (std::fread(&v, sizeof(v), 1, f) != 1) {
+ bad = true;
+ }
+ return v;
+ }
+ std::string str() {
+ const uint64_t n = get<uint64_t>();
+ if (bad || n > (1u << 24)) {
+ bad = true;
+ return {};
+ }
+ std::string s(n, '\0');
+ if (n && std::fread(s.data(), 1, n, f) != n) {
+ bad = true;
+ }
+ return s;
+ }
+ void skip(uint64_t n) {
+ if (std::fseek(f, (long) n, SEEK_CUR) != 0) {
+ bad = true;
+ }
+ }
+};
+
+uint64_t scalar_size(uint32_t t) {
+ switch (t) {
+ case U8: case I8: case BOOL: return 1;
+ case U16: case I16: return 2;
+ case U32: case I32: case F32: return 4;
+ case U64: case I64: case F64: return 8;
+ default: return 0;
+ }
+}
+
+// A scalar as an integer; 0 for strings and arrays.
+uint64_t read_int(Reader & r, uint32_t t) {
+ switch (t) {
+ case U8: return r.get<uint8_t>();
+ case I8: return (uint64_t) r.get<int8_t>();
+ case U16: return r.get<uint16_t>();
+ case I16: return (uint64_t) r.get<int16_t>();
+ case U32: return r.get<uint32_t>();
+ case I32: return (uint64_t) r.get<int32_t>();
+ case U64: return r.get<uint64_t>();
+ case I64: return (uint64_t) r.get<int64_t>();
+ case BOOL: return r.get<uint8_t>();
+ case F32: return (uint64_t) r.get<float>();
+ case F64: return (uint64_t) r.get<double>();
+ default: return 0;
+ }
+}
+
+std::string lower(std::string s) {
+ for (auto & c : s) {
+ c = (char) std::tolower((unsigned char) c);
+ }
+ return s;
+}
+
+} // namespace
+
+ModelInfo read_model_info(const std::string & path) {
+ ModelInfo m;
+ m.path = path;
+ m.file_name = path.substr(path.find_last_of('/') + 1);
+ m.name = m.file_name;
+
+ FILE * f = std::fopen(path.c_str(), "rb");
+ if (!f) {
+ m.error = "cannot open";
+ return m;
+ }
+ Reader r{ f };
+ if (r.get<uint32_t>() != 0x46554747u) { // "GGUF"
+ std::fclose(f);
+ m.error = "not a GGUF file";
+ return m;
+ }
+ r.get<uint32_t>(); // version
+ r.get<uint64_t>(); // tensor count
+ const uint64_t n_kv = r.get<uint64_t>();
+
+ std::map<std::string, uint64_t> ints; // every scalar, and the largest element of numeric arrays
+ std::map<std::string, std::string> strs;
+ for (uint64_t i = 0; i < n_kv && !r.bad; i++) {
+ const std::string key = r.str();
+ const uint32_t t = r.get<uint32_t>();
+ if (t == STRING) {
+ strs[key] = r.str();
+ } else if (t == ARRAY) {
+ const uint32_t et = r.get<uint32_t>();
+ const uint64_t n = r.get<uint64_t>();
+ if (et == STRING) {
+ for (uint64_t j = 0; j < n && !r.bad; j++) {
+ r.skip(r.get<uint64_t>());
+ }
+ } else if (n <= 4096) { // per-layer values, such as head_count_kv
+ uint64_t mx = 0;
+ for (uint64_t j = 0; j < n; j++) {
+ mx = std::max(mx, read_int(r, et));
+ }
+ ints[key] = mx;
+ } else {
+ r.skip(n * scalar_size(et)); // vocabularies and the like
+ }
+ } else {
+ ints[key] = read_int(r, t);
+ }
+ }
+ std::fseek(f, 0, SEEK_END);
+ m.file_bytes = (uint64_t) std::ftell(f);
+ std::fclose(f);
+ if (r.bad) {
+ m.error = "metadata could not be read";
+ return m;
+ }
+
+ m.arch = strs["general.architecture"];
+ if (!strs["general.name"].empty()) {
+ m.name = strs["general.name"];
+ }
+ const std::string a = m.arch + ".";
+ auto num = [&](const std::string & k, uint64_t def = 0) {
+ auto it = ints.find(a + k);
+ return it == ints.end() ? def : it->second;
+ };
+ // block_count includes the MTP blocks some files carry (Qwen 3.8's
+ // UD-Q2_K_XL has 65); they are not part of the main stack.
+ m.n_layer = (uint32_t) (num("block_count") - num("nextn_predict_layers"));
+ const uint64_t n_head = num("attention.head_count");
+ m.n_head_kv = (uint32_t) num("attention.head_count_kv", n_head);
+ const uint64_t n_embd = num("embedding_length");
+ const uint64_t head_dim = n_head ? n_embd / n_head : 0;
+ m.key_length = (uint32_t) num("attention.key_length", head_dim);
+ m.value_length = (uint32_t) num("attention.value_length", head_dim);
+ m.full_attn_every = (uint32_t) std::max<uint64_t>(1, num("full_attention_interval", 1));
+ m.n_ctx_train = (uint32_t) num("context_length");
+ m.n_expert = (uint32_t) num("expert_count");
+
+ // Recurrent layers (Gated DeltaNet, Mamba): a fixed f32 state each,
+ // the recurrent matrix plus the convolution window.
+ const uint64_t inner = num("ssm.inner_size"), state = num("ssm.state_size");
+ if (inner && state) {
+ const uint64_t groups = num("ssm.group_count", 1), conv = num("ssm.conv_kernel", 4);
+ const uint64_t n_rec = m.n_layer - m.n_kv_layers();
+ m.state_bytes = n_rec * (inner * state + (conv - 1) * (inner + 2 * groups * state)) * 4;
+ }
+ m.ok = m.n_layer > 0;
+ if (!m.ok) {
+ m.error = "no layer count for architecture '" + m.arch + "'";
+ }
+ return m;
+}
+
+double kv_bytes_per_token(const ModelInfo & m, const std::string & type) {
+ const double per_elem = type == "f16" ? 2.0 : type == "q8_0" ? 34.0 / 32 : 18.0 / 32; // q4_0
+ return (double) m.n_kv_layers() * m.n_head_kv * (m.key_length + m.value_length) * per_elem;
+}
+
+// Sampling as each model's authors recommend it. Settings the planner works
+// out (context, cache type) are not here.
+const std::vector<Preset> & presets() {
+ static const std::vector<Preset> table = {
+ { "qwen3.8", "Qwen 3.8", "27B hybrid Gated DeltaNet; UD-Q2_K_XL holds 64k context on the GPU",
+ { "--temp", "0.6", "--top-p", "0.95", "--top-k", "20", "--min-p", "0" } },
+ { "qwen3.6", "Qwen 3.6", "MoE 35B-A3B or dense 27B; the MoE decodes fastest",
+ { "--temp", "0.6", "--top-p", "0.95", "--top-k", "20", "--min-p", "0" } },
+ { "qwen3.5", "Qwen 3.5", "hybrid like 3.8; 9B at Q4_K_M leaves room for long context",
+ { "--temp", "0.6", "--top-p", "0.95", "--top-k", "20", "--min-p", "0" } },
+ { "ornith", "Ornith 1.5", "Qwen 3.5 based 9B",
+ { "--temp", "0.6", "--top-p", "0.95", "--top-k", "20", "--min-p", "0" } },
+ { "gemma", "Gemma 4", "12B at Q3_K_M / IQ4_XS, E4B at Q4_0",
+ { "--temp", "1.0", "--top-p", "0.95", "--top-k", "64" } },
+ { "gpt-oss", "gpt-oss", "20B MoE, 3.6B active; MXFP4 is its only size, so context stays short",
+ { "--temp", "1.0", "--top-p", "1.0" }, 16384 },
+ { "granite", "Granite 4.2", "3B and 8B, fast and plain",
+ { "--temp", "0.0" } },
+ { "nemotron", "Nemotron 3 Nano", "hybrid Mamba, 1M context trained",
+ { "--temp", "0.6", "--top-p", "0.95" } },
+ { "mistral", "Mistral", "7B v0.3 and the Small line",
+ { "--temp", "0.15" } },
+ { "llama", "Llama 3", "8B fits at any 4-bit quant",
+ { "--temp", "0.6", "--top-p", "0.9" } },
+ };
+ return table;
+}
+
+const Preset * find_preset(const ModelInfo & m) {
+ const std::string hay = lower(m.file_name + " " + m.name + " " + m.arch);
+ for (const auto & p : presets()) {
+ if (hay.find(p.match) != std::string::npos) {
+ return &p;
+ }
+ }
+ return nullptr;
+}
+
+Plan plan_model(const ModelInfo & m, double budget_gib, uint32_t ctx_cap) {
+ Plan p;
+ const Preset * preset = find_preset(m);
+ if (preset && preset->max_ctx) {
+ ctx_cap = std::min(ctx_cap, preset->max_ctx);
+ }
+ if (m.n_ctx_train) {
+ ctx_cap = std::min(ctx_cap, m.n_ctx_train);
+ }
+ p.weights_gib = m.file_bytes / kGiB;
+ p.state_gib = m.state_bytes / kGiB;
+ // Compute buffers and the CPU side (the title heap shares the pool), plus
+ // one prompt checkpoint of the recurrent state. Calibrated on the console:
+ // Qwen3.8-27B UD-Q2_K_XL at 64k with a q4_0 cache ran with 11.38 GiB free.
+ p.reserve_gib = 0.75 + p.state_gib;
+
+ // The longest context first, and at each length the best cache that fits.
+ static const uint32_t ctxs[] = { 131072, 65536, 32768, 16384, 8192, 4096 };
+ static const char * types[] = { "f16", "q8_0", "q4_0" };
+ for (uint32_t ctx : ctxs) {
+ if (ctx > ctx_cap) {
+ continue;
+ }
+ for (const char * t : types) {
+ // f16 only where it is cheap: q8_0 costs nothing measurable in quality.
+ if (std::strcmp(t, "f16") == 0 && ctx > 8192) {
+ continue;
+ }
+ const double kv = kv_bytes_per_token(m, t) * ctx / kGiB;
+ const double total = p.weights_gib + kv + p.state_gib + p.reserve_gib;
+ if (total <= budget_gib) {
+ p.fits = true;
+ p.ctx = ctx;
+ p.kv_type = t;
+ p.kv_gib = kv;
+ p.total_gib = total;
+ break;
+ }
+ }
+ if (p.fits) {
+ break;
+ }
+ }
+
+ char why[160];
+ if (!p.fits) {
+ p.total_gib = p.weights_gib + p.state_gib + p.reserve_gib;
+ std::snprintf(why, sizeof(why), "needs %.1f GiB with a 4k context; %.1f GiB is free", p.total_gib, budget_gib);
+ p.why = why;
+ return p;
+ }
+ std::snprintf(why, sizeof(why), "%uk context, %s cache: %.1f of %.1f GiB", p.ctx / 1024, p.kv_type.c_str(),
+ p.total_gib, budget_gib);
+ p.why = why;
+
+ p.args = { "-m", m.path, "-ngl", "999", "-fit", "off", "-lm", "none",
+ "-c", std::to_string(p.ctx), "-fa", "on", "-ctk", p.kv_type, "-ctv", p.kv_type,
+ "-b", "256", "-ub", "64", "-np", "1",
+ // One prompt checkpoint and no RAM cache: llama-server's defaults
+ // (32 checkpoints, 8 GiB) are for a PC, and the CPU shares this pool.
+ "-ctxcp", "1", "-cram", "0",
+ "--host", "0.0.0.0", "--port", "8081" };
+ if (preset) {
+ p.args.insert(p.args.end(), preset->args.begin(), preset->args.end());
+ }
+ return p;
+}
+
+std::vector<std::string> find_models(const std::vector<std::string> & dirs) {
+ std::vector<std::string> out;
+ for (const auto & d : dirs) {
+ DIR * dir = opendir(d.c_str());
+ if (!dir) {
+ continue;
+ }
+ while (dirent * e = readdir(dir)) {
+ const std::string n = e->d_name;
+ if (n.size() < 6 || lower(n.substr(n.size() - 5)) != ".gguf" || n[0] == '.') {
+ continue;
+ }
+ // Split models: only the first part, which llama.cpp opens.
+ const auto split = n.find("-of-");
+ if (split != std::string::npos && n.find("-00001-of-") == std::string::npos) {
+ continue;
+ }
+ // Vision projectors are not models on their own.
+ if (lower(n).find("mmproj") != std::string::npos) {
+ continue;
+ }
+ out.push_back(d + "/" + n);
+ }
+ closedir(dir);
+ }
+ std::sort(out.begin(), out.end());
+ return out;
+}
+
+} // namespace ps5lm
diff --git a/ps5/app/model_plan.hpp b/ps5/app/model_plan.hpp
new file mode 100644
index 0000000..e25328a
--- /dev/null
+++ b/ps5/app/model_plan.hpp
@@ -0,0 +1,76 @@
+// Model plans for the PS5LM app: what is in a GGUF, and the llama-server
+// arguments that fit it on the console's GPU.
+//
+// A plan picks the longest context and the best KV cache type that fit the
+// device memory left after the weights, from the model's own metadata (only
+// attention layers keep a KV cache; hybrid models such as Qwen 3.5/3.8 keep
+// one in four, and their recurrent layers a fixed state). Known models add
+// their recommended sampling from the preset table.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include <cstdint>
+#include <string>
+#include <vector>
+
+namespace ps5lm {
+
+struct ModelInfo {
+ std::string path;
+ std::string file_name;
+ std::string name; // general.name, or the file name
+ std::string arch; // general.architecture
+ uint64_t file_bytes = 0;
+ uint32_t n_layer = 0;
+ uint32_t n_head_kv = 0;
+ uint32_t key_length = 0; // per KV head
+ uint32_t value_length = 0;
+ uint32_t full_attn_every = 1; // 1: every layer is attention
+ uint32_t n_ctx_train = 0;
+ uint32_t n_expert = 0; // 0: dense
+ uint64_t state_bytes = 0; // recurrent state, all layers (f32)
+ bool ok = false;
+ std::string error;
+
+ uint32_t n_kv_layers() const { return full_attn_every > 1 ? n_layer / full_attn_every : n_layer; }
+};
+
+// Reads the metadata of a GGUF file (not its tensors).
+ModelInfo read_model_info(const std::string & path);
+
+// Bytes of KV cache per token of context, for a cache type ("f16", "q8_0", "q4_0").
+double kv_bytes_per_token(const ModelInfo & m, const std::string & type);
+
+struct Preset {
+ const char * match; // case-insensitive substring of the file or model name
+ const char * label; // what the UI calls it
+ const char * note; // one line about it on a PS5
+ std::vector<std::string> args; // sampling and template arguments
+ uint32_t max_ctx = 0; // 0: the planner decides
+};
+
+const std::vector<Preset> & presets();
+const Preset * find_preset(const ModelInfo & m);
+
+struct Plan {
+ bool fits = false;
+ uint32_t ctx = 0;
+ std::string kv_type; // cache type for K and V
+ double weights_gib = 0;
+ double kv_gib = 0;
+ double state_gib = 0;
+ double reserve_gib = 0; // compute buffers and the CPU side
+ double total_gib = 0;
+ std::string why; // one line for the UI
+ std::vector<std::string> args; // full llama-server arguments
+};
+
+// budget_gib: device memory free before loading (the app reads it from ggml).
+Plan plan_model(const ModelInfo & m, double budget_gib, uint32_t ctx_cap = 65536);
+
+// Every .gguf in the given folders (not recursive), sorted by name. Split
+// files are listed by their first part only.
+std::vector<std::string> find_models(const std::vector<std::string> & dirs);
+
+} // namespace ps5lm
diff --git a/ps5/app/model_plan_test.cpp b/ps5/app/model_plan_test.cpp
new file mode 100644
index 0000000..425f0fc
--- /dev/null
+++ b/ps5/app/model_plan_test.cpp
@@ -0,0 +1,50 @@
+// Host check for model_plan.cpp against real GGUF files:
+// c++ -std=c++17 -O1 ps5/app/model_plan_test.cpp ps5/app/model_plan.cpp -o build/model_plan_test
+// build/model_plan_test models
+// Uses the budget the console reported (11.38 GiB free) and the two
+// configurations measured there.
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "model_plan.hpp"
+
+#include <cassert>
+#include <cstdio>
+#include <string>
+
+int main(int argc, char ** argv) {
+ const std::string dir = argc > 1 ? argv[1] : "models";
+ const double budget = 11.38;
+ bool saw_q2k = false, saw_iq2xxs = false;
+ for (const auto & path : ps5lm::find_models({ dir })) {
+ const ps5lm::ModelInfo m = ps5lm::read_model_info(path);
+ assert(m.ok);
+ const ps5lm::Plan p = ps5lm::plan_model(m, budget);
+ const ps5lm::Preset * pre = ps5lm::find_preset(m);
+ std::printf("%-34s %s L%u kvL%u kvh%u k%u v%u state %.0f MiB | %s | %s\n", m.file_name.c_str(),
+ m.arch.c_str(), m.n_layer, m.n_kv_layers(), m.n_head_kv, m.key_length, m.value_length,
+ m.state_bytes / 1048576.0, p.why.c_str(), pre ? pre->label : "-");
+ if (m.arch == "qwen35") {
+ // Qwen 3.8 27B: 16 of 64 layers keep a KV cache, 4 heads of 256.
+ assert(m.n_layer == 64 && m.n_kv_layers() == 16 && m.n_head_kv == 4 && m.key_length == 256);
+ assert(kv_bytes_per_token(m, "f16") == 65536.0);
+ assert(m.state_bytes > 140.0 * 1048576 && m.state_bytes < 170.0 * 1048576);
+ assert(pre && std::string(pre->label) == "Qwen 3.8");
+ }
+ if (m.file_name.find("UD-Q2_K_XL") != std::string::npos) {
+ // Ran on the console: 64k with a q4_0 cache.
+ assert(p.fits && p.ctx == 65536 && p.kv_type == "q4_0");
+ saw_q2k = true;
+ }
+ if (m.file_name.find("UD-IQ2_XXS") != std::string::npos) {
+ // 4k at f16 ran; 64k needs at most q8_0 at this size.
+ assert(p.fits && p.ctx == 65536 && p.kv_type == "q8_0");
+ saw_iq2xxs = true;
+ }
+ }
+ assert(saw_q2k && saw_iq2xxs);
+ // Nothing fits in too little memory.
+ ps5lm::ModelInfo big;
+ big.ok = true; big.file_bytes = 20ull << 30; big.n_layer = 32; big.n_head_kv = 8;
+ big.key_length = big.value_length = 128;
+ assert(!ps5lm::plan_model(big, budget).fits);
+ std::puts("model_plan_test: ok");
+}
diff --git a/ps5/app/preview_main.cpp b/ps5/app/preview_main.cpp
new file mode 100644
index 0000000..10712ab
--- /dev/null
+++ b/ps5/app/preview_main.cpp
@@ -0,0 +1,237 @@
+// PC preview of the PS5LM dashboard: the same drawing code as the console,
+// rendered off-screen through Mesa's surfaceless EGL to PNG files, with
+// sample data shaped like a real run (Qwen 3.8 27B at 64k on the GPU).
+//
+// scripts/preview-app-ui.sh writes build/app-preview/*.png
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "dashboard.hpp"
+
+#include "core/save_file.hpp"
+#include "gfx/gl_program.hpp"
+#include "gfx/renderer.hpp"
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+#include <GL/glcorearb.h>
+
+#define STB_IMAGE_WRITE_IMPLEMENTATION
+#include "stb/stb_image_write.h"
+
+#include <cmath>
+#include <cstdio>
+#include <string>
+#include <vector>
+
+namespace {
+
+bool open_context() {
+ auto get_display = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
+ EGLDisplay display = get_display ? get_display(EGL_PLATFORM_SURFACELESS_MESA, EGL_DEFAULT_DISPLAY, nullptr)
+ : eglGetDisplay(EGL_DEFAULT_DISPLAY);
+ EGLint major = 0, minor = 0;
+ if (display == EGL_NO_DISPLAY || !eglInitialize(display, &major, &minor) || !eglBindAPI(EGL_OPENGL_API)) {
+ return false;
+ }
+ const EGLint attrs[] = { EGL_CONTEXT_MAJOR_VERSION, 4, EGL_CONTEXT_MINOR_VERSION, 5,
+ EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT, EGL_NONE };
+ EGLContext context = eglCreateContext(display, EGL_NO_CONFIG_KHR, EGL_NO_CONTEXT, attrs);
+ return context != EGL_NO_CONTEXT && eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, context);
+}
+
+bool load_font(hui::gfx::Renderer & r, const std::string & path, hui::gfx::Font * font, hui::ui::FontRef * ref) {
+ std::string data;
+ if (!hui::save::read_file(path, &data) || !font->load(data)) {
+ std::fprintf(stderr, "cannot load font %s\n", path.c_str());
+ return false;
+ }
+ ref->font = font;
+ ref->texture = r.batch().create_font_texture(*font);
+ return true;
+}
+
+// A run as the console measured it: Qwen3.8-27B UD-Q2_K_XL, 64k, q4_0 cache.
+float g_loading = -1; // >= 0: draw the loading state at this progress
+
+ps5lm::Live sample(float seconds, bool generating) {
+ ps5lm::Live L;
+ L.state = generating ? ps5lm::ServerState::generating : ps5lm::ServerState::ready;
+ L.address = "http://192.168.1.19:8081";
+ L.model_label = "Qwen3.8-27B-UD-Q2_K_XL";
+ L.model_arch = "qwen35";
+ L.preset = "Qwen 3.8";
+ L.model_gib = 9.15;
+ L.ctx = 65536;
+ L.kv_type = "q4_0";
+ L.kv_gib = 1.13;
+ L.pool_gib = 11.44;
+ L.free_gib = 0.31;
+ L.heap_gib = 0.76;
+ L.cpus = 13;
+ L.uptime_s = 754 + seconds;
+ L.tokens = 4182;
+ L.prompt_tokens = 9120;
+ L.requests = 14;
+ L.ctx_used = 2318;
+ L.prompt_tps = 45.4f;
+ for (int i = 0; i < 120; ++i) {
+ const bool on = (i / 14) % 3 != 0 || i > 104;
+ const float tps = on ? 20.4f + 0.6f * std::sin(i * 0.7f) : 0.0f;
+ L.gen_history.push_back(tps);
+ L.gpu_history.push_back(on ? 0.92f + 0.05f * std::sin(i * 0.3f) : 0.03f);
+ L.cpu_history.push_back(on ? 0.11f + 0.03f * std::sin(i * 0.5f) : 0.02f);
+ }
+ L.gen_tps = generating ? 20.7f : 0.0f;
+ L.gpu_busy = generating ? 0.94f : 0.02f;
+ L.cpu_use = generating ? 0.12f : 0.02f;
+ const char * files[] = { "Qwen3.8-27B-UD-IQ1_S", "Qwen3.8-27B-UD-IQ2_S", "Qwen3.8-27B-UD-IQ2_XXS",
+ "Qwen3.8-27B-UD-Q2_K_XL", "gpt-oss-20b-MXFP4" };
+ const char * plans[] = { "64k context, q8_0 cache: 8.9 of 11.0 GiB", "64k context, q8_0 cache: 11.0 of 11.0 GiB",
+ "64k context, q8_0 cache: 9.9 of 11.0 GiB", "64k context, q4_0 cache: 11.3 of 11.4 GiB",
+ "needs 11.6 GiB with a 4k context; 11.0 GiB is free" };
+ const double sizes[] = { 5.77, 7.80, 6.77, 9.15, 11.3 };
+ for (int i = 0; i < 5; ++i) {
+ ps5lm::ModelRow m;
+ m.file = std::string("/data/PS5LM/models/") + files[i] + ".gguf";
+ m.label = files[i];
+ m.plan = plans[i];
+ m.size_gib = sizes[i];
+ m.fits = i < 4;
+ m.current = i == 3;
+ m.preset = i < 4 ? "Qwen 3.8" : "gpt-oss";
+ L.models.push_back(m);
+ }
+ ps5lm::ModelRow usb;
+ usb.file = "/mnt/usb0/PS5LM/models/Ornith-1.5-9B-Q4_K_M.gguf";
+ usb.label = "Ornith-1.5-9B-Q4_K_M";
+ usb.plan = "64k context, q8_0 cache: 8.6 of 11.0 GiB";
+ usb.preset = "Ornith 1.5";
+ usb.size_gib = 5.38;
+ usb.fits = usb.on_usb = true;
+ L.models.push_back(usb);
+ usb.file = "/mnt/usb0/PS5LM/models/gemma-4-12b-it-Q3_K_M.gguf";
+ usb.label = "gemma-4-12b-it-Q3_K_M";
+ usb.plan = "64k context, q8_0 cache: 9.1 of 11.0 GiB";
+ usb.preset = "Gemma 4";
+ usb.size_gib = 5.30;
+ usb.import_progress = 0.42f;
+ L.models.push_back(usb);
+ if (g_loading >= 0) {
+ L.state = ps5lm::ServerState::loading;
+ L.load_progress = g_loading;
+ L.free_gib = 11.03 - g_loading * (L.model_gib + L.kv_gib);
+ L.gen_tps = 0;
+ L.gpu_busy = 0;
+ }
+ return L;
+}
+
+} // namespace
+
+int main(int argc, char ** argv) {
+ if (argc < 3) {
+ std::fprintf(stderr, "usage: %s <kit assets dir> <output dir>\n", argv[0]);
+ return 2;
+ }
+ const std::string assets = argv[1], out = argv[2];
+ const int W = 1920, H = 1080;
+ if (!open_context()) {
+ std::fprintf(stderr, "no surfaceless EGL OpenGL 4.5 context\n");
+ return 1;
+ }
+ hui::gfx::set_glsl_prefix("#version 450 core\n");
+ hui::gfx::Renderer renderer;
+ hui::gfx::Font regular, semibold, display, mono, pixel, hand;
+ hui::ui::Fonts fonts;
+ if (!renderer.init() || !load_font(renderer, assets + "/fonts/inter-regular.huifont", &regular, &fonts.regular) ||
+ !load_font(renderer, assets + "/fonts/inter-semibold.huifont", &semibold, &fonts.semibold) ||
+ !load_font(renderer, assets + "/fonts/montserrat-medium.huifont", &display, &fonts.display) ||
+ !load_font(renderer, assets + "/fonts/dejavu-sans-mono.huifont", &mono, &fonts.mono)) {
+ return 1;
+ }
+ fonts.pixel = fonts.mono;
+ fonts.hand = fonts.regular;
+
+ GLuint fb = 0, color = 0;
+ glGenFramebuffers(1, &fb);
+ glGenRenderbuffers(1, &color);
+ glBindRenderbuffer(GL_RENDERBUFFER, color);
+ glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, W, H);
+ glBindFramebuffer(GL_FRAMEBUFFER, fb);
+ glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
+
+ hui::save::ensure_directory(out);
+ ps5lm::Dashboard dash(fonts);
+ std::vector<unsigned char> pixels((size_t) W * H * 4);
+ stbi_flip_vertically_on_write(1);
+ hui::ui::Feedback feedback;
+ float t = 0;
+ bool ok = true;
+
+ const auto step = [&](int frames, hui::InputFrame input, bool generating) {
+ for (int i = 0; i < frames; ++i) {
+ dash.set_live(sample(t, generating));
+ feedback.clear();
+ dash.update(i == 0 ? input : hui::InputFrame{}, 1.0f / 60.0f, feedback);
+ t += 1.0f / 60.0f;
+ }
+ };
+ const auto shot = [&](const char * name) {
+ ps5lm::DashboardFrame frame;
+ frame.glass_texture = renderer.glass_texture();
+ dash.draw(frame);
+ renderer.begin();
+ renderer.backdrop(frame.backdrop);
+ renderer.draw(frame.scene);
+ if (frame.glass) {
+ renderer.glass();
+ }
+ renderer.draw(frame.overlay);
+ glBindFramebuffer(GL_FRAMEBUFFER, fb);
+ glClearColor(0, 0, 0, 1);
+ glClear(GL_COLOR_BUFFER_BIT);
+ renderer.present(fb, W, H);
+ glBindFramebuffer(GL_FRAMEBUFFER, fb);
+ glReadPixels(0, 0, W, H, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
+ for (size_t i = 3; i < pixels.size(); i += 4) {
+ pixels[i] = 255;
+ }
+ const std::string path = out + "/" + name + ".png";
+ ok = stbi_write_png(path.c_str(), W, H, 4, pixels.data(), W * 4) != 0 && ok;
+ std::fprintf(stderr, "wrote %s: %zu shapes, %zu draw calls, GL error 0x%x\n", path.c_str(),
+ renderer.last_instances(), renderer.last_draw_calls(), glGetError());
+ };
+ auto press = [](hui::Action a) {
+ hui::InputFrame in;
+ in.connected = true;
+ in.pressed = hui::action_bit(a);
+ return in;
+ };
+ auto nav = [](hui::Direction d) {
+ hui::InputFrame in;
+ in.connected = true;
+ in.nav = d;
+ return in;
+ };
+
+ g_loading = 0.45f;
+ step(90, {}, false);
+ shot("00-loading");
+ g_loading = -1;
+ step(120, {}, true);
+ shot("01-dashboard");
+ // To the Model tile: right twice along the top, then down.
+ step(20, nav(hui::Direction::right), true);
+ step(20, nav(hui::Direction::right), true);
+ step(40, nav(hui::Direction::down), false);
+ shot("02-model-focus");
+ step(60, press(hui::Action::confirm), false);
+ shot("03-library");
+ step(30, nav(hui::Direction::up), false);
+ shot("04-library-choose");
+ for (int i = 0; i < 3; ++i) {
+ step(12, nav(hui::Direction::down), false);
+ }
+ shot("05-library-usb");
+ return ok ? 0 : 1;
+}
diff --git a/ps5/app/stats.cpp b/ps5/app/stats.cpp
new file mode 100644
index 0000000..3a43250
--- /dev/null
+++ b/ps5/app/stats.cpp
@@ -0,0 +1,259 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "stats.hpp"
+
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <sys/resource.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <chrono>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <thread>
+
+#ifdef __PROSPERO__
+#include <ps5platform/heap.h>
+#include <ps5platform/kernel.h>
+extern "C" int scePthreadGetaffinity(void * thread, uint64_t * mask);
+extern "C" void * scePthreadSelf(void);
+#endif
+
+namespace ps5lm {
+
+namespace {
+
+constexpr size_t kHistory = 120; // seconds of history in the charts
+constexpr double kGiB = 1073741824.0;
+
+void push(std::vector<float> & v, float x) {
+ v.push_back(x);
+ if (v.size() > kHistory) {
+ v.erase(v.begin());
+ }
+}
+
+double now_s() {
+ return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
+}
+
+double cpu_seconds() {
+ rusage u = {};
+ getrusage(RUSAGE_SELF, &u);
+ return u.ru_utime.tv_sec + u.ru_stime.tv_sec + (u.ru_utime.tv_usec + u.ru_stime.tv_usec) / 1e6;
+}
+
+int usable_cpus() {
+#ifdef __PROSPERO__
+ // The CPUs the title may run on (the app is not given all sixteen).
+ uint64_t mask = 0;
+ if (scePthreadGetaffinity(scePthreadSelf(), &mask) == 0 && mask != 0) {
+ return __builtin_popcountll(mask);
+ }
+#endif
+ const unsigned n = std::thread::hardware_concurrency();
+ return n ? (int) n : 1;
+}
+
+// A JSON integer after "key": in the first slot of /slots (one slot runs).
+long json_int(const std::string & text, const char * key) {
+ const std::string k = std::string("\"") + key + "\":";
+ const size_t at = text.find(k);
+ return at == std::string::npos ? 0 : std::strtol(text.c_str() + at + k.size(), nullptr, 10);
+}
+
+} // namespace
+
+std::string http_get(int port, const char * path, int timeout_s) {
+ const int fd = socket(AF_INET, SOCK_STREAM, 0);
+ if (fd < 0) {
+ return {};
+ }
+ timeval tv = { timeout_s, 0 };
+ setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
+ setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
+ sockaddr_in addr = {};
+#ifdef __FreeBSD__
+ addr.sin_len = sizeof(addr);
+#endif
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons((uint16_t) port);
+ addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ std::string out;
+ if (connect(fd, (sockaddr *) &addr, sizeof(addr)) == 0) {
+ char req[160];
+ const int n = std::snprintf(req, sizeof(req), "GET %s HTTP/1.0\r\nHost: localhost\r\n\r\n", path);
+ send(fd, req, (size_t) n, 0);
+ char buf[4096];
+ for (ssize_t got; (got = recv(fd, buf, sizeof(buf), 0)) > 0;) {
+ out.append(buf, (size_t) got);
+ if (out.size() > (1u << 20)) {
+ break;
+ }
+ }
+ }
+ close(fd);
+ const size_t body = out.find("\r\n\r\n");
+ return body == std::string::npos ? std::string() : out.substr(body + 4);
+}
+
+double metric(const std::string & text, const char * name) {
+ const std::string key = std::string("\nllamacpp:") + name + " ";
+ const size_t at = text.find(key);
+ return at == std::string::npos ? 0.0 : std::strtod(text.c_str() + at + key.size(), nullptr);
+}
+
+std::string local_address() {
+ const int fd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (fd < 0) {
+ return {};
+ }
+ sockaddr_in to = {};
+#ifdef __FreeBSD__
+ to.sin_len = sizeof(to);
+#endif
+ to.sin_family = AF_INET;
+ to.sin_port = htons(53);
+ inet_pton(AF_INET, "1.1.1.1", &to.sin_addr);
+ std::string ip;
+ if (connect(fd, (sockaddr *) &to, sizeof(to)) == 0) {
+ sockaddr_in me = {};
+ socklen_t len = sizeof(me);
+ char buf[INET_ADDRSTRLEN] = {};
+ if (getsockname(fd, (sockaddr *) &me, &len) == 0 && inet_ntop(AF_INET, &me.sin_addr, buf, sizeof(buf))) {
+ ip = buf;
+ }
+ }
+ close(fd);
+ return ip == "0.0.0.0" ? std::string() : ip;
+}
+
+void StatsCollector::start(int port) {
+ port_ = port;
+ {
+ std::lock_guard<std::mutex> lock(mutex_);
+ live_.cpus = usable_cpus();
+ const std::string ip = local_address();
+ live_.address = "http://" + (ip.empty() ? std::string("<console IP>") : ip) + ":" + std::to_string(port);
+ }
+ std::thread([this] { run(); }).detach();
+}
+
+Live StatsCollector::snapshot() {
+ std::lock_guard<std::mutex> lock(mutex_);
+ return live_;
+}
+
+void StatsCollector::set_model(const Live & m) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ live_.model_label = m.model_label;
+ live_.model_arch = m.model_arch;
+ live_.preset = m.preset;
+ live_.model_gib = m.model_gib;
+ live_.ctx = m.ctx;
+ live_.kv_type = m.kv_type;
+ live_.kv_gib = m.kv_gib;
+}
+
+void StatsCollector::set_models(const std::vector<ModelRow> & rows) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ live_.models = rows;
+}
+
+void StatsCollector::set_state(ServerState s) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ live_.state = s;
+ // Loading starts from what the pool holds now; progress is measured against
+ // what the model and its cache will add.
+ load_base_ = s == ServerState::loading ? live_.pool_gib - live_.free_gib : -1;
+ live_.load_progress = s == ServerState::loading ? 0.0f : -1.0f;
+}
+
+void StatsCollector::run() {
+ const double start = now_s();
+ double last_wall = start, last_cpu = cpu_seconds();
+ double last_busy = 0, last_tokens = 0, last_gen_s = 0;
+ bool have_last = false, last_working = false;
+ double last_ptok = 0, last_psec = 0;
+ for (;;) {
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ const double wall = now_s(), cpu = cpu_seconds();
+
+ const std::string m = http_get(port_, "/metrics");
+ const bool up = !m.empty();
+ const double busy_s = metric(m, "prompt_seconds_total") + metric(m, "tokens_predicted_seconds_total");
+ const double tokens = metric(m, "tokens_predicted_total");
+ const double gen_s = metric(m, "tokens_predicted_seconds_total");
+ const bool working = metric(m, "requests_processing") > 0;
+ std::string slots;
+ if (up) {
+ slots = http_get(port_, "/slots");
+ }
+
+ double pool = 0, free = 0, heap = 0;
+#ifdef __PROSPERO__
+ const int64_t pool_bytes = sceKernelGetDirectMemorySize();
+ int64_t at = 0;
+ size_t avail = 0;
+ sceKernelAvailableDirectMemorySize(0, pool_bytes, 2 * 1024 * 1024, &at, &avail);
+ struct ps5_heap_stats hs = {};
+ ps5_heap_stats(&hs);
+ pool = pool_bytes / kGiB;
+ free = avail / kGiB;
+ heap = hs.mapped_bytes / kGiB;
+#endif
+
+ std::lock_guard<std::mutex> lock(mutex_);
+ const double dt = wall - last_wall;
+ live_.uptime_s = wall - start;
+ live_.pool_gib = pool;
+ live_.free_gib = free;
+ live_.heap_gib = heap;
+ live_.cpu_use = (float) std::min(1.0, (cpu - last_cpu) / (dt * live_.cpus));
+ if (live_.state == ServerState::loading && load_base_ >= 0 && live_.model_gib + live_.kv_gib > 0) {
+ const double added = (pool - free) - load_base_;
+ live_.load_progress = (float) std::clamp(added / (live_.model_gib + live_.kv_gib), 0.0, 0.99);
+ }
+ push(live_.cpu_history, live_.cpu_use);
+ if (up) {
+ if (live_.state == ServerState::loading || live_.state == ServerState::ready ||
+ live_.state == ServerState::generating) {
+ live_.state = working ? ServerState::generating : ServerState::ready;
+ live_.load_progress = -1;
+ }
+ live_.tokens = (uint64_t) tokens;
+ live_.prompt_tokens = (uint64_t) metric(m, "prompt_tokens_total");
+ if (last_working && !working && tokens > last_tokens) {
+ live_.requests++; // a reply finished
+ }
+ last_working = working;
+ // The last prompt's speed, from the counters (the server's own
+ // average reads 0 on this build).
+ const double ptok = metric(m, "prompt_tokens_total"), psec = metric(m, "prompt_seconds_total");
+ if (have_last && psec - last_psec > 0.01) {
+ live_.prompt_tps = (float) ((ptok - last_ptok) / (psec - last_psec));
+ }
+ last_ptok = ptok;
+ last_psec = psec;
+ if (have_last) {
+ live_.gpu_busy = (float) std::min(1.0, std::max(0.0, (busy_s - last_busy) / dt));
+ const double dgen = gen_s - last_gen_s;
+ live_.gen_tps = dgen > 0.05 ? (float) ((tokens - last_tokens) / dgen) : 0.0f;
+ }
+ live_.ctx_used = (uint32_t) (json_int(slots, "n_prompt_tokens") + json_int(slots, "n_decoded"));
+ have_last = true;
+ last_busy = busy_s;
+ last_tokens = tokens;
+ last_gen_s = gen_s;
+ }
+ push(live_.gpu_history, live_.gpu_busy);
+ push(live_.gen_history, live_.gen_tps);
+ last_wall = wall;
+ last_cpu = cpu;
+ }
+}
+
+} // namespace ps5lm
diff --git a/ps5/app/stats.hpp b/ps5/app/stats.hpp
new file mode 100644
index 0000000..2423777
--- /dev/null
+++ b/ps5/app/stats.hpp
@@ -0,0 +1,106 @@
+// What the dashboard shows, and the thread that measures it.
+//
+// llama-server reports its own work through /metrics and /slots, read here
+// over loopback: tokens per second from the predicted-token counters, and
+// how busy the GPU is from the seconds the server spent computing prompts
+// and tokens (every layer runs on the GPU, so that time is GPU time). CPU
+// use comes from getrusage, memory from the kernel's direct-memory pool and
+// the platform heap.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#pragma once
+
+#include <cstdint>
+#include <mutex>
+#include <string>
+#include <vector>
+
+namespace ps5lm {
+
+enum class ServerState : uint8_t { loading, ready, generating, failed, no_model };
+
+struct ModelRow {
+ std::string file; // path
+ std::string label; // file name without .gguf
+ std::string arch;
+ std::string plan; // the planner's line ("64k context, q8_0 cache: 9.9 of 11.0 GiB")
+ std::string preset; // the preset that filled its sampling ("Qwen 3.8"), or empty
+ double size_gib = 0;
+ bool fits = false;
+ bool current = false;
+ bool on_usb = false; // found on a USB drive: importable to internal storage
+ float import_progress = -1; // 0..1 while it is being copied in
+};
+
+struct Live {
+ ServerState state = ServerState::loading;
+ std::string address; // "http://192.168.1.19:8081"
+
+ // The model that runs.
+ std::string model_label;
+ std::string model_arch;
+ std::string preset; // "Qwen 3.8"
+ double model_gib = 0;
+ uint32_t ctx = 0;
+ std::string kv_type;
+ double kv_gib = 0;
+
+ // Speed.
+ float gen_tps = 0; // over the last generation interval
+ float prompt_tps = 0; // last prompt
+ std::vector<float> gen_history; // tok/s per second, oldest first (0 while idle)
+ std::vector<float> gpu_history; // GPU busy 0..1 per second
+ std::vector<float> cpu_history; // CPU use 0..1 per second
+
+ // Load.
+ float gpu_busy = 0; // 0..1
+ float cpu_use = 0; // 0..1 of the CPUs the app may use
+ int cpus = 0;
+
+ // Memory, GiB.
+ double pool_gib = 0; // the title's direct memory
+ double free_gib = 0;
+ double heap_gib = 0; // CPU heap, part of the pool
+
+ // Loading: how much of the model and its cache is on the GPU, 0..1, or -1.
+ float load_progress = -1;
+
+ // Session.
+ double uptime_s = 0;
+ uint64_t tokens = 0; // generated since start
+ uint64_t prompt_tokens = 0;
+ uint64_t requests = 0;
+ uint32_t ctx_used = 0; // in the running slot
+
+ std::vector<ModelRow> models;
+};
+
+// The sampling thread. Fill the static parts (model, address) with set_*;
+// snapshot() copies everything for the frame.
+class StatsCollector {
+ public:
+ void start(int port);
+ Live snapshot();
+ void set_model(const Live & model_part); // model_* , ctx, kv_*, preset
+ void set_models(const std::vector<ModelRow> & rows);
+ void set_state(ServerState s);
+
+ private:
+ void run();
+ std::mutex mutex_;
+ Live live_;
+ double load_base_ = -1; // pool in use when the load began
+ int port_ = 8081;
+};
+
+// The console's address on the LAN, as the PC sees it ("192.168.1.19"), or
+// empty. Asks the routing table through an unconnected UDP socket: nothing is sent.
+std::string local_address();
+
+// A tiny HTTP GET over loopback; returns the body, or empty.
+std::string http_get(int port, const char * path, int timeout_s = 1);
+
+// One value from Prometheus text ("llamacpp:tokens_predicted_total 42").
+double metric(const std::string & text, const char * name);
+
+} // namespace ps5lm
diff --git a/scripts/build-app.sh b/scripts/build-app.sh
index 2d83ef4..ebeb0b4 100755
--- a/scripts/build-app.sh
+++ b/scripts/build-app.sh
@@ -20,10 +20,11 @@ tool="$vk/build/host/ps5-native-tool"
llama="$root/third_party/llama.cpp"
out="$root/build/app-ps5"
headers="$root/.deps/ps5-vk-headers"
+glsdk=${PS5_OPENGL_SDK:-$root/.deps/ps5-opengl/ps5-opengl-sdk-1.0.0/sdk}
sce_sys="$root/ps5/app/sce_sys"
param="$sce_sys/param.json"
-for f in "$archive" "$tool" "$vk/runtime/libc.prx" "$sdk/bin/prospero-lld"; do
+for f in "$archive" "$tool" "$vk/runtime/libc.prx" "$sdk/bin/prospero-lld" "$glsdk/lib/libPS5OpenGL.a"; do
[ -e "$f" ] || { echo "build-app: missing $f" >&2; exit 2; }
done
export PS5_PAYLOAD_SDK="$sdk" PS5_VULKAN_ROOT="$vk"
@@ -81,7 +82,10 @@ cmake --build "$out/llama" --target llama-server-impl
obj="$out/obj"
mkdir -p "$obj" "$out/stubs"
cxx() { "$root/ps5/app/ps5cxx" -std=c++17 -O2 -ffunction-sections -fdata-sections "$@"; }
-cxx -fexceptions -fcxx-exceptions -frtti -I "$headers/include" -I "$llama/ggml/include" -c "$root/ps5/app/main.cpp" -o "$obj/main.o"
+cxx -std=c++20 -fexceptions -fcxx-exceptions -frtti -I "$headers/include" -I "$llama/ggml/include" -I "$root/third_party/ps5-homebrew-ui/src" \
+ -I "${PS5_OPENGL_SDK:-$root/.deps/ps5-opengl/ps5-opengl-sdk-1.0.0/sdk}/include" -DGL_GLEXT_PROTOTYPES=1 \
+ -c "$root/ps5/app/main.cpp" -o "$obj/main.o"
+cxx -fexceptions -fcxx-exceptions -c "$root/ps5/app/model_plan.cpp" -o "$obj/model_plan.o"
"$root/ps5/app/ps5cc" -O2 -c "$root/ps5/app/compat_app.c" -o "$obj/compat_app.o"
# In an archive, so --exclude-libs keeps them local: a stub module defines some
# of the same names, and lld would otherwise export ours to interpose them,
@@ -89,12 +93,72 @@ cxx -fexceptions -fcxx-exceptions -frtti -I "$headers/include" -I "$llama/ggml/i
rm -f "$obj/libps5app_compat.a"
"$sdk/bin/llvm-ar" rcs "$obj/libps5app_compat.a" "$obj/compat_app.o"
cxx -std=c++20 -fno-exceptions -fno-rtti -c "$vk/tooling/native/app_crt.cpp" -o "$obj/app_crt.o"
-cxx -std=c++20 -fno-exceptions -fno-rtti -c "$vk/tooling/native/app_cpp_runtime.cpp" -o "$obj/app_cpp_runtime.o"
+cxx -std=c++20 -fexceptions -fcxx-exceptions -c "$root/ps5/app/cpp_runtime.cpp" -o "$obj/app_cpp_runtime.o"
-# AGC comes from system modules; these stubs only name the imports.
-for pair in libSceAgc:agc_canary_link_stub.c libSceAgcDriver:agc_driver_canary_link_stub.c; do
- lib=${pair%%:*}
- "$root/ps5/app/ps5cc" -O2 -fPIC -c "$vk/vendor/ps5/sdk/stubs/${pair#*:}" -o "$obj/${lib}_stub.o"
+# The screen: ps5-opengl (EGL, OpenGL 4.6) is a Mesa of its own, and shares
+# thousands of symbol names with RADV's. Linked into one object with only the
+# GL and EGL API left global, the two never meet. Rebuilt when the SDK changes.
+gl="$out/gl/gl_api.o"
+if [ ! -f "$gl" ] || [ "$glsdk/lib/libPS5OpenGL.a" -nt "$gl" ]; then
+ mkdir -p "$out/gl"
+ gllibs=()
+ for l in $(sed -n '/GROUP (/,/)/p' "$glsdk/lib/libPS5OpenGL.a" | grep -oE 'lib[a-z0-9_.]+\.a'); do
+ gllibs+=("$glsdk/lib/$l")
+ done
+ "$sdk/bin/llvm-nm" --defined-only -g "$glsdk/lib/libps5_opengl_core33.a" "$glsdk/lib/libglapi_bridge.a" \
+ "$glsdk/lib/libglapi.a" 2>/dev/null | awk 'NF>=3 && $2 ~ /[TW]/ {print $3}' |
+ grep -E '^(gl|egl)[A-Z]|^ps5_opengl' | sort -u > "$out/gl/api.txt"
+ us=()
+ while read -r name; do us+=(-u "$name"); done < "$out/gl/api.txt"
+ "$sdk/bin/ld.lld" -r "${us[@]}" -u ps5_agc_gate2_run --start-group "${gllibs[@]}" --end-group \
+ -o "$out/gl/gl_all.o"
+ "$sdk/bin/llvm-objcopy" --keep-global-symbols="$out/gl/api.txt" "$out/gl/gl_all.o" "$gl"
+ rm -f "$out/gl/gl_all.o" "$out/gl/libps5lm_gl.a"
+ # In an archive, so --exclude-libs keeps the API local: SDK stub modules
+ # define EGL names too, and lld would export ours to interpose them.
+ "$sdk/bin/llvm-ar" rcs "$out/gl/libps5lm_gl.a" "$gl"
+fi
+# The dashboard: ps5-homebrew-ui's kit (gfx, ui, core, audio and its console
+# platform layer, without its shell, designs or demo), drawn through ps5-opengl.
+kit="$root/third_party/ps5-homebrew-ui"
+kitflags=(-std=c++20 -O2 -ffunction-sections -fdata-sections -DGL_GLEXT_PROTOTYPES=1 -I "$kit/src" -I "$kit/third_party"
+ -I "$glsdk/include" -I "$root/ps5/app")
+mkdir -p "$obj/kit"
+mapfile -t kit_sources < <(find "$kit/src/gfx" "$kit/src/ui" "$kit/src/core" "$kit/src/audio" "$kit/src/third_party" \
+ "$kit/src/platform/ps5" -type f \( -name '*.cpp' -o -name '*.c' \) ! -name display_egl.cpp | sort)
+kit_objects=()
+for src in "${kit_sources[@]}"; do
+ o="$obj/kit/$(echo "${src#"$kit/src/"}" | tr '/' '_').o"
+ kit_objects+=("$o")
+ [ -f "$o" ] && [ "$o" -nt "$src" ] && continue
+ if [[ $src == *.c ]]; then
+ "$root/ps5/app/ps5cc" -std=c11 -O2 -w -I "$kit/src" -c "$src" -o "$o" &
+ else
+ cxx "${kitflags[@]}" -w -c "$src" -o "$o" &
+ fi
+ (( $(jobs -r | wc -l) >= $(nproc) )) && wait -n
+done
+wait
+rm -f "$obj/libps5lm_kit.a"
+"$sdk/bin/llvm-ar" rcs "$obj/libps5lm_kit.a" "${kit_objects[@]}"
+for src in display dashboard stats; do
+ cxx -fexceptions -fcxx-exceptions "${kitflags[@]}" -c "$root/ps5/app/$src.cpp" -o "$obj/$src.o"
+done
+
+# AGC comes from system modules; these stubs only name the imports, every
+# one either driver (the GL object, RADV) makes.
+agc_imports=$("$sdk/bin/llvm-nm" -u "$gl" "$archive" 2>/dev/null | awk '{print $NF}' | grep -E '^sceAgc' | sort -u)
+for lib in libSceAgc libSceAgcDriver; do
+ stub_c="$out/stubs/$lib.c"
+ : > "$stub_c"
+ for name in $agc_imports; do
+ case $lib:$name in
+ libSceAgcDriver:sceAgcDriver*) echo "void $name(void) {}" >> "$stub_c" ;;
+ libSceAgc:sceAgcDriver*) ;;
+ libSceAgc:*) echo "void $name(void) {}" >> "$stub_c" ;;
+ esac
+ done
+ "$root/ps5/app/ps5cc" -O2 -fPIC -c "$stub_c" -o "$obj/${lib}_stub.o"
"$sdk/bin/prospero-lld" --shared -soname "$lib.prx" -o "$out/stubs/$lib.so" "$obj/${lib}_stub.o"
done
@@ -106,13 +170,13 @@ flags=()
for f in "${radv_link_flags[@]}"; do
case $f in --defsym=getaddrinfo=*|--defsym=freeaddrinfo=*) ;; *) flags+=("$f") ;; esac
done
-radv_link_flags=("${flags[@]}")
+radv_link_flags=("${flags[@]}" --wrap=sceAgcInit)
mapfile -t libs < <(find "$out/llama" -name '*.a' | sort)
"$sdk/bin/prospero-lld" "${radv_linker_script[@]}" --eh-frame-hdr "${radv_link_flags[@]}" \
--version-script "$vk/tooling/native/app-symbols.map" --exclude-libs=ALL \
-e _start -o "$out/llvm-pie.elf" \
- "$obj/app_crt.o" "$obj/app_cpp_runtime.o" "$obj/main.o" \
- --start-group "${libs[@]}" "$obj/libps5app_compat.a" --end-group \
+ "$obj/app_crt.o" "$obj/app_cpp_runtime.o" "$obj/main.o" "$obj/display.o" "$obj/model_plan.o" "$obj/dashboard.o" "$obj/stats.o" \
+ --start-group "${libs[@]}" "$obj/libps5lm_kit.a" "$obj/libps5app_compat.a" "$out/gl/libps5lm_gl.a" --end-group \
"$out/stubs/libSceAgc.so" "$out/stubs/libSceAgcDriver.so" \
"${radv_link_inputs[@]}" \
--as-needed "$sdk"/target/lib/*.so
@@ -139,5 +203,12 @@ if [ ! -f "$sce_sys/pic0.dds" ] || [ "$sce_sys/source/background.svg" -nt "$sce_
fi
cp "$sce_sys/icon0.png" "$sce_sys/pic0.dds" "$sce_sys/pic1.dds" "$app/sce_sys/"
cp "$vk/runtime/libc.prx" "$app/sce_module/libc.prx"
+# The dashboard's fonts and interface sounds (ps5-homebrew-ui's, with their licences).
+mkdir -p "$app/assets/fonts" "$app/assets/audio"
+for font in inter-regular inter-semibold montserrat-medium dejavu-sans-mono; do
+ cp "$root/third_party/ps5-homebrew-ui/assets/fonts/$font.huifont" "$app/assets/fonts/"
+done
+cp "$root/third_party/ps5-homebrew-ui/assets/fonts/"*LICENSE* "$app/assets/fonts/"
+cp -r "$root/third_party/ps5-homebrew-ui/assets/audio/sfx" "$app/assets/audio/"
"$tool" self --inspect --file "$app/eboot.bin" > /dev/null
echo "build-app: $app ($(stat -c %s "$app/eboot.bin") bytes)"
diff --git a/scripts/preview-app-ui.sh b/scripts/preview-app-ui.sh
new file mode 100755
index 0000000..d5f9206
--- /dev/null
+++ b/scripts/preview-app-ui.sh
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+# Renders the app's dashboard on the PC, off-screen through Mesa (surfaceless
+# EGL), to PNG files: the same drawing code as the console, with sample data.
+#
+# scripts/preview-app-ui.sh build/app-preview/*.png
+#
+# Needs clang++ and the EGL and GL development files (glvnd or Mesa).
+set -euo pipefail
+
+root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
+kit="$root/third_party/ps5-homebrew-ui"
+out="$root/build/app-preview"
+obj="$out/obj"
+mkdir -p "$obj"
+cxx=${HOST_CXX:-clang++}
+cc=${HOST_CC:-clang}
+
+# The kit's own code that PS5LM uses (no shell, designs or demo), the kit's
+# host platform functions, and the app's dashboard.
+mapfile -t sources < <(find "$kit/src/gfx" "$kit/src/ui" "$kit/src/core" "$kit/src/audio" "$kit/src/third_party" \
+ -type f \( -name '*.cpp' -o -name '*.c' \) | sort)
+sources+=("$kit/host/platform_host.cpp" "$root/ps5/app/dashboard.cpp" "$root/ps5/app/stats.cpp"
+ "$root/ps5/app/preview_main.cpp")
+
+compile() {
+ local src=$1 o
+ o="$obj/$(echo "${src#"$root/"}" | tr '/' '_').o"
+ if [ ! -f "$o" ] || [ "$src" -nt "$o" ] || [ "$root/ps5/app/dashboard.hpp" -nt "$o" ] || [ "$root/ps5/app/stats.hpp" -nt "$o" ]; then
+ if [[ $src == *.c ]]; then
+ $cc -std=c11 -O2 -w -I"$kit/src" -c "$src" -o "$o"
+ else
+ $cxx -std=c++20 -O2 -Wall -Wextra -DGL_GLEXT_PROTOTYPES=1 -I"$kit/src" -I"$kit/host" -I"$kit/third_party" \
+ -I"$root/ps5/app" -c "$src" -o "$o"
+ fi
+ fi
+ echo "$o"
+}
+export -f compile
+export root kit obj cxx cc
+mapfile -t objects < <(printf '%s\n' "${sources[@]}" | xargs -P "$(nproc)" -I{} bash -c 'compile "$@"' _ {})
+$cxx "${objects[@]}" -lEGL -lGL -lm -pthread -o "$out/ps5lm_preview"
+"$out/ps5lm_preview" "$kit/assets" "$out"
diff --git a/scripts/ps5lm-app.sh b/scripts/ps5lm-app.sh
new file mode 100755
index 0000000..08b0982
--- /dev/null
+++ b/scripts/ps5lm-app.sh
@@ -0,0 +1,85 @@
+#!/usr/bin/env bash
+# Drives the PS5LM app on the console from the PC, with no controller:
+#
+# scripts/ps5lm-app.sh close ask the running app to end itself, and wait
+# scripts/ps5lm-app.sh deploy upload build/app-ps5/title/<TITLE_ID>
+# scripts/ps5lm-app.sh launch start it (ps5-homebrew-dev-protocol's launch payload)
+# scripts/ps5lm-app.sh cycle close, deploy, launch
+# scripts/ps5lm-app.sh screenshot [file] what the TV shows, as a BMP
+# scripts/ps5lm-app.sh load <model path> switch the model, as the library does
+# scripts/ps5lm-app.sh import <path> copy a .gguf into /data/PS5LM/models
+# scripts/ps5lm-app.sh unload unload the model
+#
+# The app is never killed: it polls /data/PS5LM/quit and leaves through
+# sceSystemServiceLoadExec("exit"), since killing a title that is rendering
+# has been followed by lost consoles. Needs ftpsrv (2121) and elfldr (9021).
+set -euo pipefail
+
+root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
+host=${PS5_HOST:?set PS5_HOST to the console IP}
+ftp="ftp://$host:${PS5_FTP_PORT:-2121}"
+title=$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["titleId"])' "$root/ps5/app/sce_sys/param.json")
+app="$root/build/app-ps5/title/$title"
+protocol=${PS5_PROTOCOL:-$root/.deps/src/ps5-homebrew-dev-protocol}
+
+close() {
+ printf 'quit\n' | curl -sS -T - "$ftp/data/PS5LM/quit"
+ for _ in $(seq 1 30); do
+ sleep 2
+ if ! curl -sf "$ftp/data/PS5LM/quit" -o /dev/null; then
+ # The app took the request; give the system a moment to end it.
+ sleep 5
+ echo "ps5lm-app: closed"
+ return 0
+ fi
+ done
+ curl -s -Q "DELE /data/PS5LM/quit" "$ftp/" -o /dev/null || true
+ echo "ps5lm-app: the app did not take the quit request (not running?)"
+}
+
+deploy() {
+ [ -f "$app/eboot.bin" ] || { echo "ps5lm-app: build it first (scripts/build-app.sh)" >&2; exit 1; }
+ (cd "$app" && find . -type f | while read -r f; do
+ curl -sS --ftp-create-dirs -T "$f" "$ftp/data/homebrew/$title/${f#./}" ||
+ { echo "ps5lm-app: upload of $f failed (is the app still running?)" >&2; exit 1; }
+ done)
+ echo "ps5lm-app: deployed $title"
+}
+
+launch() {
+ # The protocol's script builds the payload; it is sent with socat, as the
+ # other scripts here do (the script's own sender needs GNU nc's -q).
+ local tmp=${TMPDIR:-/tmp}
+ PS5_PAYLOAD_SDK=${PS5_PAYLOAD_SDK:-$root/.deps/ps5-payload-sdk} PS5_CONTROLLER_COMPILE_ONLY=1 \
+ bash "$protocol/scripts/send-controller.sh" launch "$title" "$host" "${PS5_PORT:-9021}" > /dev/null
+ socat -u "FILE:$tmp/ps5-homebrew-protocol/launch-$title.elf" "TCP:$host:${PS5_PORT:-9021}"
+ echo "ps5lm-app: launch sent"
+}
+
+# Requests the running app takes once a second.
+request() {
+ printf '%s\n' "${2:--}" | curl -sS -T - "$ftp/data/PS5LM/$1"
+}
+
+screenshot() {
+ request screenshot
+ for _ in $(seq 1 20); do
+ sleep 1
+ curl -sf "$ftp/data/PS5LM/screenshot" -o /dev/null || break
+ done
+ sleep 2
+ curl -sS "$ftp/data/PS5LM/screen.bmp" -o "${1:-build/app-ps5/screen.bmp}"
+ echo "ps5lm-app: screenshot in ${1:-build/app-ps5/screen.bmp}"
+}
+
+case ${1:-} in
+ load) request load "${2:?model path on the console}" ;;
+ import) request import "${2:?file path on the console}" ;;
+ unload) request unload ;;
+ screenshot) screenshot "${2:-}" ;;
+ close) close ;;
+ deploy) deploy ;;
+ launch) launch ;;
+ cycle) close; deploy; launch ;;
+ *) echo "usage: $0 close|deploy|launch|cycle|screenshot|load|unload|import" >&2; exit 2 ;;
+esac
diff --git a/third_party/ps5-homebrew-ui b/third_party/ps5-homebrew-ui
new file mode 160000
index 0000000..bf44d80
--- /dev/null
+++ b/third_party/ps5-homebrew-ui
@@ -0,0 +1 @@
+Subproject commit bf44d80384d5b8f8f4f213cf98d748887211328d