Skip to content

Official Codebase of 3DCodeVerse

Python

finetune: GLSL render verification (persistent-browser WebGL2 harness), image-conditioned dataset builder, EVAL_DESIGN.md — benchmark design for text/image to 3D code

gaoypengc1d41c01c4f7
diff --git a/finetune/configs/lf/export_27b_v4.yaml b/finetune/configs/lf/export_27b_v4.yaml
new file mode 100644
index 00000000..a440f3cc
--- /dev/null
+++ b/finetune/configs/lf/export_27b_v4.yaml
@@ -0,0 +1,12 @@
+### Merge a LoRA adapter into the base model (LLaMA-Factory): llamafactory-cli export <this file>
+model_name_or_path: /wekafs/ict/hx_624/models/Qwen3.8-27B
+adapter_name_or_path: /wekafs/ict/hx_624/llm-ft/runs/lf_qwen38_27b_lora_v4
+template: qwen3_5
+enable_thinking: false
+trust_remote_code: true
+finetuning_type: lora
+
+export_dir: /wekafs/ict/hx_624/llm-ft/runs/lf_qwen38_27b_lora_v4/merged
+export_size: 5
+export_device: cpu
+export_legacy_format: false
diff --git a/finetune/configs/lf/lora_27b_v5.yaml b/finetune/configs/lf/lora_27b_v5.yaml
new file mode 100644
index 00000000..b7a2e607
--- /dev/null
+++ b/finetune/configs/lf/lora_27b_v5.yaml
@@ -0,0 +1,44 @@
+model_name_or_path: /wekafs/ict/hx_624/models/Qwen3.8-27B
+trust_remote_code: true
+flash_attn: fa2
+stage: sft
+do_train: true
+finetuning_type: lora
+lora_rank: 64
+lora_alpha: 128
+lora_dropout: 0.05
+lora_target: all
+freeze_vision_tower: true
+freeze_multi_modal_projector: true
+dataset_dir: /wekafs/ict/hx_624/llm-ft/data/lf
+dataset: md_27b_v5_train
+eval_dataset: md_27b_v5_val
+template: qwen3_5
+enable_thinking: false
+cutoff_len: 3072
+preprocessing_num_workers: 16
+dataloader_num_workers: 4
+neat_packing: true
+packing: true
+tokenized_path: /wekafs/ict/hx_624/llm-ft/data/lf/tok_v5_3072
+output_dir: /wekafs/ict/hx_624/llm-ft/runs/lf_qwen38_27b_lora_v5
+logging_steps: 5
+save_steps: 300
+save_only_model: true
+plot_loss: true
+overwrite_output_dir: true
+report_to: tensorboard
+per_device_train_batch_size: 1
+gradient_accumulation_steps: 6
+learning_rate: 0.0001
+num_train_epochs: 1.0
+lr_scheduler_type: cosine
+warmup_ratio: 0.05
+bf16: true
+gradient_checkpointing: true
+ddp_timeout: 180000000
+resume_from_checkpoint: null
+per_device_eval_batch_size: 1
+eval_strategy: steps
+eval_steps: 300
+save_total_limit: 8
diff --git a/finetune/configs/lf/lora_9b_clean.yaml b/finetune/configs/lf/lora_9b_clean.yaml
new file mode 100644
index 00000000..1b6717f3
--- /dev/null
+++ b/finetune/configs/lf/lora_9b_clean.yaml
@@ -0,0 +1,44 @@
+model_name_or_path: /wekafs/ict/hx_624/models/Qwen3.5-9B
+trust_remote_code: true
+flash_attn: fa2
+stage: sft
+do_train: true
+finetuning_type: lora
+lora_rank: 64
+lora_alpha: 128
+lora_dropout: 0.05
+lora_target: all
+freeze_vision_tower: true
+freeze_multi_modal_projector: true
+dataset_dir: /wekafs/ict/hx_624/llm-ft/data/lf
+dataset: md_9b_clean_train
+eval_dataset: md_9b_clean_val
+template: qwen3_5
+enable_thinking: false
+cutoff_len: 8192
+preprocessing_num_workers: 16
+dataloader_num_workers: 4
+neat_packing: true
+packing: true
+tokenized_path: /wekafs/ict/hx_624/llm-ft/data/lf/tok_9b_clean_8192
+output_dir: /wekafs/ict/hx_624/llm-ft/runs/lf_qwen35_9b_lora_clean
+logging_steps: 5
+save_steps: 400
+save_only_model: true
+plot_loss: true
+overwrite_output_dir: true
+report_to: tensorboard
+per_device_train_batch_size: 1
+gradient_accumulation_steps: 8
+learning_rate: 0.0001
+num_train_epochs: 1.0
+lr_scheduler_type: cosine
+warmup_ratio: 0.05
+bf16: true
+gradient_checkpointing: true
+ddp_timeout: 180000000
+resume_from_checkpoint: null
+per_device_eval_batch_size: 1
+eval_strategy: steps
+eval_steps: 400
+save_total_limit: 8
diff --git a/finetune/data_specs/dataset_info.json b/finetune/data_specs/dataset_info.json
index a876a31b..ec4b72e6 100644
--- a/finetune/data_specs/dataset_info.json
+++ b/finetune/data_specs/dataset_info.json
@@ -523,5 +523,37 @@
"messages": "conversations",
"system": "system"
}
+ },
+ "md_27b_v5_train": {
+ "file_name": "md_27b_v5_train.json",
+ "formatting": "sharegpt",
+ "columns": {
+ "messages": "conversations",
+ "system": "system"
+ }
+ },
+ "md_27b_v5_val": {
+ "file_name": "md_27b_v5_val.json",
+ "formatting": "sharegpt",
+ "columns": {
+ "messages": "conversations",
+ "system": "system"
+ }
+ },
+ "md_9b_clean_train": {
+ "file_name": "md_9b_clean_train.json",
+ "formatting": "sharegpt",
+ "columns": {
+ "messages": "conversations",
+ "system": "system"
+ }
+ },
+ "md_9b_clean_val": {
+ "file_name": "md_9b_clean_val.json",
+ "formatting": "sharegpt",
+ "columns": {
+ "messages": "conversations",
+ "system": "system"
+ }
}
}
\ No newline at end of file
diff --git a/finetune/eval/glsl_render.py b/finetune/eval/glsl_render.py
new file mode 100644
index 00000000..5061be39
--- /dev/null
+++ b/finetune/eval/glsl_render.py
@@ -0,0 +1,119 @@
+"""Render a Shadertoy fragment shader in a real WebGL2 context and check that it draws something.
+
+Compiling is necessary but not sufficient: a shader can compile and still paint a constant colour. This runs the
+shader in headless Chromium (the same browser the three.js runner uses), samples two frames (iTime = 0 and 1.7 s)
+and reports, per frame, how many distinct quantised colours the canvas holds and its pixel standard deviation.
+
+ render_glsl(code, workdir) -> {status, error, distinct, std, changed, shot}
+ status OK renders a non-uniform image
+ STATIC renders, but the image is (near) uniform — usually a black screen
+ FAIL shader/link error inside the browser, or the page threw
+ changed True if frame(1.7s) differs from frame(0) — i.e. the shader animates
+
+usage as a script: python eval/glsl_render.py file1.glsl [file2.glsl ...]
+"""
+import json
+import os
+import sys
+
+HTML = """<!doctype html><html><body style="margin:0">
+<canvas id=c width=320 height=240></canvas>
+<script>
+const FS_BODY = %s;
+const vs = `#version 300 es
+in vec2 p; void main(){ gl_Position = vec4(p,0.,1.); }`;
+const fsHead = `#version 300 es
+precision highp float; precision highp int;
+uniform vec3 iResolution; uniform float iTime; uniform float iTimeDelta; uniform int iFrame;
+uniform vec4 iMouse; uniform vec4 iDate; uniform float iSampleRate; uniform float iFrameRate;
+uniform float iChannelTime[4]; uniform vec3 iChannelResolution[4];
+uniform sampler2D iChannel0; uniform sampler2D iChannel1; uniform sampler2D iChannel2; uniform sampler2D iChannel3;
+out vec4 _fragColor;
+`;
+const fsTail = `
+void main(){ mainImage(_fragColor, gl_FragCoord.xy); }`;
+window.__result = {ok:false, err:null};
+(function(){
+ const c = document.getElementById('c');
+ const gl = c.getContext('webgl2', {preserveDrawingBuffer:true});
+ if (!gl) { window.__result.err = 'no webgl2'; return; }
+ function sh(t, src){ const s = gl.createShader(t); gl.shaderSource(s, src); gl.compileShader(s);
+ if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(s).split('\\n')[0]); return s; }
+ try {
+ const body = FS_BODY.replace(/^\\s*#version[^\\n]*\\n/, '');
+ const p = gl.createProgram();
+ gl.attachShader(p, sh(gl.VERTEX_SHADER, vs));
+ gl.attachShader(p, sh(gl.FRAGMENT_SHADER, fsHead + body + fsTail));
+ gl.linkProgram(p);
+ if (!gl.getProgramParameter(p, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(p).split('\\n')[0]);
+ gl.useProgram(p);
+ const buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 3,-1, -1,3]), gl.STATIC_DRAW);
+ const loc = gl.getAttribLocation(p, 'p'); gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
+ const tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([128,128,128,255]));
+ for (let i=0;i<4;i++){ const u = gl.getUniformLocation(p, 'iChannel'+i); if (u) gl.uniform1i(u, 0); }
+ window.__draw = function(t){
+ gl.uniform3f(gl.getUniformLocation(p,'iResolution'), c.width, c.height, 1);
+ gl.uniform1f(gl.getUniformLocation(p,'iTime'), t);
+ gl.uniform1f(gl.getUniformLocation(p,'iTimeDelta'), 1/60);
+ gl.uniform1i(gl.getUniformLocation(p,'iFrame'), Math.floor(t*60));
+ gl.uniform1f(gl.getUniformLocation(p,'iFrameRate'), 60);
+ gl.viewport(0,0,c.width,c.height); gl.drawArrays(gl.TRIANGLES, 0, 3);
+ const px = new Uint8Array(c.width*c.height*4);
+ gl.readPixels(0,0,c.width,c.height, gl.RGBA, gl.UNSIGNED_BYTE, px);
+ const seen = new Set(); let sum=0, sum2=0, n=0;
+ for (let i=0;i<px.length;i+=4*7){ const r=px[i]>>4, g=px[i+1]>>4, b=px[i+2]>>4;
+ seen.add((r<<8)|(g<<4)|b); const v=(px[i]+px[i+1]+px[i+2])/3; sum+=v; sum2+=v*v; n++; }
+ const mean=sum/n; return {distinct: seen.size, std: Math.sqrt(Math.max(0, sum2/n - mean*mean)), mean};
+ };
+ window.__result.ok = true;
+ } catch(e) { window.__result.err = String(e.message || e); }
+})();
+</script></body></html>"""
+
+
+def render_glsl(code, workdir, timeout=40):
+ os.makedirs(workdir, exist_ok=True)
+ html = HTML % json.dumps(code)
+ p = os.path.join(workdir, "index.html")
+ open(p, "w").write(html)
+ rep = {"status": "FAIL", "error": None, "distinct": 0, "std": 0.0, "changed": False, "shot": None}
+ try:
+ from playwright.sync_api import sync_playwright
+ with sync_playwright() as pw:
+ b = pw.chromium.launch(args=["--use-gl=swiftshader", "--enable-unsafe-swiftshader", "--no-sandbox"])
+ pg = b.new_page()
+ errs = []
+ pg.on("pageerror", lambda e: errs.append(str(e)[:200]))
+ pg.goto("file://" + os.path.abspath(p), wait_until="load", timeout=timeout * 1000)
+ res = pg.evaluate("window.__result")
+ if not res or not res.get("ok"):
+ rep["error"] = (res or {}).get("err") or (errs[0] if errs else "no result")
+ b.close()
+ return rep
+ a = pg.evaluate("window.__draw(0.0)")
+ c = pg.evaluate("window.__draw(1.7)")
+ try: # a screenshot failure must not discard a good render
+ shot = os.path.join(workdir, "frame.png")
+ pg.screenshot(path=shot)
+ rep["shot"] = shot
+ except Exception:
+ rep["shot"] = None
+ b.close()
+ rep["distinct"] = max(a["distinct"], c["distinct"])
+ rep["std"] = round(max(a["std"], c["std"]), 2)
+ rep["changed"] = abs(a["mean"] - c["mean"]) > 0.5 or a["distinct"] != c["distinct"]
+ rep["status"] = "OK" if (rep["distinct"] > 3 and rep["std"] > 2.0) else "STATIC"
+ if rep["status"] == "STATIC":
+ rep["error"] = f"uniform image (distinct={rep['distinct']}, std={rep['std']})"
+ except Exception as e:
+ rep["error"] = str(e)[:200]
+ return rep
+
+
+if __name__ == "__main__":
+ import tempfile
+ for f in sys.argv[1:]:
+ r = render_glsl(open(f).read(), tempfile.mkdtemp())
+ print(f"{r['status']:6s} distinct={r['distinct']:3d} std={r['std']:6.2f} animated={r['changed']} {os.path.basename(f)} {r['error'] or ''}")
diff --git a/finetune/eval/glsl_render_batch.py b/finetune/eval/glsl_render_batch.py
new file mode 100644
index 00000000..0fab900a
--- /dev/null
+++ b/finetune/eval/glsl_render_batch.py
@@ -0,0 +1,143 @@
+"""Render many Shadertoy shaders in ONE persistent WebGL2 context.
+
+Launching a browser per shader costs 3-5 s and dominates everything (the first version of this check ran for
+3h43m without finishing 250 shaders). The page below compiles a new fragment program in place, so each shader
+costs ~50-150 ms. Two frames are sampled (iTime 0 and 1.7 s) and the canvas is read back with readPixels — no
+screenshots, which is what silently discarded good renders in the first version.
+
+ with BatchRenderer() as r:
+ rep = r.run(shader_source) # {status: OK|STATIC|FAIL, distinct, std, changed, error}
+"""
+import os
+
+HARNESS = """<!doctype html><html><body style="margin:0"><canvas id=c width=320 height=240></canvas><script>
+const c = document.getElementById('c');
+const gl = c.getContext('webgl2', {preserveDrawingBuffer:true});
+const VS = `#version 300 es
+in vec2 p; void main(){ gl_Position = vec4(p,0.,1.); }`;
+const HEAD = `#version 300 es
+precision highp float; precision highp int;
+uniform vec3 iResolution; uniform float iTime; uniform float iTimeDelta; uniform int iFrame;
+uniform vec4 iMouse; uniform vec4 iDate; uniform float iSampleRate; uniform float iFrameRate;
+uniform float iChannelTime[4]; uniform vec3 iChannelResolution[4];
+uniform sampler2D iChannel0; uniform sampler2D iChannel1; uniform sampler2D iChannel2; uniform sampler2D iChannel3;
+out vec4 _fragColor;
+`;
+const TAIL = `
+void main(){ mainImage(_fragColor, gl_FragCoord.xy); }`;
+let vs = null, buf = null, tex = null;
+if (gl) {
+ vs = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vs, VS); gl.compileShader(vs);
+ buf = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buf);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1,-1, 3,-1, -1,3]), gl.STATIC_DRAW);
+ tex = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, tex);
+ gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, new Uint8Array([128,128,128,255]));
+}
+function measure(){
+ const px = new Uint8Array(c.width*c.height*4);
+ gl.readPixels(0,0,c.width,c.height, gl.RGBA, gl.UNSIGNED_BYTE, px);
+ const seen = new Set(); let sum=0, sum2=0, n=0;
+ for (let i=0;i<px.length;i+=4*7){ const r=px[i]>>4, g=px[i+1]>>4, b=px[i+2]>>4;
+ seen.add((r<<8)|(g<<4)|b); const v=(px[i]+px[i+1]+px[i+2])/3; sum+=v; sum2+=v*v; n++; }
+ const mean=sum/n; return {distinct: seen.size, std: Math.sqrt(Math.max(0, sum2/n-mean*mean)), mean};
+}
+window.__run = function(src){
+ if (!gl) return {status:'FAIL', error:'no webgl2', distinct:0, std:0, changed:false};
+ let prog = null, fs = null;
+ try {
+ const body = src.replace(/^\\s*#version[^\\n]*\\n/, '');
+ fs = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fs, HEAD + body + TAIL); gl.compileShader(fs);
+ if (!gl.getShaderParameter(fs, gl.COMPILE_STATUS))
+ return {status:'FAIL', error:(gl.getShaderInfoLog(fs)||'').split('\\n')[0].slice(0,140), distinct:0, std:0, changed:false};
+ prog = gl.createProgram(); gl.attachShader(prog, vs); gl.attachShader(prog, fs); gl.linkProgram(prog);
+ if (!gl.getProgramParameter(prog, gl.LINK_STATUS))
+ return {status:'FAIL', error:(gl.getProgramInfoLog(prog)||'').split('\\n')[0].slice(0,140), distinct:0, std:0, changed:false};
+ gl.useProgram(prog);
+ gl.bindBuffer(gl.ARRAY_BUFFER, buf);
+ const loc = gl.getAttribLocation(prog, 'p');
+ if (loc >= 0) { gl.enableVertexAttribArray(loc); gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0); }
+ gl.activeTexture(gl.TEXTURE0); gl.bindTexture(gl.TEXTURE_2D, tex);
+ for (let i=0;i<4;i++){ const u = gl.getUniformLocation(prog,'iChannel'+i); if (u) gl.uniform1i(u, 0); }
+ const out = [];
+ for (const t of [0.0, 1.7]) {
+ gl.uniform3f(gl.getUniformLocation(prog,'iResolution'), c.width, c.height, 1);
+ gl.uniform1f(gl.getUniformLocation(prog,'iTime'), t);
+ gl.uniform1f(gl.getUniformLocation(prog,'iTimeDelta'), 1/60);
+ gl.uniform1i(gl.getUniformLocation(prog,'iFrame'), Math.floor(t*60));
+ gl.uniform1f(gl.getUniformLocation(prog,'iFrameRate'), 60);
+ gl.clearColor(0,0,0,1); gl.clear(gl.COLOR_BUFFER_BIT);
+ gl.viewport(0,0,c.width,c.height); gl.drawArrays(gl.TRIANGLES, 0, 3);
+ out.push(measure());
+ }
+ const a = out[0], b = out[1];
+ const distinct = Math.max(a.distinct, b.distinct), std = Math.max(a.std, b.std);
+ return {status: (distinct > 3 && std > 2.0) ? 'OK' : 'STATIC', distinct: distinct, std: std,
+ changed: Math.abs(a.mean-b.mean) > 0.5 || a.distinct !== b.distinct, error: null};
+ } catch(e) {
+ return {status:'FAIL', error:String((e && e.message) || e).slice(0,140), distinct:0, std:0, changed:false};
+ } finally { if (prog) gl.deleteProgram(prog); if (fs) gl.deleteShader(fs); }
+};
+window.__ready = true;
+</script></body></html>"""
+
+
+class BatchRenderer:
+ def __init__(self, timeout_ms=20000):
+ self.timeout_ms = timeout_ms
+ self._pw = self._b = self._pg = None
+
+ def _new_page(self):
+ self._pg = self._b.new_page()
+ self._pg.set_default_timeout(self.timeout_ms)
+ self._pg.set_content(HARNESS, wait_until="load")
+ self._pg.wait_for_function("window.__ready === true", timeout=self.timeout_ms)
+
+ def __enter__(self):
+ os.environ.setdefault("PLAYWRIGHT_BROWSERS_PATH", "/wekafs/ict/hx_624/cache/ms-playwright")
+ from playwright.sync_api import sync_playwright
+ self._pw = sync_playwright().start()
+ self._b = self._pw.chromium.launch(args=["--use-gl=swiftshader", "--enable-unsafe-swiftshader", "--no-sandbox"])
+ self._new_page()
+ return self
+
+ def _relaunch(self):
+ """a crashed page can be replaced; a crashed browser cannot — rebuild whichever died, so that one bad
+ shader cannot turn every later shader handled by this worker into a false FAIL"""
+ for x in (self._pg, self._b):
+ try:
+ x.close()
+ except Exception:
+ pass
+ self._b = self._pw.chromium.launch(args=["--use-gl=swiftshader", "--enable-unsafe-swiftshader", "--no-sandbox"])
+ self._new_page()
+
+ def run(self, src, _retry=True):
+ try:
+ return self._pg.evaluate("s => window.__run(s)", src)
+ except Exception as e:
+ msg = str(e)[:140]
+ try:
+ self._new_page()
+ except Exception:
+ try:
+ self._relaunch()
+ except Exception:
+ return {"status": "FAIL", "error": "renderer unrecoverable: " + msg,
+ "distinct": 0, "std": 0.0, "changed": False}
+ if _retry: # one honest second chance on a fresh context
+ try:
+ return self.run(src, _retry=False)
+ except Exception:
+ pass
+ return {"status": "FAIL", "error": msg, "distinct": 0, "std": 0.0, "changed": False}
+
+ def __exit__(self, *a):
+ for x in (self._pg, self._b):
+ try:
+ x.close()
+ except Exception:
+ pass
+ try:
+ self._pw.stop()
+ except Exception:
+ pass
diff --git a/finetune/scripts/build_image_dataset.py b/finetune/scripts/build_image_dataset.py
new file mode 100644
index 00000000..f92b014b
--- /dev/null
+++ b/finetune/scripts/build_image_dataset.py
@@ -0,0 +1,163 @@
+"""Build an image-conditioned (render → code) dataset in LLaMA-Factory multimodal format.
+
+The `qwen3_5` template already registers a multimodal plugin (`qwen3_vl`, image token `<|image_pad|>`), so an
+`images` column plus an `<image>` placeholder in the user turn is all that is needed — the only change to a normal
+run is `freeze_vision_tower: false` if you want the tower to adapt.
+
+Why this matters for this project: the report shows execution rate responds to more data, but *geometric fidelity*
+(F-score / Chamfer) barely moved across every text-only SFT variant. A reference render carries exactly the signal
+the caption lacks.
+
+Sources (all already on disk):
+ bioinspired3d tars 4 views + code per sample, Blender-Python — the dialect 3DCodeBench measures
+ blender_distill 4 views per sample
+ threejs_distill 4 views per sample
+ animation2code 4 views per sample
+
+usage: python scripts/build_image_dataset.py [--out data/img_code] [--views 1] [--max_per_source 20000]
+"""
+import argparse
+import collections
+import glob
+import json
+import os
+import tarfile
+
+IMG_ROOT = "/wekafs/ict/hx_624/data/images"
+SYS = {
+ "blender": "You are an expert in procedural 3D modeling with Blender Python (bpy). Given reference renders of an object, write a complete, standalone Blender 5 Python script that reproduces it (clear the default scene first). Output ONLY the code in one ```python block.",
+ "threejs": "You are an expert three.js developer. Given reference renders, write a complete, standalone HTML page that reproduces the scene with three.js. Output ONLY the code in one ```html block.",
+ "web": "You are an expert creative web developer. Given reference frames of an animation, write a complete, standalone HTML page (inline CSS/JS) that reproduces it. Output ONLY the code in one ```html block.",
+}
+ASK = {
+ "blender": "Reproduce this 3D object with Blender Python code.",
+ "threejs": "Reproduce this scene as a single-file three.js page.",
+ "web": "Reproduce this animation as a single-file animated web page.",
+}
+FENCE = {"blender": "python", "threejs": "html", "web": "html"}
+
+
+def from_tars(pattern, dialect, out_rows, views, cap, caption_too=True):
+ n = 0
+ for t in sorted(glob.glob(pattern)):
+ sub = os.path.basename(os.path.dirname(t))
+ try:
+ tf = tarfile.open(t)
+ except Exception:
+ continue
+ members = collections.defaultdict(dict)
+ for m in tf.getmembers():
+ if not m.isfile():
+ continue
+ parts = m.name.split("/")
+ if len(parts) < 2:
+ continue
+ key = parts[0]
+ if m.name.endswith("code.py"):
+ members[key]["code"] = m
+ elif m.name.endswith("captions.json"):
+ members[key]["caps"] = m
+ elif "/renders/view_" in m.name and m.name.endswith(".png"):
+ members[key].setdefault("imgs", []).append(m)
+ for key, d in members.items():
+ if "code" not in d or "imgs" not in d:
+ continue
+ try:
+ code = tf.extractfile(d["code"]).read().decode("utf-8", "replace")
+ except Exception:
+ continue
+ if len(code.strip()) < 40:
+ continue
+ caps = {}
+ if caption_too and "caps" in d:
+ try:
+ caps = json.load(tf.extractfile(d["caps"]))
+ except Exception:
+ caps = {}
+ imgs = sorted(d["imgs"], key=lambda m: m.name)[:views]
+ paths = []
+ for i, m in enumerate(imgs):
+ dst = os.path.join(IMG_ROOT, sub, key, os.path.basename(m.name))
+ if not os.path.exists(dst):
+ os.makedirs(os.path.dirname(dst), exist_ok=True)
+ with open(dst, "wb") as f:
+ f.write(tf.extractfile(m).read())
+ paths.append(dst)
+ if not paths:
+ continue
+ hint = (caps.get("instruction") or caps.get("detailed") or "").strip()
+ ask = ASK[dialect] + (f"\nHint: {hint}" if hint and len(hint) < 400 else "")
+ out_rows.append({"id": f"{sub}/{key}", "dialect": dialect, "images": paths,
+ "system": SYS[dialect],
+ "conversations": [{"from": "human", "value": "<image>" * len(paths) + "\n" + ask},
+ {"from": "gpt", "value": f"```{FENCE[dialect]}\n{code.strip()}\n```"}]})
+ n += 1
+ if n >= cap:
+ tf.close()
+ return n
+ tf.close()
+ return n
+
+
+def from_tree(root, dialect, out_rows, views, cap, code_name):
+ n = 0
+ for cp in sorted(glob.glob(f"/wekafs/ict/hx_624/data/3dcodeverse_files/{root}/*/*/{code_name}")):
+ d = os.path.dirname(cp)
+ imgs = sorted(glob.glob(os.path.join(d, "renders", "*.png")))[:views]
+ if not imgs:
+ continue
+ code = open(cp, encoding="utf-8", errors="replace").read()
+ if len(code.strip()) < 40:
+ continue
+ caps = {}
+ cj = os.path.join(d, "captions.json")
+ if os.path.exists(cj):
+ try:
+ caps = json.load(open(cj))
+ except Exception:
+ pass
+ hint = (caps.get("user_instruction") or caps.get("instruction") or caps.get("brief") or "").strip()
+ ask = ASK[dialect] + (f"\nHint: {hint}" if hint and len(hint) < 400 else "")
+ out_rows.append({"id": os.path.relpath(d, "/wekafs/ict/hx_624/data/3dcodeverse_files"),
+ "dialect": dialect, "images": imgs, "system": SYS[dialect],
+ "conversations": [{"from": "human", "value": "<image>" * len(imgs) + "\n" + ask},
+ {"from": "gpt", "value": f"```{FENCE[dialect]}\n{code.strip()}\n```"}]})
+ n += 1
+ if n >= cap:
+ return n
+ return n
+
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--out", default="/wekafs/ict/hx_624/llm-ft/data/img_code")
+ ap.add_argument("--views", type=int, default=1)
+ ap.add_argument("--max_per_source", type=int, default=20000)
+ a = ap.parse_args()
+ os.makedirs(IMG_ROOT, exist_ok=True)
+ rows = []
+ got = {}
+ got["bioinspired3d"] = from_tars("/wekafs/ict/hx_624/data/3dcodeverse_tars/bioinspired3d/*/*.tar",
+ "blender", rows, a.views, a.max_per_source)
+ got["blender_distill"] = from_tree("blender_distill", "blender", rows, a.views, a.max_per_source, "code.py")
+ got["threejs_distill"] = from_tree("threejs_distill", "threejs", rows, a.views, a.max_per_source, "code.html")
+ got["animation2code"] = from_tree("animation2code", "web", rows, a.views, a.max_per_source, "index.html")
+ os.makedirs(a.out, exist_ok=True)
+ import random
+ random.seed(3)
+ random.shuffle(rows)
+ val, train = rows[:200], rows[200:]
+ for name, part in (("train", train), ("val", val)):
+ with open(os.path.join(a.out, f"{name}.json"), "w") as f:
+ json.dump(part, f, ensure_ascii=False)
+ json.dump({"img_code_train": {"file_name": "train.json", "formatting": "sharegpt",
+ "columns": {"messages": "conversations", "system": "system", "images": "images"}},
+ "img_code_val": {"file_name": "val.json", "formatting": "sharegpt",
+ "columns": {"messages": "conversations", "system": "system", "images": "images"}}},
+ open(os.path.join(a.out, "dataset_info.json"), "w"), indent=2)
+ print(f"[img] {len(rows)} samples ({dict(got)}), {a.views} view(s) each -> {a.out}")
+ print("[img] dialects:", dict(collections.Counter(r["dialect"] for r in rows)))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/finetune/scripts/queue.txt b/finetune/scripts/queue.txt
index 89ba99a4..cfceb234 100644
--- a/finetune/scripts/queue.txt
+++ b/finetune/scripts/queue.txt
@@ -1 +1,2 @@
-v4_full|scripts/run_27b_v4.sh
+v5|scripts/run_27b_v5.sh
+9b_clean|scripts/run_9b_clean.sh
diff --git a/finetune/scripts/render_verify_glsl.py b/finetune/scripts/render_verify_glsl.py
new file mode 100644
index 00000000..c3773022
--- /dev/null
+++ b/finetune/scripts/render_verify_glsl.py
@@ -0,0 +1,78 @@
+"""Render-verify the compile-verified Shadertoy corpus: compiling is not the same as drawing something.
+
+Samples (or exhaustively walks) the unique programs in the flattened shadertoy datasets, renders each in headless
+WebGL2, and records status / distinct colours / pixel std / whether the image changes with iTime. Writes
+`renders_ok`, `renders_status` back into the parquet rows when run with --write.
+
+usage: python scripts/render_verify_glsl.py [--sample 3000] [--workers 8] [--write]
+"""
+import argparse, collections, glob, hashlib, json, os, random, re, sys, tempfile
+from concurrent.futures import ProcessPoolExecutor
+import pandas as pd
+sys.path.insert(0, "/wekafs/ict/hx_624/llm-ft/eval")
+os.environ.setdefault("PLAYWRIGHT_BROWSERS_PATH", "/wekafs/ict/hx_624/cache/ms-playwright")
+
+def code_of(m):
+ x = re.findall(r"```[a-z]*\n(.*?)```", m, flags=re.S)
+ return (x[0] if x else m).strip()
+
+def job(chunk):
+ """one persistent browser per worker, many shaders per browser (a browser launch costs 3-5 s)"""
+ from glsl_render_batch import BatchRenderer
+ out = []
+ with BatchRenderer() as r:
+ for h, code in chunk:
+ x = r.run(code)
+ out.append((h, x.get("status", "FAIL"), x.get("distinct", 0), x.get("std", 0.0),
+ bool(x.get("changed")), (x.get("error") or "")[:120]))
+ return out
+
+def main():
+ ap = argparse.ArgumentParser()
+ ap.add_argument("--sample", type=int, default=3000)
+ ap.add_argument("--workers", type=int, default=8)
+ ap.add_argument("--write", action="store_true")
+ a = ap.parse_args()
+ uniq, where = {}, collections.defaultdict(list)
+ for f in sorted(glob.glob("/wekafs/ict/hx_624/hf_subdirs/shadertoy/*_llamafactory_flat/train.parquet")):
+ df = pd.read_parquet(f)
+ if "compiles" not in df: continue
+ for i, row in enumerate(df[df["compiles"]].itertuples()):
+ b = code_of(row.conversations[1]["value"]); h = hashlib.md5(b.encode()).hexdigest()
+ uniq.setdefault(h, b); where[h].append(f)
+ items = list(uniq.items())
+ print(f"[render] {len(items)} unique compile-verified programs", flush=True)
+ random.seed(11); random.shuffle(items)
+ if a.sample: items = items[:a.sample]
+ res, stats = {}, collections.Counter()
+ CH = 60
+ chunks = [items[i:i+CH] for i in range(0, len(items), CH)]
+ done = 0
+ with ProcessPoolExecutor(a.workers) as ex:
+ for out in ex.map(job, chunks):
+ for h, st, dis, std, ch, err in out:
+ res[h] = (st, dis, std, ch, err); stats[st] += 1
+ done += len(out)
+ if done % 1200 < CH:
+ print(f"[render] {done}/{len(items)} {dict(stats)}", flush=True)
+ animated = sum(1 for v in res.values() if v[3])
+ rep = {"checked": len(res), "status": dict(stats), "animated": animated,
+ "render_rate": round(stats["OK"]/max(1,len(res)), 4),
+ "static_examples": [{"hash": h, "err": v[4]} for h, v in list(res.items()) if v[0] == "STATIC"][:5],
+ "fail_examples": [{"hash": h, "err": v[4]} for h, v in list(res.items()) if v[0] == "FAIL"][:5]}
+ json.dump(rep, open("/wekafs/ict/hx_624/hf_subdirs/glsl_render_report.json", "w"), indent=2)
+ print(f"[render] OK {stats['OK']}/{len(res)} ({100*stats['OK']/max(1,len(res)):.1f}%) | animated {animated} | {dict(stats)}", flush=True)
+ if a.write:
+ for f in sorted(glob.glob("/wekafs/ict/hx_624/hf_subdirs/shadertoy/*_llamafactory_flat/train.parquet")):
+ df = pd.read_parquet(f)
+ if "compiles" not in df: continue
+ st = []
+ for row in df.itertuples():
+ b = code_of(row.conversations[1]["value"]); h = hashlib.md5(b.encode()).hexdigest()
+ st.append(res.get(h, ("unchecked",))[0])
+ df["renders_status"] = st; df["renders_ok"] = [s == "OK" for s in st]
+ df.to_parquet(f, index=False, compression="zstd")
+ print("[render] wrote renders_status/renders_ok into the parquets", flush=True)
+
+if __name__ == "__main__":
+ main()
diff --git a/finetune/scripts/run_27b_v5.sh b/finetune/scripts/run_27b_v5.sh
new file mode 100755
index 00000000..46e0feff
--- /dev/null
+++ b/finetune/scripts/run_27b_v5.sh
@@ -0,0 +1,51 @@
+#!/bin/bash
+# 27B v4: v2's winning blender-heavy core + CadQuery raised 33k -> 105k unique samples (one caption each).
+# Question: can a single 27B top both 3DCodeBench (v2: 93.4%) and CadQuery (9B md_xl: 97.5%)?
+#
+# 27B LoRA on 80 GB cards sits right at the memory edge — the identical recipe that trained v2 OOMs on other
+# days. So instead of guessing, try a ladder of settings (keeping lora_rank 64, the quality-relevant knob;
+# every sample in this mix is <=3400 tokens, so a smaller cutoff truncates nothing) and take the first that
+# survives 4 real steps.
+set -uo pipefail
+cd /wekafs/ict/hx_624/llm-ft
+GPUS_USE=$(scripts/free_gpus.sh); NGPU=$(echo "$GPUS_USE" | awk -F, '{print NF}')
+[ -z "$GPUS_USE" ] && { echo "[27bv5] no free GPUs"; exit 1; }
+RUN=runs/lf_qwen38_27b_lora_v5
+
+WIN=""
+for SPEC in "3584 5 adamw_torch" "3072 6 adamw_torch" "3072 6 adamw_8bit" "2560 8 adamw_8bit"; do
+ set -- $SPEC; CUT=$1; ACC=$2; OPT=$3
+ echo "[27bv5] $(date) probing cutoff=$CUT accum=$ACC optim=$OPT"
+ EXTRA=""; [ "$OPT" = "adamw_8bit" ] && EXTRA="optim=adamw_8bit"
+ GPUS=$GPUS_USE scripts/lf_train.sh configs/lf/lora_27b_v5.yaml max_steps=4 save_steps=100000 eval_steps=100000 \
+ cutoff_len=$CUT gradient_accumulation_steps=$ACC $EXTRA \
+ tokenized_path=/wekafs/ict/hx_624/llm-ft/data/lf/tok_v5_$CUT \
+ output_dir=/wekafs/ict/hx_624/llm-ft/runs/_probe_v4 > logs/probe_v4_$CUT$OPT.log 2>&1
+ rm -rf runs/_probe_v4
+ if grep -q "train_runtime" logs/probe_v4_$CUT$OPT.log; then
+ S=$(grep -oE "[0-9]/4 \[[0-9:]+<[0-9:]+, *[0-9.]+s/it" logs/probe_v4_$CUT$OPT.log | tail -1)
+ echo "[27bv5] OK at cutoff=$CUT accum=$ACC optim=$OPT ($S)"; WIN="$CUT $ACC $OPT"; break
+ fi
+ ERR=$(grep -oE "torch.OutOfMemoryError[^\"]*|Triton Error \[CUDA\]: out of memory|RuntimeError: [^\"]{0,120}" logs/probe_v4_$CUT$OPT.log | tail -1)
+ echo "[27bv5] FAILED at cutoff=$CUT accum=$ACC optim=$OPT -> ${ERR:-unknown error, see logs/probe_v4_$CUT$OPT.log}"
+ case "$ERR" in *"Triton >= 3.4"*|*"tilelang"*) echo "[27bv5] ABORT: the lf env's triton was downgraded (Hopper needs 3.7.1) — fix the env, not the config"; exit 2;; esac
+done
+[ -z "$WIN" ] && { echo "[27bv5] every setting OOMed — needs more GPUs or a sharded optimizer"; exit 1; }
+set -- $WIN; CUT=$1; ACC=$2; OPT=$3
+EXTRA=""; [ "$OPT" = "adamw_8bit" ] && EXTRA="optim=adamw_8bit"
+
+echo "[27bv5] $(date) train (183k pairs / 130M tok, cutoff=$CUT accum=$ACC optim=$OPT, GPUs $GPUS_USE)"
+GPUS=$GPUS_USE scripts/lf_train.sh configs/lf/lora_27b_v5.yaml cutoff_len=$CUT gradient_accumulation_steps=$ACC $EXTRA \
+ tokenized_path=/wekafs/ict/hx_624/llm-ft/data/lf/tok_v5_$CUT > logs/train_27b_v4.log 2>&1
+[ -f $RUN/train_results.json ] || { echo "[27bv5] TRAIN FAILED: $(grep -E 'out of memory|Error' logs/train_27b_v4.log | grep -v errors | tail -1 | cut -c1-160)"; exit 1; }
+echo "[27bv5] $(date) train done: $(tr -d '\n' < $RUN/train_results.json | cut -c1-160)"
+
+sed -e "s|model_name_or_path:.*|model_name_or_path: /wekafs/ict/hx_624/models/Qwen3.8-27B|" \
+ -e "s|adapter_name_or_path:.*|adapter_name_or_path: $(readlink -f $RUN)|" \
+ -e "s|export_dir:.*|export_dir: $(readlink -f $RUN)/merged|" configs/lf/export_lora_template.yaml > configs/lf/export_27b_v5.yaml
+scripts/lf_export.sh configs/lf/export_27b_v5.yaml > logs/export_27b_v5.log 2>&1 || { echo "[27bv5] EXPORT FAILED"; exit 1; }
+echo "[27bv5] $(date) eval greedy"
+GPUS=$GPUS_USE TP=2 eval/eval_all_run2.sh $RUN/merged q27b_v5 --max_new 8192 2>&1 | grep -E "gen-multi|status counts|dialect|DONE" | tail -20
+echo "[27bv5] $(date) eval sampled (openscad/glsl)"
+GPUS=$GPUS_USE TP=2 eval/eval_all_run2.sh $RUN/merged q27b_v5_T07 --temp 0.7 --seed 1 --max_new 8192 --suites openscad glsl 2>&1 | grep -E "dialect|DONE" | tail -4
+echo "[27bv5] ALL DONE"
diff --git a/finetune/scripts/run_9b_clean.sh b/finetune/scripts/run_9b_clean.sh
new file mode 100755
index 00000000..06eeb13d
--- /dev/null
+++ b/finetune/scripts/run_9b_clean.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+# 9B on the cleaned corpus: compile-verified GLSL (flattened, Common merged), recovered URDF (7th dialect),
+# deduped per-subdir data, one caption per sample, plus the execution-verified bootstrapped Blender set.
+set -uo pipefail; cd /wekafs/ict/hx_624/llm-ft
+G=$(scripts/free_gpus.sh); N=$(echo "$G" | awk -F, '{print NF}')
+echo "[9bclean] $(date) train on GPUs $G (191.9k pairs / 177M tok, 7 dialects)"
+GPUS=$G scripts/lf_train.sh configs/lf/lora_9b_clean.yaml > logs/train_9b_clean.log 2>&1
+RUN=runs/lf_qwen35_9b_lora_clean
+[ -f $RUN/train_results.json ] || { echo "[9bclean] TRAIN FAILED: $(grep -E 'out of memory|Error' logs/train_9b_clean.log | grep -v errors | tail -1 | cut -c1-160)"; exit 1; }
+echo "[9bclean] $(date) train done: $(tr -d '\n' < $RUN/train_results.json | cut -c1-160)"
+sed -e "s|model_name_or_path:.*|model_name_or_path: /wekafs/ict/hx_624/models/Qwen3.5-9B|" -e "s|adapter_name_or_path:.*|adapter_name_or_path: $(readlink -f $RUN)|" -e "s|export_dir:.*|export_dir: $(readlink -f $RUN)/merged|" configs/lf/export_lora_template.yaml > configs/lf/export_9b_clean.yaml
+scripts/lf_export.sh configs/lf/export_9b_clean.yaml > logs/export_9b_clean.log 2>&1 || { echo "[9bclean] EXPORT FAILED"; exit 1; }
+GPUS=0 TP=1 eval/eval_all_run2.sh $RUN/merged q9b_clean --max_new 8192 2>&1 | grep -E "gen-multi|status counts|dialect|DONE" | tail -20
+GPUS=1 TP=1 eval/eval_all_run2.sh $RUN/merged q9b_clean_T07 --temp 0.7 --seed 1 --max_new 8192 --suites openscad glsl 2>&1 | grep -E "dialect|DONE" | tail -4
+echo "[9bclean] ALL DONE"