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

Get models: follow redirects ourselves, 4 MiB reads, write() downloads

Abhirama936cdb8d8ac
diff --git a/patches/0003-tools-root.patch b/patches/0003-tools-root.patch
index bee9bd5..c44bfc5 100644
--- a/patches/0003-tools-root.patch
+++ b/patches/0003-tools-root.patch
@@ -1,10 +1,23 @@
-PS5LM: confine llama-server's built-in tools to LLAMA_TOOLS_ROOT (the app's scratch folder).
+PS5LM: confine llama-server's built-in tools to LLAMA_TOOLS_ROOT (the app's scratch folder): relative paths start there, and a path outside it is taken as relative to it.
diff --git a/tools/server/server-tools.cpp b/tools/server/server-tools.cpp
-index 12e9dbb8c..8ff130fab 100644
+index 12e9dbb8c..91fd57213 100644
--- a/tools/server/server-tools.cpp
+++ b/tools/server/server-tools.cpp
-@@ -323,6 +323,15 @@ public:
+@@ -306,6 +306,12 @@ public:
+ const std::string p = expand_home(path);
+
+ fs::path full = path_from_utf8(p);
++ // PS5LM: with LLAMA_TOOLS_ROOT set, a relative path starts there (a
++ // PS5 title cannot read its working directory).
++ const char * tools_root = std::getenv("LLAMA_TOOLS_ROOT");
++ if (!full.is_absolute() && tools_root && *tools_root) {
++ full = path_from_utf8(tools_root) / full;
++ }
+ if (!full.is_absolute()) {
+ if (cwd.empty()) {
+ std::error_code ec;
+@@ -323,6 +329,15 @@ public:
if (!full.has_filename() && full != full.root_path()) {
full = full.parent_path();
}
diff --git a/ps5/app/fetch.cpp b/ps5/app/fetch.cpp
index b6d855a..17fc098 100644
--- a/ps5/app/fetch.cpp
+++ b/ps5/app/fetch.cpp
@@ -4,6 +4,8 @@
#include <chrono>
#include <cstdio>
#include <mutex>
+#include <strings.h>
+#include <unistd.h>
#include <vector>
extern "C" {
@@ -19,6 +21,8 @@ int sceHttp2GetStatusCode(int req, int * status);
int sceHttp2GetResponseContentLength(int req, int * result, uint64_t * length);
int sceHttp2ReadData(int req, void * buf, size_t size);
int sceHttp2DeleteRequest(int req);
+int sceHttp2SetAutoRedirect(int id, int enable);
+int sceHttp2GetAllResponseHeaders(int req, char ** headers, size_t * size);
}
namespace ps5lm {
@@ -62,37 +66,97 @@ bool init(std::string * error) {
} // namespace
-FetchResult https_get(const std::string & url, const std::string & range,
+namespace {
+
+// The Location header of a redirect, or empty.
+std::string location(int req) {
+ char * headers = nullptr;
+ size_t size = 0;
+ if (sceHttp2GetAllResponseHeaders(req, &headers, &size) != 0 || !headers) {
+ return {};
+ }
+ const std::string h(headers, size);
+ for (size_t at = 0; at < h.size();) {
+ const size_t end = h.find('\n', at);
+ std::string line = h.substr(at, end == std::string::npos ? std::string::npos : end - at);
+ at = end == std::string::npos ? h.size() : end + 1;
+ if (line.size() > 9 && strncasecmp(line.c_str(), "location:", 9) == 0) {
+ line = line.substr(9);
+ line.erase(0, line.find_first_not_of(" \t"));
+ line.erase(line.find_last_not_of("\r\n \t") + 1);
+ return line;
+ }
+ }
+ return {};
+}
+
+} // namespace
+
+FetchResult https_get(const std::string & url_in, const std::string & range,
const std::function<bool(const char *, size_t)> & sink) {
FetchResult r;
const auto t0 = std::chrono::steady_clock::now();
if (!init(&r.error)) {
return r;
}
- char text[96];
- const int req = sceHttp2CreateRequestWithURL(g_template, "GET", url.c_str(), 0);
- if (req < 0) {
- std::snprintf(text, sizeof(text), "sceHttp2CreateRequestWithURL 0x%x", req);
- r.error = text;
- return r;
- }
- if (!range.empty()) {
- sceHttp2AddRequestHeader(req, "Range", range.c_str(), 0);
- }
- if (int rc = sceHttp2SendRequest(req, nullptr, 0); rc < 0) {
- std::snprintf(text, sizeof(text), "sceHttp2SendRequest 0x%x", rc);
- r.error = text;
+ char text[160];
+ // Redirects are followed here, not by the library: after following one
+ // itself (Hugging Face's file links go to a CDN) it reported the length
+ // but handed over no body.
+ std::string url = url_in;
+ int req = -1;
+ for (int hop = 0; hop < 6; ++hop) {
+ req = sceHttp2CreateRequestWithURL(g_template, "GET", url.c_str(), 0);
+ if (req < 0) {
+ std::snprintf(text, sizeof(text), "sceHttp2CreateRequestWithURL 0x%x", req);
+ r.error = text;
+ return r;
+ }
+ sceHttp2SetAutoRedirect(req, 0);
+ if (!range.empty()) {
+ sceHttp2AddRequestHeader(req, "Range", range.c_str(), 0);
+ }
+ if (int rc = sceHttp2SendRequest(req, nullptr, 0); rc < 0) {
+ std::snprintf(text, sizeof(text), "sceHttp2SendRequest 0x%x", rc);
+ r.error = text;
+ sceHttp2DeleteRequest(req);
+ return r;
+ }
+ sceHttp2GetStatusCode(req, &r.status);
+ if (r.status < 300 || r.status >= 400) {
+ break;
+ }
+ const std::string next = location(req);
sceHttp2DeleteRequest(req);
+ req = -1;
+ if (next.empty()) {
+ r.error = "redirect without a Location";
+ return r;
+ }
+ // A relative Location keeps the scheme and host.
+ url = next.rfind("http", 0) == 0 ? next : url.substr(0, url.find('/', url.find("//") + 2)) + next;
+ }
+ if (req < 0) {
+ r.error = "too many redirects";
return r;
}
- sceHttp2GetStatusCode(req, &r.status);
int has_length = 0;
uint64_t length = 0;
if (sceHttp2GetResponseContentLength(req, &has_length, &length) == 0 && has_length == 0) {
r.length = (int64_t) length; // result 0: the length is known
}
- std::vector<char> buf(1u << 20);
- for (int n; (n = sceHttp2ReadData(req, buf.data(), buf.size())) > 0;) {
+ // Large reads: 64 KiB ones ran at about 200 KB/s.
+ std::vector<char> buf(4u << 20);
+ for (;;) {
+ const int n = sceHttp2ReadData(req, buf.data(), buf.size());
+ if (n < 0) {
+ std::snprintf(text, sizeof(text), "sceHttp2ReadData 0x%x after %llu bytes", n, (unsigned long long) r.bytes);
+ r.error = text;
+ break;
+ }
+ if (n == 0) {
+ break;
+ }
r.bytes += (uint64_t) n;
if (!sink(buf.data(), (size_t) n)) {
break;
diff --git a/ps5/app/market.cpp b/ps5/app/market.cpp
index 3660573..e89a4e5 100644
--- a/ps5/app/market.cpp
+++ b/ps5/app/market.cpp
@@ -11,6 +11,7 @@ extern "C" {
}
#include <cstdio>
+#include <fcntl.h>
#include <unistd.h>
namespace ps5lm {
@@ -211,13 +212,14 @@ void Market::download(int repo, int file, const std::string & dir) {
const size_t slash = name.find_last_of('/');
const std::string to = dir + "/" + name.substr(slash == std::string::npos ? 0 : slash + 1);
const std::string part = to + ".part";
- FILE * out = std::fopen(part.c_str(), "wb");
+ // write(), not stdio: on /data stdio managed 18 MB/s, write() 230 MB/s.
+ const int out = open(part.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666);
sha256_t h;
sha256_init(&h);
uint64_t done = 0;
const auto t0 = std::chrono::steady_clock::now();
const FetchResult r = https_get(std::string(kHub) + "/" + id + "/resolve/main/" + name, "", [&](const char * p, size_t n) {
- if (!out || std::fwrite(p, 1, n, out) != n) {
+ if (out < 0 || write(out, p, n) != (ssize_t) n) {
return false;
}
sha256_update(&h, (const unsigned char *) p, n);
@@ -228,9 +230,9 @@ void Market::download(int repo, int file, const std::string & dir) {
view_.download_mbps = s > 0 ? done / 1e6 / s : 0;
return true;
});
- bool ok = out && std::fflush(out) == 0 && fsync(fileno(out)) == 0;
- if (out) {
- std::fclose(out);
+ bool ok = out >= 0 && fsync(out) == 0;
+ if (out >= 0) {
+ close(out);
}
unsigned char digest[32];
sha256_final(&h, digest);