Convert Figma logo to code with AI

tloen logoalpaca-lora

Instruct-tune LLaMA on consumer hardware

18,524
2,212
18,524
369

Top Related Projects

Code and documentation to train Stanford's Alpaca models, and generate the data.

10,276

Code for loralib, an implementation of "LoRA: Low-Rank Adaptation of Large Language Models"

15,745

🤗 PEFT: State-of-the-art Parameter-Efficient Fine-Tuning.

55,392

Inference code for Llama models

A Gradio web UI for Large Language Models.

Stable Diffusion web UI

Quick Overview

tloen/alpaca-lora is a GitHub repository that provides a method for fine-tuning large language models using the Low-Rank Adaptation (LoRA) technique. It focuses on creating an open-source alternative to the Alpaca model, which is based on the LLaMA architecture. The project aims to make it easier and more cost-effective to train and deploy custom language models.

Pros

  • Enables fine-tuning of large language models with significantly reduced computational resources
  • Provides a more accessible approach to creating custom AI models
  • Offers compatibility with popular frameworks like Hugging Face Transformers
  • Includes scripts and tools for data preparation, training, and inference

Cons

  • Requires access to the base LLaMA model, which has restricted availability
  • May have limitations in performance compared to fully fine-tuned models
  • Potential legal and ethical concerns regarding the use of proprietary model architectures
  • Limited documentation and support compared to more established projects

Code Examples

  1. Loading a fine-tuned model:
from peft import PeftModel
from transformers import LLaMATokenizer, LLaMAForCausalLM

model = LLaMAForCausalLM.from_pretrained("decapoda-research/llama-7b-hf")
tokenizer = LLaMATokenizer.from_pretrained("decapoda-research/llama-7b-hf")

model = PeftModel.from_pretrained(model, "tloen/alpaca-lora-7b")
  1. Generating text with the model:
prompt = "What is the capital of France?"
input_ids = tokenizer(prompt, return_tensors="pt").input_ids

outputs = model.generate(input_ids, max_length=100, num_return_sequences=1)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(generated_text)
  1. Fine-tuning the model on custom data:
from peft import get_peft_model, LoraConfig, TaskType

peft_config = LoraConfig(
    task_type=TaskType.CAUSAL_LM,
    inference_mode=False,
    r=8,
    lora_alpha=32,
    lora_dropout=0.1,
)

model = get_peft_model(model, peft_config)
model.train()

Getting Started

To get started with tloen/alpaca-lora:

  1. Clone the repository:

    git clone https://github.com/tloen/alpaca-lora.git
    cd alpaca-lora
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Prepare your data and configure the training script:

    python prepare_data.py --input_file your_data.json --output_file processed_data.json
    
  4. Run the fine-tuning script:

    python finetune.py --base_model "decapoda-research/llama-7b-hf" --data_path processed_data.json
    
  5. Use the fine-tuned model for inference:

    python generate.py --prompt "Your prompt here" --model_path "path/to/your/fine-tuned/model"
    

Competitor Comparisons

Code and documentation to train Stanford's Alpaca models, and generate the data.

Pros of Stanford Alpaca

  • Provides a more comprehensive dataset for fine-tuning language models
  • Offers detailed documentation on the data generation process
  • Includes scripts for reproducing the dataset creation

Cons of Stanford Alpaca

  • Requires more computational resources for training
  • Less focused on specific fine-tuning techniques like LoRA
  • May have a steeper learning curve for beginners

Code Comparison

Stanford Alpaca:

def generate_prompt(instruction, input=None):
    if input:
        return f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
    else:
        return f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:"

Alpaca-LoRA:

def generate_prompt(instruction, input=None):
    if input:
        return f"### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:\n"
    else:
        return f"### Instruction:\n{instruction}\n\n### Response:\n"

The main difference is in the prompt structure, with Stanford Alpaca providing more detailed instructions and context.

10,276

Code for loralib, an implementation of "LoRA: Low-Rank Adaptation of Large Language Models"

Pros of LoRA

  • More comprehensive and generalized implementation of LoRA
  • Supports a wider range of models and applications
  • Extensive documentation and examples for various use cases

