namespace-Pt/Llama-3-8B-Instruct-80K-QLoRA

Model

We extend the context length of Llama-3-8B-Instruct to 80K using QLoRA and 3.5K long-context training data synthesized from GPT-4. The entire training cycle is super efficient, which takes 8 hours on a 8xA800 (80G) machine. Yet, the resulted model achieves remarkable performance on a series of downstream long-context evaluation benchmarks.

24

19 commits

1 linked in READMEs

updated Apr 30, 2024

See the code

README

Llama-3-8B-Instruct-80K-QLoRA

[Data&Code]

We extend the context length of Llama-3-8B-Instruct to 80K using QLoRA and 3.5K long-context training data synthesized from GPT-4. The entire training cycle is super efficient, which takes 8 hours on a 8xA800 (80G) machine. Yet, the resulted model achieves remarkable performance on a series of downstream long-context evaluation benchmarks.

Evaluation

All the following evaluation results can be reproduced following instructions here.

Needle in a Haystack

We evaluate the model on the Needle-In-A-HayStack task using the official setting. The blue vertical line indicates the training context length, i.e. 80K.

LongBench

We evaluate the model on LongBench using 32K context length and the official prompt template. For meta-llama/Meta-Llama-3-8B-Instruct, we use 8K context length.

ModelSingle-Doc QAMulti-Doc QASummarizationFew-Shot LearningSyntheticCodeAvg
meta-llama/Meta-Llama-3-8B-Instruct37.3336.0426.8369.5637.7553.2443.20
gradientai/Llama-3-8B-Instruct-262k37.2931.2026.1867.2544.2562.7143.73
Llama-3-8B-Instruct-80K-QLoRA43.5743.0728.9369.1548.5051.9547.19

InfiniteBench

We evaluate the model on InfiniteBench using 80K context length and the official prompt template. The results of GPT-4 is copied from the paper. For meta-llama/Meta-Llama-3-8B-Instruct, we use 8K context length.

ModelLongBookQA EngLongBookSum Eng
GPT-422.2214.73
meta-llama/Meta-Llama-3-8B-Instruct7.0016.40
gradientai/Llama-3-8B-Instruct-262k20.3010.34
Llama-3-8B-Instruct-80K-QLoRA30.9214.73

Topic Retrieval

We evaluate the model on Topic Retrieval task with [5,10,15,20,25,30,40,50,60,70] topics.

MMLU

We evaluate the model's zero-shot performance on MMLU benchmark as a reflection of its short-context capability.

ModelSTEMSocial SciencesHumanitiesOthersAvg
Llama-2-7B-Chat35.9254.3751.7451.4247.22
Mistral-7B-v0.2-Instruct48.7969.9564.9961.6460.10
meta-llama/Meta-Llama-3-8B-Instruct53.8775.6669.4469.7565.91
gradientai/Llama-3-8B-Instruct-262k52.1073.2667.1569.8064.34
Llama-3-8B-Instruct-80K-QLoRA53.1073.2467.3268.7964.44

Environment

torch==2.2.2
flash_attn==2.5.6
transformers==4.39.3
peft==0.10.0

Usage

import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
peft_id = "namespace-Pt/Llama-3-8B-Instruct-80K-QLoRA"

torch_dtype = torch.bfloat16
# place the model on GPU
device_map = {"": "cuda"}

tokenizer = AutoTokenizer.from_pretrained(model_id)

base_model = AutoModelForCausalLM.from_pretrained(
  model_id, 
  torch_dtype=torch.bfloat16,
  device_map=device_map,
  attn_implementation="flash_attention_2",

  # NOTE: expand rope base
  rope_theta=200e6,
)

model = PeftModel.from_pretrained(
    base_model, 
    peft_id,
    torch_dtype=torch.bfloat16,
    device_map=device_map,
)
# NOTE: merge LoRA weights
model = model.merge_and_unload().eval()

with torch.no_grad():
  # short context
  messages = [{"role": "user", "content": "Tell me about yourself."}]
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to("cuda")
  outputs = model.generate(**inputs, max_new_tokens=50)[:, inputs["input_ids"].shape[1]:]
  print(f"Input Length: {inputs['input_ids'].shape[1]}")
  print(f"Output:       {tokenizer.decode(outputs[0])}")

  # long context
  with open("data/narrativeqa.json", encoding="utf-8") as f:
    example = json.load(f)
  messages = [{"role": "user", "content": example["context"]}]
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to("cuda")
  outputs = model.generate(**inputs, do_sample=False, top_p=1, temperature=1, max_new_tokens=20)[:, inputs["input_ids"].shape[1]:]
  print("*"*20)
  print(f"Input Length: {inputs['input_ids'].shape[1]}")
  print(f"Answers:      {example['answer']}")
  print(f"Prediction:   {tokenizer.decode(outputs[0])}")

You may observe messages like: This is a friendly reminder - the current text generation call will exceed the model's predefined maximum length (8192). Depending on the model, you may observe exceptions, performance degradation, or nothing at all. or Setting pad_token_id to eos_token_id:128001 for open-end generation. They do not matter. Just ignore them.

conversational
safetensors
text-generation

Contributors

namespace-Pt

19 commits

