Text Generation
PEFT
Safetensors
conversational
File size: 10,486 Bytes
e535118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86cda19
 
e535118
 
 
 
 
 
 
 
 
b08c48b
 
 
e535118
 
 
86cda19
e535118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b08c48b
e535118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86cda19
e535118
 
 
 
 
 
 
 
 
86cda19
 
e535118
 
 
 
 
 
 
86cda19
e535118
 
 
 
b08c48b
 
e535118
 
 
 
 
 
 
 
 
 
 
 
 
86cda19
e535118
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
---
base_model: models/gemma-2-9b
library_name: peft
license: cc-by-sa-4.0
datasets:
- universal-dependencies/universal_dependencies
language:
- en
- ja
- zh
- ko
- fr
- de
- sl
metrics:
- LAS
- UAS
- UPOS
pipeline_tag: text-generation
---
# Model Card for prompt-parsing-v0-gemma-2-9b-lora

[megagonlabs/prompt-based-parsing-gemma-2-9b-lora-v1](https://huggingface.co/megagonlabs/prompt-based-parsing-gemma-2-9b-lora-v1) is a dependency parsing model which analyze a gold token sequence in user prompt in step-by-step way.
This model is trained using the Universal Dependencies datasets over 7 languages, and provides SoTA-level accuracy for UPOS, UAS, and LAS.

[megagonlabs/prompt-based-parsing-gemma-2-9b-lora-v1](https://huggingface.co/megagonlabs/prompt-based-parsing-gemma-2-9b-lora-v1)はユーザプロンプトで与えられた正解トークン列に対してstep-by-stepで依存構造解析を行うモデルです。
このモデルはUniversal Dependenciesの7つの言語のデータセットを用いて訓練されており、UPOS, UAS, LASにおいてSoTAレベルの解析精度を持ちます。

## Terms of Use

This LoRA adapter package is released under the CC BY-SA 4.0.  
However, please note the following important conditions regarding its usage:  
- This package **does not contain any part of the original Gemma 2 model**.
- In order to use this package, you must obtain and use the base model distributed from Google:
  [Gemma 2 9B base on Hugging Face](https://huggingface.co/google/gemma-2-9b)
- **Use of the Gemma models requires agreement to the [Gemma Terms of Use](https://ai.google.dev/gemma/terms)**.

利用規約 (Japanese version of the Terms of Use)

このLoRAアダプタパッケージは、CC BY-SA 4.0に基づいてリリースされています。  
ただし、使用に関しては以下の重要な利用条件に注意してください。  
- このパッケージには**オリジナルのGemma 2モデルは含まれていません**
- このパッケージを使用するには、Googleが配布するGemmaモデルを入手して使用する必要があります:
  [Gemma 2 9B base on Hugging Face](https://huggingface.co/google/gemma-2-9b)
- **Gemmaモデルの使用には[Gemma Terms of Use](https://ai.google.dev/gemma/terms)への同意が必要です**

## Usage

- Install
```Console
# for CUDA 12.1
pip install torch==2.5.1 --index-url https://download.pytorch.org/whl/cu121
pip install vllm==0.7.2 sudachipy sudachidict-core
```

In this first release, we only provide code example using the [sudachipy](https://github.com/WorksApplications/SudachiPy) tokenizer, which matches the token boundaries of UD Japanese datasets.
Code examples for other languages ​​will be provided in upcoming releases.  
本リリースでは、UD Japanese データセットのトークン境界との親和性の高い[sudachipy](https://github.com/WorksApplications/SudachiPy)をトークナイザーに使用したサンプルコードのみを提供します。
他の言語向けのサンプルコードは、今後のリリースで提供予定です。

- Code example
```Python
import json
import sudachipy
from vllm import LLM, SamplingParams
from vllm.lora.request import LoRARequest

base_model = "google/gemma-2-9b"
adapter_model = "megagonlabs/prompt-based-parsing-gemma-2-9b-lora-v1"
input_language = "Japanese"
input_sentences = ["銀座でランチをご一緒しましょう。", "この時代から、日本列島に人類が住んだ遺跡や遺物が多く発見されている。"]

tokenizer = sudachipy.Dictionary().create(mode=sudachipy.Tokenizer.SplitMode.A)

def tokenize_japanese_space_after(sentence) -> list[str]:
    tokens = []
    for m in tokenizer.tokenize(sentence):
        surface = m.surface()
        if surface in [" ", " "]:
            if tokens and tokens[-1][-1] != " ":
                tokens[-1] += " "
        else:
            tokens.append(surface)
    if tokens and tokens[-1][-1] != " ":
        tokens[-1] += " "
    return tokens

def apply_template(language: str, sentence: str, tokens: list[str]) -> list:
    return """You are an <<<LANGUAGE>>> linguist and specialize in <<<LANGUAGE>>> dependency analysis based on Universal Dependencies.
We will now perform dependency parsing on <<<LANGUAGE>>> sentence.
After splitting the input sentence into words as shown below, execute following three tasks:

- Task 1

Create a TSV with three fields: word index from 1 to <<<TOKEN_NUM>>> + word + part of speech.

- Task 2
Add a field for the dependent word indexes to each row to the output of Task 1.
However, for the word that is the main predicate of the sentence, the dependent word index should be 0.

- Task 3

Add a field for the Universal Dependencies relation labels to the output of Task 2.


input sentence:
<<<SENTENCE>>>

words:
<<<TOKENS>>>
""".replace("<<<LANGUAGE>>>", language).replace("<<<TOKEN_NUM>>>", str(len(tokens))).replace("<<<SENTENCE>>>", sentence).replace("<<<TOKENS>>>", "\n".join(tokens))

input_prompts = [
    [
        {
            "role": "user",
            "content": apply_template(input_language, s, tokenize_japanese_space_after(s)),
        }
    ] for s in input_sentences
]

llm = LLM(
    model=base_model,
    enable_lora=True,
    tokenizer=adapter_model,
    dtype="bfloat16",
    gpu_memory_utilization=0.9,
    tensor_parallel_size=1,
    enforce_eager=True,
)
sampling_params = SamplingParams(
    temperature=0.,
    max_tokens=1024,  # <= 8192
)
lora_request = LoRARequest("adapter", 1, adapter_model)

results = llm.chat(
    messages=input_prompts,
    sampling_params=sampling_params,
    use_tqdm=False,
    lora_request=lora_request,
)
for sentence, result in zip(input_sentences, results):
    print("# text =", sentence)
    print(result.outputs[0].text)
```

- Outputs of Code example
```
# text = 銀座でランチをご一緒しましょう。
- Task 1

1	銀座	PROPN
2	で	ADP
3	ランチ	NOUN
4	を	ADP
5	ご	NOUN
6	一緒	NOUN
7	し	AUX
8	ましょう	AUX
9	。 	PUNCT

- Task 2

1	銀座	PROPN	6
2	で	ADP	1
3	ランチ	NOUN	6
4	を	ADP	3
5	ご	NOUN	6
6	一緒	NOUN	0
7	し	AUX	6
8	ましょう	AUX	6
9	。 	PUNCT	6

- Task 3

1	銀座	PROPN	6	nmod
2	で	ADP	1	case
3	ランチ	NOUN	6	obj
4	を	ADP	3	case
5	ご	NOUN	6	compound
6	一緒	NOUN	0	root
7	し	AUX	6	aux
8	ましょう	AUX	6	aux
9	。 	PUNCT	6	punct


# text = この時代から、日本列島に人類が住んだ遺跡や遺物が多く発見されている。
- Task 1

1	この	DET
2	時代	NOUN
3	から	ADP
4	、	PUNCT
5	日本	PROPN
6	列島	NOUN
7	に	ADP
8	人類	NOUN
9	が	ADP
10	住ん	VERB
11	だ	AUX
12	遺跡	NOUN
13	や	ADP
14	遺物	NOUN
15	が	ADP
16	多く	ADJ
17	発見	VERB
18	さ	AUX
19	れ	AUX
20	て	SCONJ
21	いる	VERB
22	。 	PUNCT

- Task 2

1	この	DET	2
2	時代	NOUN	17
3	から	ADP	2
4	、	PUNCT	2
5	日本	PROPN	6
6	列島	NOUN	10
7	に	ADP	6
8	人類	NOUN	10
9	が	ADP	8
10	住ん	VERB	12
11	だ	AUX	10
12	遺跡	NOUN	14
13	や	ADP	12
14	遺物	NOUN	17
15	が	ADP	14
16	多く	ADJ	17
17	発見	VERB	0
18	さ	AUX	17
19	れ	AUX	17
20	て	SCONJ	17
21	いる	VERB	20
22	。 	PUNCT	17

- Task 3

1	この	DET	2	det
2	時代	NOUN	17	obl
3	から	ADP	2	case
4	、	PUNCT	2	punct
5	日本	PROPN	6	compound
6	列島	NOUN	10	obl
7	に	ADP	6	case
8	人類	NOUN	10	nsubj
9	が	ADP	8	case
10	住ん	VERB	12	acl
11	だ	AUX	10	aux
12	遺跡	NOUN	14	nmod
13	や	ADP	12	case
14	遺物	NOUN	17	nsubj
15	が	ADP	14	case
16	多く	ADJ	17	advcl
17	発見	VERB	0	root
18	さ	AUX	17	aux
19	れ	AUX	17	aux
20	て	SCONJ	17	mark
21	いる	VERB	20	fixed
22	。 	PUNCT	17	punct
```

## Training and Evaluation

### Training Data and Hyper-parameters

We used the train-sets of the UD datasets below for LoRA SFT.  
本モデルのLoRA SFTには次のUDデータセットのtrainセットを使用しました。
- [UD_English-EWT](https://github.com/UniversalDependencies/UD_English-EWT) r2.15
- [UD_Japanese-GSD](https://github.com/UniversalDependencies/UD_Japanese-GSD) r2.15
- [UD_Chinese-GSDSimp](https://github.com/UniversalDependencies/UD_Chinese-GSDSimp) r2.15
- [UD_Korean-GSD](https://github.com/UniversalDependencies/UD_Korean-GSD) r2.15
- [UD_French-GSD](https://github.com/UniversalDependencies/UD_French-GSD) r2.15
- [UD_German-GSD](https://github.com/UniversalDependencies/UD_German-GSD) r2.15
- [UD_Slovenian-SSJ](https://github.com/UniversalDependencies/UD_Slovenian-SSJ) r2.15

We also used the training hyper-parameters below:  
また訓練時には次のパイパーパラメータを使用しています。
- lr: 5e-5
- num_train_epochs: 2
- lora_target_modules: "all-linear"
- lora_r: 8
- lora_alpha: 8
- lora_dropout: 0.05

The details of the experimental conditions will be released later.  
実験条件の詳細については後日公開予定です。

### Evaluation Results

The accuracies in the table below are based on the simple recovery process applied to the TSV output in Step 3, by using the gold tokens from the test set of the UD dataset for the seven languages ​​mentioned above.  
次の表に記載した精度は、前述の7言語のUDデータセットのtestセットの正解トークンを用いて、Step 3のTSV出力に簡易なリカバリ処理を適用した上で評価を行っています。
| dataset | UPOS | UAS | LAS |
| ---- | ---- | ---- | ---- |
| [UD_English-EWT](https://github.com/UniversalDependencies/UD_English-EWT) | 0.982 | 0.951 | 0.937 |
| [UD_Japanese-GSD](https://github.com/UniversalDependencies/UD_Japanese-GSD) | 0.987 | 0.952 | 0.939 |
| [UD_Chinese-GSDSimp](https://github.com/UniversalDependencies/UD_Chinese-GSDSimp) | 0.972 | 0.889 | 0.862 |
| [UD_Korean-GSD](https://github.com/UniversalDependencies/UD_Korean-GSD) | 0.970 | 0.898 | 0.868 |
| [UD_French-GSD](https://github.com/UniversalDependencies/UD_French-GSD) | 0.981 | 0.956 | 0.943 |
| [UD_German-GSD](https://github.com/UniversalDependencies/UD_German-GSD) | 0.974 | 0.908 | 0.873 |
| [UD_Slovenian-SSJ](https://github.com/UniversalDependencies/UD_Slovenian-SSJ) | 0.989 | 0.954 | 0.939 |

### Framework versions

- TRL v0.15.2 (for training)
- PEFT v0.14.0 (for training)
- vLLM 0.7.2 (for inference)

## Citation

```bibtex
@article{matsuda-nl263,
  title={大規模言語モデルによる対話型依存構造解析},
  author={松田寛},
  journal={研究報告自然言語処理 (NL)},
  volume={2025},
  number={17},
  pages={1--7},
  year={2025},
  publisher={情報処理学会}
}
```