Home

Awesome

<div align="center">

HammerLLM🔨

English | 中文

</div> <h5 align=center>

hf License Hits GitHub stars

</h5>

Welcome to the pre-training repository for our small-size Large Language Model (sLLM) with 1.4 billion parameters, leveraged on the Llama 2 architecture. Our sLLM exhibits advantages in following features:

  1. Llama-2 Compatible LLM for Chinese and English🦙
  2. Simple yet Efficient Training Codebase🚀
  3. Complete Open-Source: model weights, environment, codebase, and hyper-parameters🔗
  4. Comparable performance with advanced sLLMs with the similar model scale🥇
  5. Tokenizer with highest compression rate and 100% Chinese character coverage🏆

🔥What's New!

🧾 Next

Table of Contents

⚙️Inference

Example Code

Here is a code snippet to show you how to play with our model with HuggingFace transformers:

import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = 'DataHammer/hammerllm-1.4b-222k'
text = '北京理工大学是'
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
# if your device donot support the bfloat16, you could remove it
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16)

input_ids = tokenizer(text, return_tensors='pt').input_ids
output = model.generate(
    input_ids=input_ids.cuda(),
    max_length=min(int(len(input_ids) + 100), 1024),
    do_sample=True,
    top_p=0.95
).tolist()

generation = tokenizer.decode(output[0])
print(generation)

🙌Cases

Here, we show some cases about text completion by our model on Chinese, English, and Code setting.

Chinese Generation

Prompt: 北京理工大学是
Generation: <s> 北京理工大学是教育部直属全国重点大学,是我国“211工程”和“985工程”重点建设的教育部直属理工类全国重点大学,直属于工业 和信息化部,是全国首批正式获得博士学位和博士后研究资格的高校之一,首批获得公安专业硕士、公安专业博士和司法专业博士学位的22所高校之 一,是博士学位授予单位、法学(一级学科)和公安学(一级学科)博士授权高校,是一所以工为主、理工结合、多学科协调发展的全国重点大学。
北京理工大学是中央部门所属高校,中央共建高校,教育部直属高校。
一、大学院(院系)
北京理工大学现有48个博士学位授权一级学科,涵盖17个学科门类。
二、专业院系
北京理工大学设有11个一级学科博士后科研流动站
三、研究生院
北京理工大学设有29个研究所,其中国家重点实验室2个; 博士后科研流动站18个;博士后科研工作站3个。
北京理工大学研究生院招生专业:</s>
Prompt: 小米公司是一家
Generation: <s> 小米公司是一家专注于智能硬件和电子产品研发的互联网公司,公司产品包括智能手机、笔记本电脑、路由器等。小米公司一直专 注于技术创新和卓越的体验,致力于为用户提供更好的产品。
小米公司拥有一支经验丰富的研发团队,在产品开发、用户体验等方面积累了丰富的经验。公司研发团队中的大部分成员来自微软、谷歌等知名科技 公司,对互联网和移动互联网技术有着深刻理解。此外,小米公司还拥有专业的开发团队和良好的技术支持。
小米公司的核心产品之一是小米手机。小米公司的目标是让用户可以在手机中轻松分享图片、视频、音乐和音乐文件等。小米公司提供了一种简单易 用的移动媒体解决方案,让用户可以利用智能手机来轻松享受音乐、照片、视频和游戏等移动媒体内容。
小米手机还支持多种功能,包括智能语音助手、远程控制、远程定位、移动支付等。小米手机还支持各种不同的应用和服务,包括社交媒体、游戏、 视频分享、本地化服务等。
小米公司还拥有丰富的硬件产品线,包括智能手机、笔记本电脑、平板电脑、电视等。小米公司还推出了许多极具竞争力的产品,如智能手环、智能 手表等。</s>

English Generation

Prompt: Google is
Generation: <s> Google is to deliver its latest Pixel phone to AT&T, instead of the original, cheaper Pixel 3a.
AT&T US is expected to unveil the Pixel 5a on the 8th of February, or in the evening of Friday the 12th next year.
However, it appears Google has announced the Pixel 5a as the second Pixel phone to be delivered to AT&T, rather than the original Pixel 3a.
According to a blog post published by Pixel US on 26th October, the Pixel 5a will be available on the 9th of March with a price of $599.99.
Google has also promised the Pixel 5a will be the first Pixel phone sold in the U.S., US.
In the announcement made the company is also hinting that the Pixel 5a will be available on Sprint's network, meaning the Pixel 5a won't be delivered to T-Mobile US.
A couple of months ago, the Google Pixel 5a, then known as the Pixel 4a, was revealed to the world on the 22nd of October alongside an invite for beta program for the Pixel 5a.
The Pixel 5a is believed to be the largest smartphone Pixel has ever produced.</s>