Cons of LoRA

  • Steeper learning curve due to its broader scope
  • May require more setup and configuration for specific tasks

Code Comparison

LoRA:

import loralib as lora

# Apply LoRA to a linear layer
lora_layer = lora.Linear(in_features, out_features, r=rank)

Alpaca-LoRA:

from peft import LoraConfig, get_peft_model

config = LoraConfig(r=8, lora_alpha=32, target_modules=["q_proj", "v_proj"])
model = get_peft_model(model, config)

Key Differences

  • LoRA provides a more flexible and general-purpose implementation
  • Alpaca-LoRA is specifically tailored for fine-tuning LLMs like GPT models
  • LoRA offers more customization options, while Alpaca-LoRA simplifies the process for specific use cases

Use Cases

  • LoRA: Suitable for a wide range of machine learning tasks and model architectures
  • Alpaca-LoRA: Ideal for quickly fine-tuning large language models with minimal resources

Community and Support

  • LoRA: Backed by Microsoft, with extensive documentation and community support
  • Alpaca-LoRA: Growing community, focused on LLM fine-tuning applications
15,745

🤗 PEFT: State-of-the-art Parameter-Efficient Fine-Tuning.

Pros of PEFT

  • More comprehensive and flexible, supporting various parameter-efficient fine-tuning methods
  • Integrated with Hugging Face's ecosystem, offering seamless compatibility with transformers library
  • Actively maintained with regular updates and improvements

Cons of PEFT

  • Steeper learning curve due to its broader scope and more complex API
  • May require more setup and configuration for specific use cases

Code Comparison

PEFT:

from peft import get_peft_model, LoraConfig, TaskType

peft_config = LoraConfig(task_type=TaskType.CAUSAL_LM, r=8, lora_alpha=32, lora_dropout=0.1)
model = get_peft_model(model, peft_config)

Alpaca-LoRA:

from alpaca_lora import create_lora_model

model = create_lora_model(base_model, r=8, alpha=32, dropout=0.1)

Summary

PEFT offers a more comprehensive solution for parameter-efficient fine-tuning, with broader method support and integration within the Hugging Face ecosystem. However, it may be more complex to use for specific tasks. Alpaca-LoRA provides a simpler, more focused approach for LoRA fine-tuning, particularly tailored for instruction-following models like Alpaca. The choice between the two depends on the specific use case and desired flexibility.

55,392

Inference code for Llama models

Pros of Llama

  • Developed by Meta, leveraging extensive resources and expertise
  • Designed as a foundational model with broader applicability
  • Potentially more powerful and versatile for various NLP tasks

Cons of Llama

  • Restricted access due to licensing and ethical considerations
  • Requires significant computational resources for fine-tuning and deployment
  • Less community-driven development compared to Alpaca-LoRA

Code Comparison

Alpaca-LoRA:

from peft import PeftModel
from transformers import LlamaTokenizer, LlamaForCausalLM

model = LlamaForCausalLM.from_pretrained("decapoda-research/llama-7b-hf")
model = PeftModel.from_pretrained(model, "tloen/alpaca-lora-7b")

Llama:

from transformers import LlamaTokenizer, LlamaForCausalLM

tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")

While both repositories utilize the Llama architecture, Alpaca-LoRA focuses on fine-tuning using the LoRA technique, making it more accessible for smaller-scale applications. Llama, being the base model, offers more flexibility but requires additional steps for task-specific adaptation.

A Gradio web UI for Large Language Models.

Pros of text-generation-webui

  • User-friendly web interface for interacting with various language models
  • Supports multiple models and architectures (e.g., GPT-J, GPT-Neo, LLAMA)
  • Offers advanced features like chat, instruct mode, and notebook interface

Cons of text-generation-webui

  • More complex setup process due to broader feature set
  • Potentially higher resource requirements for running the web interface
  • May have a steeper learning curve for users new to language models

Code Comparison

text-generation-webui:

