Fix engine compilation errors
Browse files- fix_compile.py +104 -0
fix_compile.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Fix compilation errors and test again."""
|
| 3 |
+
import subprocess, os
|
| 4 |
+
|
| 5 |
+
TOKEN = "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"
|
| 6 |
+
subprocess.run(["git", "clone", f"https://{TOKEN}@github.com/ticketguy/Lila.git", "/app/lila"], check=True)
|
| 7 |
+
os.chdir("/app/lila")
|
| 8 |
+
subprocess.run(["git", "config", "user.name", "0xticketguy"], check=True)
|
| 9 |
+
subprocess.run(["git", "config", "user.email", "0xticketguy@harboria.dev"], check=True)
|
| 10 |
+
|
| 11 |
+
# Fix 1: model.c needs _GNU_SOURCE for madvise
|
| 12 |
+
with open("engine/runtime/model.c", "r") as f:
|
| 13 |
+
content = f.read()
|
| 14 |
+
if "#define _GNU_SOURCE" not in content:
|
| 15 |
+
content = "#define _GNU_SOURCE\n" + content
|
| 16 |
+
with open("engine/runtime/model.c", "w") as f:
|
| 17 |
+
f.write(content)
|
| 18 |
+
|
| 19 |
+
# Fix 2: attention.c calls dequant_matvec but it's static in inference.c
|
| 20 |
+
# Make dequant_matvec non-static and add proper declaration
|
| 21 |
+
with open("engine/runtime/inference.c", "r") as f:
|
| 22 |
+
content = f.read()
|
| 23 |
+
content = content.replace("static void dequant_matvec(", "void dequant_matvec(")
|
| 24 |
+
content = content.replace("static void matvec(", "void matvec(")
|
| 25 |
+
with open("engine/runtime/inference.c", "w") as f:
|
| 26 |
+
f.write(content)
|
| 27 |
+
|
| 28 |
+
# Fix 3: Remove the extern declaration at bottom of attention.c (conflicts)
|
| 29 |
+
with open("engine/runtime/attention.c", "r") as f:
|
| 30 |
+
content = f.read()
|
| 31 |
+
content = content.replace("extern void dequant_matvec(float *out, const LilaQuantWeight *w, const float *vec);", "")
|
| 32 |
+
# Add forward declaration at top instead
|
| 33 |
+
if "void dequant_matvec(" not in content.split("void lila_attention")[0]:
|
| 34 |
+
content = content.replace('#include "model.h"', '#include "model.h"\n\n/* From inference.c */\nextern void dequant_matvec(float *out, const LilaQuantWeight *w, const float *vec);')
|
| 35 |
+
with open("engine/runtime/attention.c", "w") as f:
|
| 36 |
+
f.write(content)
|
| 37 |
+
|
| 38 |
+
# Fix 4: transformer.c also needs the extern declaration fixed
|
| 39 |
+
with open("engine/runtime/transformer.c", "r") as f:
|
| 40 |
+
content = f.read()
|
| 41 |
+
# Remove bad extern and fix
|
| 42 |
+
content = content.replace("extern void lila_rmsnorm_avx2(float *out, const float *x, const float *weight, int size, float eps);", "")
|
| 43 |
+
content = content.replace("extern void lila_attention(float *output, const float *input, LilaLayer *layer,\n LilaKVCache *cache, int layer_idx, int position);", "")
|
| 44 |
+
content = content.replace("extern void dequant_matvec(float *out, const LilaQuantWeight *w, const float *vec);", "")
|
| 45 |
+
# Add clean declarations at top
|
| 46 |
+
header_block = """/* External declarations */
|
| 47 |
+
extern void lila_rmsnorm_avx2(float *out, const float *x, const float *weight, int size, float eps);
|
| 48 |
+
extern void lila_attention(float *output, const float *input, LilaLayer *layer,
|
| 49 |
+
LilaKVCache *cache, int layer_idx, int position);
|
| 50 |
+
extern void dequant_matvec(float *out, const LilaQuantWeight *w, const float *vec);
|
| 51 |
+
"""
|
| 52 |
+
if "External declarations" not in content:
|
| 53 |
+
content = content.replace('#include "model.h"\n#include <math.h>', f'#include "model.h"\n#include <math.h>\n\n{header_block}')
|
| 54 |
+
with open("engine/runtime/transformer.c", "w") as f:
|
| 55 |
+
f.write(content)
|
| 56 |
+
|
| 57 |
+
# Fix 5: Add CFLAGS -D_GNU_SOURCE to Makefile
|
| 58 |
+
with open("engine/Makefile", "r") as f:
|
| 59 |
+
content = f.read()
|
| 60 |
+
content = content.replace("CFLAGS := -O3", "CFLAGS := -D_GNU_SOURCE -O3")
|
| 61 |
+
with open("engine/Makefile", "w") as f:
|
| 62 |
+
f.write(content)
|
| 63 |
+
|
| 64 |
+
# Commit
|
| 65 |
+
subprocess.run(["git", "add", "-A"], check=True)
|
| 66 |
+
subprocess.run(["git", "commit", "-m", "Fix engine compilation errors\n\n- Add _GNU_SOURCE for madvise/MADV_SEQUENTIAL\n- Fix dequant_matvec linkage (non-static + extern declarations)\n- Fix transformer.c extern declarations\n- Add -D_GNU_SOURCE to CFLAGS"], check=True)
|
| 67 |
+
subprocess.run(["git", "push", "origin", "main"], check=True)
|
| 68 |
+
print("✅ Fixes pushed")
|
| 69 |
+
|
| 70 |
+
# Now try to compile
|
| 71 |
+
os.chdir("/app/lila/engine")
|
| 72 |
+
subprocess.run(["apt-get", "update", "-qq"], check=False, capture_output=True)
|
| 73 |
+
subprocess.run(["apt-get", "install", "-y", "-qq", "nasm"], check=False, capture_output=True)
|
| 74 |
+
|
| 75 |
+
print("\n=== Building engine ===")
|
| 76 |
+
result = subprocess.run(["make"], capture_output=True, text=True)
|
| 77 |
+
print(result.stdout[-500:] if result.stdout else "no stdout")
|
| 78 |
+
if result.returncode != 0:
|
| 79 |
+
print("ERRORS:", result.stderr[-1000:])
|
| 80 |
+
|
| 81 |
+
# Try with stubs
|
| 82 |
+
print("\n=== Trying C-only with stubs ===")
|
| 83 |
+
with open("/tmp/stubs.c", "w") as f:
|
| 84 |
+
f.write('#include <stdint.h>\n#include <math.h>\n'
|
| 85 |
+
'void lila_matvec_avx2(float *o, const float *m, const float *v, int r, int c) { for(int i=0;i<r;i++){float s=0;for(int j=0;j<c;j++)s+=m[i*c+j]*v[j];o[i]=s;}}\n'
|
| 86 |
+
'void lila_rmsnorm_avx2(float *o, const float *x, const float *w, int sz, float e) { float ss=0;for(int i=0;i<sz;i++)ss+=x[i]*x[i];ss=1.0f/sqrtf(ss/sz+e);for(int i=0;i<sz;i++)o[i]=x[i]*ss*w[i];}\n'
|
| 87 |
+
'void lila_dequant_int4_avx2(float *o, const uint8_t *i, const float *c, const float *s, int n, int g) {}\n'
|
| 88 |
+
'void lila_print_cpu_features(void) { printf("CPU: x86_64 (stubs)\\n"); }\n')
|
| 89 |
+
|
| 90 |
+
c_files = ["runtime/model.c", "runtime/inference.c", "runtime/attention.c",
|
| 91 |
+
"runtime/transformer.c", "runtime/tokenizer.c", "runtime/dispatch.c",
|
| 92 |
+
"interface/cli.c", "/tmp/stubs.c"]
|
| 93 |
+
cmd = ["gcc", "-D_GNU_SOURCE", "-O2", "-Wall", "-std=c11", "-o", "lila-engine"] + c_files + ["-lm", "-lpthread", "-I", "runtime/"]
|
| 94 |
+
r2 = subprocess.run(cmd, capture_output=True, text=True)
|
| 95 |
+
if r2.returncode == 0:
|
| 96 |
+
print("✅ ENGINE COMPILED (C with scalar stubs)")
|
| 97 |
+
r3 = subprocess.run(["./lila-engine", "--test"], capture_output=True, text=True)
|
| 98 |
+
print(f"Test: {r3.stdout.strip()}")
|
| 99 |
+
else:
|
| 100 |
+
print(f"❌ Still failing: {r2.stderr[:500]}")
|
| 101 |
+
else:
|
| 102 |
+
print("✅ ENGINE COMPILED WITH ASSEMBLY KERNELS")
|
| 103 |
+
r3 = subprocess.run(["./lila-engine", "--test"], capture_output=True, text=True)
|
| 104 |
+
print(f"Test: {r3.stdout.strip()}")
|