### Setup Environment and Dependencies Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Commands to create a conda environment and install required dependencies for Open-R1 compatibility. ```bash env=openr1 conda create -n $env python=3.11 -y -c anaconda conda activate $env pip install vllm==0.8.4 pip install setuptools pip install flash-attn==2.8.0.post1 --no-build-isolation pip install -e ".[code]" ``` -------------------------------- ### Start Inference Demo Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Launch the interactive inference demo script. ```bash python inference_demo.py ``` -------------------------------- ### Start GRPO Training Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Command to initiate the training process using the provided run script. ```bash cd .. bash run.sh # in `run.sh`, we start e2b server locally, but you can also run it on CPU clusters. ``` -------------------------------- ### Run Inference with DiffuCoder Instruct Model Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Load the Instruct model and perform chat-based code generation using a specific prompt template. ```python import torch from transformers import AutoModel, AutoTokenizer model_path = "apple/DiffuCoder-7B-cpGRPO" model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) model = model.to("cuda").eval() query = "Write a function to find the shared elements from the given two lists." prompt = f"""<|im_start|>system You are a helpful assistant.<|im_end|> <|im_start|>user {query.strip()} <|im_end|> <|im_start|>assistant """ ## following the template of qwen; you can also use apply_chat_template function TOKEN_PER_STEP = 1 # diffusion timesteps * TOKEN_PER_STEP = total new tokens inputs = tokenizer(prompt, return_tensors="pt") input_ids = inputs.input_ids.to(device="cuda") attention_mask = inputs.attention_mask.to(device="cuda") output = model.diffusion_generate( input_ids, attention_mask=attention_mask, max_new_tokens=256, output_history=True, return_dict_in_generate=True, steps=256//TOKEN_PER_STEP, temperature=0.4, top_p=0.95, alg="entropy", alg_temp=0., ) generations = [ tokenizer.decode(g[len(p) :].tolist()) for p, g in zip(input_ids, output.sequences) ] print(generations[0].split('<|dlm_pad|>')[0]) ``` -------------------------------- ### Prepare GRPO Training Data Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Script to process the AceCode-89k dataset into a hard-difficulty JSONL file. ```bash cd recipes python process_data.py --dataset_path "TIGER-Lab/AceCode-89K" --output_path "./acecode_hard.jsonl" --difficulty "hard" ``` -------------------------------- ### Run Inference with DiffuCoder Base Model Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Load the Base model and perform code completion using diffusion generation. Requires torch and transformers with trust_remote_code enabled. ```python import torch from transformers import AutoModel, AutoTokenizer model_path = "apple/DiffuCoder-7B-Base" model = AutoModel.from_pretrained(model_path, torch_dtype=torch.bfloat16, trust_remote_code=True) tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) model = model.to("cuda").eval() prompt = """ from typing import List def has_close_elements(numbers: List[float], threshold: float) -> bool: """ Check if in given list of numbers, are any two numbers closer to each other than given threshold. >>> has_close_elements([1.0, 2.0, 3.0], 0.5) False >>> has_close_elements([1.0, 2.8, 3.0, 4.0, 5.0, 2.0], 0.3) True """ """ TOKEN_PER_STEP = 1 # diffusion timesteps * TOKEN_PER_STEP = total new tokens inputs = tokenizer(prompt, return_tensors="pt") input_ids = inputs.input_ids.to(device="cuda") attention_mask = inputs.attention_mask.to(device="cuda") output = model.diffusion_generate( input_ids, attention_mask=attention_mask, max_new_tokens=256, output_history=True, return_dict_in_generate=True, steps=256//TOKEN_PER_STEP, temperature=0.2, top_p=0.95, alg="entropy", alg_temp=0., ) generations = [ tokenizer.decode(g[len(p) :].tolist()) for p, g in zip(input_ids, output.sequences) ] print(generations[0].split(tokenizer.eos_token)[0]) ``` -------------------------------- ### Cite DiffuCoder Source: https://github.com/apple/ml-diffucoder/blob/main/README.md Use this BibTeX entry to cite the DiffuCoder research paper in academic publications. ```bibtex @article{gong2025diffucoder, title={DiffuCoder: Understanding and Improving Masked Diffusion Models for Code Generation}, author={Shansan Gong, Ruixiang Zhang, Huangjie Zheng, Jiatao Gu, Navdeep Jaitly, Lingpeng Kong, Yizhe Zhang}, year={2025}, eprint={2506.20639}, archivePrefix={arXiv}, primaryClass={cs.CL}, url={https://arxiv.org/abs/2506.20639}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.