File size: 8,372 Bytes
7f1e4f7 159f686 7f1e4f7 40f6038 33fc9e4 40f6038 33fc9e4 40f6038 33fc9e4 1caec0f 33fc9e4 40f6038 33fc9e4 40f6038 33fc9e4 40f6038 33fc9e4 40f6038 1caec0f 33fc9e4 1caec0f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
---
base_model: fine-tuned-model
language:
- ko
license: apache-2.0
tags:
- text-generation-inference
- transformers
- unsloth
- gemma2
- trl
---
# Uploaded model
- **Developed by:** limecoding
- **License:** apache-2.0
- **Finetuned from model :** fine-tuned-model
This gemma2 model was trained 2x faster with [Unsloth](https://github.com/unslothai/unsloth) and Huggingface's TRL library.
[<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>](https://github.com/unslothai/unsloth)
## Model Overview
This model is fine-tuned to assist with drafting patent specifications based on a general description of an invention.
The base model is unsloth/gemma-2-2b-it, and I used unsloth to merge the fine-tuned adapter.
## Dataset
The dataset used for fine-tuning includes a combination of research paper
summary datasets from AI-Hub and patent claims data directly retrieved from KIPRIS
(Korea Intellectual Property Rights Information Service).
Model Training
The model was trained using LoRA (Low-Rank Adaptation). The following code was used for training:
```
model = FastLanguageModel.get_peft_model(
model,
r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
"gate_proj", "up_proj", "down_proj",],
lora_alpha = 16,
lora_dropout = 0, # Supports any, but = 0 is optimized
bias = "none", # Supports any, but = "none" is optimized
# [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
random_state = 3407,
use_rslora = False, # We support rank stabilized LoRA
loftq_config = None, # And LoftQ
)
```
```
from trl import SFTTrainer
from transformers import TrainingArguments
from unsloth import is_bfloat16_supported
trainer = SFTTrainer(
model = model,
tokenizer = tokenizer,
train_dataset = train_data,
max_seq_length = max_seq_length,
formatting_func = generate_prompt,
dataset_num_proc = 2,
packing = False, # Can make training 5x faster for short sequences.
args = TrainingArguments(
per_device_train_batch_size = 2,
gradient_accumulation_steps = 4,
warmup_steps = 5,
num_train_epochs = 1, # Set this for 1 full training run.
# max_steps = 100,
learning_rate = 2e-4,
fp16 = not is_bfloat16_supported(),
bf16 = is_bfloat16_supported(),
logging_steps = 10,
optim = "adamw_8bit",
weight_decay = 0.01,
lr_scheduler_type = "linear",
seed = 3407,
output_dir = "outputs",
),
)
```
## How to Use the Model
1. Install unsloth:
```
%%capture
!pip install unsloth
# Also get the latest nightly Unsloth!
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
# Install Flash Attention 2 for softcapping support
import torch
if torch.cuda.get_device_capability()[0] >= 8:
!pip install --no-deps packaging ninja einops "flash-attn>=2.6.3"
```
2. Load the fine-tuned model and use it for inference:
```
from unsloth import FastLanguageModel
import torch
max_seq_length = 4096
dtype = None
load_in_4bit = True
token = "your-huggingface-token"
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "limecoding/gemma2-2b-it-finetuned-patent",
max_seq_length = max_seq_length,
dtype = dtype,
load_in_4bit = load_in_4bit,
token = token
)
```
3. Write a prompt and generate text:
```
input = """
์์ ํ ๊ณผ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ์ฌ, ๋ณธ ๊ณ ์์ ๋ด๋ถ์ ๋ณด๊ดํ ๋ฌผ๊ฑด์ ๋ฃ์ ์ ์๋ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ๊ณผ ์ด๋ฅผ ๋๋ฌ์ผ
์ธํผ๋ฅผ ํฌํจํ๋ ๊ฐ๋ฐฉ์ ์์ด์, ์๊ธฐ ์ธํผ์๋ ์ด๋ฆฌ๊ณ ๋ซํ๋ ํ์ฅ ์ธํผ ์งํผ๊ฐ ํ์ฑ๋์ด ์๊ณ , ์๊ธฐ ํ์ฅ ์ธ
ํผ ์งํผ์ ๋ด์ธก์๋ ์๊ธฐ ํ์ฅ ์ธํผ ์งํผ๊ฐ ์ด๋ฆฌ๋ ๊ฒฝ์ฐ ํผ์ณ์ง๋ ํ์ฅ ๋ดํผ๋ฅผ ๋ ํฌํจํ๋, ์๊ธฐ ํ์ฅ ๋ดํผ์
๋ด์ธก์ผ๋ก ์ถ๊ฐ ๊ณต๊ฐ์ด ํ์ฑ๋์ด ์ถ๊ฐ ์๋ฉ๊ณต๊ฐ์ ๊ตฌ๋นํ ๋ก ํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ๋ ์ถ๊ฐ ์๋ฉ๊ณต๊ฐ์ด ๊ตฌ๋น๋ ๊ฐ
๋ฐฉ์ ์ ๊ณตํ๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ํ์ฅ ์ธํผ ์งํผ๋ ์๊ธฐ ๊ฐ๋ฐฉ์ ์ธ์ฃผ ์ ์ฒด๋ฅผ ๊ฐ์ธ๋ฉด์, ์๊ธฐ ํ์ฅ ๋ดํผ๋ก ์ฐ์ฅ๋์ด, ์๊ธฐ ํ์ฅ
์ธํผ ์งํผ๋ฅผ ์ ๋ถ ์ฌ๋ ๊ฒฝ์ฐ ์๊ธฐ ์ธํผ๊ฐ ์๊ธฐ ํ์ฅ ๋ดํผ๋ก ์ฐ๊ฒฐ๋๋ฉด์ ๋ถ๋ฆฌ๋์ด ๊ทธ ๋ด๋ถ์ ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ์
ํ์ฑํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ์ ์๊ธฐ ๊ฐ๋ฐฉ์ ์์ธก์ ๊ตฌ๋น๋๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
์๊ธฐ ๊ฐ๋ฐฉ์ ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ์ด ํ์ฅ๋ ์ ์๋ ์๋จ์ ๋ ํฌํจํ๋, ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ์ด ํ์ฅ๋ ์ ์
๋ ์๋จ์ ์๊ธฐ ํ์ฅ ์ธํผ ์งํผ์ ๋ด์ธก์ ํ์ฑ๋ ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ์ด ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ๊ณผ ํตํ์ฌ ์๊ธฐ ๊ธฐ๋ณธ ๋ด
์ฅ ๊ณต๊ฐ์ด ํ์ฅ๋๋๋ก ํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ๊ณผ ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ ์ฌ์ด์๋ ๊ฒฉ๋ฒฝ์ด ํ์ฑ๋์ด ๋ณ๋์ ์ถ๊ฐ ์๋ฉ๊ณต๊ฐ์ด ํ์ฑ๋๋
๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๊ฒฉ๋ฒฝ์ ์๊ธฐ ๊ฐ๋ฐฉ์ ๋ด์ธก์์ ํ์ฐฉ๋๋ ๊ฒ์ผ๋ก์, ํ์์ ๋ฐ๋ผ ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ๊ณผ ์๊ธฐ ์ถ
๊ฐ ๊ณต๊ฐ์ ๋ถ๋ฆฌ์ํค๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ์ ๋ด์ธก์๋ ๋ถ๋ฆฌํ ์นธ๋ง์ด๊ฐ ํ์ฐฉ ๊ฐ๋ฅํ๊ฒ ๋ถ์ค๋์ด ์๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์
์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ์ธํผ์ ์ธ์ธก์ผ๋ก ๋ณด์กฐํฌ์ผ์ด ํ์ฑ๋์ด ๋ณ๋์ ์๋ฉ๊ณต๊ฐ์ด ํ์ฑ๋๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๋ณด์กฐํฌ์ผ์ ๋ด๋ถ์๋ ํ๋ ฅ๋ฐด๋๊ฐ ๋ถ์ฐฉ๋๋ ๊ฐ๊ฒฉ์ ๋๊ณ ๊ทธ ์ผ๋ถ๊ฐ ๋ถ์ฐฉ๋จ์ผ๋ก์จ ๋ถ์ฐฉ๋์ง ์๋
๊ณต๊ฐ์ผ๋ก ๋ณด๊ดํ๋ ๋ฌผ๊ฑด์ ๋ผ์๋ ์ ์๋๋ก ํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ํ์ฅ ๋ดํผ์ ์๋ถ์๋ ๋ดํผ ๊ฐํ ์งํผ๊ฐ ํ์ฑ๋์ด, ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ์ ๋ด๋ถ๋ฅผ ์ด๊ณ ๋ซ์ ์ ์๋
๋ก ํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ์ถ๊ฐ ๊ณต๊ฐ์ ํ์ฑ๋ ์๊ธฐ ๋ดํผ ๊ฐํ ์งํผ์ ์์ชฝ๋ถ๋ ๋ด๋ถ๊ฐ ๋ณด์ด๋ ๋ง์ฌํ ์ง๋ฌผ๋ถ๋ก ํ์ฑํ์ฌ
๋ด์ฅ๋ ๋ฌผํ์ ๋ฐ๋ก ํ์ธํ ์ ์๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๊ฐ๋ฐฉ์ ๊ฐ๋ฐฉ ํด๋์๊ฐ ์ด๊นจ์ ๋ฉ ์ ์๋๋ก ์ด๊นจ์ฉ ๋ ์ฐ๊ฒฐ๋ถ๊ฐ ํ์ฑ๋์ด ์๋ ๊ฒ์ ํน์ง์ผ๋ก
ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ์ด๊นจ์ฉ ๋ ์ฐ๊ฒฐ๋ถ์ ์์ธก ๋๋จ์ด ๊ณ ์ ๋๋ ์ด๊นจ์ฉ ๋์ ๋ ํฌํจํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ ์ ์๋ค.
๋ณธ ๊ณ ์์ ์๊ธฐ ๊ฐ๋ฐฉ์ ์ธํผ์ ๋ถ์ฐฉ๋์ด ์๊ธฐ ๊ฐ๋ฐฉ์ ๋ค ์ ์๋๋ก ํ์ฑ๋๋ ์์ก์ด๋ฅผ ๋ ํฌํจํ๋ ๊ฒ์ ํน์ง์ผ
๋ก ํ ์ ์๋ค
"""
FastLanguageModel.for_inference(model)
inputs = tokenizer(
[
r"""<bos><start_of_turn>user
๋ค์ ๊ณผ์ ํด๊ฒฐ์๋จ์ ๋ณด๊ณ ๋ฐ๋ช
์ ๋ช
์นญ, ๊ธฐ์ ๋ถ์ผ, ์ฒญ๊ตฌํญ์ ๋ฝ์์ฃผ์ธ์.: {}<end_of_turn>
<start_of_turn>model""".format(input)
], return_tensors = "pt").to("cuda")
from transformers import TextStreamer
text_streamer = TextStreamer(tokenizer)
_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 1000)
```
## Model Results
The model was tested using the "Means to Solve the Problem" section from actual patent specifications.
When compared with real patent documents, the model generated content that was relatively similar in
structure and meaning.
```
[๋ฐ๋ช
์ ๋ช
์นญ]
๊ฐ๋ฐฉ
[๊ธฐ์ ๋ถ์ผ]
๋ณธ ๋ฐ๋ช
์ ๊ฐ๋ฐฉ์ ๊ดํ ๊ฒ์ผ๋ก, ๋ณด๋ค ์์ธํ๊ฒ๋ ํ์ฅ ๊ฐ๋ฅํ ๊ฐ๋ฐฉ์ ๊ดํ ๊ฒ์ด๋ค.
[์ฒญ๊ตฌํญ]
๋ด๋ถ์ ๋ณด๊ดํ ๋ฌผ๊ฑด์ ๋ฃ์ ์ ์๋ ๊ธฐ๋ณธ ๋ด์ฅ ๊ณต๊ฐ๊ณผ ์ด๋ฅผ ๋๋ฌ์ผ ์ธํผ๋ฅผ ํฌํจํ๋ ๊ฐ๋ฐฉ์ ์์ด์,
์๊ธฐ ์ธํผ์๋ ์ด๋ฆฌ๊ณ ๋ซํ๋ ํ์ฅ ์ธํผ ์งํผ๊ฐ ํ์ฑ๋์ด ์๊ณ ,
์๊ธฐ ํ์ฅ ์ธํผ ์งํผ์ ๋ด์ธก์๋ ์๊ธฐ ํ์ฅ ์ธํผ ์งํผ๊ฐ ์ด๋ฆฌ๋ ๊ฒฝ์ฐ ํผ์ณ์ง๋ ํ์ฅ ๋ดํผ๋ฅผ ๋ ํฌํจํ๋,
์๊ธฐ ํ์ฅ ๋ดํผ์ ๋ด์ธก์ผ๋ก ์ถ๊ฐ ๊ณต๊ฐ์ด ํ์ฑ๋์ด ์ถ๊ฐ ์๋ฉ๊ณต๊ฐ์ ๊ตฌ๋นํ ๋ก ํ๋ ๊ฒ์ ํน์ง์ผ๋ก ํ๋ ์ถ๊ฐ ์๋ฉ๊ณต๊ฐ์ด ๊ตฌ๋น๋ ๊ฐ๋ฐฉ.<end_of_turn>
```
|