tchbcb commited on
Commit
5073c4a
·
verified ·
1 Parent(s): 8f4b74f

colab T4 llama.cpp CUDA sm_75 build chain

Browse files
artifacts/m15_scripts/colab_compile.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """colab_compile.py — Colab T4 专用 llama.cpp CUDA 编译链 (sm_75)
4
+ 编译 -> 打包 llama_cpp_cuda_colab_t4.tar.gz -> 第一时间推仓 (tchbcb/samai-9b)
5
+ 幂等旗标: /content/k8b/CC_*
6
+ 日志: /content/k8b/colab_compile.log"""
7
+ import os, subprocess, sys, time, traceback
8
+
9
+ K8B = "/content/k8b"
10
+ BIN = "/content/lcbin"
11
+ LCSRC = "/content/lcbuild/llama.cpp"
12
+ LOG = K8B + "/colab_compile.log"
13
+ os.makedirs(K8B, exist_ok=True)
14
+ log = open(LOG, "a", buffering=1)
15
+
16
+ def P(m):
17
+ log.write("[%s] %s\n" % (time.strftime("%m-%d %H:%M:%S"), m))
18
+
19
+ def sh(c, t=7200):
20
+ p = subprocess.run(c, shell=True, capture_output=True, text=True, timeout=t, errors="replace")
21
+ return ((p.stdout or "") + (p.stderr or ""))[-1500:]
22
+
23
+ def flag(n, c="1"):
24
+ open(K8B + "/" + n, "w").write(str(c)[:400])
25
+
26
+ def have(n):
27
+ return os.path.exists(K8B + "/" + n)
28
+
29
+ def hf_token():
30
+ return open("/root/.cache/huggingface/token").read().strip()
31
+
32
+ def main():
33
+ t0 = time.time()
34
+ P("==== colab compile start ====")
35
+ try:
36
+ # CC1: clone
37
+ if not have("CC_CLONE"):
38
+ if not os.path.exists(LCSRC + "/CMakeLists.txt"):
39
+ r = sh("rm -rf /content/lcbuild && mkdir -p /content/lcbuild && "
40
+ "git clone --depth 1 https://github.com/ggml-org/llama.cpp %s" % LCSRC, 900)
41
+ if not os.path.exists(LCSRC + "/CMakeLists.txt"):
42
+ raise RuntimeError("clone failed: " + r[-300:])
43
+ flag("CC_CLONE", sh("cd %s && git rev-parse --short HEAD" % LCSRC, 15))
44
+ P("clone ok")
45
+ # CC2: cmake + build (sm_75, 2 核 -j2)
46
+ if not have("CC_BUILD"):
47
+ r = sh("cd /content/lcbuild && cmake -S %s -B %s/build -DGGML_CUDA=ON "
48
+ "-DCMAKE_CUDA_ARCHITECTURES=75 -DLLAMA_CURL=OFF -DCMAKE_BUILD_TYPE=Release "
49
+ "> %s/cmake.log 2>&1; echo RC=$?; tail -3 %s/cmake.log" % (LCSRC, LCSRC, LCSRC, LCSRC), 600)
50
+ P("cmake: " + r[-200:])
51
+ if "RC=0" not in r:
52
+ raise RuntimeError("cmake failed")
53
+ r = sh("cd %s/build && cmake --build . --config Release -j2 > %s/build.log 2>&1; echo RC=$?" % (LCSRC, LCSRC), 7200)
54
+ P("build: " + r[-200:])
55
+ if "RC=0" not in r:
56
+ raise RuntimeError("build failed: " + sh("tail -20 %s/build.log" % LCSRC)[-600:])
57
+ flag("CC_BUILD")
58
+ P("build ok (%.0fs)" % (time.time() - t0))
59
+ # CC3: 平铺 bin+so -> /content/lcbin
60
+ if not have("CC_FLAT"):
61
+ os.makedirs(BIN, exist_ok=True)
62
+ sh("cp -a %s/build/bin/* %s/ 2>/dev/null" % (LCSRC, BIN), 300)
63
+ sh("find %s/build -name '*.so*' -exec cp -a {} %s/ \\; 2>/dev/null" % (LCSRC, BIN), 300)
64
+ need = ["llama-server", "llama-quantize", "llama-cli"]
65
+ miss = [b for b in need if not os.path.exists(BIN + "/" + b)]
66
+ if miss:
67
+ raise RuntimeError("binaries missing: %s" % miss)
68
+ r = sh("ls -la %s | head -20" % BIN, 15)
69
+ P("flat ok:\n" + r)
70
+ flag("CC_FLAT")
71
+ # CC4: 打包 + 第一时间推仓
72
+ if not have("CC_PUSH"):
73
+ tar = K8B + "/llama_cpp_cuda_colab_t4.tar.gz"
74
+ sh("tar czf %s -C %s . > /dev/null 2>&1" % (tar, BIN), 600)
75
+ if os.path.getsize(tar) < 1e8:
76
+ raise RuntimeError("tar too small: %d" % os.path.getsize(tar))
77
+ from huggingface_hub import HfApi
78
+ api = HfApi(token=hf_token())
79
+ api.upload_file(path_or_fileobj=tar, path_in_repo="llama_cpp_cuda_colab_t4.tar.gz",
80
+ repo_id="tchbcb/samai-9b", repo_type="model",
81
+ commit_message="colab T4 (sm_75) CUDA build, first-push")
82
+ flag("CC_PUSH", "%.0fs" % (time.time() - t0))
83
+ P("CUDA tar pushed (%.0f MB, %.0fs)" % (os.path.getsize(tar) / 1e6, time.time() - t0))
84
+ P("==== COMPILE_ALL_DONE in %.0fs ====" % (time.time() - t0))
85
+ flag("COMPILE_ALL_DONE", "%.0fs" % (time.time() - t0))
86
+ except Exception as e:
87
+ traceback.print_exc(file=log)
88
+ flag("COMPILE_FAIL", repr(e)[:200])
89
+ P("==== COMPILE_FAIL: %s ====" % repr(e)[:200])
90
+
91
+ if __name__ == "__main__":
92
+ main()