CoolFace
Modelpublic

ce-lery/japanese-mistral-300m-instruction

sourceHugging Faceupdated 3y agoView on Hugging Face
3likes50downloads
Model Card

<!-- 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 ...

Yukkuri shite ittene!

How to use the model

python
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

Training LossEpochStepValidation Loss
3.5953.51403.5299
3.47697.02803.3722
3.303710.531203.1871
3.125514.051603.0088
2.961517.562002.8684
2.846821.072402.7808
2.769924.582802.7205
2.713928.093202.6793
2.671231.63602.6509
2.635635.124002.6294
2.604838.634402.6120
2.582342.144802.5974
2.553645.655202.5849
2.529349.165602.5740
2.505852.676002.5644
2.48256.196402.5556
2.457559.76802.5477
2.433963.217202.5405
2.407366.727602.5350
2.384570.238002.5303
2.360673.748402.5253
2.32977.268802.5215
2.307180.779202.5185
2.276884.289602.5155
2.247987.7910002.5144
2.218191.310402.5151
2.190194.8110802.5139
2.157198.3311202.5148
2.1308101.8411602.5166
2.1032105.3512002.5193
2.0761108.8612402.5204
2.0495112.3712802.5269
2.0231115.8813202.5285
2.0021119.413602.5328
1.9793122.9114002.5383
1.9575126.4214402.5442
1.9368129.9314802.5488
1.9216133.4415202.5534
1.902136.9515602.5584
1.8885140.4716002.5609
1.8728143.9816402.5657
1.8605147.4916802.5697
1.8476151.017202.5741
1.8402154.5117602.5770
1.8274158.0218002.5803
1.8218161.5418402.5829
1.8144165.0518802.5847
1.8097168.5619202.5867
1.8076172.0719602.5883
1.8014175.5820002.5892
1.8001179.0920402.5899
1.7987182.6120802.5903
1.7971186.1221202.5906
1.7979189.6321602.5907
1.7975193.1422002.5907

Framework versions

  • —Transformers 4.35.2
  • —Pytorch 2.1.1+cu121
  • —Datasets 2.14.5
  • —Tokenizers 0.14.1