def generate_reply(question, max_new_tokens=200, do_sample=True, temperature=0.7):
    tokens = tokenizer.encode(question, return_tensors="pt")
    output = model.generate(tokens, max_new_tokens=max_new_tokens, do_sample=do_sample, temperature=temperature)
    return tokenizer.decode(output[0], skip_special_tokens=True)

alpaca-lora:

def generate(prompt, max_length=128):
    input_ids = tokenizer.encode(prompt, return_tensors="pt")
    output = model.generate(input_ids, max_length=max_length)
    return tokenizer.decode(output[0], skip_special_tokens=True)

The code snippets show that text-generation-webui offers more customizable generation parameters, while alpaca-lora provides a simpler interface for text generation. text-generation-webui's approach allows for finer control over the output, but alpaca-lora's implementation is more straightforward for basic use cases.

Stable Diffusion web UI

Pros of stable-diffusion-webui

  • More comprehensive and feature-rich UI for image generation
  • Extensive customization options and settings
  • Large community with frequent updates and extensions

Cons of stable-diffusion-webui

  • Steeper learning curve due to complexity
  • Requires more computational resources
  • Primarily focused on image generation, not text generation

Code Comparison

stable-diffusion-webui:

import modules.scripts
from modules.processing import process_images

class Script(modules.scripts.Script):
    def run(self, p, *args):
        return process_images(p)

alpaca-lora:

from peft import PeftModel
from transformers import LlamaTokenizer, LlamaForCausalLM

model = LlamaForCausalLM.from_pretrained(BASE_MODEL)
model = PeftModel.from_pretrained(model, LORA_WEIGHTS)

The code snippets highlight the different focus areas of the two projects. stable-diffusion-webui is centered around image processing and generation, while alpaca-lora deals with text generation using language models.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

🦙🌲🤏 Alpaca-LoRA

This repository contains code for reproducing the Stanford Alpaca results using low-rank adaptation (LoRA). We provide an Instruct model of similar quality to text-davinci-003 that can run on a Raspberry Pi (for research), and the code is easily extended to the 13b, 30b, and 65b models.

In addition to the training code, which runs within hours on a single RTX 4090, we publish a script for downloading and inference on the foundation model and LoRA, as well as the resulting LoRA weights themselves. To fine-tune cheaply and efficiently, we use Hugging Face's PEFT as well as Tim Dettmers' bitsandbytes.

Without hyperparameter tuning, the LoRA model produces outputs comparable to the Stanford Alpaca model. (Please see the outputs included below.) Further tuning might be able to achieve better performance; I invite interested users to give it a try and report their results.

Local Setup

  1. Install dependencies

    pip install -r requirements.txt
    
  2. If bitsandbytes doesn't work, install it from source. Windows users can follow these instructions.

Training (finetune.py)

This file contains a straightforward application of PEFT to the LLaMA model, as well as some code related to prompt construction and tokenization. PRs adapting this code to support larger models are always welcome.

Example usage:

python finetune.py \
    --base_model 'decapoda-research/llama-7b-hf' \
    --data_path 'yahma/alpaca-cleaned' \
    --output_dir './lora-alpaca'

We can also tweak our hyperparameters:

python finetune.py \
    --base_model 'decapoda-research/llama-7b-hf' \
    --data_path 'yahma/alpaca-cleaned' \
    --output_dir './lora-alpaca' \
    --batch_size 128 \
    --micro_batch_size 4 \
    --num_epochs 3 \
    --learning_rate 1e-4 \
    --cutoff_len 512 \
    --val_set_size 2000 \
    --lora_r 8 \
    --lora_alpha 16 \
    --lora_dropout 0.05 \
    --lora_target_modules '[q_proj,v_proj]' \
    --train_on_inputs \
    --group_by_length

Inference (generate.py)

This file reads the foundation model from the Hugging Face model hub and the LoRA weights from tloen/alpaca-lora-7b, and runs a Gradio interface for inference on a specified input. Users should treat this as example code for the use of the model, and modify it as needed.

Example usage:

python generate.py \
    --load_8bit \
    --base_model 'decapoda-research/llama-7b-hf' \
    --lora_weights 'tloen/alpaca-lora-7b'