Code Generation

Prompt: This is a code snippet for sorting a list of number in Python language with python in-built function `sorted`:
"""python
number_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Generation: <s> This is a code snippet for sorting a list of number in Python language with python in-built function `sorted`:
"""python
number_list = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(sorted(number_list))
"""

[`sorted`](https://docs.python.org/2/library/ordereddict.html) is a function that helps you sort a list of values in Python language.
For the list of items like number, this function is also used to sort the list.
</s>

🤗Pretrained Models

We publicly release our checkpoints on Huggingface. Welcome to test and utilize our models!

EpochStepPretrained tokensSequence LengthHuggingFace checkpoint
125k50B2048DataHammer/hammerllm-1.4b-25k
150k100B2048DataHammer/hammerllm-1.4b-50k
175k150B2048DataHammer/hammerllm-1.4b-75k
1100k200B2048DataHammer/hammerllm-1.4b-100k
1125k250B2048DataHammer/hammerllm-1.4b-125k
1150k300B2048DataHammer/hammerllm-1.4b-150k
1172.5k345B2048DataHammer/hammerllm-1.4b-172k
2197k395B2048DataHammer/hammerllm-1.4b-197k
2222k445B2048DataHammer/hammerllm-1.4b-222k

📊Performance

Perplexity on Latest Test Dataset

For base models that are not instruct-tuned, it is important to show their perplexity on the latest fresh test dataset, which reflects their prediction capability without training data leakage. To achieve this goal, we compare the perplexity of our model and some famous sLLMs on famous Chinese and English latest test set.

Our perplexity calculation follows the settings of Skywork, please refer to this link for more details about their implementation.

Our model's performance on perplexity (ppl) testing is second only to the Qwen-1.5-1.8B model, outperforming Gemma-2B, InterLM2-1.8B, and MiniCPM-2B (350k). Given the fact that the our model size is smaller (1.4B < 1.8B), these experimental results are surprising.

Chinese Corpus (Skywork-ChineseDomainModelingEval)

TokensTechMovieGovernmentGameFinanceGeneralAvg.
Gemma-2B6T71.92271.9593.16122.5946.5374.9392.92
InterLM2-1.8B-19.2036.887.1829.679.5111.8116.03
MiniCPM-2B(350k)-17.3236.447.0026.597.9210.2614.56
Qwen-1.5(1.8B)-16.8833.396.6325.447.439.7713.80
Qwen-1.5(0.5B)-20.7041.598.2431.649.4111.9817.13
Ours50B19.5944.587.4833.558.1011.3116.49
Ours100B18.2340.267.1130.807.4210.4715.23
Ours150B17.5938.866.9129.767.1910.2014.75
Ours200B17.2438.036.8429.017.0510.0414.48
Ours250B17.0837.536.8128.636.979.9114.32
Ours300B16.9237.176.8228.376.899.8214.21
Ours345B16.7936.896.7328.116.849.7514.09
Ours(epoch-2)395B16.7136.646.6028.016.859.7214.00
Ours(epoch-2)445B16.6736.436.6827.866.759.6513.95

English Corpus (RealTimeData-Latest Collection)

Tokensarxiv-latestbbc-latestgithub-latestwikitext-latestAvg.
TinyLlama-1.1B3T8.218.275.907.387.37
Gemma-2B6T28.9637.8322.1028.1128.72
Phi-1.5(1.3B)30B * 513.0814.0710.4215.1613.06
InterLM2-1.8B-7.848.756.878.527.96
MiniCPM-2B(350k)-10.819.15nan8.089.28
Qwen-1.5(1.8B)-8.708.92nan8.758.79
Qwen-1.5(0.5B)-10.4210.93nan10.5810.64
Ours50B10.6713.199.1410.4710.77
Ours100B9.8711.978.469.719.93
Ours150B9.5711.638.169.389.61
Ours200B9.4111.438.069.219.45
Ours250B9.3911.217.969.139.35
Ours300B9.7911.247.909.039.41
Ours345B9.5911.117.869.049.33
Ours(epoch-2)395B9.6511.067.869.019.32
Ours(epoch-2)445B9.3411.077.869.049.26

NOTE: Average PPL of Qwen-1.5 and MiniCPM models have excluded GitHub dataset for NaN value.

📓Tokenizer

To reveal the effectiveness of our tokenizer, we compare it with some famous open-source LLMs' tokenizers by using following metrics:

  1. Compression Rate: We compare two kinds of compression rate of tokenizer with tokenizers of some open-source LLMs:

  2. Chinese Character Coverage: a good Chinese LLM should cover more characters. In our work, we leverage vocab-coverage for computing the coverage of Chinese characters, which includes:

    • First-level Chinese character Coverage (FCC) contains 3500 widely-used Chinese characters.
    • Second-level Chinese character Coverage (SCC) contains 3000 Chinese characters.
    • Third-level Chinese character Coverage (TCC) contains 1605 uncommon Chinese characters.

The experimental results are shown as follows:

TokenizerFCCSCCTCCByte per Token $\uparrow$Comparied Compression $\downarrow$
Chatglm-6b99.97%57.47%2.99%4.29110.5303
Chatglm2/3-6b100.00%77.83%13.89%4.03290.5642
Baichuan2-7b/14b100.00%99.8%86.48%4.18270.5440
Internlm-7b/20b100.00%65.93%5.67%4.31330.5276
Qwen-7b/14b/72b100.00%100.00%100.00%4.13260.5506
Llama-2-7b/13b/70b17.29%0.13%0.00%2.27551.00
Ours100.00%100.00%100.00%4.31430.5274

The experimental results reveal the advantages of our tokenizer over existing popular LLMs' tokenizers in compression rate (on Chinese, English, and Code), and the Chinese character coverage. Please refer to REPRODUCE.md for more details about this experiment.

⏩Acceleration

With the help of these two stragegies, we could achieve a high throughput of 16k tokens per GPU per second for training:

The setting for computing the throughput is:

Settingstokens per GPU per second
NoneCUDA OOM
Flash Attention 213k
torch.compileCUDA OOM
Flash Attention 2 + torch.compile16k

To cover more Chinese characters, our tokenizer is much larger than TinyLlama (105789 > 32000), leading to the lower throughput than TinyLlama (16k < 24k).

However, our throughput is comparable with TinyLlama, when the size of the tokenizer is the same (per_device_train_batch_size is set to 20 for this).

Settingstokens per GPU per second
Flash Attention 2 + torch.compile24k

Unlike TinyLlama that leverages some complex the operations fusion, we achieve this throughput solely based on torch.compile and flash attention 2.

📈Training Progress

So far, we have optimized our model on over 400B Chinese, English, and Code tokens. Perplexity results in Chinese and English test set demonstrate that our model has not yet converged, hence we are currently continuing to optimize it.

We will periodically upload and make our checkpoints publicly available for analysis and research by the open-source community.

The trend of loss changes during the training process is shown in the following figure: Training Loss

The trends of standardized perplexity variation based on Skywork series models on the Skywork-ChineseDomainModelingEval and RealTimeData-Latest Collection datasets are:

<table rules="none" align="center"> <tr> <td> <center> <img src=".github/images/zh-ppl.jpg" /> <br/> <center>Chinese PPL</center> </center> </td> <td> <center> <img src=".github/images/en-ppl.jpg" /> <br/> <center>English PPL</center> </center> </td> </tr> </table>

♻️Reproduce Our Work

One of contriution is that we have publicly release the steps to train our model from scratch. You can refer to this file for more details about our work.

🖊️Citing

@software{Ziao_HammerLLM_2024,
author = {Ziao, Ma and Tian, Lan and Yizhe, Yang and Yong, Hu},
month = apr,
title = {{HammerLLM}},
url = {https://github.com/Academic-Hammer/HammerLLM},
version = {1.0.0},
year = {2024}
}

🤝Contact Us

You can contact us through the email address:

💳License

This project is released under the MIT license.

💪Contributing

<!-- Guidelines for how others can contribute to this repository, including coding standards, pull requests, and other relevant information. -->

We hope this documentation is helpful for understanding and utilizing our LLM model effectively. For further questions or support, please open an issue on this repository.