Files changed (1) hide show
  1. README.md +44 -0
README.md CHANGED
@@ -22,5 +22,49 @@ The DiffuCoder-7B-Instruct model builds on the DiffuCoder-7B-Base checkpoint wit
22
 
23
  - GitHub: https://github.com/apple/ml-diffucoder
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  #### Acknowledgement
26
  To power this HuggingFace model release, we reuse [Dream](https://huggingface.co/Dream-org/Dream-v0-Base-7B)'s modeling architecture and generation utils.
 
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-Instruct"
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
+ query = "Write a function to find the shared elements from the given two lists."
35
+ prompt = f"""<|im_start|>system
36
+ You are a helpful assistant.<|im_end|>
37
+ <|im_start|>user
38
+ {query.strip()}
39
+ <|im_end|>
40
+ <|im_start|>assistant
41
+ """ ## following the template of qwen; you can also use apply_chat_template function
42
+
43
+ TOKEN_PER_STEP = 1 # diffusion timesteps * TOKEN_PER_STEP = total new tokens
44
+
45
+ inputs = tokenizer(prompt, return_tensors="pt")
46
+ input_ids = inputs.input_ids.to(device="cuda")
47
+ attention_mask = inputs.attention_mask.to(device="cuda")
48
+
49
+ output = model.diffusion_generate(
50
+ input_ids,
51
+ attention_mask=attention_mask,
52
+ max_new_tokens=256,
53
+ output_history=True,
54
+ return_dict_in_generate=True,
55
+ steps=256//TOKEN_PER_STEP,
56
+ temperature=0.3,
57
+ top_p=0.95,
58
+ alg="entropy",
59
+ alg_temp=0.,
60
+ )
61
+ generations = [
62
+ tokenizer.decode(g[len(p) :].tolist())
63
+ for p, g in zip(input_ids, output.sequences)
64
+ ]
65
+
66
+ print(generations[0].split('<|dlm_pad|>')[0])
67
+ ```
68
+
69
  #### Acknowledgement
70
  To power this HuggingFace model release, we reuse [Dream](https://huggingface.co/Dream-org/Dream-v0-Base-7B)'s modeling architecture and generation utils.