"""Apply reversible rank-3 orthogonal subspace edit + harness-agnostic GGUF chat template fix.""" import argparse import hashlib import json import sys from pathlib import Path import gguf import numpy as np ROOT = Path(__file__).resolve().parent DEFAULT_a.source = ROOT.parent / 'Cyber-Tiel-Coder-35B-A3B-MTP-UD-Q4_K_XL.gguf' DEFAULT_a.target = ROOT.parent / 'Cyber-Tiel-Coder-35B-A3B-LoopFix-MTP-UD-Q4_K_XL.gguf' TPL_START = 10935828 TPL_END = 10966357 TPL_LEN = TPL_END - TPL_START def build_patched_template(orig_text: str) -> bytes: text = orig_text # 1. Prevent llama-server from auto-enabling _preserve_thinking=true via preserve_reasoning=true default injection; # default _preserve_thinking to false unless preserve_thinking=true is explicitly passed. old_preserve_block = """{%- if preserve_reasoning is defined and preserve_reasoning is not none %} {%- set _preserve_thinking = preserve_reasoning %} {%- elif preserve_thinking is defined and preserve_thinking is not none %} {%- set _preserve_thinking = preserve_thinking %} {%- else %} {%- set _preserve_thinking = true %} {%- endif %}""" new_preserve_block = """{%- if preserve_thinking is defined and preserve_thinking is not none %} {%- set _preserve_thinking = preserve_thinking %} {%- else %} {%- set _preserve_thinking = false %} {%- endif %}""" assert old_preserve_block in text, "old_preserve_block not found" text = text.replace(old_preserve_block, new_preserve_block, 1) # 2. Replace _terse_core with 100% harness-agnostic anti-loop, act-over-ask, and raw-output discipline old_terse_core = """{%- set _terse_core %} Never: open with preamble or pleasantries; restate the question; add filler transitions; hedge with niceties; or repeat a point you've already made. Always: keep essential steps, caveats, uncertainties, and specifics — never drop correctness or a needed warning for brevity. Keep the final answer lean. Use the least structure that conveys it (plain prose when short; lists or code only when they earn their place). If genuinely uncertain, say so and explain why — never omit uncertainty for the sake of brevity. If a user request is genuinely ambiguous, ask a sharp question, don't guess. {%- endset %}""" new_terse_core = """{%- set _terse_core %} Never: open with preamble or pleasantries; restate the question; add filler transitions; repeat or reconsider a conclusion already settled by evidence; manually recount raw data arrays in thinking; or wrap a requested single command, patch, or JSON object in backticks or prose. Always: once evidence settles the next step, commit and act directly without re-deliberating. Prefer reasonable, conservative action over asking clarifying questions. Treat tool schemas, parameter roles, error messages, and the full grammar shown in the prompt (including any leading target/header line) as authoritative; correct rejected syntax on the next turn rather than repeating it. When asked to return ONLY code, JSON, a patch, or a shell command, output ONLY that exact raw text with no Markdown backticks or explanation. {%- endset %}""" assert old_terse_core in text, "old_terse_core not found" text = text.replace(old_terse_core, new_terse_core, 1) # 3. Bound intra-turn retention in multi-step tool runs to the most recent 1 step old_keep = " {%- if (_preserve_thinking or loop.index0 > ns.last_query_index) %}" new_keep = " {%- if (_preserve_thinking or (loop.index0 > ns.last_query_index and loop.index0 >= _last_idx - 2)) %}" assert old_keep in text, "old_keep not found" text = text.replace(old_keep, new_keep, 1) # 4. Adjust the Jinja comment blocks so UTF-8 byte length is 100% identical to TPL_LEN (30529 bytes) old_comments = """{#- `terse` chat-template kwarg: default true, so behaviour is unchanged unless a caller opts out. Pass {"terse": false} via chat_template_kwargs to serve the model with only its own system prompt -- useful for A/B-ing the terseness effect, or when a downstream prompt conflicts. #}""" assert old_comments in text, "old_comments not found" old_comments_2 = """{#- Some runtimes (LM Studio's MLX backend) have no native tool-call parser and prepend their own protocol into the system content. Emitting ours as well gives the model two contradictory tool protocols and it follows one at random. Detect that marker and stand down; the `suppress_tool_instructions` kwarg overrides either way where a runtime can pass template kwargs. #}""" assert old_comments_2 in text, "old_comments_2 not found" text_no_c1 = text.replace(old_comments, "{#-PAD-#}", 1).replace(old_comments_2, "{#-PAD2-#}", 1) base_bytes = len(text_no_c1.encode("utf-8")) - len("{#-PAD-#}") - len("{#-PAD2-#}") needed = TPL_LEN - base_bytes assert needed >= 20, f"Not enough comment slack: needed={needed}" pad1_len = needed // 2 pad2_len = needed - pad1_len pad1 = "{#-" + (" " * (pad1_len - 6)) + "-#}" pad2 = "{#-" + (" " * (pad2_len - 6)) + "-#}" final_text = text_no_c1.replace("{#-PAD-#}", pad1, 1).replace("{#-PAD2-#}", pad2, 1) final_bytes = final_text.encode("utf-8") assert len(final_bytes) == TPL_LEN, f"Length mismatch: {len(final_bytes)} != {TPL_LEN}" return final_bytes def main(): p = argparse.ArgumentParser() p.add_argument('--source', type=Path, default=DEFAULT_a.source) p.add_argument('--target', type=Path, default=DEFAULT_a.target) p.add_argument('--alpha1', type=float, default=0.30) p.add_argument('--alpha2', type=float, default=0.15) p.add_argument('--alpha3', type=float, default=0.10) p.add_argument('--layers1', default='26,34') p.add_argument('--layers2', default='26,34') p.add_argument('--layers3', default='26,34') p.add_argument('--blocks', type=int, default=2) p.add_argument('--patch-template', action='store_true') a = p.parse_args() assert a.source.resolve() != a.target.resolve() and a.source.stat().st_ino != a.target.stat().st_ino # Restore original template bytes first orig_tpl_bytes = (ROOT / 'original-chat-template.jinja').read_bytes() assert len(orig_tpl_bytes) == TPL_LEN with a.target.open('r+b') as f: f.seek(TPL_START) f.write(orig_tpl_bytes) if a.patch_template: patched_tpl_bytes = build_patched_template(orig_tpl_bytes.decode('utf-8')) f.seek(TPL_START) f.write(patched_tpl_bytes) (ROOT / 'patched-chat-template.jinja').write_bytes(patched_tpl_bytes) f.flush() source = gguf.GGUFReader(str(a.source)) target = gguf.GGUFReader(str(a.target), mode='r+') v1_reader = gguf.GGUFReader(str(ROOT / 'loop-direction.gguf')) v2_reader = gguf.GGUFReader(str(ROOT / 'loop-direction-v2.gguf')) v3_reader = gguf.GGUFReader(str(ROOT / 'loop-direction-v3-only.gguf')) src = {t.name: t for t in source.tensors} dst = {t.name: t for t in target.tensors} dirs1 = {int(t.name.split('.')[1]) - 1: t.data.reshape(-1).astype(np.float32) for t in v1_reader.tensors} dirs2 = {int(t.name.split('.')[1]) - 1: t.data.reshape(-1).astype(np.float32) for t in v2_reader.tensors} dirs3 = {int(t.name.split('.')[1]) - 1: t.data.reshape(-1).astype(np.float32) for t in v3_reader.tensors} previous = ROOT / 'candidate-manifest.json' if previous.exists(): for edit in json.loads(previous.read_text()).get('edits', []): name = edit['tensor'] dst[name].data[:] = src[name].data set1 = {int(x) for x in a.layers1.split(',') if x.strip()} set2 = {int(x) for x in a.layers2.split(',') if x.strip()} set3 = {int(x) for x in a.layers3.split(',') if x.strip()} all_layers = sorted(set1 | set2 | set3) edits = [] for layer in all_layers: name = f'blk.{layer}.ffn_down_shexp.weight' t = src[name] out = dst[name] assert t.tensor_type == gguf.GGMLQuantizationType.Q8_0 assert t.shape.tolist() == out.shape.tolist() w = gguf.dequantize(t.data, t.tensor_type) # Gram-Schmidt orthogonal basis {u1, u2_eff, u3_eff} u1 = dirs1[layer].copy() u1 /= np.linalg.norm(u1) u2 = dirs2[layer].copy() u2_eff = u2 - np.dot(u1, u2) * u1 u2_eff /= np.linalg.norm(u2_eff) u3 = dirs3[layer].copy() u3_eff = u3 - np.dot(u1, u3) * u1 - np.dot(u2_eff, u3) * u2_eff u3_eff /= np.linalg.norm(u3_eff) coeff1 = u1 @ w coeff2 = u2_eff @ w coeff3 = u3_eff @ w w1 = a.alpha1 if layer in set1 else 0.0 w2 = a.alpha2 if layer in set2 else 0.0 w3 = a.alpha3 if layer in set3 else 0.0 # Select top Q8_0 column blocks by primary v1+v2 energy so block selection stays stable score_vec = (w1 > 0) * (coeff1 ** 2) + 0.5 * (w2 > 0) * (coeff2 ** 2) block_scores = score_vec.reshape(-1, 32).sum(axis=1) blocks = np.argsort(block_scores)[-a.blocks:] cols = np.concatenate([np.arange(b * 32, (b + 1) * 32) for b in blocks]) delta = np.zeros((w.shape[0], len(cols)), dtype=np.float32) if w1 > 0: delta += w1 * np.outer(u1, coeff1[cols]) if w2 > 0: delta += w2 * np.outer(u2_eff, coeff2[cols]) if w3 > 0: delta += w3 * np.outer(u3_eff, coeff3[cols]) edited = w.copy() edited[:, cols] -= delta q = gguf.quantize(edited, t.tensor_type) qblocks = q.reshape(w.shape[0], -1, 34) targetblocks = out.data.reshape(w.shape[0], -1, 34) for block in blocks: targetblocks[:, block, :] = qblocks[:, block, :] actual = gguf.dequantize(out.data, out.tensor_type) mask = np.ones(w.shape[1], dtype=bool) mask[cols] = False assert np.array_equal(actual[:, mask], w[:, mask]) assert np.isfinite(actual).all() edits.append(dict( tensor=name, layer=layer, alpha1=w1, alpha2=w2, alpha3=w3, blocks=blocks.tolist(), columns=cols.tolist(), changed_values=int(np.count_nonzero(actual != w)), relative_l2=float(np.linalg.norm(actual - w) / np.linalg.norm(w)), bytes_changed=int(np.count_nonzero(out.data != t.data)), before_sha256=hashlib.sha256(t.data.tobytes()).hexdigest(), after_sha256=hashlib.sha256(out.data.tobytes()).hexdigest() )) target.data.flush() manifest = dict( source=str(a.source), target=str(a.target), version='v3-agnostic', alpha1=a.alpha1, alpha2=a.alpha2, alpha3=a.alpha3, layers1=sorted(set1), layers2=sorted(set2), layers3=sorted(set3), patch_template=a.patch_template, edits=edits, status='Validated v3 rank-3 Gram-Schmidt late-layer subspace edit + harness-agnostic GGUF template' ) previous.write_text(json.dumps(manifest, indent=2) + '\n') print(json.dumps(manifest, indent=2)) if __name__ == '__main__': main()