ce-lery/japanese-mistral-300m-instruction
350
<!-- This model card has been generated automatically according to the information the Trainer had access to. You should probably proofread and complete it, then remove this comment. -->
japanese-mistral-300m-instruction
Overview
Welcome to my model card!
This Model feature is ...
- Suppression of unknown word generation by using byte fallback in SentencePiece tokenizer and conversion to huggingface Tokenizers format
- Pretrained by wikipedia dataset and cc100 dataset
- Use of Mistral 300M
- Fine-tuning ce-lery/japanese-mistral-300m-base with kunishou/databricks-dolly-15k-ja
Yukkuri shite ittene!
How to use the model
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import os
MODEL_NAME = "ce-lery/japanese-mistral-300m-instruction"
torch.set_float32_matmul_precision('high')
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False,trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME,trust_remote_code=True).to(device)
MAX_ASSISTANT_LENGTH = 100
MAX_INPUT_LENGTH = 128
INPUT_PROMPT = r'<s>\n以下は、タスクを説明する指示と、文脈のある入力の組み合わせです。要求を適切に満たす応答を書きなさい。\n[SEP]\n指示:\n{instruction}\n[SEP]\n入力:\n{input}\n[SEP]\n応答:\n'
NO_INPUT_PROMPT = r'<s>\n以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。\n[SEP]\n指示:\n{instruction}\n[SEP]\n応答:\n'
def prepare_input(instruction, input_text):
if input_text != "":
prompt = INPUT_PROMPT.format(instruction=instruction, input=input_text)
else:
prompt = NO_INPUT_PROMPT.format(instruction=instruction)
return prompt
def format_output(output):
output = output.lstrip("<s>").rstrip("</s>").replace("[SEP]", "").replace("\\n", "\n")
return output
def generate_response(instruction, input_text):
prompt = prepare_input(instruction, input_text)
token_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
n = len(token_ids[0])
# print(n)
with torch.no_grad():
output_ids = model.generate(
token_ids.to(model.device),
min_length=n,
max_length=min(MAX_INPUT_LENGTH, n + MAX_ASSISTANT_LENGTH),
top_p=0.95,
top_k=50,
temperature=0.4,
do_sample=True,
no_repeat_ngram_size=2,
num_beams=3,
pad_token_id=tokenizer.pad_token_id,
bos_token_id=tokenizer.bos_token_id,
eos_token_id=tokenizer.eos_token_id,
bad_words_ids=[[tokenizer.unk_token_id]]
)
output = tokenizer.decode(output_ids.tolist()[0])
formatted_output_all = format_output(output)
response = f"Assistant:{formatted_output_all.split('応答:')[-1].strip()}"
return formatted_output_all, response
instruction = "あなたは何でも正確に答えられるAIです。"
questions = [
"日本で一番高い山は?",
"日本で一番広い湖は?",
"世界で一番高い山は?",
"世界で一番広い湖は?",
"冗談を言ってください。",
]
# 各質問に対して応答を生成して表示
for question in questions:
formatted_output_all, response = generate_response(instruction, question)
print(response)
Receipe
If you want to restruct this model, you can refer this Github repository.
I wrote the receipe for struction this model. For example,
- Preprocess with sentencepiece
- Pretraining with flash attention2 and torch.compile and DeepSpeed
- Fine-tuning with databricks-dolly-15k-ja
If you find my mistake,error,...etc, please create issue. If you create pulreqest, I'm very happy!
Training procedure
Training hyperparameters
The following hyperparameters were used during training:
- learning_rate: 5e-06
- trainbatchsize: 4
- evalbatchsize: 4
- seed: 42
- distributed_type: multi-GPU
- gradientaccumulationsteps: 64
- totaltrainbatch_size: 256
- optimizer: Adam with betas=(0.9,0.95) and epsilon=0.0001
- lrschedulertype: cosine
- lrschedulerwarmup_steps: 1000
- num_epochs: 200
- mixedprecisiontraining: Native AMP
Training results
Framework versions
- Transformers 4.35.2
- Pytorch 2.1.1+cu121
- Datasets 2.14.5
- Tokenizers 0.14.1