namespace-Pt/Llama-3-8B-Instruct-80K-QLoRA

Model

We extend the context length of Llama-3-8B-Instruct to 80K using QLoRA and 3.5K long-context training data synthesized from GPT-4. The entire training cycle is super efficient, which takes 8 hours on a 8xA800 (80G) machine. Yet, the resulted model achieves remarkable performance on a series of downstream long-context evaluation benchmarks.

24

19 commits

1 linked in READMEs

updated Apr 30, 2024

See the code

README

Llama-3-8B-Instruct-80K-QLoRA

[Data&Code]

We extend the context length of Llama-3-8B-Instruct to 80K using QLoRA and 3.5K long-context training data synthesized from GPT-4. The entire training cycle is super efficient, which takes 8 hours on a 8xA800 (80G) machine. Yet, the resulted model achieves remarkable performance on a series of downstream long-context evaluation benchmarks.

Evaluation

All the following evaluation results can be reproduced following instructions here.

Needle in a Haystack

We evaluate the model on the Needle-In-A-HayStack task using the official setting. The blue vertical line indicates the training context length, i.e. 80K.

LongBench

We evaluate the model on LongBench using 32K context length and the official prompt template. For meta-llama/Meta-Llama-3-8B-Instruct, we use 8K context length.

ModelSingle-Doc QAMulti-Doc QASummarizationFew-Shot LearningSyntheticCodeAvg
meta-llama/Meta-Llama-3-8B-Instruct37.3336.0426.8369.5637.7553.2443.20
gradientai/Llama-3-8B-Instruct-262k37.2931.2026.1867.2544.2562.7143.73
Llama-3-8B-Instruct-80K-QLoRA43.5743.0728.9369.1548.5051.9547.19

InfiniteBench

We evaluate the model on InfiniteBench using 80K context length and the official prompt template. The results of GPT-4 is copied from the paper. For meta-llama/Meta-Llama-3-8B-Instruct, we use 8K context length.

ModelLongBookQA EngLongBookSum Eng
GPT-422.2214.73
meta-llama/Meta-Llama-3-8B-Instruct7.0016.40
gradientai/Llama-3-8B-Instruct-262k20.3010.34
Llama-3-8B-Instruct-80K-QLoRA30.9214.73

Topic Retrieval

We evaluate the model on Topic Retrieval task with [5,10,15,20,25,30,40,50,60,70] topics.

MMLU

We evaluate the model's zero-shot performance on MMLU benchmark as a reflection of its short-context capability.

ModelSTEMSocial SciencesHumanitiesOthersAvg
Llama-2-7B-Chat35.9254.3751.7451.4247.22
Mistral-7B-v0.2-Instruct48.7969.9564.9961.6460.10
meta-llama/Meta-Llama-3-8B-Instruct53.8775.6669.4469.7565.91
gradientai/Llama-3-8B-Instruct-262k52.1073.2667.1569.8064.34
Llama-3-8B-Instruct-80K-QLoRA53.1073.2467.3268.7964.44

Environment

torch==2.2.2
flash_attn==2.5.6
transformers==4.39.3
peft==0.10.0

Usage

import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel

model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
peft_id = "namespace-Pt/Llama-3-8B-Instruct-80K-QLoRA"

torch_dtype = torch.bfloat16
# place the model on GPU
device_map = {"": "cuda"}

tokenizer = AutoTokenizer.from_pretrained(model_id)

base_model = AutoModelForCausalLM.from_pretrained(
  model_id, 
  torch_dtype=torch.bfloat16,
  device_map=device_map,
  attn_implementation="flash_attention_2",

  # NOTE: expand rope base
  rope_theta=200e6,
)

model = PeftModel.from_pretrained(
    base_model, 
    peft_id,
    torch_dtype=torch.bfloat16,
    device_map=device_map,
)
# NOTE: merge LoRA weights
model = model.merge_and_unload().eval()

with torch.no_grad():
  # short context
  messages = [{"role": "user", "content": "Tell me about yourself."}]
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to("cuda")
  outputs = model.generate(**inputs, max_new_tokens=50)[:, inputs["input_ids"].shape[1]:]
  print(f"Input Length: {inputs['input_ids'].shape[1]}")
  print(f"Output:       {tokenizer.decode(outputs[0])}")

  # long context
  with open("data/narrativeqa.json", encoding="utf-8") as f:
    example = json.load(f)
  messages = [{"role": "user", "content": example["context"]}]
  inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt", return_dict=True).to("cuda")
  outputs = model.generate(**inputs, do_sample=False, top_p=1, temperature=1, max_new_tokens=20)[:, inputs["input_ids"].shape[1]:]
  print("*"*20)
  print(f"Input Length: {inputs['input_ids'].shape[1]}")
  print(f"Answers:      {example['answer']}")
  print(f"Prediction:   {tokenizer.decode(outputs[0])}")

You may observe messages like: This is a friendly reminder - the current text generation call will exceed the model's predefined maximum length (8192). Depending on the model, you may observe exceptions, performance degradation, or nothing at all. or Setting pad_token_id to eos_token_id:128001 for open-end generation. They do not matter. Just ignore them.

conversational
safetensors
text-generation

Contributors

namespace-Pt

19 commits