xxccho commited on
Commit
b375100
·
verified ·
1 Parent(s): 55ef881

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +67 -5
README.md CHANGED
@@ -19,12 +19,74 @@ It has been trained using [TRL](https://github.com/huggingface/trl).
19
  ## Quick start
20
 
21
  ```python
22
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- text = "The capital of France is Paris."
25
- rewarder = pipeline(model="None", device="cuda")
26
- output = rewarder(text)[0]
27
- print(output["score"])
28
  ```
29
 
30
  ## Training procedure
 
19
  ## Quick start
20
 
21
  ```python
22
+ import torch
23
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
24
+ from peft import PeftModel, PeftConfig
25
+
26
+ # 1. Define the PEFT model ID
27
+ peft_model_id = "xxccho/margin_reg_baseline"
28
+
29
+ # 2. Load the PEFT config
30
+ config = PeftConfig.from_pretrained(peft_model_id)
31
+
32
+ # 3. Load tokenizer from base model (safer)
33
+ tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
34
+
35
+ # Llama padding fix
36
+ if tokenizer.pad_token is None:
37
+ tokenizer.pad_token = tokenizer.eos_token
38
+
39
+ # 4. Load base model
40
+ base_model = AutoModelForSequenceClassification.from_pretrained(
41
+ config.base_model_name_or_path,
42
+ num_labels=1,
43
+ torch_dtype=torch.bfloat16,
44
+ device_map="auto"
45
+ )
46
+
47
+ # 5. Apply LoRA adapter
48
+ model = PeftModel.from_pretrained(base_model, peft_model_id)
49
+ model.config.pad_token_id = tokenizer.pad_token_id
50
+ model.eval()
51
+
52
+ # -----------------------------
53
+ # Example Usage (chat format)
54
+ # -----------------------------
55
+ messages = [
56
+ {"role": "user", "content": "What is the capital of France?"},
57
+ {"role": "assistant", "content": "The capital of France is Paris."}
58
+ ]
59
+
60
+ # Format prompt using chat template
61
+ formatted_prompt = tokenizer.apply_chat_template(
62
+ messages,
63
+ tokenize=False,
64
+ add_generation_prompt=False
65
+ )
66
+
67
+ inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device)
68
+
69
+ # Get reward score
70
+ with torch.no_grad():
71
+ outputs = model(**inputs)
72
+ reward_score = outputs.logits.squeeze().item()
73
+
74
+ print(f"[Chat] Reward Score: {reward_score:.4f}")
75
+
76
+
77
+ # -----------------------------
78
+ # Example Usage (plain text)
79
+ # -----------------------------
80
+ text = "User: What is the capital of France?\nAssistant: Paris."
81
+
82
+ inputs = tokenizer(text, return_tensors="pt").to(model.device)
83
+
84
+ with torch.no_grad():
85
+ outputs = model(**inputs)
86
+ reward_score = outputs.logits.squeeze().item()
87
+
88
+ print(f"[Plain] Reward Score: {reward_score:.4f}")
89
 
 
 
 
 
90
  ```
91
 
92
  ## Training procedure