Files changed (1) hide show
  1. README.md +48 -1
README.md CHANGED
@@ -16,12 +16,59 @@ The DiffuCoder-7B-Base model is our foundational masked diffusion LLM for code g
16
 
17
  - Benchmarks: Strong baseline performance on HumanEval, MBPP and BigCodeBench.
18
 
19
-
20
  #### More details and usage examples:
21
 
22
  - Paper: [DiffuCoder: Understanding and Improving Masked Diffusion Models for Code Generation](https://arxiv.org/abs/2506.20639)
23
 
24
  - GitHub: https://github.com/apple/ml-diffucoder
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  #### Acknowledgement
27
  To power this HuggingFace model release, we reuse [Dream](https://huggingface.co/Dream-org/Dream-v0-Base-7B)'s modeling architecture and generation utils.
 
16
 
17
  - Benchmarks: Strong baseline performance on HumanEval, MBPP and BigCodeBench.
18
 
 
19
  #### More details and usage examples:
20
 
21
  - Paper: [DiffuCoder: Understanding and Improving Masked Diffusion Models for Code Generation](https://arxiv.org/abs/2506.20639)
22
 
23
  - GitHub: https://github.com/apple/ml-diffucoder
24
 
25
+ ```
26
+ import torch
27
+ from transformers import AutoModel, AutoTokenizer
28
+
29
+ model_path = "apple/DiffuCoder-7B-Base"
30
+ model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, trust_remote_code=True)
31
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
32
+ model = model.to("cuda").eval()
33
+
34
+ prompt = """
35
+ from typing import List
36
+
37
+ def has_close_elements(numbers: List[float], threshold: float) -> bool:
38
+ \"\"\"
39
+ Check if in given list of numbers, are any two numbers closer to each other than given threshold.
40
+ >>> has_close_elements([1.0, 2.0, 3.0], 0.5)
41
+ False
42
+ >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3)
43
+ True
44
+ \"\"\"
45
+ """
46
+
47
+ TOKEN_PER_STEP = 1 # diffusion timesteps * TOKEN_PER_STEP = total new tokens
48
+
49
+ inputs = tokenizer(prompt, return_tensors="pt")
50
+ input_ids = inputs.input_ids.to(device="cuda")
51
+ attention_mask = inputs.attention_mask.to(device="cuda")
52
+
53
+ output = model.diffusion_generate(
54
+ input_ids,
55
+ attention_mask=attention_mask,
56
+ max_new_tokens=256,
57
+ output_history=True,
58
+ return_dict_in_generate=True,
59
+ steps=256//TOKEN_PER_STEP,
60
+ temperature=0.2,
61
+ top_p=0.95,
62
+ alg="entropy",
63
+ alg_temp=0.,
64
+ )
65
+ generations = [
66
+ tokenizer.decode(g[len(p) :].tolist())
67
+ for p, g in zip(input_ids, output.sequences)
68
+ ]
69
+
70
+ print(generations[0].split(tokenizer.eos_token)[0])
71
+ ```
72
+
73
  #### Acknowledgement
74
  To power this HuggingFace model release, we reuse [Dream](https://huggingface.co/Dream-org/Dream-v0-Base-7B)'s modeling architecture and generation utils.