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>
```