Transformers
Safetensors
English
Japanese
text-generation-inference
unsloth
llama
trl
Inference Endpoints
File size: 7,661 Bytes
d51d158
d9928ad
d51d158
d9928ad
 
d51d158
d9928ad
 
 
 
 
 
 
f903613
d9928ad
d51d158
 
d9928ad
 
 
 
 
 
 
 
 
 
 
 
2feaf31
d9928ad
 
 
2feaf31
 
d9928ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2feaf31
 
 
 
 
 
 
 
 
 
 
d9928ad
 
 
 
 
 
 
 
 
 
 
 
2feaf31
 
d9928ad
 
 
2feaf31
 
 
 
d9928ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2feaf31
d9928ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d63b7ed
d9928ad
 
 
ee2684c
d9928ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f903613
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
---
base_model: llm-jp/llm-jp-3-13b
tags:
- text-generation-inference
- transformers
- unsloth
- llama
- trl
license: other
language:
- en
- ja
datasets:
- Aratako/Magpie-Tanuki-8B-annotated-96k
- tohoku-nlp/abc-multiple-choice
---

# Uploaded  model

- **Developed by:** rlcgn589
- **License:** other
- **Finetuned from model :** llm-jp/llm-jp-3-13b

This llama 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)

# Usage(Code for inference)
本モデルを用いてELYZA-tasks-100-TVの出力を得るためのコードです。 
なお、[松尾研LLM講座2024](https://weblab.t.u-tokyo.ac.jp/lecture/course-list/large-language-model/)の最終コンペ課題の提出物を得ることを目的としたコードです。そのため、コンペの評価用途以外は使用禁止とします。
このコードで生成されたjsonlファイルは課題の成果として提出可能なフォーマットになっています。

## 環境構築
omnicampusとGoogle Colab下での環境構築方法は以下の通りです。
### omnicampus
omnicampus環境下で事前にterminalで環境構築の必要があります。  
```
# conda環境の構築
wget "https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-$(uname)-$(uname -m).sh"  

# このコマンドではいくつか質問があるので答えて下さい
bash Miniforge3-$(uname)-$(uname -m).sh  

# 以下、インストール先が/root/miniforge3であることを前提とします  
export PATH=/root/miniforge3/bin:$PATH  
conda init  

# ここで一度、terminalを立ち上げ直す必要があります。  
# 以下のリンク先に従い環境を作ります
# https://docs.unsloth.ai/get-started/installation/conda-install  
conda create --name unsloth_env python=3.10 pytorch-cuda=12.1 pytorch cudatoolkit xformers -c pytorch -c nvidia -c xformers -y  
conda activate unsloth_env  
pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"  
pip install --no-deps "trl<0.9.0" peft accelerate bitsandbytes

# jupyter notebook用のセットアップ。  
conda install -c conda-forge ipykernel -y
python -m ipykernel install --user --name=unsloth_env --display-name "Python (unsloth_env)"  
```

### Google Colab
Google Colab環境下で推論前に環境構築の必要があります。
以下に示す推論用コードはRuntime TypeとしてPython 3、Hardware acceleratorとしてT4 GPU以上を想定しています。

```
!pip install unsloth
!pip uninstall unsloth -y && pip install --upgrade --no-cache-dir "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install -U torch
!pip install -U peft
```

## 推論用コードの実行
```python
# 必要なライブラリを読み込み
from unsloth import FastLanguageModel
from peft import PeftModel
import torch
import json
from tqdm import tqdm
import re

# ベースとなるモデルと学習したLoRAのアダプタ。
# model_idの値はomnicampusの環境におけるモデルのパスを表しており、それ以外の環境で実行する場合は変更の必要があります。
model_id = "models/models--llm-jp--llm-jp-3-13b/snapshots/cd3823f4c1fcbb0ad2e2af46036ab1b0ca13192a" # omnicampus環境の場合
model_id = "llm-jp/llm-jp-3-13b" # Google Colab環境の場合
adapter_id = "rlcgn589/llm-jp-3-13b-it-12_lora"

# Hugging Face Token を指定。
HF_TOKEN = "your Hugging Face Token" # omnicampus環境の場合

from google.colab import userdata
HF_TOKEN = userdata.get('HF_TOKEN') # Google Colab環境の場合

# unslothのFastLanguageModelで元のモデルをロード。
dtype = None # Noneにしておけば自動で設定
load_in_4bit = True # 今回は13Bモデルを扱うためTrue

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name=model_id,
    dtype=dtype,
    load_in_4bit=load_in_4bit,
    trust_remote_code=True,
)

# 元のモデルにLoRAのアダプタを統合。
model = PeftModel.from_pretrained(model, adapter_id, token = HF_TOKEN)

# データセットの読み込み。
# omnicampusの開発環境では、左にタスクのjsonlをドラッグアンドドロップしてから実行。
# Google Colab環境下では、左のファイルアイコンをクリックしタスクのjsonlをドラッグアンドドロップしてから実行。
datasets = []
with open("./elyza-tasks-100-TV_0.jsonl", "r") as f:
    item = ""
    for line in f:
      line = line.strip()
      item += line
      if item.endswith("}"):
        datasets.append(json.loads(item))
        item = ""

# モデルを用いてタスクの推論。

# 推論するためにモデルのモードを変更
FastLanguageModel.for_inference(model)

results = []
for dt in tqdm(datasets):
  input = dt["input"]

  prompt = f"""### 指示\n{input}\n### 回答\n"""

  inputs = tokenizer([prompt], return_tensors = "pt").to(model.device)

  outputs = model.generate(**inputs, max_new_tokens = 1024, use_cache = True, do_sample=False, repetition_penalty=1.2)
  prediction = tokenizer.decode(outputs[0], skip_special_tokens=True).split('\n### 回答')[-1]

  results.append({"task_id": dt["task_id"], "input": input, "output": prediction})

# 結果をjsonlで保存。
jsonl_id = re.sub(".*/", "", adapter_id)
with open(f"./{jsonl_id}-outputs.jsonl", 'w', encoding='utf-8') as f:
    for result in results:
        json.dump(result, f, ensure_ascii=False)  # ensure_ascii=False for handling non-ASCII characters
        f.write('\n')
```

## Datasets

### Instruction tuning
本モデルは以下のデータセットでファインチューニングしました。

| Language | Dataset | description |
|:---|:---|:---|
|Japanese| Aratako/Magpie-Tanuki-8B-annotated-96k | MagpieとTanuki-8B-dpo-v1.0で作成されたデータにcalm3-22bでアノテーションを施した合成データを一部使用 |
|Japanese| tohoku-nlp/abc-multiple-choice | オリジナルの4択の選択肢問題を一部加工(クイズと回答は変えていない) |

### Data Source
ファインチューニング用のデータセット作成に作成者が使用したモデルとツールを示します。データを使わせていただきありがとうございます。

#### Tanuki-8B-dpo-v1.0
https://huggingface.co/weblab-GENIAC/Tanuki-8B-dpo-v1.0

#### Magpie
```tex
@misc{xu2024magpiealignmentdatasynthesis,
      title={Magpie: Alignment Data Synthesis from Scratch by Prompting Aligned LLMs with Nothing}, 
      author={Zhangchen Xu and Fengqing Jiang and Luyao Niu and Yuntian Deng and Radha Poovendran and Yejin Choi and Bill Yuchen Lin},
      year={2024},
      eprint={2406.08464},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2406.08464}, 
}
```

#### CyberAgentLM3-22B-Chat 
```tex
@misc{cyberagent-calm3-22b-chat,
      title={cyberagent/calm3-22b-chat},
      url={https://huggingface.co/cyberagent/calm3-22b-chat},
      author={Ryosuke Ishigami},
      year={2024},
}
```

#### abc-multiple-choice
https://github.com/cl-tohoku/abc-multiple-choice
https://jedworkshop.github.io/JLR2024/materials/a-1.pdf

abc-multiple-choiceのライセンスに関する記載を抜粋して記します。
- 本データセットのクイズ問題の著作権は abc/EQIDEN 実行委員会 に帰属します。
- 本データセットは研究目的での利用許諾を得ているものです。商用目的での利用は不可とします。