Update README.md
Browse files
README.md
CHANGED
|
@@ -20,3 +20,94 @@ language:
|
|
| 20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
| 21 |
|
| 22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
This llama model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
|
| 21 |
|
| 22 |
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
### **📝 Load and Run Inference**
|
| 26 |
+
```bash
|
| 27 |
+
|
| 28 |
+
from unsloth import FastLanguageModel
|
| 29 |
+
from peft import PeftModel
|
| 30 |
+
import torch
|
| 31 |
+
|
| 32 |
+
# Load the base model
|
| 33 |
+
base_model_name = "unsloth/Phi-4-unsloth-bnb-4bit",
|
| 34 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 35 |
+
model_name=base_model_name,
|
| 36 |
+
max_seq_length=2048, # Must match fine-tuning
|
| 37 |
+
load_in_4bit=True,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Load the fine-tuned LoRA adapter
|
| 41 |
+
lora_model_name = "Machlovi/Phi4_Safe"
|
| 42 |
+
model = PeftModel.from_pretrained(model, lora_model_name)
|
| 43 |
+
|
| 44 |
+
# Run inference
|
| 45 |
+
input_text = "Why do we need to go to see something?"
|
| 46 |
+
inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
|
| 47 |
+
|
| 48 |
+
with torch.no_grad():
|
| 49 |
+
outputs = model.generate(**inputs, max_new_tokens=4)
|
| 50 |
+
|
| 51 |
+
# Decode and print response
|
| 52 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
```
|
| 57 |
+
### **📝Inference with TextStreamer**
|
| 58 |
+
```bash
|
| 59 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
| 60 |
+
|
| 61 |
+
messages=[
|
| 62 |
+
{'content': 'why even let a tranny in they arent really even human',
|
| 63 |
+
'role': 'user'},
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
tokenizer = get_chat_template(
|
| 67 |
+
tokenizer,
|
| 68 |
+
chat_template = "Phi-4", # Supports zephyr, chatml, mistral, llama, alpaca, vicuna, vicuna_old, unsloth
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
inputs = tokenizer.apply_chat_template(
|
| 74 |
+
messages,
|
| 75 |
+
tokenize = True,
|
| 76 |
+
add_generation_prompt = True, # Must add for generation
|
| 77 |
+
return_tensors = "pt",
|
| 78 |
+
).to("cuda")
|
| 79 |
+
|
| 80 |
+
from transformers import TextStreamer
|
| 81 |
+
text_streamer = TextStreamer(tokenizer)
|
| 82 |
+
_ = model.generate(input_ids = inputs, streamer = text_streamer, max_new_tokens = 10, use_cache = True)
|
| 83 |
+
|
| 84 |
+
Hate speech, personal attacks, and discrimination
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
### Prompt
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
`
|
| 92 |
+
alpaca_prompt = """<|im_start|>system<|im_sep|>
|
| 93 |
+
system<|im_end|>
|
| 94 |
+
<|im_start|>user<|im_sep|>
|
| 95 |
+
{}
|
| 96 |
+
<|im_end|>
|
| 97 |
+
<|im_start|>assistant<|im_sep|>
|
| 98 |
+
{}
|
| 99 |
+
|
| 100 |
+
"""
|
| 101 |
+
`
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
### Safety Policies Instructions
|
| 105 |
+
`
|
| 106 |
+
You are a policy expert trying to help determine whether a user prompt is in violation of the defined safety policies.
|
| 107 |
+
* "No Harassment": The input shall not contain or seek generation of content that is malicious, intimidating, bullying, or abusive content targeting another individual (e.g., physical threats, denial of tragic events, disparaging victims of violence).
|
| 108 |
+
* "No Dangerous Content": The input shall not contain or seek generation of content that harming oneself and/or others (e.g., accessing or building firearms and explosive devices, promotion of terrorism, instructions for suicide, violent protest).
|
| 109 |
+
* "No Hate Speech": The input shall not contain or seek generation of content that targeting identity and/or protected attributes (e.g., racial slurs, promotion of discrimination, calls to violence against protected groups) or dehumanizing/belittling/vilifying on the bases of race, ethnicity, religion, disability, age, nationality, veteran status, sexual orientation, gender, gender identity, caste, or any other protected status.
|
| 110 |
+
* "No Sexually Explicit Information": The input shall not contain or seek generation of content that contains references to sexual acts or other lewd content (e.g., sexually arousal descriptions, content aimed at causing arousal, sexual assault (ex: rape),sexual harassment (physical, verbal, or visual)). Medical or scientific terms related to human anatomy or sex education are permitted.
|
| 111 |
+
Does the human question violate the above principle? Your answer must start with 'Safe' or 'Unsafe'.
|
| 112 |
+
- First line must read 'safe' or 'unsafe'.
|
| 113 |
+
- If unsafe, a second line must include a comma-separated list of violated categories.`
|