HelloSun commited on
Commit
f0079cc
·
verified ·
1 Parent(s): 0039a57

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +165 -0
README.md ADDED
@@ -0,0 +1,165 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ - zh
5
+ base_model: openpangu/openPangu-R-7B-Diffusion
6
+ tags:
7
+ - gguf
8
+ - llama.cpp
9
+ - diffusion
10
+ - block-diffusion
11
+ - openPangu
12
+ license: other
13
+ ---
14
+
15
+ # openPangu-R-7B-Diffusion — Q4_K_M GGUF (llama.cpp)
16
+
17
+ Quantized **Q4_K_M** GGUF of [`openpangu/openPangu-R-7B-Diffusion`](https://huggingface.co/openpangu/openPangu-R-7B-Diffusion),
18
+ together with a llama.cpp patch that adds native support for the model's
19
+ **context-causal block-diffusion** decoding.
20
+
21
+ > `openPangu-R-7B-Diffusion` is a 7B block-diffusion LLM. Generation is *not* plain
22
+ > next-token autoregression: a block of tokens (default length **32**) is denoised in
23
+ > parallel with **full (bi-directional) attention inside the block** and **causal
24
+ > attention for the preceding context**, revealing the most confident tokens first.
25
+ > The model can also be run in ordinary autoregressive mode.
26
+
27
+ Upstream llama.cpp (as of tag `b11719`) ships the `PanguEmbedded` architecture
28
+ (used by the autoregressive `openPangu-Embedded`) but does **not** implement the
29
+ diffusion decoding this model needs, hence the patch.
30
+
31
+ ## Files
32
+
33
+ | File | Description |
34
+ |---|---|
35
+ | `pangu-diffusion-Q4_K_M.gguf` | Q4_K_M quantized weights (~4.6 GB), ready to run |
36
+ | `pangu-diffusion-llamacpp.patch` | llama.cpp patch adding openPangu-R-Diffusion support |
37
+ | `build_llamacpp_pangu_diffusion.sh` | one-click script: clone llama.cpp → apply patch → build |
38
+
39
+ ## Quick start
40
+
41
+ ### 1. Build llama.cpp with the patch (one-click)
42
+
43
+ ```bash
44
+ bash build_llamacpp_pangu_diffusion.sh
45
+ # binaries end up in ./pangu-diffusion-build/llama.cpp/build/bin/
46
+ ```
47
+
48
+ Or manually:
49
+
50
+ ```bash
51
+ git clone --depth 1 --branch b11719 https://github.com/ggml-org/llama.cpp.git
52
+ cd llama.cpp
53
+ git apply /path/to/pangu-diffusion-llamacpp.patch
54
+ cmake -B build -DGGML_NATIVE=ON -DLLAMA_CURL=OFF
55
+ cmake --build build -j --target llama-diffusion-cli llama-cli llama-quantize
56
+ ```
57
+
58
+ ### 2. Diffusion decoding (recommended)
59
+
60
+ The chat format is
61
+
62
+ ```
63
+ [unused9]系统:<system>[unused10][unused9]用户:<user>[unused10][unused9]助手:
64
+ ```
65
+
66
+ ```bash
67
+ ./build/bin/llama-diffusion-cli \
68
+ -m pangu-diffusion-Q4_K_M.gguf \
69
+ -p "[unused9]系统:[unused10][unused9]用户:Which is bigger, 9.11 or 9.9?[unused10][unused9]助手:" \
70
+ -ub 256 \
71
+ --diffusion-block-length 32 \
72
+ --diffusion-small-block 4 \
73
+ --diffusion-algorithm 1 \
74
+ --temp 0 -t 16 --seed 42
75
+ ```
76
+
77
+ Recommended sampling (from the official model card):
78
+ `alg = entropy`, `num_small_blocks = 8` → `--diffusion-small-block 4` (with block length 32),
79
+ `top_p = 1`, `temperature = 0`. For coding tasks the authors suggest
80
+ `alg = entropy, num_small_blocks = 32 (→ --diffusion-small-block 1), top_p = 0.8, temperature = 1`.
81
+
82
+ `--diffusion-algorithm` values: `0` origin, `1` entropy, `2` margin, `3` random, `4` confidence.
83
+ `-ub N` is the maximum sequence length; the tail that does not fill a whole block is dropped.
84
+
85
+ ### 3. Autoregressive decoding
86
+
87
+ ```bash
88
+ ./build/bin/llama-cli -m pangu-diffusion-Q4_K_M.gguf \
89
+ -p "[unused9]系统:[unused10][unused9]用户:Hi![unused10][unused9]助手:" -n 128 -t 16
90
+ ```
91
+
92
+ ## Example
93
+
94
+ Prompt (translated): *“Which is bigger, 9.11 or 9.9?”* — command as in §2:
95
+
96
+ ```
97
+ # Which is bigger, 9.11 or 9.9?
98
+
99
+ I need to compare 9.11 and 9.9 to see which one is bigger. Both numbers are
100
+ between 9 and 10, so they're both greater than 9 but less than 10.
101
+
102
+ Let me think about this carefully. 9.11 means 9 and 11 hundredths, which is
103
+ 9 + 11/100. Similarly, 9.9 means 9 and 9 tenths, which is 9 + 9/10.
104
+
105
+ To compare them, I should convert them to the same format. Let me convert both
106
+ to hundredths.
107
+
108
+ 9.9 = 9.90 (adding a zero to make it 9.90)
109
+
110
+ Now I have:
111
+ - 9.11
112
+ - 9.90
113
+
114
+ Since both have the same number of decimal places, I can compare them directly.
115
+
116
+ The first digit after the decimal is 1 for 9.11 and 9 for 9.90. Since 9 > 1,
117
+ 9.90 is larger than 9.11.
118
+
119
+ So, 9.9 is bigger than 9.11.
120
+ ```
121
+
122
+ ## What the patch does
123
+
124
+ 1. **`src/llama-arch.cpp`** — mark `LLM_ARCH_PANGU_EMBED` as a diffusion architecture
125
+ (`llm_arch_is_diffusion()`), so `llama-diffusion-cli` accepts the model.
126
+ 2. **`examples/diffusion/diffusion.{h,cpp}`** — add a *context-causal block-diffusion*
127
+ decoder (`diffusion.block_attention`):
128
+ - prefill the prompt with a causal forward (kept in the KV cache);
129
+ - denoise each block with `causal_attn = false` → full attention over the cached
130
+ context **plus** the current block (the KV-cache kq mask keeps every non-empty
131
+ cached cell when non-causal);
132
+ - re-decode the block each step, dropping its stale keys (`llama_memory_seq_rm`),
133
+ reveal the most-confident masked token of the current small block, left to right;
134
+ - commit the block with a causal forward so the KV cache holds correct causal
135
+ context, then sample the first token of the next block from the last position.
136
+ 3. **`examples/diffusion/diffusion-cli.cpp`** — read `diffusion.block_attention`,
137
+ add `--diffusion-small-block`, use greedy sampling when `temperature == 0`.
138
+ 4. **`gguf-py` + `conversion/pangu.py`** — write the new `diffusion.block_attention`
139
+ metadata key for diffusion checkpoints (alongside `diffusion.shift_logits` and
140
+ `tokenizer.ggml.mask_token_id`).
141
+
142
+ ### Model metadata in the GGUF
143
+
144
+ ```
145
+ general.architecture = pangu-embedded
146
+ diffusion.shift_logits = true
147
+ diffusion.block_attention = true
148
+ tokenizer.ggml.mask_token_id = 45830
149
+ ```
150
+
151
+ ## Notes / limitations
152
+
153
+ - CPU-only, `-t 16`, ≈ 4.6 GB file: a 256-token block-diffusion run takes a few minutes
154
+ (`block_length` forwards per block). Throughput can be improved with
155
+ `alg = confidence_threshold, num_small_blocks = 1` (see the official README).
156
+ - The committed-block KV-cache path was numerically cross-checked against the
157
+ Hugging Face reference (`cos ≈ 0.996`, f16 vs bf16), and the same binaries reproduce
158
+ the model's reference-quality output above.
159
+ - Quantization may slightly affect the tricky reasoning cases; use `alg = entropy`,
160
+ `temp = 0` for the most stable results.
161
+
162
+ ## Original model
163
+
164
+ - Weights: [`openpangu/openPangu-R-7B-Diffusion`](https://huggingface.co/openpangu/openPangu-R-7B-Diffusion)
165
+ - Architecture details, benchmarks and citation: see the original model card.