HelloSun commited on
Commit
328fe35
·
verified ·
1 Parent(s): ee6912b

Upload pangu-diffusion-llamacpp.patch with huggingface_hub

Browse files
Files changed (1) hide show
  1. pangu-diffusion-llamacpp.patch +83 -18
pangu-diffusion-llamacpp.patch CHANGED
@@ -1,8 +1,8 @@
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 }));
@@ -10,15 +10,20 @@ index 8b302d5..ac587a2 100644
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
@@ -26,6 +31,12 @@ index 0c2d5e4..386de7f 100644
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
@@ -75,10 +86,10 @@ index 74c7653..6b00cec 100644
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
 
@@ -95,23 +106,25 @@ index c646113..76b3dd0 100644
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
 
@@ -120,10 +133,10 @@ index c646113..76b3dd0 100644
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
 
@@ -187,6 +200,10 @@ index 0ff57d9..3e5ce80 100644
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) {
@@ -208,6 +225,33 @@ index 0ff57d9..3e5ce80 100644
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
  +
@@ -258,10 +302,17 @@ index 0ff57d9..3e5ce80 100644
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;
@@ -271,13 +322,26 @@ index 0ff57d9..3e5ce80 100644
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
  + }
@@ -323,7 +387,7 @@ index 0ff57d9..3e5ce80 100644
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
 
@@ -336,15 +400,16 @@ index 0ff57d9..3e5ce80 100644
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
 
 
1
  diff --git a/common/arg.cpp b/common/arg.cpp
2
+ index 8b302d5..9c2c761 100644
3
  --- a/common/arg.cpp
4
  +++ b/common/arg.cpp
5
+ @@ -4522,6 +4522,16 @@ 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 }));
 
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-confidence-threshold"}, "F",
16
+ + string_format("context-causal block diffusion: reveal every masked token above this confidence each step, 0 = one token per step (default: %.2f)", (double) params.diffusion.confidence_threshold),
17
+ + [](common_params & params, const std::string & value) { params.diffusion.confidence_threshold = std::stof(value); }
18
  + ).set_examples({ LLAMA_EXAMPLE_DIFFUSION }));
