File size: 4,140 Bytes
3409be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1293eb9
3409be9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
008d217
 
 
 
 
 
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
---
license: gemma
library_name: transformers
pipeline_tag: text-generation
base_model: google/gemma-2-27b-it
language:
- en
- zh
tags:
- llama-factory
- orpo
---

❗️❗️❗️NOTICE: For optimal performance, we refrain from fine-tuning the model's identity. Thus, inquiries such as "Who are you" or "Who developed you" may yield random responses that are not necessarily accurate. 


# Updates

- 🚀🚀🚀 [Jul 2, 2024] We now introduce Gemma-2-27B-Chinese-Chat, which is **the first instruction-tuned language model built upon [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it) for Chinese & English users** with various abilities such as roleplaying & tool-using.
- 🔥🔥🔥 We provide various GGUF files (including q4_k_m, q_4_0, q_8_0) at https://huggingface.co/shenzhi-wang/Gemma-2-27B-Chinese-Chat/tree/main/gguf_models.

# Model Summary

Gemma-2-27B-Chinese-Chat is **the first instruction-tuned language model built upon [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it) for Chinese & English users** with various abilities such as roleplaying & tool-using.

Developed by: [Shenzhi Wang](https://shenzhi-wang.netlify.app) (王慎执) and [Yaowei Zheng](https://github.com/hiyouga) (郑耀威)

- License: [Gemma License](https://ai.google.dev/gemma/terms)
- Base Model: [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it)
- Model Size: 27.2B
- Context length: 8K

# 1. Introduction

This is the first model specifically fine-tuned for Chinese & English users based on the [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it) with a preference dataset with more than 100K preference pairs. The fine-tuning algorithm we employ is ORPO [1].

**Compared to the original [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it), our Gemma-2-27B-Chinese-Chat model significantly reduces the issues of "Chinese questions with English answers" and the mixing of Chinese and English in responses, with enhanced performance in roleplay, tool-using, and math.**

[1] Hong, Jiwoo, Noah Lee, and James Thorne. "Reference-free Monolithic Preference Optimization with Odds Ratio." arXiv preprint arXiv:2403.07691 (2024).

Training framework: [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory).

Training details:

- epochs: 3
- learning rate: 3e-6
- learning rate scheduler type: cosine
- Warmup ratio: 0.1
- cutoff len (i.e. context length): 8192
- orpo beta (i.e. $\lambda$ in the ORPO paper): 0.05
- global batch size: 128
- fine-tuning type: full parameters
- optimizer: paged_adamw_32bit

# 2. Usage

## 2.1 Usage of Our BF16 Model

1. Please upgrade the `transformers` package to ensure it supports Gemma-2 models. The current version we are using is `4.42.2`.

2. Use the following Python script to download our BF16 model

```python
from huggingface_hub import snapshot_download
snapshot_download(repo_id="shenzhi-wang/Gemma-2-27B-Chinese-Chat", ignore_patterns=["*.gguf"])  # Download our BF16 model without downloading GGUF models.
```

3. Inference with the BF16 model

```python
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "/Your/Local/Path/to/Gemma-2-27B-Chinese-Chat"
dtype = torch.bfloat16

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    device_map="cuda",
    torch_dtype=dtype,
)

chat = [
    {"role": "user", "content": "写一首关于机器学习的诗。"},
]
input_ids = tokenizer.apply_chat_template(
    chat, tokenize=True, add_generation_prompt=True, return_tensors="pt"
).to(model.device)

outputs = model.generate(
    input_ids,
    max_new_tokens=8192,
    do_sample=True,
    temperature=0.6,
    top_p=0.9,
)
response = outputs[0][input_ids.shape[-1] :]
print(tokenizer.decode(response, skip_special_tokens=True))
```

## 2.2 Usage of Our GGUF Models

1. Download our GGUF models from the [gguf_models folder](https://huggingface.co/shenzhi-wang/Gemma-2-27B-Chinese-Chat/tree/main/gguf_models).
2. Use the GGUF models with [LM Studio](https://lmstudio.ai/) version 0.2.26.