sasakipeter
commited on
Commit
•
0a9ad2f
1
Parent(s):
297adc1
Add Usage
Browse files
README.md
CHANGED
@@ -31,6 +31,101 @@ The dataset includes Japanese instruction-response pairs and has been tailored f
|
|
31 |
|
32 |
---
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
## License
|
35 |
|
36 |
This model is released under the **CC-BY-NC-SA 4.0** license.
|
|
|
31 |
|
32 |
---
|
33 |
|
34 |
+
## Usage
|
35 |
+
|
36 |
+
1. Install Required Libraries
|
37 |
+
|
38 |
+
```
|
39 |
+
!pip install -U bitsandbytes
|
40 |
+
!pip install -U transformers
|
41 |
+
!pip install -U accelerate
|
42 |
+
!pip install -U datasets
|
43 |
+
!pip install -U peft
|
44 |
+
```
|
45 |
+
|
46 |
+
2. Load the Model and Libraries
|
47 |
+
|
48 |
+
```
|
49 |
+
from transformers import (
|
50 |
+
AutoModelForCausalLM,
|
51 |
+
AutoTokenizer,
|
52 |
+
BitsAndBytesConfig,
|
53 |
+
)
|
54 |
+
from peft import PeftModel
|
55 |
+
import torch
|
56 |
+
|
57 |
+
# Hugging Face Token (recommended to set via environment variable)
|
58 |
+
HF_TOKEN = "YOUR_HF_ACCESS_TOKEN"
|
59 |
+
|
60 |
+
# Model and adapter IDs
|
61 |
+
base_model_id = "llm-jp/llm-jp-3-13b" # Base model
|
62 |
+
adapter_id = "sasakipeter/llm-jp-3-13b-finetune"
|
63 |
+
|
64 |
+
# QLoRA (4-bit quantization) configuration
|
65 |
+
bnb_config = BitsAndBytesConfig(
|
66 |
+
load_in_4bit=True,
|
67 |
+
bnb_4bit_quant_type="nf4",
|
68 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
69 |
+
)
|
70 |
+
```
|
71 |
+
|
72 |
+
3. Load the Base Model and LoRA Adapter
|
73 |
+
|
74 |
+
```
|
75 |
+
# Load base model with 4-bit quantization
|
76 |
+
model = AutoModelForCausalLM.from_pretrained(
|
77 |
+
base_model_id,
|
78 |
+
quantization_config=bnb_config,
|
79 |
+
device_map="auto",
|
80 |
+
token=HF_TOKEN
|
81 |
+
)
|
82 |
+
|
83 |
+
# Load tokenizer
|
84 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
85 |
+
base_model_id,
|
86 |
+
trust_remote_code=True,
|
87 |
+
token=HF_TOKEN
|
88 |
+
)
|
89 |
+
|
90 |
+
# Integrate LoRA adapter into the base model
|
91 |
+
model = PeftModel.from_pretrained(model, adapter_id, token=HF_TOKEN)
|
92 |
+
```
|
93 |
+
|
94 |
+
4. Perform Inference
|
95 |
+
|
96 |
+
```
|
97 |
+
# Example input prompt
|
98 |
+
input_text = """次の文章を要約してください。
|
99 |
+
|
100 |
+
日本は四季があり、春には桜が咲き、夏には暑さが続きます。秋には紅葉が美しく、冬には雪が降ります。"""
|
101 |
+
|
102 |
+
# Format the input prompt
|
103 |
+
prompt = f"""### 指示
|
104 |
+
{input_text}
|
105 |
+
### 回答
|
106 |
+
"""
|
107 |
+
|
108 |
+
# Tokenize input and move to the model's device
|
109 |
+
tokenized_input = tokenizer(prompt, return_tensors="pt").to(model.device)
|
110 |
+
|
111 |
+
# Generate output
|
112 |
+
with torch.no_grad():
|
113 |
+
outputs = model.generate(
|
114 |
+
**tokenized_input,
|
115 |
+
max_new_tokens=100,
|
116 |
+
do_sample=False,
|
117 |
+
repetition_penalty=1.2,
|
118 |
+
pad_token_id=tokenizer.eos_token_id
|
119 |
+
)
|
120 |
+
|
121 |
+
# Decode the output
|
122 |
+
output = tokenizer.decode(outputs[0][tokenized_input.input_ids.size(1):], skip_special_tokens=True)
|
123 |
+
print("Output:")
|
124 |
+
print(output)
|
125 |
+
```
|
126 |
+
|
127 |
+
---
|
128 |
+
|
129 |
## License
|
130 |
|
131 |
This model is released under the **CC-BY-NC-SA 4.0** license.
|