interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / engine / mojo_engine.c

154 lines · 5336 bytes Raw Blame History
1/* mojo_engine.c — Minimal inference engine binary.
2 *
3 * Links against ../kernels/q4_kernel_omp.so for quantised matmul kernels.
4 * Exposes an HTTP /health endpoint for readiness checks.
5 *
6 * Usage: ./mojo_engine --model <path> --port <num> --threads <num>
7 **/
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <pthread.h>
14#include <signal.h>
15#include <errno.h>
16#include <sys/socket.h>
17#include <netinet/in.h>
18#include <omp.h>
19
20/* ── Kernel symbols from q4_kernel_omp.so ────────────────────────── */
21extern void q4_matmul_omp(const uint8_t *w, const float *x, float *out,
22 int n_rows, int n_cols, int type_size);
23extern void q4_set_num_threads(int n);
24extern int q4_get_max_threads(void);
25
26/* ── Global configuration ───────────────────────────────────────── */
27static int g_port = 8080;
28static int g_threads = 4;
29static const char *g_model_path = NULL;
30static volatile int g_running = 1;
31
32/* ── Signal handler ──────────────────────────────────────────────── */
33static void handle_signal(int sig) {
34 (void)sig;
35 g_running = 0;
36}
37
38/* ── Simple HTTP response helpers ─────────────────────────────────── */
39static const char HTTP_200_JSON[] =
40 "HTTP/1.1 200 OK\r\n"
41 "Content-Type: application/json\r\n"
42 "Content-Length: %zu\r\n"
43 "Connection: close\r\n\r\n%s";
44
45static const char HTTP_404[] =
46 "HTTP/1.1 404 Not Found\r\n"
47 "Content-Length: 0\r\n"
48 "Connection: close\r\n\r\n";
49
50static void send_response(int client_fd, const char *header, const char *body) {
51 char buf[4096];
52 int n = snprintf(buf, sizeof(buf), header, strlen(body), body);
53 send(client_fd, buf, n, 0);
54}
55
56/* ── HTTP server thread ───────────────────────────────────────────── */
57static void *http_server(void *arg) {
58 (void)arg;
59 int server_fd = socket(AF_INET, SOCK_STREAM, 0);
60 if (server_fd < 0) {
61 perror("socket");
62 return NULL;
63 }
64
65 int opt = 1;
66 setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
67
68 struct sockaddr_in addr = {
69 .sin_family = AF_INET,
70 .sin_addr.s_addr = INADDR_ANY,
71 .sin_port = htons(g_port),
72 };
73
74 if (bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
75 perror("bind");
76 close(server_fd);
77 return NULL;
78 }
79
80 if (listen(server_fd, 16) < 0) {
81 perror("listen");
82 close(server_fd);
83 return NULL;
84 }
85
86 fprintf(stderr, "[engine] listening on port %d (threads=%d, model=%s)\n",
87 g_port, g_threads, g_model_path ? g_model_path : "(none)");
88
89 while (g_running) {
90 fd_set rfds;
91 struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
92 FD_ZERO(&rfds);
93 FD_SET(server_fd, &rfds);
94 int sel = select(server_fd + 1, &rfds, NULL, NULL, &tv);
95 if (sel <= 0) continue;
96
97 int client_fd = accept(server_fd, NULL, NULL);
98 if (client_fd < 0) continue;
99
100 char req[2048] = {0};
101 recv(client_fd, req, sizeof(req) - 1, 0);
102
103 /* Very minimal routing */
104 if (strncmp(req, "GET /health", 11) == 0) {
105 const char *body = "{\"status\":\"ok\"}";
106 send_response(client_fd, HTTP_200_JSON, body);
107 } else {
108 send(client_fd, HTTP_404, strlen(HTTP_404), 0);
109 }
110 close(client_fd);
111 }
112
113 close(server_fd);
114 return NULL;
115}
116
117/* ── Argument parsing ────────────────────────────────────────────── */
118static void parse_args(int argc, char **argv) {
119 for (int i = 1; i < argc; i++) {
120 if (strcmp(argv[i], "--model") == 0 && i + 1 < argc) {
121 g_model_path = argv[++i];
122 } else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
123 g_port = atoi(argv[++i]);
124 } else if (strcmp(argv[i], "--threads") == 0 && i + 1 < argc) {
125 g_threads = atoi(argv[++i]);
126 } else {
127 fprintf(stderr, "Unknown or incomplete option: %s\n", argv[i]);
128 fprintf(stderr, "Usage: %s --model <path> --port <num> --threads <num>\n",
129 argv[0]);
130 exit(1);
131 }
132 }
133}
134
135/* ── Main ────────────────────────────────────────────────────────── */
136int main(int argc, char **argv) {
137 parse_args(argc, argv);
138
139 /* Configure OpenMP threads via kernel helper */
140 q4_set_num_threads(g_threads);
141 fprintf(stderr, "[engine] OMP max threads: %d\n", q4_get_max_threads());
142
143 signal(SIGINT, handle_signal);
144 signal(SIGTERM, handle_signal);
145
146 pthread_t server_tid;
147 pthread_create(&server_tid, NULL, http_server, NULL);
148
149 /* Block until shutdown */
150 pthread_join(server_tid, NULL);
151
152 fprintf(stderr, "[engine] shut down.\n");
153 return 0;
154}