PEFT
Safetensors
nielsr HF Staff commited on
Commit
50072b7
·
verified ·
1 Parent(s): fa03b55

Improve model card: Add pipeline tag, license, GitHub link, abstract, and sample usage

Browse files

This PR significantly enhances the model card for the `StarCoder2-SFT` adapter by:

* Adding the `pipeline_tag: text-generation` to improve discoverability on the Hugging Face Hub (e.g., at https://huggingface.co/models?pipeline_tag=text-generation).
* Specifying the `license: other` as no explicit license was found in the repository or paper.
* Including the `tags: - code-generation` to further categorize the model accurately.
* Updating the paper link to the official Hugging Face paper page: https://huggingface.co/papers/2506.00419.
* Adding a direct link to the associated GitHub repository: https://github.com/StonyBrookNLP/disco-lpo.
* Incorporating the paper's abstract to provide immediate context about the model's purpose and methodology.
* Adding a "Usage" section with a Python code snippet that demonstrates how to load this PEFT adapter with its base model and perform text generation using the `transformers` and `peft` libraries.
* Including the BibTeX citation from the GitHub README for proper attribution.
* Removing the non-standard "File information" section for better readability.

These changes aim to make the model card more comprehensive, discoverable, and user-friendly.

Files changed (1) hide show
  1. README.md +73 -3
README.md CHANGED
@@ -1,10 +1,80 @@
1
  ---
2
  base_model: bigcode/starcoder2-7b
3
  library_name: peft
 
 
 
 
4
  ---
5
 
6
  # Model Card for StarCoder2-SFT
7
 
8
- This is the adapter of the Starcoder2 model trained using SFT on DiSCo for the paper "Teaching an Old LLM Secure Coding:
9
- Localized Preference Optimization on Distilled Preferences" (https://arxiv.org/abs/2506.00419). Merge it to the base model "bigcode/starcoder2-7b"
10
- in order to use for downstream tasks.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  base_model: bigcode/starcoder2-7b
3
  library_name: peft
4
+ pipeline_tag: text-generation
5
+ license: other
6
+ tags:
7
+ - code-generation
8
  ---
9
 
10
  # Model Card for StarCoder2-SFT
11
 
12
+ This is the adapter of the Starcoder2 model trained using Supervised Fine-Tuning (SFT) on the DiSCo dataset, presented in the paper [Teaching an Old LLM Secure Coding: Localized Preference Optimization on Distilled Preferences](https://huggingface.co/papers/2506.00419). This model aims to improve secure code generation in Large Language Models.
13
+
14
+ To use this model for downstream tasks, you need to merge this adapter with its base model, "bigcode/starcoder2-7b", or load it directly on top of the base model using the `peft` library.
15
+
16
+ **Code Repository**: https://github.com/StonyBrookNLP/disco-lpo
17
+
18
+ ## Abstract
19
+
20
+ LLM generated code often contains security issues. We address two key challenges in improving secure code generation. First, obtaining high quality training data covering a broad set of security issues is critical. To address this, we introduce a method for distilling a preference dataset of insecure and secure code pairs from frontier LLMs, along with a security reasoning that explains the issues and the fix. The key idea here is to make use of security knowledge sources to devise a systematic prompting strategy that ensures broad coverage. Second, aligning models to secure code requires focusing on localized regions of code. Direct preference optimization methods, like SimPO, are not designed to handle these localized differences and turn out to be ineffective. We address this with a new localized preference optimization algorithm that masks the security related tokens in both the winning (secure) and losing (insecure) responses. To prevent loss in code quality, we also add a regularizer. Evaluations show that both training on our dataset, DiSCo, and the new preference optimization algorithm, LPO, yield substantial reductions in code insecurity while also improving overall code quality.
21
+
22
+ ## Usage
23
+
24
+ To use this adapter for code generation, first load the base model, then load this PEFT adapter on top of it.
25
+
26
+ ```python
27
+ from transformers import AutoTokenizer, AutoModelForCausalLM
28
+ from peft import PeftModel
29
+ import torch
30
+
31
+ base_model_id = "bigcode/starcoder2-7b"
32
+ adapter_id = "StonyBrookNLP/StarCoder2-SFT" # This model adapter
33
+
34
+ # Load the tokenizer for the base model
35
+ tokenizer = AutoTokenizer.from_pretrained(base_model_id)
36
+
37
+ # Load the base model
38
+ model = AutoModelForCausalLM.from_pretrained(
39
+ base_model_id,
40
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
41
+ device_map="auto" # or specify your device, e.g., "cuda:0"
42
+ )
43
+
44
+ # Load the PEFT adapter on top of the base model
45
+ model = PeftModel.from_pretrained(model, adapter_id)
46
+ model.eval() # Set the model to evaluation mode
47
+
48
+ # Example prompt for secure code generation
49
+ prompt = "# Write a Python function that safely handles user input.
50
+ def get_safe_input(prompt):
51
+ "
52
+ input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
53
+
54
+ # Generate text
55
+ with torch.no_grad():
56
+ generated_ids = model.generate(
57
+ input_ids,
58
+ max_new_tokens=100,
59
+ do_sample=True,
60
+ temperature=0.7,
61
+ top_p=0.9,
62
+ pad_token_id=tokenizer.eos_token_id # Important for generation
63
+ )
64
+
65
+ # Decode and print the generated text
66
+ print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
67
+ ```
68
+
69
+ ## Citation
70
+
71
+ Please include the following citation if you are using resources provided in this work:
72
+
73
+ ```bibtex
74
+ @article{saqib2025teaching,
75
+ title={Teaching an Old LLM Secure Coding: Localized Preference Optimization on Distilled Preferences},
76
+ author={Saqib, Mohammad and Chakraborty, Saikat and Karmaker, Santu and Balasubramanian, Niranjan},
77
+ journal={arXiv preprint arXiv:2506.00419},
78
+ year={2025}
79
+ }
80
+ ```