Official weights

The most recent "official" Alpaca-LoRA adapter available at tloen/alpaca-lora-7b was trained on March 26 with the following command:

python finetune.py \
    --base_model='decapoda-research/llama-7b-hf' \
    --num_epochs=10 \
    --cutoff_len=512 \
    --group_by_length \
    --output_dir='./lora-alpaca' \
    --lora_target_modules='[q_proj,k_proj,v_proj,o_proj]' \
    --lora_r=16 \
    --micro_batch_size=8

Checkpoint export (export_*_checkpoint.py)

These files contain scripts that merge the LoRA weights back into the base model for export to Hugging Face format and to PyTorch state_dicts. They should help users who want to run inference in projects like llama.cpp or alpaca.cpp.

Docker Setup & Inference

  1. Build the container image:
docker build -t alpaca-lora .
  1. Run the container (you can also use finetune.py and all of its parameters as shown above for training):
docker run --gpus=all --shm-size 64g -p 7860:7860 -v ${HOME}/.cache:/root/.cache --rm alpaca-lora generate.py \
    --load_8bit \
    --base_model 'decapoda-research/llama-7b-hf' \
    --lora_weights 'tloen/alpaca-lora-7b'
  1. Open https://localhost:7860 in the browser

Docker Compose Setup & Inference

  1. (optional) Change desired model and weights under environment in the docker-compose.yml

  2. Build and run the container

docker-compose up -d --build
  1. Open https://localhost:7860 in the browser

  2. See logs:

docker-compose logs -f
  1. Clean everything up:
docker-compose down --volumes --rmi all

Notes

  • We can likely improve our model performance significantly if we had a better dataset. Consider supporting the LAION Open Assistant effort to produce a high-quality dataset for supervised fine-tuning (or bugging them to release their data).
  • We're continually fixing bugs and conducting training runs, and the weights on the Hugging Face Hub are being updated accordingly. In particular, those facing issues with response lengths should make sure that they have the latest version of the weights and code.
  • Users with multiple GPUs should take a look here.
  • We include the Stanford Alpaca dataset, which was made available under the ODC Attribution License.

Resources

Example outputs

Instruction: Tell me about alpacas.

Alpaca-LoRA: Alpacas are members of the camelid family and are native to the Andes Mountains of South America. They are known for their soft, luxurious fleece, which is used to make clothing, blankets, and other items. Alpacas are herbivores and graze on grasses and other plants. They are social animals and live in herds of up to 20 individuals.

Stanford Alpaca: Alpacas are small, fluffy animals related to camels and llamas. They are native to Peru and Bolivia, and were first domesticated around 5,000 years ago. They are kept mainly for their fine, soft fleece, which is used to make knitwear and other garments. Alpacas are herd animals and live in small family groups, led by an older male. They are highly social animals and can form strong bonds with their owners.

text-davinci-003: Alpacas are a domesticated species of South American camelid, similar to llamas. They are native to the Andes Mountains in South America and are kept as livestock for their soft, luxurious wool. Alpacas are social animals and live in herds of up to 20 individuals. They are typically kept in small herds of two to five animals, and are relatively easy to care for. Alpacas are herbivores and feed on grass, hay, and other vegetation. They are also known for their gentle and friendly nature, making them popular as pets.


Instruction: Tell me about the president of Mexico in 2019.

Alpaca-LoRA: The president of Mexico in 2019 was Andrés Manuel López Obrador, who took office on December 1, 2018. He is a member of the National Regeneration Movement (MORENA) political party and is the first left-wing president of Mexico since 1946. He is known for his anti-corruption and anti-neoliberal policies, as well as his commitment to improving the living conditions of the Mexican people.

Stanford Alpaca: In 2019, Andres Manuel Lopez Obrador was sworn in as President of Mexico. He is the country’s 58th president and the first from the left-wing Morena party. He was elected in 2018 on a promise to combat corruption and reduce poverty and has faced criticism for his handling of the economy, crime and the country’s relationship with the United States. He is also known for his ambitious social welfare programs and has been praised for raising the minimum wage and providing aid to low-income families. He is seen as a champion of democracy and human rights in Mexico.

