Skip to content

Distribute and run LLMs with a single file.

Put new server code in its own namespace

Justine Tunney8a0ec61bd41d
diff --git a/llamafile/chatbot_eval.cpp b/llamafile/chatbot_eval.cpp
index 397f8cb4..03aadc78 100644
--- a/llamafile/chatbot_eval.cpp
+++ b/llamafile/chatbot_eval.cpp
@@ -89,6 +89,7 @@ bool eval_image_embed(const llava_image_embed *image_embed) {
g_history.push_back(IMAGE_PLACEHOLDER_TOKEN);
}
clear_ephemeral();
+ llama_synchronize(g_ctx);
return true;
}
diff --git a/llamafile/server/atob.cpp b/llamafile/server/atob.cpp
index 8d125127..e1705b8e 100644
--- a/llamafile/server/atob.cpp
+++ b/llamafile/server/atob.cpp
@@ -18,6 +18,9 @@
#include "utils.h"
#include <string_view>
+namespace lf {
+namespace server {
+
bool
atob(std::string_view val, bool dflt)
{
@@ -31,3 +34,6 @@ atob(std::string_view val, bool dflt)
return true;
return dflt;
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/atom.cpp b/llamafile/server/atom.cpp
index da9f9a9e..e6273457 100644
--- a/llamafile/server/atom.cpp
+++ b/llamafile/server/atom.cpp
@@ -19,6 +19,9 @@
#include "image.h"
#include <cassert>
+namespace lf {
+namespace server {
+
/**
* @fileoverview Context window content container.
*
@@ -139,3 +142,6 @@ operator==(const Atom& lhs, const Atom& rhs)
return lhs.image() == rhs.image();
}
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/atom.h b/llamafile/server/atom.h
index 444020d4..a09f40ed 100644
--- a/llamafile/server/atom.h
+++ b/llamafile/server/atom.h
@@ -16,7 +16,9 @@
// limitations under the License.
#pragma once
-#include <__fwd/string.h>
+
+namespace lf {
+namespace server {
class Image;
@@ -45,3 +47,6 @@ operator<(const Atom&, const Atom&);
bool
operator==(const Atom&, const Atom&);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/atom_test.cpp b/llamafile/server/atom_test.cpp
index d9e61c00..59f8a3b3 100644
--- a/llamafile/server/atom_test.cpp
+++ b/llamafile/server/atom_test.cpp
@@ -19,7 +19,11 @@
#include "image.h"
#include <cstdlib>
-static const Atom atoms[] = {
+namespace lf {
+namespace server {
+namespace {
+
+const Atom atoms[] = {
Atom(),
Atom(-1),
Atom(0),
@@ -101,9 +105,19 @@ test_atom_operator_eq()
exit(8);
}
-int
-main()
+void
+atom_test()
{
test_atom_operator_lt();
test_atom_operator_eq();
}
+
+} // namespace
+} // namespace server
+} // namespace lf
+
+int
+main()
+{
+ lf::server::atom_test();
+}
diff --git a/llamafile/server/atomize.cpp b/llamafile/server/atomize.cpp
index a4c4fe35..8c2e37cb 100644
--- a/llamafile/server/atomize.cpp
+++ b/llamafile/server/atomize.cpp
@@ -25,6 +25,9 @@
#include <string>
#include <vector>
+namespace lf {
+namespace server {
+
static void
append_tokens(const llama_model* model,
std::vector<Atom>* result,
@@ -63,7 +66,7 @@ atomize(const llama_model* model,
} catch (const base64_error& e) {
continue;
}
- if (!lf::get_image_type(image))
+ if (!get_image_type(image))
continue;
append_tokens(model, result, s.substr(0, pos), parse_special);
result->emplace_back(new Image(image, -1));
@@ -71,3 +74,6 @@ atomize(const llama_model* model,
i = 0;
}
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/buffer.cpp b/llamafile/server/buffer.cpp
index ecd41226..30d0e1fd 100644
--- a/llamafile/server/buffer.cpp
+++ b/llamafile/server/buffer.cpp
@@ -19,6 +19,9 @@
#include <sys/mman.h>
#include <unistd.h>
+namespace lf {
+namespace server {
+
static int pagesz = getpagesize();
Buffer::Buffer(size_t capacity) noexcept
@@ -45,3 +48,6 @@ Buffer::~Buffer() noexcept
if (munmap(p, c + pagesz))
__builtin_trap();
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/buffer.h b/llamafile/server/buffer.h
index 5a53dd91..340ee4dd 100644
--- a/llamafile/server/buffer.h
+++ b/llamafile/server/buffer.h
@@ -18,6 +18,9 @@
#pragma once
#include <cstddef>
+namespace lf {
+namespace server {
+
struct Buffer
{
size_t i;
@@ -28,3 +31,6 @@ struct Buffer
explicit Buffer(size_t) noexcept;
~Buffer() noexcept;
};
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/cleanup.cpp b/llamafile/server/cleanup.cpp
index 6d2d7b02..f3d30071 100644
--- a/llamafile/server/cleanup.cpp
+++ b/llamafile/server/cleanup.cpp
@@ -16,11 +16,12 @@
// limitations under the License.
#include "cleanup.h"
-
+#include "llama.cpp/llama.h"
#include <unistd.h>
#include <vector>
-#include "llama.cpp/llama.h"
+namespace lf {
+namespace server {
void
cleanup_fildes(void* arg)
@@ -53,3 +54,6 @@ cleanup_llama_context(void* arg)
{
llama_free((llama_context*)arg);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/cleanup.h b/llamafile/server/cleanup.h
index 5fc0221a..ca19a9fb 100644
--- a/llamafile/server/cleanup.h
+++ b/llamafile/server/cleanup.h
@@ -17,6 +17,16 @@
#pragma once
+namespace lf {
+namespace server {
+
+struct Cleanup
+{
+ Cleanup* next;
+ void (*func)(void*);
+ void* arg;
+};
+
void
cleanup_fildes(void*);
@@ -31,3 +41,6 @@ cleanup_llama_batch(void*);
void
cleanup_llama_context(void*);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/client.cpp b/llamafile/server/client.cpp
index 30b42280..aee6ad52 100644
--- a/llamafile/server/client.cpp
+++ b/llamafile/server/client.cpp
@@ -16,7 +16,19 @@
// limitations under the License.
#include "client.h"
-
+#include "llama.cpp/llama.h"
+#include "llamafile/flags.h"
+#include "llamafile/llamafile.h"
+#include "llamafile/server/cleanup.h"
+#include "llamafile/server/log.h"
+#include "llamafile/server/server.h"
+#include "llamafile/server/time.h"
+#include "llamafile/server/tokenbucket.h"
+#include "llamafile/server/worker.h"
+#include "llamafile/string.h"
+#include "llamafile/threadlocal.h"
+#include "llamafile/trust.h"
+#include "llamafile/version.h"
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -28,26 +40,14 @@
#include <unistd.h>
#include <vector>
-#include "llama.cpp/llama.h"
-#include "llamafile/flags.h"
-#include "llamafile/llamafile.h"
-#include "llamafile/string.h"
-#include "llamafile/threadlocal.h"
-#include "llamafile/trust.h"
-#include "llamafile/version.h"
-
-#include "cleanup.h"
-#include "log.h"
-#include "server.h"
-#include "time.h"
-#include "tokenbucket.h"
-#include "worker.h"
-
#define STANDARD_RESPONSE_HEADERS \
"Server: llamafile/" LLAMAFILE_VERSION_STRING "\r\n" \
"Referrer-Policy: origin\r\n" \
"Cache-Control: private; max-age=0\r\n"
+namespace lf {
+namespace server {
+
static void
on_http_cancel(Client* client)
{
@@ -644,7 +644,7 @@ Client::dispatcher()
// serve static endpoints
int infd;
size_t size;
- resolved_ = lf::resolve(FLAG_www_root, p1);
+ resolved_ = resolve(FLAG_www_root, p1);
for (;;) {
infd = open(resolved_.c_str(), O_RDONLY);
if (infd == -1) {
@@ -670,7 +670,7 @@ Client::dispatcher()
break;
} else if (S_ISDIR(st.st_mode)) {
::close(infd);
- resolved_ = lf::resolve(resolved_, "index.html");
+ resolved_ = resolve(resolved_, "index.html");
} else {
::close(infd);
SLOG("won't serve special file: %s", resolved_.c_str());
@@ -734,3 +734,6 @@ Client::param(std::string_view key)
url_.params.p[i].val.n));
return {};
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/client.h b/llamafile/server/client.h
index a7fd9fcf..1df06dbc 100644
--- a/llamafile/server/client.h
+++ b/llamafile/server/client.h
@@ -16,9 +16,8 @@
// limitations under the License.
#pragma once
-
#include "buffer.h"
-
+#include <ctime>
#include <libc/fmt/itoa.h>
#include <libc/str/slice.h>
#include <net/http/http.h>
@@ -26,7 +25,6 @@
#include <optional>
#include <string>
#include <sys/resource.h>
-#include <time.h>
#define HasHeader(H) (!!msg_.headers[H].a)
#define HeaderData(H) (ibuf_.p + msg_.headers[H].a)
@@ -36,20 +34,18 @@
#define HeaderEqualCase(H, S) \
SlicesEqualCase(S, strlen(S), HeaderData(H), HeaderLength(H))
+struct llama_model;
+
+namespace lf {
+namespace server {
+
+struct Cleanup;
struct Slot;
struct Worker;
-struct llama_model;
struct TokenizeParams;
struct EmbeddingParams;
struct V1ChatCompletionParams;
-struct Cleanup
-{
- Cleanup* next;
- void (*func)(void*);
- void* arg;
-};
-
struct Client
{
int fd_ = -1;
@@ -117,3 +113,6 @@ struct Client
bool slotz() __wur;
};
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/embedding.cpp b/llamafile/server/embedding.cpp
index 94d927c4..fa986694 100644
--- a/llamafile/server/embedding.cpp
+++ b/llamafile/server/embedding.cpp
@@ -16,18 +16,19 @@
// limitations under the License.
#include "client.h"
-
-#include <math.h>
-#include <string.h>
-#include <sys/resource.h>
-#include <vector>
-
#include "llama.cpp/llama.h"
#include "llamafile/server/cleanup.h"
#include "llamafile/server/fastjson.h"
#include "llamafile/server/json.h"
#include "llamafile/server/log.h"
#include "llamafile/server/utils.h"
+#include <cmath>
+#include <cstring>
+#include <sys/resource.h>
+#include <vector>
+
+namespace lf {
+namespace server {
struct EmbeddingParams
{
@@ -332,3 +333,6 @@ Client::embedding()
p = stpcpy(p, "\r\n");
return send_response(headers, p, content);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/fastjson.cpp b/llamafile/server/fastjson.cpp
index 2c2c21ff..62d46ce8 100644
--- a/llamafile/server/fastjson.cpp
+++ b/llamafile/server/fastjson.cpp
@@ -16,13 +16,14 @@
// limitations under the License.
#include "fastjson.h"
-
+#include "double-conversion/double-to-string.h"
+#include "double-conversion/utils.h"
#include <cosmo.h>
#include <net/http/escape.h>
#include <string>
-#include "double-conversion/double-to-string.h"
-#include "double-conversion/utils.h"
+namespace lf {
+namespace server {
static const char kEscapeLiteral[128] = {
9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 2, 9, 4, 3, 9, 9, // 0x00
@@ -179,3 +180,6 @@ encode_js_string_literal(char* p, const std::string_view s) noexcept
*p = 0;
return p;
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/fastjson.h b/llamafile/server/fastjson.h
index c0b02ccc..c906a277 100644
--- a/llamafile/server/fastjson.h
+++ b/llamafile/server/fastjson.h
@@ -18,6 +18,9 @@
#pragma once
#include <__fwd/string_view.h>
+namespace lf {
+namespace server {
+
char*
encode_bool(char*, bool) noexcept;
@@ -44,3 +47,6 @@ encode_json(char*, const std::string_view) noexcept;
char*
encode_js_string_literal(char*, const std::string_view) noexcept;
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/fastjson_test.cpp b/llamafile/server/fastjson_test.cpp
index d63d3fb1..fb115108 100644
--- a/llamafile/server/fastjson_test.cpp
+++ b/llamafile/server/fastjson_test.cpp
@@ -17,11 +17,14 @@
#include "fastjson.h"
-#include <limits.h>
-#include <math.h>
-#include <stdio.h>
+#include <climits>
+#include <cmath>
+#include <cstdio>
#include <string>
+namespace lf {
+namespace server {
+
std::string
encode_json(int x) noexcept
{
@@ -72,9 +75,8 @@ encode_js_string_literal(const std::string_view x) noexcept
}
int
-main(int argc, char* argv[])
+fastjson_test()
{
-
if (encode_json(0) != "0")
return 1;
if (encode_json(INT_MAX) != "2147483647")
@@ -124,3 +126,12 @@ main(int argc, char* argv[])
return 0;
}
+
+} // namespace server
+} // namespace lf
+
+int
+main()
+{
+ return lf::server::fastjson_test();
+}
diff --git a/llamafile/server/hextoint.cpp b/llamafile/server/hextoint.cpp
index f6bec4f7..dc7eafb3 100644
--- a/llamafile/server/hextoint.cpp
+++ b/llamafile/server/hextoint.cpp
@@ -17,6 +17,9 @@
#include "utils.h"
+namespace lf {
+namespace server {
+
alignas(signed char) const signed char kHexToInt[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x00
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0x10
@@ -35,3 +38,6 @@ alignas(signed char) const signed char kHexToInt[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xe0
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 0xf0
};
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/image.cpp b/llamafile/server/image.cpp
index be684429..abb84005 100644
--- a/llamafile/server/image.cpp
+++ b/llamafile/server/image.cpp
@@ -20,6 +20,9 @@
#include <cassert>
#include <utility>
+namespace lf {
+namespace server {
+
Image::~Image() = default;
Image::Image(const Image& old) : Image(old.bytes_, old.ctx_used_)
@@ -68,3 +71,6 @@ operator==(const Image& lhs, const Image& rhs)
{
return lhs.bytes() == rhs.bytes();
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/image.h b/llamafile/server/image.h
index 547444c6..2251b8fb 100644
--- a/llamafile/server/image.h
+++ b/llamafile/server/image.h
@@ -18,6 +18,9 @@
#pragma once
#include <string>
+namespace lf {
+namespace server {
+
class Image
{
public:
@@ -37,3 +40,6 @@ operator<(const Image&, const Image&);
bool
operator==(const Image&, const Image&);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/image_test.cpp b/llamafile/server/image_test.cpp
index 6f0982fc..812c5b7d 100644
--- a/llamafile/server/image_test.cpp
+++ b/llamafile/server/image_test.cpp
@@ -19,7 +19,11 @@
#include "image.h"
#include <cstdlib>
-static const Image images[] = {
+namespace lf {
+namespace server {
+namespace {
+
+const Image images[] = {
Image("", 0),
Image("hello", 1),
Image("there", 1),
@@ -99,9 +103,19 @@ test_image_operator_eq()
exit(8);
}
-int
-main()
+void
+image_test()
{
test_image_operator_lt();
test_image_operator_eq();
}
+
+} // namespace
+} // namespace server
+} // namespace lf
+
+int
+main()
+{
+ lf::server::image_test();
+}
diff --git a/llamafile/server/listen.cpp b/llamafile/server/listen.cpp
index 1c50ec08..d39d4986 100644
--- a/llamafile/server/listen.cpp
+++ b/llamafile/server/listen.cpp
@@ -15,19 +15,19 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "server.h"
-
+#include "llamafile/llamafile.h"
+#include "llamafile/server/log.h"
+#include "llamafile/server/server.h"
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
#include <sys/socket.h>
-#include "llamafile/llamafile.h"
-#include "llamafile/server/log.h"
-#include "llamafile/server/server.h"
+namespace lf {
+namespace server {
void
print_listening_url(unsigned ip, int port)
@@ -116,3 +116,6 @@ create_listening_socket(const char* hostport)
freeaddrinfo(ai);
return fd;
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/log.cpp b/llamafile/server/log.cpp
index a6f07530..f39ff3d6 100644
--- a/llamafile/server/log.cpp
+++ b/llamafile/server/log.cpp
@@ -17,8 +17,11 @@
#include "log.h"
#include "time.h"
+#include <cstring>
#include <pthread.h>
-#include <string.h>
+
+namespace lf {
+namespace server {
static thread_local char g_thread_name[128];
@@ -86,3 +89,6 @@ get_log_timestamp(void)
s[25] = '0' + x / 1000 % 10;
return s;
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/log.h b/llamafile/server/log.h
index 16423414..775c334c 100644
--- a/llamafile/server/log.h
+++ b/llamafile/server/log.h
@@ -26,6 +26,9 @@
get_thread_name(), \
##__VA_ARGS__)
+namespace lf {
+namespace server {
+
const char*
get_thread_name(void);
@@ -34,3 +37,6 @@ get_log_timestamp(void);
void
set_thread_name(const char*);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/main.cpp b/llamafile/server/main.cpp
index cbe87c10..4659c994 100644
--- a/llamafile/server/main.cpp
+++ b/llamafile/server/main.cpp
@@ -15,9 +15,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include <assert.h>
-#include <cosmo.h>
-
#include "llama.cpp/llama.h"
#include "llamafile/llamafile.h"
#include "llamafile/pool.h"
@@ -29,6 +26,11 @@
#include "llamafile/server/tokenbucket.h"
#include "llamafile/server/utils.h"
#include "llamafile/version.h"
+#include <cassert>
+#include <cosmo.h>
+
+namespace lf {
+namespace server {
Server* g_server;
@@ -136,4 +138,14 @@ main(int argc, char* argv[])
while (!pthread_orphan_np())
pthread_decimate_np();
CheckForMemoryLeaks();
+ return 0;
+}
+
+} // namespace server
+} // namespace lf
+
+int
+main(int argc, char* argv[])
+{
+ return lf::server::main(argc, argv);
}
diff --git a/llamafile/server/or_empty.cpp b/llamafile/server/or_empty.cpp
index 93c76dfd..67d60a03 100644
--- a/llamafile/server/or_empty.cpp
+++ b/llamafile/server/or_empty.cpp
@@ -16,9 +16,11 @@
// limitations under the License.
#include "utils.h"
-
#include <string_view>
+namespace lf {
+namespace server {
+
std::string_view
or_empty(std::optional<std::string_view> x)
{
@@ -26,3 +28,6 @@ or_empty(std::optional<std::string_view> x)
return x.value();
return {};
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/server.cpp b/llamafile/server/server.cpp
index 32d06bbf..67f02884 100644
--- a/llamafile/server/server.cpp
+++ b/llamafile/server/server.cpp
@@ -16,21 +16,22 @@
// limitations under the License.
#include "server.h"
-#include "slots.h"
-
-#include <assert.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <stdio.h>
-#include <sys/socket.h>
-#include <time.h>
-#include <unistd.h>
-
#include "llamafile/crash.h"
#include "llamafile/llamafile.h"
#include "llamafile/server/log.h"
#include "llamafile/server/server.h"
+#include "llamafile/server/slots.h"
#include "llamafile/server/worker.h"
+#include <cassert>
+#include <cstdio>
+#include <ctime>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <sys/socket.h>
+#include <unistd.h>
+
+namespace lf {
+namespace server {
Server::Server(int fd, Slots* slots, llama_model* model)
: fd(fd), slots_(slots), model_(model)
@@ -194,3 +195,6 @@ Server::shutdown()
unlock();
}
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/server.h b/llamafile/server/server.h
index ebce3a41..05a515c5 100644
--- a/llamafile/server/server.h
+++ b/llamafile/server/server.h
@@ -20,9 +20,13 @@
#include <cosmo.h>
#include <pthread.h>
-struct Slots;
struct llama_model;
+namespace lf {
+namespace server {
+
+struct Slots;
+
struct Server
{
Server(int, Slots*, llama_model*);
@@ -54,3 +58,6 @@ extern Server* g_server;
int
create_listening_socket(const char*);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/signals.cpp b/llamafile/server/signals.cpp
index 09bf00d8..554f1170 100644
--- a/llamafile/server/signals.cpp
+++ b/llamafile/server/signals.cpp
@@ -16,14 +16,15 @@
// limitations under the License.
#include "signals.h"
-
-#include <signal.h>
-#include <ucontext.h>
-
#include "llamafile/crash.h"
#include "llamafile/server/log.h"
#include "llamafile/server/server.h"
#include "llamafile/threadlocal.h"
+#include <signal.h>
+#include <ucontext.h>
+
+namespace lf {
+namespace server {
static struct
{
@@ -106,3 +107,6 @@ signals_destroy(void)
sigaction(SIGXCPU, &old.sigxcpu, 0);
sigaction(SIGXFSZ, &old.sigxfsz, 0);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/signals.h b/llamafile/server/signals.h
index 1cc1d288..4e584550 100644
--- a/llamafile/server/signals.h
+++ b/llamafile/server/signals.h
@@ -18,10 +18,14 @@
#pragma once
#include <signal.h>
-struct StackFrame;
+namespace lf {
+namespace server {
void
signals_init(void);
void
signals_destroy(void);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slot.cpp b/llamafile/server/slot.cpp
index a58eb9d9..82d57109 100644
--- a/llamafile/server/slot.cpp
+++ b/llamafile/server/slot.cpp
@@ -31,6 +31,9 @@
#include <cassert>
#include <cosmo.h>
+namespace lf {
+namespace server {
+
static int
choose_ctx_size(llama_model* model)
{
@@ -295,7 +298,10 @@ Slot::dump(std::string* result)
*result +=
llamafile_token_to_piece(ctx_, token, RENDER_SPECIAL_TOKENS);
} else if (history_[i].is_image()) {
- lf::convert_image_to_uri(result, history_[i].image().bytes());
+ convert_image_to_uri(result, history_[i].image().bytes());
}
}
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slot.h b/llamafile/server/slot.h
index 0e019070..593f7a3c 100644
--- a/llamafile/server/slot.h
+++ b/llamafile/server/slot.h
@@ -16,16 +16,20 @@
// limitations under the License.
#pragma once
+#include <ctime>
#include <string>
-#include <time.h>
#include <vector>
-struct Atom;
-struct Image;
struct llama_context;
struct llama_model;
struct clip_ctx;
+namespace lf {
+namespace server {
+
+struct Atom;
+struct Image;
+
struct Slot
{
enum
@@ -60,3 +64,6 @@ struct Slot
void tokenize(std::vector<Atom>*, std::string_view, bool);
void dump(std::string*);
};
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slot_entry.cpp b/llamafile/server/slot_entry.cpp
index a3dc2932..01451d4e 100644
--- a/llamafile/server/slot_entry.cpp
+++ b/llamafile/server/slot_entry.cpp
@@ -16,9 +16,12 @@
// limitations under the License.
#include "slot_entry.h"
-#include "atom.h"
-#include "slot.h"
-#include <assert.h>
+#include "llamafile/server/atom.h"
+#include "llamafile/server/slot.h"
+#include <cassert>
+
+namespace lf {
+namespace server {
SlotEntry::SlotEntry(Slot* slot) : slot_(slot), key_(nullptr)
{
@@ -51,3 +54,6 @@ operator<(const SlotEntry& lhs, const SlotEntry& rhs)
{
return *lhs.key() < *rhs.key();
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slot_entry.h b/llamafile/server/slot_entry.h
index e30bed0d..9d7e8c08 100644
--- a/llamafile/server/slot_entry.h
+++ b/llamafile/server/slot_entry.h
@@ -18,6 +18,9 @@
#pragma once
#include <vector>
+namespace lf {
+namespace server {
+
struct Slot;
struct Atom;
@@ -37,3 +40,6 @@ class SlotEntry
bool
operator<(const SlotEntry&, const SlotEntry&);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slots.cpp b/llamafile/server/slots.cpp
index 56c4bd7d..c0db2279 100644
--- a/llamafile/server/slots.cpp
+++ b/llamafile/server/slots.cpp
@@ -21,7 +21,10 @@
#include "llamafile/server/slot.h"
#include "llamafile/server/slot_entry.h"
#include "llamafile/vector.h"
-#include <assert.h>
+#include <cassert>
+
+namespace lf {
+namespace server {
Slots::Slots(llama_model* model) : model_(model)
{
@@ -87,7 +90,7 @@ Slots::take(const std::vector<Atom>& prefix)
--i;
Slot* slot = i->slot();
unassert(slot);
- int cpl = lf::vector_common_prefix_length(slot->history_, prefix);
+ int cpl = vector_common_prefix_length(slot->history_, prefix);
if (cpl == slot->history_.size()) {
slots_.erase(i);
pthread_mutex_unlock(&lock_);
@@ -121,3 +124,6 @@ Slots::give(Slot* slot)
pthread_cond_signal(&cond_);
pthread_mutex_unlock(&lock_);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slots.h b/llamafile/server/slots.h
index d7020761..de5a2bd2 100644
--- a/llamafile/server/slots.h
+++ b/llamafile/server/slots.h
@@ -20,10 +20,14 @@
#include <set>
#include <vector>
+struct llama_model;
+
+namespace lf {
+namespace server {
+
class Atom;
struct Slot;
class SlotEntry;
-struct llama_model;
struct Slots
{
@@ -40,3 +44,6 @@ struct Slots
Slot* take(const std::vector<Atom>&);
void give(Slot*);
};
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/slotz.cpp b/llamafile/server/slotz.cpp
index f00b0a77..97ad0f90 100644
--- a/llamafile/server/slotz.cpp
+++ b/llamafile/server/slotz.cpp
@@ -22,6 +22,9 @@
#include "utils.h"
#include "worker.h"
+namespace lf {
+namespace server {
+
bool
Client::slotz()
{
@@ -38,3 +41,6 @@ Client::slotz()
p = stpcpy(p, "Content-Type: text/plain\r\n");
return send_response(obuf_.p, p, dump);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/time.cpp b/llamafile/server/time.cpp
index 1ef3a741..1159f27e 100644
--- a/llamafile/server/time.cpp
+++ b/llamafile/server/time.cpp
@@ -16,19 +16,20 @@
// limitations under the License.
#include "time.h"
-
+#include "llamafile/crash.h"
+#include "llamafile/server/log.h"
#include <atomic>
+#include <csignal>
#include <pthread.h>
-#include <signal.h>
#include <unistd.h>
-#include "llamafile/crash.h"
-#include "llamafile/server/log.h"
-
//
// lockless implementation of gmtime_r() and localtime_r()
//
+namespace lf {
+namespace server {
+
struct Clock
{
std::atomic_uint roll;
@@ -203,3 +204,6 @@ localtime_lockless(long now, tm* tm)
{
time_lockless(&g_clck[1], now, tm);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/time.h b/llamafile/server/time.h
index d23a5f51..ed0c5892 100644
--- a/llamafile/server/time.h
+++ b/llamafile/server/time.h
@@ -18,6 +18,9 @@
#pragma once
#include <time.h>
+namespace lf {
+namespace server {
+
void
time_init();
@@ -29,3 +32,6 @@ gmtime_lockless(long, tm*);
void
localtime_lockless(long, tm*);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/tokenbucket.cpp b/llamafile/server/tokenbucket.cpp
index c04d5f44..a277e8d1 100644
--- a/llamafile/server/tokenbucket.cpp
+++ b/llamafile/server/tokenbucket.cpp
@@ -23,6 +23,9 @@
#include <signal.h>
#include <stdatomic.h>
+namespace lf {
+namespace server {
+
union TokenBucket
{
atomic_uint_fast64_t* w;
@@ -121,3 +124,6 @@ tokenbucket_acquire(unsigned ip)
{
return acquire_token(g_tokens.b, ip, FLAG_token_cidr);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/tokenbucket.h b/llamafile/server/tokenbucket.h
index 62fb290d..4baca060 100644
--- a/llamafile/server/tokenbucket.h
+++ b/llamafile/server/tokenbucket.h
@@ -17,6 +17,9 @@
#pragma once
+namespace lf {
+namespace server {
+
void
tokenbucket_init();
@@ -28,3 +31,6 @@ tokenbucket_acquire(unsigned);
void
tokenbucket_replenish();
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/tokenbucket_test.cpp b/llamafile/server/tokenbucket_test.cpp
index 789e4b87..149f5f00 100644
--- a/llamafile/server/tokenbucket_test.cpp
+++ b/llamafile/server/tokenbucket_test.cpp
@@ -15,50 +15,63 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "tokenbucket.h"
-
+#include "llamafile/llamafile.h"
+#include "llamafile/server/tokenbucket.h"
+#include <climits>
#include <cosmo.h>
-#include <limits.h>
+#include <cstdlib>
-#include "llamafile/llamafile.h"
+namespace lf {
+namespace server {
+namespace {
-int
-main(int argc, char* argv[])
+void
+tokenbucket_test()
{
FLAG_token_rate = .0001;
FLAG_token_cidr = 24;
tokenbucket_init();
if (tokenbucket_acquire(0x7f000001) != 0)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000002) != 1)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000003) != 2)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000100) != 0)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000101) != 1)
- return 1;
+ exit(1);
tokenbucket_replenish();
tokenbucket_replenish();
if (tokenbucket_acquire(0x7f000003) != 1)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000003) != 2)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000101) != 0)
- return 1;
+ exit(1);
tokenbucket_replenish();
tokenbucket_replenish();
tokenbucket_replenish();
if (tokenbucket_acquire(0x7f000003) != 0)
- return 1;
+ exit(1);
if (tokenbucket_acquire(0x7f000101) != 0)
- return 1;
+ exit(1);
tokenbucket_destroy();
CheckForMemoryLeaks();
}
+
+} // namespace
+} // namespace server
+} // namespace lf
+
+int
+main()
+{
+ lf::server::tokenbucket_test();
+}
diff --git a/llamafile/server/tokenize.cpp b/llamafile/server/tokenize.cpp
index d32a732d..cc20e829 100644
--- a/llamafile/server/tokenize.cpp
+++ b/llamafile/server/tokenize.cpp
@@ -23,11 +23,14 @@
#include "llamafile/server/log.h"
#include "llamafile/server/signals.h"
#include "llamafile/server/utils.h"
-#include <string.h>
+#include <cstring>
#include <sys/resource.h>
#include <utility>
#include <vector>
+namespace lf {
+namespace server {
+
struct TokenizeParams
{
bool add_special;
@@ -171,3 +174,6 @@ Client::tokenize()
p = stpcpy(p, "\r\n");
return send_response(headers, p, content);
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/utils.h b/llamafile/server/utils.h
index 30205316..bd00dad5 100644
--- a/llamafile/server/utils.h
+++ b/llamafile/server/utils.h
@@ -21,9 +21,13 @@
#include <__fwd/vector.h>
#include <optional>
-class Atom;
struct llama_model;
+namespace lf {
+namespace server {
+
+class Atom;
+
extern const signed char kHexToInt[256];
bool
@@ -37,3 +41,6 @@ atomize(const llama_model* model,
std::vector<Atom>* result,
std::string_view s,
bool parse_special);
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/v1_chat_completions.cpp b/llamafile/server/v1_chat_completions.cpp
index d880658b..c517ee4a 100644
--- a/llamafile/server/v1_chat_completions.cpp
+++ b/llamafile/server/v1_chat_completions.cpp
@@ -32,11 +32,14 @@
#include "llamafile/server/worker.h"
#include "llamafile/string.h"
#include "llamafile/vector.h"
-#include <math.h>
-#include <string.h>
+#include <cmath>
+#include <cstring>
#include <sys/resource.h>
#include <vector>
+namespace lf {
+namespace server {
+
struct V1ChatCompletionParams
{
bool stream = false;
@@ -61,7 +64,7 @@ struct V1ChatCompletionParams
bool should_stop(const std::vector<Atom>& history)
{
for (const auto& suffix : stop)
- if (lf::vector_ends_with(history, suffix))
+ if (vector_ends_with(history, suffix))
return true;
return false;
}
@@ -576,3 +579,6 @@ Client::v1_chat_completions()
return send_response(obuf_.p, p, response->content);
}
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/worker.cpp b/llamafile/server/worker.cpp
index 1ae3240c..668a115c 100644
--- a/llamafile/server/worker.cpp
+++ b/llamafile/server/worker.cpp
@@ -24,10 +24,13 @@
#include "llamafile/server/tokenbucket.h"
#include "llamafile/threadlocal.h"
#include "llamafile/trust.h"
-#include <assert.h>
+#include <cassert>
#include <exception>
#include <pthread.h>
+namespace lf {
+namespace server {
+
Worker::Worker(Server* server, llama_model* model)
: server_(server), client_(model)
{
@@ -156,3 +159,6 @@ Worker::run()
cleanup.set(nullptr);
retire();
}
+
+} // namespace server
+} // namespace lf
diff --git a/llamafile/server/worker.h b/llamafile/server/worker.h
index a40b240b..e23b8630 100644
--- a/llamafile/server/worker.h
+++ b/llamafile/server/worker.h
@@ -22,9 +22,13 @@
#define WORKER(e) DLL_CONTAINER(Worker, elem_, e)
-struct Server;
struct llama_model;
+namespace lf {
+namespace server {
+
+struct Server;
+
struct Worker
{
Server* server_;
@@ -42,3 +46,6 @@ struct Worker
void retire();
void kill();
};
+
+} // namespace server
+} // namespace lf