KhalilAlgoWorks / patterns /02_constrained_decoding_and_pruning.md
enver's picture
Release KhalilAlgoWorks v1.0: 16 classical books, 84 verified (121/121) AI & SWE algorithm principles
13310fd verified
|
Raw History Blame Contribute Delete
13.4 kB
# KhalilAlgoWorks Playbook: Constrained Decoding, Speculation & Pruning
> Mustaʿmal/Muhmal validity masks, O(1) structural hallucination guards, lossless KV/context pruning, lookahead speculation, and layer elision.
**Total Principles Synthesized:** `18` (`PROVEN`: `7`, `PARTIAL`: `9`, `UNTAPPED`: `2`)
## `PROVEN` Principles (7)
### `GH-02` — Al-Sabr wa-l-Taqsīm (Exhaustive Partition & Elimination)
- **Source:** Abū Ḥāmid al-Ghazālī (505) — *Al-Ghazālī — *Al-Mustaṣfā min ʿIlm al-Uṣūl** ([dossier](../books/01_ghazali_mustasfa.md), **L1206, L12534–12543**)
- **Verbatim Quote:** **L1206, L12534–12543**: «السبر والتقسيم وهو دليل صحيح... لا علة له إلا كذا أو كذا، وقد بطل أحدهما فتعين الآخر» / «أن يكون سبره حاصرا»
- **Engineering Pattern:** **Disjunctive Hypothesis Elimination / Channel Pruning & Root-Cause Localization**: Enumerate exhaustive failure/channel hypotheses, falsify invalid branches via execution/energy probes, retain the sole surviving operative cause.
### `KH-01` — Al-Taqālīb ($n!$ Combinatorial Search) × *Mustaʿmal* vs. *Muhmal* Pruning
- **Source:** al-Khalīl ibn Aḥmad al-Farāhīdī (170/175) — *Al-Khalīl ibn Aḥmad — *Kitāb al-ʿAyn** ([dossier](../books/02_khalil_al_ayn.md), **L164**)
- **Verbatim Quote:** **L164**: «تتصرَّفُ على ستة أوجُه... أربعة وعشرين وَجْهاً، يُكَتَب مُسْتَعْمَلها. ويُلغى مُهْمَلها»
- **Engineering Pattern:** **Structured Combinatorial Exploration + Deterministic Validity Pruning**: Systematically explore orthogonal candidate permutations (on-policy rollouts / AST templates) and deterministically discard invalid (*muhmal*) branches before scoring.
### `SB-03` — Sadd al-Masadd (Structural Substitution / Functional Absorption)
- **Source:** ʿAmr ibn ʿUthmān Sībawayh (180) — *Sībawayh — *Al-Kitāb** ([dossier](../books/03_sibawayh_al_kitab.md), **L7108–7111**)
- **Verbatim Quote:** **L7108–7111**: «باب ما يقع موقع الاسم المبتدإ ويسد مسده... كل واحد منهما لا يستغنى به عن صاحبه»
- **Engineering Pattern:** **Layer Elision + Low-Rank Residual Compensation**: Prune redundant transformer blocks/modules and substitute a compact functional adapter that preserves downstream interface contracts.
### `SB-04` — Bāb al-Zakn (Contextual Intent Lookahead)
- **Source:** ʿAmr ibn ʿUthmān Sībawayh (180) — *Sībawayh — *Al-Kitāb** ([dossier](../books/03_sibawayh_al_kitab.md), **L3228–3230**)
- **Verbatim Quote:** **L3228–3230**: «مكة ورب الكعبة. حيث زكنت أنه يريد مكة»
- **Engineering Pattern:** **Zero-Loss Prompt/Context-Grounded Speculative Decoding**: Predict multi-token continuations deterministically from observable prompt/state trajectory cues and verify in one pass.
### `AZ-04` — Frequency-Driven Collocation Fusion (*al-Naḥt bi-Kathrat al-Istiʿmāl*)
- **Source:** Abū Manṣūr al-Azharī (370) — *Al-Azharī — *Tahdhīb al-Lughah** ([dossier](../books/13_azhari_tahdhib_al_lughah.md), **L50369–50370**)
- **Verbatim Quote:** **L50369–50370**: «والعرب تفعل هكذا إذا كثر استعمالهم الكلمتين ضموا بعض حروف إحداهما إلى بعض حروف الأخرى»
- **Engineering Pattern:** **PMI-Guided Macro-Token / Phrase-Draft Fusion**: Fuse recurring high-frequency multi-token collocations into O(1) macro-draft units for lossless speculative decoding and latency reduction.
### `JW-04` — Frequency-Driven Structural Compression with Plural/Inverse Recovery
- **Source:** Ismāʿīl ibn Ḥammād al-Jawharī (393) — *Al-Jawharī — *Tāj al-Lughah wa-Ṣiḥāḥ al-ʿArabiyyah** ([dossier](../books/14_jawhari_al_sihah.md), **L42159–42160, L53202**)
- **Verbatim Quote:** **L42159–42160, L53202**: «ثم تركت همزته لكثرة الاستعمال، فقيل ملك، فلما جمعوه ردوها إليه فقالوا ملائكة» / «حذفت ألفها لكثرة الاستعمال، وجعلا اسما واحدا»
- **Engineering Pattern:** **Reversible Fast-Path Shorthand with Full-Expansion Fallback**: Compress high-frequency operations/prompts in the hot path while retaining exact canonical expansion whenever complex/plural edge cases arise.
### `FS-04` — Al-Naḥt (Lossy-Surface / Lossless-Meaning Multi-Word Compression)
- **Source:** Aḥmad ibn Fāris al-Rāzī (395) — *Ibn Fāris — *Al-Ṣāḥibī fī Fiqh al-Lughah** ([dossier](../books/15_ibn_faris_al_sahibi.md), **L3919–3920**)
- **Verbatim Quote:** **L3919–3920**: «باب النحت: العرب تنحت من كلمتين كلمة واحدة، وهو جنس من الاختصار»
- **Engineering Pattern:** **Compositional Macro-Operation / Pipeline Fusion**: Synthesize compact fused compound operators or multi-token speculative chunks from recurring multi-step idioms.
## `PARTIAL` Principles (9)
### `KH-03` — Articulatory-Axis Canonical Ordering (*Makhārij* Manifold)
- **Source:** al-Khalīl ibn Aḥmad al-Farāhīdī (170/175) — *Al-Khalīl ibn Aḥmad — *Kitāb al-ʿAyn** ([dossier](../books/02_khalil_al_ayn.md), **L17, L158**)
- **Verbatim Quote:** **L17, L158**: «فوضعها على قدر مخرجها من الحلق... ع، ح، هـ، خ، غ» / «القاف والكاف لهَويتان»
- **Engineering Pattern:** **Physical/Functional Coordinate Embeddings & Feature-Ordered Indexing**: Index vocabulary/symbols along continuous generative-mechanism axes rather than arbitrary lexical/UTF-8 order for compact manifold sharing.
### `KH-04` — Hard Adjacent-Pair Exclusion with Explicit Composition Exception
- **Source:** al-Khalīl ibn Aḥmad al-Farāhīdī (170/175) — *Al-Khalīl ibn Aḥmad — *Kitāb al-ʿAyn** ([dossier](../books/02_khalil_al_ayn.md), **L179, L19594**)
- **Verbatim Quote:** **L179, L19594**: «العَيْن لا تَأْتَلِف مع الحاء في كلمة واحدة لقُرْب مَخْرَجَيْهما إلا أنّ يُشْتَقَّ فِعلٌ من جمعٍ بين كلمتين» / «القاف والكاف لا يجتمعان... إلا أن تكون الكلمة معربة»
- **Engineering Pattern:** **Tiered Hard Grammar Masks with Provenance-Aware Escape Hatches**: Block illegal token/AST transitions by default, while explicitly exempting cross-module/foreign-FFI/compound boundaries (`loanword` / `external_lib` namespace).
### `MB-01` — Information-Theoretic Lossless Compression (*al-Ḥadhf maʿa Amān al-Labs*)
- **Source:** Muḥammad ibn Yazīd al-Mubarrad (285) — *Al-Mubarrad — *Al-Muqtaḍab** ([dossier](../books/05_mubarrad_al_muqtadab.md), **L5049–5057**)
- **Verbatim Quote:** **L5049–5057**: «ما يحذف استخفافا / لأن اللبس فيه مأمون / ومنه ما يحذف لأن ما يبقى دال عليه»
- **Engineering Pattern:** **Ambiguity-Safe KV-Cache / Context / Token Pruning**: Prune a token, layer, or prompt clause for efficiency **iff** surviving context uniquely entails it (`ma yabqa dallun 'alayh`) with zero ambiguity risk (`aman al-labs`).
### `MB-02` — Completeness Stopping Criterion (*Ḥusn al-Sukūt & Wujūb al-Fāʾidah*)
- **Source:** Muḥammad ibn Yazīd al-Mubarrad (285) — *Al-Mubarrad — *Al-Muqtaḍab** ([dossier](../books/05_mubarrad_al_muqtadab.md), **L98–99, L13255**)
- **Verbatim Quote:** **L98–99, L13255**: «جملة يحسن عليها السكوت، وتجب بها الفائدة للمخاطب» / «فإذا أوقعت فيه الفائدة فالباب فيه التصرف»
- **Engineering Pattern:** **Semantic EOS Calibration & Anti-Early-EOS / Anti-Vacuous-Output Gate**: Block premature `<eos>` before a self-contained semantic unit (*fāʾidah*) is closed; reject tautological outputs (`X is X`) that deliver zero new information gain.
### `JD-01` — Maximal Feature-Space Separation / Orthogonality Law
- **Source:** Abū Bakr Muḥammad Ibn Durayd (321) — *Ibn Durayd — *Jamharat al-Lughah** ([dossier](../books/06_ibn_durayd_jamhara.md), **L207–213**)
- **Verbatim Quote:** **L207–213**: «الحروف إذا تقاربت مخارجها كانت أثقل على اللسان منها إذا تباعدت... وإذا تباعدت مخارج الحروف حسن وجه التأليف»
- **Engineering Pattern:** **Orthogonal Ensemble / Diversity-Regularized Decoding & Multi-Agent Design**: Penalize near-duplicate representations/candidates in embedding space; force diverse candidate paradigms (*taqlībāt*) and orthogonal LoRA/expert subspaces to avoid representation collapse.
### `ZI-04` — Compute/Complexity Load Balancing (*Takhfīf al-Thaqīl wa-Ilzām al-Khafīf*)
- **Source:** Abū al-Qāsim al-Zajjājī (337/340) — *Al-Zajjājī — *Al-Īḍāḥ fī ʿIlal al-Naḥw** ([dossier](../books/08_zajjaji_idah_fi_ilal.md), **L936, L1079–1081**)
- **Verbatim Quote:** **L936, L1079–1081**: «الأفعال ثقيلة، والأسماء خفيفة، فجعل لازما للأخف» / «ليعتدل الكلام فتخفيف الثقيل وإلزام بعض الثقيل للخفيف»
- **Engineering Pattern:** **Asymmetric Compute Budgeting across Pipeline Stages**: Offload heavy work away from already-expensive components (e.g., trim/apocopate heavy reasoning loops via fast O(1) deterministic pre-checks; keep heavy models light by attaching extra metadata to cheap CPU/Layer-0 ops).
### `JS-04` — Closed Finite Affix & Mutation Alphabets (10 *Ziyādah*, 11 *Ibdāl*)
- **Source:** Abū al-Fatḥ ʿUthmān Ibn Jinnī (392) — *Ibn Jinnī — *Sirr Ṣināʿat al-Iʿrāb** ([dossier](../books/11_ibn_jinni_sirr_sinat_irab.md), **L1183–1190, L3115–3117**)
- **Verbatim Quote:** **L1183–1190, L3115–3117**: «وحروف الزيادة عشرة... سألتمونيها» / «أحد عشر حرفا، تسمى حروف البدل» / «لأن قبلها حرفا مطبقا»
- **Engineering Pattern:** **Closed-Vocabulary Structural Delta Channels & Context-Triggered Deterministic Normalization**: Restrict permissible structural edits/affixes to a small verified closed set and run deterministic feature-conditioned rewrites before neural inference.
### `AZ-02` — Dataset Corruption & *Taṣḥīf* (OCR/Transcription Error) Purification
- **Source:** Abū Manṣūr al-Azharī (370) — *Al-Azharī — *Tahdhīb al-Lughah** ([dossier](../books/13_azhari_tahdhib_al_lughah.md), **L82–87**)
- **Verbatim Quote:** **L82–87**: «آفات الكتب المصحفة المدخولة... وأن أهذبها بجهدي غاية التهذيب، وأدل على التصحيف الواقع في كتب المتحاذقين»
- **Engineering Pattern:** **Pre-Training / Fine-Tuning Corpus Sanitation & Noise-Lineage Auditing**: Actively audit training corpora for OCR/dot-confusion/synthetic-contamination errors (`taṣḥīf`) and publish explicit error-correction logs rather than blindly ingesting scale.
### `FM-01` — Axiomatic Semantic Compression (*Maqāyīs Ṣaḥīḥah wa-Uṣūl*)
- **Source:** Aḥmad ibn Fāris al-Rāzī (395) — *Ibn Fāris — *Muʿjam Maqāyīs al-Lughah** ([dossier](../books/16_ibn_faris_maqayis_al_lughah.md), **L1155–1156, L3760**)
- **Verbatim Quote:** **L1155–1156, L3760**: «إن للغة العرب مقاييس صحيحة، وأصولا تتفرع منها فروع» / «الهمزة والسين والفاء أصل واحد يدل على الفوت والتلهف»
- **Engineering Pattern:** **Low-Rank Semantic Prototype / Root-Axiom Bottleneck**: Compress sprawling lexical/API/skill inventories into 1–3 orthogonal core semantic invariants per root/module so models generalize to unseen derivatives without memorization.
## `UNTAPPED` Principles (2)
### `AK-01` — Semantics-Preserving Optimization Invariant
- **Source:** Saʿīd ibn Masʿadah al-Akhfash al-Awsaṭ (215) — *Al-Akhfash al-Awsaṭ — *Maʿānī al-Qurʾān** ([dossier](../books/04_akhfash_maani_al_quran.md), **L31–33**)
- **Verbatim Quote:** **L31–33**: «فثقل عليهم ان يكونوا في كسر ثم يصيروا الى الضم. فارادوا أن يكونا جميعا مضمومين اذا كان ذلك لا يغير المعنى»
- **Engineering Pattern:** **Semantics-Invariant Graph/Prompt/Quantization Rewriting**: Allow cost-reducing structural transformations (kernel fusion, cache compression, prompt simplification) **iff** behavioral equivalence holds (`la yughayyir al-ma'na`).
### `AK-05` — Long-Distance Frame Binding Across Parenthetical Interruptions
- **Source:** Saʿīd ibn Masʿadah al-Akhfash al-Awsaṭ (215) — *Al-Akhfash al-Awsaṭ — *Maʿānī al-Qurʾān** ([dossier](../books/04_akhfash_maani_al_quran.md), **L4899–4900**)
- **Verbatim Quote:** **L4899–4900**: «يكتب الرجل: "أما بعد حفظك الله وعافاك فإني كتبت إليك" فقوله "فإني" محمول على "أما بعد"... وبينهما»
- **Engineering Pattern:** **Stack-Protected Long-Range Attention Masking / Parenthetical Bypass**: Maintain an explicit open-frame stack (`ammā ... fa-`, `try ... finally`, nested `<think>`/tool calls) immune to intermediate digression/parenthetical token decay.