text-davinci-003: The president of Mexico in 2019 is Andrés Manuel López Obrador, commonly known as AMLO. He is a Mexican politician who has been the President of Mexico since December 1, 2018. He is a member of the National Regeneration Movement (MORENA) party and previously served as the Head of Government of Mexico City from 2000 to 2005. He is a left-wing populist and has promised to reduce corruption and inequality in Mexico. He has also promised to increase public spending on social programs and infrastructure projects.


Instruction: Tell me about the king of France in 2019.

Alpaca-LoRA: The King of France in 2019 is Emmanuel Macron, who was elected in May 2017. He is the youngest president in the history of the Fifth Republic and the first president to be born after World War II. He is also the first president to have never held elected office before.

Stanford Alpaca: In 2019, the King of France was Emmanuel Macron. He was sworn in as President of France on May 14, 2017, and has been leading the country ever since. He is a member of the centrist La République En Marche! Party. He is known for his reformist agenda and has faced a number of protests from both the right and left of the political spectrum. He is the youngest French president in history.

text-davinci-003: The King of France is a largely symbolic role, as France is a republic. The current President of France is Emmanuel Macron, who was elected in 2017.


Instruction: List all Canadian provinces in alphabetical order.

Alpaca-LoRA: Alberta, British Columbia, Manitoba, New Brunswick, Newfoundland and Labrador, Nova Scotia, Ontario, Prince Edward Island, Quebec, Saskatchewan

Stanford Alpaca: Alberta, British Columbia, Manitoba, New Brunswick, Newfoundland and Labrador, Nova Scotia, Ontario, Prince Edward Island, Quebec, Saskatchewan, Northwest Territories, Nunavut, Yukon.

text-davinci-003:

  1. Alberta
  2. British Columbia
  3. Manitoba
  4. New Brunswick
  5. Newfoundland and Labrador
  6. Northwest Territories
  7. Nova Scotia
  8. Nunavut
  9. Ontario
  10. Prince Edward Island
  11. Quebec
  12. Saskatchewan
  13. Yukon

Instruction: Write a Python program that prints the first 10 Fibonacci numbers.

Alpaca-LoRA:

def fibonacci(n):
    if n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(10))

Stanford Alpaca:

numbers = [0, 1]

for i in range(2, 11):
    numbers.append(numbers[i-2] + numbers[i-1])

print(numbers)
# Output: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

text-davinci-003:

# Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones.

# Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

# Program:

# Initialize the first two numbers of the Fibonacci sequence
a = 0
b = 1

# Print the first 10 numbers of the Fibonacci sequence
print(a)
print(b)

for i in range(2,10):
    c = a + b
    a = b
    b = c
    print(c)

Instruction: Write a program that prints the numbers from 1 to 100. But for multiples of three print 'Fizz' instead of the number and for the multiples of five print 'Buzz'. For numbers which are multiples of both three and five print 'FizzBuzz'.

Alpaca-LoRA:

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

Stanford Alpaca:

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print('FizzBuzz')
    elif i % 3 == 0:
        print('Fizz')
    elif i % 5 == 0:
        print('Buzz')
    else:
        print(i)

text-davinci-003:

for (let i = 1; i <= 100; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz");
  } else if (i % 3 === 0) {
    console.log("Fizz");
  } else if (i % 5 === 0) {
    console.log("Buzz");
  } else {
    console.log(i);
  }
}

Instruction: Tell me five words that rhyme with 'shock'.

Alpaca-LoRA: Flock, lock, rock, stock, and sock.

Stanford Alpaca: Five words that rhyme with shock are: rock, pop, shock, cook, and snock.

text-davinci-003:

  1. Block
  2. Lock
  3. Dock
  4. Clock
  5. Rock

Instruction: Translate the sentence 'I have no mouth but I must scream' into Spanish.

Alpaca-LoRA: No tengo boca pero tengo que gritar.

Stanford Alpaca: No tengo boca, pero debo gritar.

text-davinci-003: No tengo boca pero debo gritar.