19
  add_opt(common_arg(
20
  {"--diffusion-cfg-scale"}, "F",
21
  string_format("llada classifier-free guidance scale (default: %.3f)", (double) params.diffusion.cfg_scale),
22
  diff --git a/common/common.h b/common/common.h
23
+ index 0c2d5e4..de32160 100644
24
  --- a/common/common.h
25
  +++ b/common/common.h
26
+ @@ -413,11 +413,13 @@ struct common_params_diffusion {
27
 
28
  float eps = 0; // epsilon for timesteps
29
  int32_t block_length = 0; // block length for generation
 
31
 
32
  int32_t algorithm = 4; // default algorithm: low-confidence
33
  float alg_temp = 0.0f; // algorithm temperature
34
+
35
+ float cfg_scale = 0; // classifier-free guidance scale
36
+ + float confidence_threshold = 0; // context-causal block diffusion: reveal all tokens above this confidence per step (>0 enables it)
37
+ bool add_gumbel_noise = false; // add gumbel noise to the logits if temp > 0.0
38
+ };
39
+
40
  diff --git a/conversion/pangu.py b/conversion/pangu.py
41
  index 74c7653..6b00cec 100644
42
  --- a/conversion/pangu.py
 
86
  if name == "lm_head.weight":
87
  if self.hparams.get("tie_word_embeddings", False):
88
  diff --git a/examples/diffusion/diffusion-cli.cpp b/examples/diffusion/diffusion-cli.cpp
89
+ index c646113..cdcd6f9 100644
90
  --- a/examples/diffusion/diffusion-cli.cpp
91
  +++ b/examples/diffusion/diffusion-cli.cpp
92
+ @@ -186,10 +186,21 @@ int main(int argc, char ** argv) {
93
  diff_params.shift_logits = true;
94
  }
95
 
 
106
  + GGML_ASSERT(!diff_params.block_attention || params.diffusion.block_length > 0);
107
  +
108
  + if (diff_params.block_attention) {
109
+ + diff_params.block_length = params.diffusion.block_length;
110
+ + diff_params.small_block_length = params.diffusion.small_block_length;
111
+ + diff_params.confidence_threshold = params.diffusion.confidence_threshold;
112
+ + diff_params.schedule = DIFFUSION_TRANSFER_SCHEDULE_BLOCK_BASED;
113
  + } else if (params.diffusion.eps) {
114
  diff_params.schedule = DIFFUSION_TRANSFER_SCHEDULE_TIMESTEP_BASED;
115
  diff_params.eps = params.diffusion.eps;
116
  } else if (params.diffusion.block_length) {
117
+ @@ -242,6 +253,9 @@ int main(int argc, char ** argv) {
118
  LOG_INF("diffusion_params: - %-25s u32 = %d\n", "block_length", diff_params.block_length);
119
  LOG_INF("diffusion_params: - %-25s f32 = %.3f\n", "cfg_scale", diff_params.cfg_scale);
120
  }
121
  + LOG_INF("diffusion_params: - %-25s bool = %s\n", "block_attention", diff_params.block_attention ? "true" : "false");
122
  + LOG_INF("diffusion_params: - %-25s u32 = %d\n", "small_block_length", diff_params.small_block_length);
123
+ + LOG_INF("diffusion_params: - %-25s f32 = %.3f\n", "confidence_threshold", diff_params.confidence_threshold);
124
 
125
  diffusion_generate(ctx, input_tokens.data(), output_tokens.data(), n_input, diff_params, n_generated);
126
 
127
+ @@ -251,6 +265,7 @@ int main(int argc, char ** argv) {
128
  LOG_INF("\033[2J\033[H");
129
  }
130
 
 
133
  std::string output_data = common_detokenize(vocab, output_tokens, false);
134
  LOG_INF("\n%s\n", output_data.c_str());
135
  diff --git a/examples/diffusion/diffusion.cpp b/examples/diffusion/diffusion.cpp
136
+ index 0ff57d9..adc6857 100644
137
  --- a/examples/diffusion/diffusion.cpp
138
  +++ b/examples/diffusion/diffusion.cpp
139
+ @@ -102,6 +102,250 @@ static std::vector<int32_t> get_num_transfer_tokens(int32_t mask_count, int32_t
140
  return num_transfer_tokens;
141
  }
142
 
 
200
  +
201
  + common_batch batch(ctx);
202
  +
203
+ + std::vector<int32_t> step_pos;
204
+ + std::vector<llama_token> step_tok;
205
+ + std::vector<float> step_conf;
206
+ +
207
  + auto run_range = [&](int32_t p0, int32_t p1) -> const float * {
208
  + batch.clear();
209
  + for (int32_t p = p0; p < p1; ++p) {
 
225
  + llama_token_data_array cur_p = { candidates.data(), (size_t) n_vocab, -1, false };
226
  + llama_sampler_apply(sampler, &cur_p);
227
  + tok = cur_p.data[cur_p.selected].id;
228
+ +
229
+ + // the greedy sampler does not compute probabilities - do it here so that the
230
+ + // confidence/entropy/margin based reveal order is meaningful
231
+ + bool probs_ready = false;
232
+ + for (size_t i = 0; i < cur_p.size; ++i) {
233
+ + if (cur_p.data[i].p != 0.0f) {
234
+ + probs_ready = true;
235
+ + break;
236
+ + }
237
+ + }
238
+ + if (!probs_ready) {
239
+ + float mx = -INFINITY;
240
+ + for (size_t i = 0; i < cur_p.size; ++i) {
241
+ + mx = std::max(mx, cur_p.data[i].logit);
242
+ + }
243
+ + float sum = 0.0f;
244
+ + for (size_t i = 0; i < cur_p.size; ++i) {
245
+ + cur_p.data[i].p = expf(cur_p.data[i].logit - mx);
246
+ + sum += cur_p.data[i].p;
247
+ + }
248
+ + if (sum > 0.0f) {
249
+ + for (size_t i = 0; i < cur_p.size; ++i) {
250
+ + cur_p.data[i].p /= sum;
251
+ + }
252
+ + }
253
+ + }
254
+ +
255
  + conf = calculate_confidence(cur_p, params.algorithm, rng);
256
  + };
257
  +
 
302
  + return;
303
  + }
304
  +
305
+ + // predict every masked position of the current small block; reveal the most
306
+ + // confident one and, when a confidence threshold is given, every position whose
307
+ + // confidence exceeds it (this is what makes the "confidence_threshold" mode fast)
308
  + float best_conf = -INFINITY;
309
  + int32_t best_pos = -1;
310
  + llama_token best_tok = 0;
311
+ +
312
+ + step_pos.clear();
313
+ + step_tok.clear();
314
+ + step_conf.clear();
315
+ +
316
  + for (int32_t p = sub_start; p < sub_end; ++p) {
317
  + if (output_tokens[p] != params.mask_token_id) {
318
  + continue;
 
322
  + llama_token tok;
323
  + float conf;
324
  + sample_from(lp, tok, conf);
325
+ +
326
+ + step_pos.push_back(p);
327
+ + step_tok.push_back(tok);
328
+ + step_conf.push_back(conf);
329
+ +
330
  + if (conf > best_conf) {
331
  + best_conf = conf;
332
  + best_pos = p;
333
  + best_tok = tok;
334
  + }
335
  + }
336
+ +
337
+ + if (params.confidence_threshold > 0.0f) {
338
+ + for (size_t i = 0; i < step_pos.size(); ++i) {
339
+ + if (step_conf[i] > params.confidence_threshold) {
340
+ + output_tokens[step_pos[i]] = step_tok[i];
341
+ + }
342
+ + }
343
+ + }
344
+ + if (best_pos >= 0 && output_tokens[best_pos] == params.mask_token_id) {
345
  + output_tokens[best_pos] = best_tok;
346
  + }
347
  + }
 
387
  void diffusion_generate(llama_context * ctx,
388
  const llama_token * input_tokens,
389
  llama_token * output_tokens,
390
+ @@ -113,6 +357,11 @@ void diffusion_generate(llama_context * ctx,
391
  return;
392
  }
393
 
 
400
 
401
  // Initialize with input and pad with mask tokens
402
  diff --git a/examples/diffusion/diffusion.h b/examples/diffusion/diffusion.h
403
+ index 7831445..5cd4c58 100644
404
  --- a/examples/diffusion/diffusion.h
405
  +++ b/examples/diffusion/diffusion.h
406
+ @@ -43,6 +43,9 @@ struct diffusion_params {
407
  float cfg_scale = 0.; // Config scale for classifier-free guidance
408
  float eps = 0.; // Timestep scheduling
409
  int32_t block_length = 0; // Block size (for block scheduling)
410
  + int32_t small_block_length = 0; // Small block size inside a block (context-causal block diffusion)
411
  + bool block_attention = false; // Context-causal block diffusion (openPangu-R-Diffusion)
412
+ + float confidence_threshold = 0.; // Reveal all masked tokens above this confidence per step (0 = one token per step)
413
  float alg_temp = 0; // algorithm temperature (0.0 = deterministic)
414
  bool add_gumbel_noise = false; // Add gumbel noise to the logits if temp > 0.0
415