Instructions to use HelloSun/openPangu-R-7B-Diffusion-Q4KM with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use HelloSun/openPangu-R-7B-Diffusion-Q4KM with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M # Run inference directly in the terminal: llama cli -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M # Run inference directly in the terminal: llama cli -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M # Run inference directly in the terminal: ./llama-cli -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M # Run inference directly in the terminal: ./build/bin/llama-cli -hf HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
Use Docker
docker model run hf.co/HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
- Llama new
- LM Studio
- Jan
- Ollama
How to use HelloSun/openPangu-R-7B-Diffusion-Q4KM with Ollama:
ollama run hf.co/HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
- Unsloth Desktop
- Docker Model Runner
How to use HelloSun/openPangu-R-7B-Diffusion-Q4KM with Docker Model Runner:
docker model run hf.co/HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
- Lemonade
How to use HelloSun/openPangu-R-7B-Diffusion-Q4KM with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull HelloSun/openPangu-R-7B-Diffusion-Q4KM:Q4_K_M
Run and chat with the model
lemonade run user.openPangu-R-7B-Diffusion-Q4KM-Q4_K_M
List all available models
lemonade list
- Atomic Chat
Upload pangu-diffusion-llamacpp.patch with huggingface_hub
Browse files- pangu-diffusion-llamacpp.patch +388 -0
pangu-diffusion-llamacpp.patch
ADDED
|
@@ -0,0 +1,388 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diff --git a/common/arg.cpp b/common/arg.cpp
|
| 2 |
+
index 8b302d5..ac587a2 100644
|
| 3 |
+
--- a/common/arg.cpp
|
| 4 |
+
+++ b/common/arg.cpp
|
| 5 |
+
@@ -4522,6 +4522,11 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
|
| 6 |
+
string_format("llada block length for generation (default: %d)", params.diffusion.block_length),
|
| 7 |
+
[](common_params & params, int value) { params.diffusion.block_length = value; }
|
| 8 |
+
).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
|
| 9 |
+
+ add_opt(common_arg(
|
| 10 |
+
+ {"--diffusion-small-block"}, "N",
|
| 11 |
+
+ string_format("small block length inside a block for context-causal block diffusion, 0 = whole block (default: %d)", params.diffusion.small_block_length),
|
| 12 |
+
+ [](common_params & params, int value) { params.diffusion.small_block_length = value; }
|
| 13 |
+
+ ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
|
| 14 |
+
add_opt(common_arg(
|
| 15 |
+
{"--diffusion-cfg-scale"}, "F",
|
| 16 |
+
string_format("llada classifier-free guidance scale (default: %.3f)", (double) params.diffusion.cfg_scale),
|
| 17 |
+
diff --git a/common/common.h b/common/common.h
|
| 18 |
+
index 0c2d5e4..386de7f 100644
|
| 19 |
+
--- a/common/common.h
|
| 20 |
+
+++ b/common/common.h
|
| 21 |
+
@@ -413,6 +413,7 @@ struct common_params_diffusion {
|
| 22 |
+
|
| 23 |
+
float eps = 0; // epsilon for timesteps
|
| 24 |
+
int32_t block_length = 0; // block length for generation
|
| 25 |
+
+ int32_t small_block_length = 0; // small block length within a block (context-causal block diffusion; 0 = whole block)
|
| 26 |
+
|
| 27 |
+
int32_t algorithm = 4; // default algorithm: low-confidence
|
| 28 |
+
float alg_temp = 0.0f; // algorithm temperature
|
| 29 |
+
diff --git a/conversion/pangu.py b/conversion/pangu.py
|
| 30 |
+
index 74c7653..6b00cec 100644
|
| 31 |
+
--- a/conversion/pangu.py
|
| 32 |
+
+++ b/conversion/pangu.py
|
| 33 |
+
@@ -1,6 +1,7 @@
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
import json
|
| 37 |
+
+import os
|
| 38 |
+
|
| 39 |
+
from typing import Iterable, TYPE_CHECKING
|
| 40 |
+
|
| 41 |
+
@@ -15,6 +16,16 @@ from .base import ModelBase, TextModel, gguf, logger
|
| 42 |
+
class PanguEmbeddedModel(TextModel):
|
| 43 |
+
model_arch = gguf.MODEL_ARCH.PANGU_EMBED
|
| 44 |
+
|
| 45 |
+
+ # openPangu-R-7B-Diffusion relies on this dedicated mask token for block-diffusion decoding
|
| 46 |
+
+ DIFFUSION_MASK_TOKEN_ID = 45830
|
| 47 |
+
+
|
| 48 |
+
+ def _is_diffusion(self) -> bool:
|
| 49 |
+
+ if os.environ.get("PANGU_EMBED_DIFFUSION"):
|
| 50 |
+
+ return True
|
| 51 |
+
+ if "diffusion" in str(self.dir_model).lower():
|
| 52 |
+
+ return True
|
| 53 |
+
+ return (self.dir_model / "inference" / "generation_utils.py").is_file()
|
| 54 |
+
+
|
| 55 |
+
def set_vocab(self):
|
| 56 |
+
self._set_vocab_sentencepiece()
|
| 57 |
+
|
| 58 |
+
@@ -39,6 +50,18 @@ class PanguEmbeddedModel(TextModel):
|
| 59 |
+
self.gguf_writer.add_key_length(rope_dim)
|
| 60 |
+
self.gguf_writer.add_value_length(rope_dim)
|
| 61 |
+
|
| 62 |
+
+ if self._is_diffusion():
|
| 63 |
+
+ # openPangu-R-7B-Diffusion: context-causal block diffusion LLM.
|
| 64 |
+
+ # - full (bi-directional) attention inside the current denoising block
|
| 65 |
+
+ # - causal attention for the preceding context (standard KV cache)
|
| 66 |
+
+ # - logits are shifted by one position (next-token style)
|
| 67 |
+
+ # - a dedicated mask token is used to seed denoising blocks
|
| 68 |
+
+ self.gguf_writer.add_diffusion_block_attention(True)
|
| 69 |
+
+ self.gguf_writer.add_diffusion_shift_logits(True)
|
| 70 |
+
+ self.gguf_writer.add_mask_token_id(self.DIFFUSION_MASK_TOKEN_ID)
|
| 71 |
+
+ logger.info("gguf: openPangu-R-Diffusion detected -> block_attention = True, shift_logits = True, mask_token_id = %d",
|
| 72 |
+
+ self.DIFFUSION_MASK_TOKEN_ID)
|
| 73 |
+
+
|
| 74 |
+
def modify_tensors(self, data_torch: Tensor, name: str, bid: int | None) -> Iterable[tuple[str, Tensor]]:
|
| 75 |
+
if name == "lm_head.weight":
|
| 76 |
+
if self.hparams.get("tie_word_embeddings", False):
|
| 77 |
+
diff --git a/examples/diffusion/diffusion-cli.cpp b/examples/diffusion/diffusion-cli.cpp
|
| 78 |
+
index c646113..76b3dd0 100644
|
| 79 |
+
--- a/examples/diffusion/diffusion-cli.cpp
|
| 80 |
+
+++ b/examples/diffusion/diffusion-cli.cpp
|
| 81 |
+
@@ -186,10 +186,20 @@ int main(int argc, char ** argv) {
|
| 82 |
+
diff_params.shift_logits = true;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
- //Use either eps or block length, but not both
|
| 86 |
+
- GGML_ASSERT((params.diffusion.eps == 0) ^ (params.diffusion.block_length == 0));
|
| 87 |
+
+ char block_attn_str[8];
|
| 88 |
+
+ if (llama_model_meta_val_str(model, "diffusion.block_attention", block_attn_str, sizeof(block_attn_str)) >= 0) {
|
| 89 |
+
+ diff_params.block_attention = (strcmp(block_attn_str, "true") == 0);
|
| 90 |
+
+ }
|
| 91 |
+
|
| 92 |
+
- if (params.diffusion.eps) {
|
| 93 |
+
+ //Use either eps or block length, but not both
|
| 94 |
+
+ GGML_ASSERT(params.diffusion.eps == 0 || params.diffusion.block_length == 0);
|
| 95 |
+
+ GGML_ASSERT(!diff_params.block_attention || params.diffusion.block_length > 0);
|
| 96 |
+
+
|
| 97 |
+
+ if (diff_params.block_attention) {
|
| 98 |
+
+ diff_params.block_length = params.diffusion.block_length;
|
| 99 |
+
+ diff_params.small_block_length = params.diffusion.small_block_length;
|
| 100 |
+
+ diff_params.schedule = DIFFUSION_TRANSFER_SCHEDULE_BLOCK_BASED;
|
| 101 |
+
+ } else if (params.diffusion.eps) {
|
| 102 |
+
diff_params.schedule = DIFFUSION_TRANSFER_SCHEDULE_TIMESTEP_BASED;
|
| 103 |
+
diff_params.eps = params.diffusion.eps;
|
| 104 |
+
} else if (params.diffusion.block_length) {
|
| 105 |
+
@@ -242,6 +252,8 @@ int main(int argc, char ** argv) {
|
| 106 |
+
LOG_INF("diffusion_params: - %-25s u32 = %d\n", "block_length", diff_params.block_length);
|
| 107 |
+
LOG_INF("diffusion_params: - %-25s f32 = %.3f\n", "cfg_scale", diff_params.cfg_scale);
|
| 108 |
+
}
|
| 109 |
+
+ LOG_INF("diffusion_params: - %-25s bool = %s\n", "block_attention", diff_params.block_attention ? "true" : "false");
|
| 110 |
+
+ LOG_INF("diffusion_params: - %-25s u32 = %d\n", "small_block_length", diff_params.small_block_length);
|
| 111 |
+
|
| 112 |
+
diffusion_generate(ctx, input_tokens.data(), output_tokens.data(), n_input, diff_params, n_generated);
|
| 113 |
+
|
| 114 |
+
@@ -251,6 +263,7 @@ int main(int argc, char ** argv) {
|
| 115 |
+
LOG_INF("\033[2J\033[H");
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
+ output_tokens.resize(n_generated);
|
| 119 |
+
output_tokens.erase(output_tokens.begin(), output_tokens.begin() + n_input);
|
| 120 |
+
std::string output_data = common_detokenize(vocab, output_tokens, false);
|
| 121 |
+
LOG_INF("\n%s\n", output_data.c_str());
|
| 122 |
+
diff --git a/examples/diffusion/diffusion.cpp b/examples/diffusion/diffusion.cpp
|
| 123 |
+
index 0ff57d9..3e5ce80 100644
|
| 124 |
+
--- a/examples/diffusion/diffusion.cpp
|
| 125 |
+
+++ b/examples/diffusion/diffusion.cpp
|
| 126 |
+
@@ -102,6 +102,199 @@ static std::vector<int32_t> get_num_transfer_tokens(int32_t mask_count, int32_t
|
| 127 |
+
return num_transfer_tokens;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
+// Context-causal block diffusion (openPangu-R-Diffusion):
|
| 131 |
+
+// - the prompt is prefilled with a causal forward and kept in the KV cache
|
| 132 |
+
+// - each block is denoised with full (bi-directional) attention over [0, block_end)
|
| 133 |
+
+// - inside a block the tokens are revealed one-by-one, most-confident first, and the
|
| 134 |
+
+// block may be split into "small blocks" that are revealed from left to right
|
| 135 |
+
+// - after denoising, the block is committed with a causal forward so that the KV cache
|
| 136 |
+
+// holds the correct causal context for the following blocks
|
| 137 |
+
+static void diffusion_generate_block_attention(llama_context * ctx,
|
| 138 |
+
+ const llama_token * input_tokens,
|
| 139 |
+
+ llama_token * output_tokens,
|
| 140 |
+
+ int32_t n_input,
|
| 141 |
+
+ const diffusion_params & params,
|
| 142 |
+
+ int32_t & n_generated) {
|
| 143 |
+
+ const llama_model * model = llama_get_model(ctx);
|
| 144 |
+
+ const llama_vocab * vocab = llama_model_get_vocab(model);
|
| 145 |
+
+ const int32_t n_vocab = llama_vocab_n_tokens(vocab);
|
| 146 |
+
+ const int32_t B = params.block_length;
|
| 147 |
+
+
|
| 148 |
+
+ std::copy(input_tokens, input_tokens + n_input, output_tokens);
|
| 149 |
+
+ std::fill(output_tokens + n_input, output_tokens + params.max_length, params.mask_token_id);
|
| 150 |
+
+
|
| 151 |
+
+ if (B <= 0) {
|
| 152 |
+
+ LOG_ERR("%s: block_length must be > 0 for block attention\n", __func__);
|
| 153 |
+
+ return;
|
| 154 |
+
+ }
|
| 155 |
+
+
|
| 156 |
+
+ const int32_t n_blocks = (params.max_length - n_input) / B;
|
| 157 |
+
+ if (n_blocks <= 0) {
|
| 158 |
+
+ LOG_ERR("%s: max_length (%d) - n_input (%d) is smaller than block_length (%d)\n",
|
| 159 |
+
+ __func__, params.max_length, n_input, B);
|
| 160 |
+
+ return;
|
| 161 |
+
+ }
|
| 162 |
+
+
|
| 163 |
+
+ int32_t small = params.small_block_length;
|
| 164 |
+
+ if (small <= 0 || small > B || (B % small) != 0) {
|
| 165 |
+
+ small = B;
|
| 166 |
+
+ }
|
| 167 |
+
+
|
| 168 |
+
+ llama_memory_t mem = llama_get_memory(ctx);
|
| 169 |
+
+
|
| 170 |
+
+ std::mt19937 rng(params.seed);
|
| 171 |
+
+ std::vector<llama_token_data> candidates(n_vocab);
|
| 172 |
+
+
|
| 173 |
+
+ struct llama_sampler * sampler = llama_sampler_chain_init(llama_sampler_chain_default_params());
|
| 174 |
+
+ if (params.top_k > 0) {
|
| 175 |
+
+ llama_sampler_chain_add(sampler, llama_sampler_init_top_k(params.top_k));
|
| 176 |
+
+ }
|
| 177 |
+
+ if (params.top_p < 1.0f) {
|
| 178 |
+
+ llama_sampler_chain_add(sampler, llama_sampler_init_top_p(params.top_p, 1));
|
| 179 |
+
+ }
|
| 180 |
+
+ if (params.temperature > 0.0f) {
|
| 181 |
+
+ llama_sampler_chain_add(sampler, llama_sampler_init_temp(params.temperature));
|
| 182 |
+
+ llama_sampler_chain_add(sampler, llama_sampler_init_dist(params.seed));
|
| 183 |
+
+ } else {
|
| 184 |
+
+ // temperature == 0 -> greedy (matches the reference `probs.max(dim=-1)`)
|
| 185 |
+
+ llama_sampler_chain_add(sampler, llama_sampler_init_greedy());
|
| 186 |
+
+ }
|
| 187 |
+
+
|
| 188 |
+
+ common_batch batch(ctx);
|
| 189 |
+
+
|
| 190 |
+
+ auto run_range = [&](int32_t p0, int32_t p1) -> const float * {
|
| 191 |
+
+ batch.clear();
|
| 192 |
+
+ for (int32_t p = p0; p < p1; ++p) {
|
| 193 |
+
+ batch.add(output_tokens[p], p, 0, true);
|
| 194 |
+
+ }
|
| 195 |
+
+ if (llama_process(ctx, LLAMA_PROCESS_TYPE_DECODE, batch.get()) != 0) {
|
| 196 |
+
+ LOG_ERR("%s: llama_process failed\n", __func__);
|
| 197 |
+
+ return nullptr;
|
| 198 |
+
+ }
|
| 199 |
+
+ return llama_get_logits(ctx);
|
| 200 |
+
+ };
|
| 201 |
+
+
|
| 202 |
+
+ auto sample_from = [&](const float * logits, llama_token & tok, float & conf) {
|
| 203 |
+
+ for (int32_t t = 0; t < n_vocab; ++t) {
|
| 204 |
+
+ candidates[t].id = t;
|
| 205 |
+
+ candidates[t].logit = logits[t];
|
| 206 |
+
+ candidates[t].p = 0.0f;
|
| 207 |
+
+ }
|
| 208 |
+
+ llama_token_data_array cur_p = { candidates.data(), (size_t) n_vocab, -1, false };
|
| 209 |
+
+ llama_sampler_apply(sampler, &cur_p);
|
| 210 |
+
+ tok = cur_p.data[cur_p.selected].id;
|
| 211 |
+
+ conf = calculate_confidence(cur_p, params.algorithm, rng);
|
| 212 |
+
+ };
|
| 213 |
+
+
|
| 214 |
+
+ // 1) prefill the prompt causally and sample the first token of the first block
|
| 215 |
+
+ if (n_input > 0) {
|
| 216 |
+
+ llama_set_causal_attn(ctx, true);
|
| 217 |
+
+ const float * logits = run_range(0, n_input);
|
| 218 |
+
+ if (!logits) {
|
| 219 |
+
+ llama_sampler_free(sampler);
|
| 220 |
+
+ return;
|
| 221 |
+
+ }
|
| 222 |
+
+ // only the last prompt position requested logits, so logits[0] is the last prompt position
|
| 223 |
+
+ llama_token tok;
|
| 224 |
+
+ float conf;
|
| 225 |
+
+ sample_from(logits, tok, conf);
|
| 226 |
+
+ output_tokens[n_input] = tok;
|
| 227 |
+
+ }
|
| 228 |
+
+
|
| 229 |
+
+ const llama_token eos = llama_vocab_eos(vocab);
|
| 230 |
+
+
|
| 231 |
+
+ for (int32_t b = 0; b < n_blocks; ++b) {
|
| 232 |
+
+ const int32_t block_start = n_input + b * B;
|
| 233 |
+
+ const int32_t block_end = block_start + B;
|
| 234 |
+
+
|
| 235 |
+
+ // 2) denoise the block with full attention over the cached context + the block itself
|
| 236 |
+
+ llama_set_causal_attn(ctx, false);
|
| 237 |
+
+
|
| 238 |
+
+ for (int32_t s = 0; s < B; s += small) {
|
| 239 |
+
+ const int32_t sub_start = block_start + s;
|
| 240 |
+
+ const int32_t sub_end = std::min(sub_start + small, block_end);
|
| 241 |
+
+ while (true) {
|
| 242 |
+
+ bool any_mask = false;
|
| 243 |
+
+ for (int32_t p = sub_start; p < sub_end; ++p) {
|
| 244 |
+
+ if (output_tokens[p] == params.mask_token_id) {
|
| 245 |
+
+ any_mask = true;
|
| 246 |
+
+ break;
|
| 247 |
+
+ }
|
| 248 |
+
+ }
|
| 249 |
+
+ if (!any_mask) {
|
| 250 |
+
+ break;
|
| 251 |
+
+ }
|
| 252 |
+
+
|
| 253 |
+
+ // the block is re-decoded on every step; drop its stale keys first
|
| 254 |
+
+ llama_memory_seq_rm(mem, 0, block_start, -1);
|
| 255 |
+
+ const float * logits = run_range(block_start, block_end);
|
| 256 |
+
+ if (!logits) {
|
| 257 |
+
+ llama_sampler_free(sampler);
|
| 258 |
+
+ return;
|
| 259 |
+
+ }
|
| 260 |
+
+
|
| 261 |
+
+ // predict every masked position of the current small block, reveal the most confident one
|
| 262 |
+
+ float best_conf = -INFINITY;
|
| 263 |
+
+ int32_t best_pos = -1;
|
| 264 |
+
+ llama_token best_tok = 0;
|
| 265 |
+
+ for (int32_t p = sub_start; p < sub_end; ++p) {
|
| 266 |
+
+ if (output_tokens[p] != params.mask_token_id) {
|
| 267 |
+
+ continue;
|
| 268 |
+
+ }
|
| 269 |
+
+ const int32_t idx = p - block_start;
|
| 270 |
+
+ const float * lp = (idx == 0) ? logits : logits + (idx - 1) * n_vocab;
|
| 271 |
+
+ llama_token tok;
|
| 272 |
+
+ float conf;
|
| 273 |
+
+ sample_from(lp, tok, conf);
|
| 274 |
+
+ if (conf > best_conf) {
|
| 275 |
+
+ best_conf = conf;
|
| 276 |
+
+ best_pos = p;
|
| 277 |
+
+ best_tok = tok;
|
| 278 |
+
+ }
|
| 279 |
+
+ }
|
| 280 |
+
+ if (best_pos >= 0) {
|
| 281 |
+
+ output_tokens[best_pos] = best_tok;
|
| 282 |
+
+ }
|
| 283 |
+
+ }
|
| 284 |
+
+ }
|
| 285 |
+
+
|
| 286 |
+
+ // 3) commit the block with a causal forward (fills the KV cache) and sample
|
| 287 |
+
+ // the first token of the next block from the last position of this block
|
| 288 |
+
+ llama_set_causal_attn(ctx, true);
|
| 289 |
+
+ llama_memory_seq_rm(mem, 0, block_start, -1);
|
| 290 |
+
+ const float * logits = run_range(block_start, block_end);
|
| 291 |
+
+ if (!logits) {
|
| 292 |
+
+ llama_sampler_free(sampler);
|
| 293 |
+
+ return;
|
| 294 |
+
+ }
|
| 295 |
+
+
|
| 296 |
+
+ bool hit_eos = false;
|
| 297 |
+
+ for (int32_t p = block_start; p < block_end; ++p) {
|
| 298 |
+
+ if (output_tokens[p] == eos) {
|
| 299 |
+
+ hit_eos = true;
|
| 300 |
+
+ }
|
| 301 |
+
+ }
|
| 302 |
+
+
|
| 303 |
+
+ n_generated = block_end;
|
| 304 |
+
+
|
| 305 |
+
+ if (hit_eos) {
|
| 306 |
+
+ break;
|
| 307 |
+
+ }
|
| 308 |
+
+
|
| 309 |
+
+ if (b < n_blocks - 1) {
|
| 310 |
+
+ // shifted logits: the token at block_end is predicted from the last block position
|
| 311 |
+
+ const float * lp = logits + (B - 1) * n_vocab;
|
| 312 |
+
+ llama_token tok;
|
| 313 |
+
+ float conf;
|
| 314 |
+
+ sample_from(lp, tok, conf);
|
| 315 |
+
+ output_tokens[block_end] = tok;
|
| 316 |
+
+ n_generated = block_end + 1;
|
| 317 |
+
+ }
|
| 318 |
+
+ }
|
| 319 |
+
+
|
| 320 |
+
+ llama_sampler_free(sampler);
|
| 321 |
+
+}
|
| 322 |
+
+
|
| 323 |
+
void diffusion_generate(llama_context * ctx,
|
| 324 |
+
const llama_token * input_tokens,
|
| 325 |
+
llama_token * output_tokens,
|
| 326 |
+
@@ -113,6 +306,11 @@ void diffusion_generate(llama_context * ctx,
|
| 327 |
+
return;
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
+ if (params.block_attention) {
|
| 331 |
+
+ diffusion_generate_block_attention(ctx, input_tokens, output_tokens, n_input, params, n_generated);
|
| 332 |
+
+ return;
|
| 333 |
+
+ }
|
| 334 |
+
+
|
| 335 |
+
const llama_model * model = llama_get_model(ctx);
|
| 336 |
+
|
| 337 |
+
// Initialize with input and pad with mask tokens
|
| 338 |
+
diff --git a/examples/diffusion/diffusion.h b/examples/diffusion/diffusion.h
|
| 339 |
+
index 7831445..85f6eb7 100644
|
| 340 |
+
--- a/examples/diffusion/diffusion.h
|
| 341 |
+
+++ b/examples/diffusion/diffusion.h
|
| 342 |
+
@@ -43,6 +43,8 @@ struct diffusion_params {
|
| 343 |
+
float cfg_scale = 0.; // Config scale for classifier-free guidance
|
| 344 |
+
float eps = 0.; // Timestep scheduling
|
| 345 |
+
int32_t block_length = 0; // Block size (for block scheduling)
|
| 346 |
+
+ int32_t small_block_length = 0; // Small block size inside a block (context-causal block diffusion)
|
| 347 |
+
+ bool block_attention = false; // Context-causal block diffusion (openPangu-R-Diffusion)
|
| 348 |
+
float alg_temp = 0; // algorithm temperature (0.0 = deterministic)
|
| 349 |
+
bool add_gumbel_noise = false; // Add gumbel noise to the logits if temp > 0.0
|
| 350 |
+
|
| 351 |
+
diff --git a/gguf-py/gguf/constants.py b/gguf-py/gguf/constants.py
|
| 352 |
+
index 32e088d..85a6517 100644
|
| 353 |
+
--- a/gguf-py/gguf/constants.py
|
| 354 |
+
+++ b/gguf-py/gguf/constants.py
|
| 355 |
+
@@ -507,6 +507,7 @@ class Keys:
|
| 356 |
+
|
| 357 |
+
class Diffusion:
|
| 358 |
+
SHIFT_LOGITS = "diffusion.shift_logits"
|
| 359 |
+
+ BLOCK_ATTENTION = "diffusion.block_attention"
|
| 360 |
+
|
| 361 |
+
class xIELU:
|
| 362 |
+
ALPHA_P = "xielu.alpha_p"
|
| 363 |
+
diff --git a/gguf-py/gguf/gguf_writer.py b/gguf-py/gguf/gguf_writer.py
|
| 364 |
+
index 93ec95e..ccc2ae1 100644
|
| 365 |
+
--- a/gguf-py/gguf/gguf_writer.py
|
| 366 |
+
+++ b/gguf-py/gguf/gguf_writer.py
|
| 367 |
+
@@ -1730,6 +1730,9 @@ class GGUFWriter:
|
| 368 |
+
def add_diffusion_shift_logits(self, value: bool) -> None:
|
| 369 |
+
self.add_bool(Keys.Diffusion.SHIFT_LOGITS, value)
|
| 370 |
+
|
| 371 |
+
+ def add_diffusion_block_attention(self, value: bool) -> None:
|
| 372 |
+
+ self.add_bool(Keys.Diffusion.BLOCK_ATTENTION, value)
|
| 373 |
+
+
|
| 374 |
+
def _pack(self, fmt: str, value: Any, skip_pack_prefix: bool = False) -> bytes:
|
| 375 |
+
pack_prefix = ''
|
| 376 |
+
if not skip_pack_prefix:
|
| 377 |
+
diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
|
| 378 |
+
index ffa35bd..ca9d9e9 100644
|
| 379 |
+
--- a/src/llama-arch.cpp
|
| 380 |
+
+++ b/src/llama-arch.cpp
|
| 381 |
+
@@ -1245,6 +1245,7 @@ bool llm_arch_is_diffusion(const llm_arch & arch) {
|
| 382 |
+
case LLM_ARCH_LLADA:
|
| 383 |
+
case LLM_ARCH_LLADA_MOE:
|
| 384 |
+
case LLM_ARCH_RND1:
|
| 385 |
+
+ case LLM_ARCH_PANGU_EMBED:
|
| 386 |
+
return true;
|
| 387 |
+
default:
|
| 388 |
+
return false;
|