diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000000..83a874f8f0 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "modules/llama.cpp"] + path = modules/llama.cpp + url = https://github.com/gkielian/llama.cpp.git diff --git a/data_augmentation/README.md b/data_augmentation/README.md new file mode 100644 index 0000000000..a1c42e57ac --- /dev/null +++ b/data_augmentation/README.md @@ -0,0 +1,25 @@ +# Running Mistral 7B Test + +Scripts here download and test the Mistral 7B model with a python wrapper for +`llama.cpp` called `llama-cpp-python`. + +## Install Steps + +1. First install the nanogpt requirements (see main [README.md](../README.md)) +2. Second install `llama-cpp-python` and dependencies via the installation + script provided in the repo root directory: + +```bash +bash install_llama_cpp_python.sh +``` +3. Finally cd into this directory and run the `download_and_test_mistral.sh` + script via sourcing (b/c will need one's python environment): + +```bash +source download_and_test_mistral7b.sh +``` + +This script will download mistral7b if not already in the `./models` directory, +and start the `llama-cpp-python_example.py` script. + +This will should complete fairly quickly with GPU acceleration. diff --git a/data_augmentation/download_and_test_mistral7b.sh b/data_augmentation/download_and_test_mistral7b.sh new file mode 100644 index 0000000000..23b5b3ae6d --- /dev/null +++ b/data_augmentation/download_and_test_mistral7b.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# This script will downlaod and test Mistral7B using llama-cpp-python + +if [ -d ./models ]; then + mkdir -p ./models +fi + +if [ -f "./models/mistral-7b-instruct-v0.1.Q5_K_M.gguf" ]; then + echo "./models/mistral-7b-instruct-v0.1.Q5_K_M.gguf file found, continuing" +else + echo "./models/mistral-7b-instruct-v0.1.Q5_K_M.gguf file not found, downloading" + wget -P ./models https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q5_K_M.gguf +fi + +python3 llama-cpp-python_example.py + diff --git a/data_augmentation/llama-cpp-python_example.py b/data_augmentation/llama-cpp-python_example.py new file mode 100644 index 0000000000..bf7dac5312 --- /dev/null +++ b/data_augmentation/llama-cpp-python_example.py @@ -0,0 +1,12 @@ +from llama_cpp import Llama + +text = """[INST] What is your favourite condiment? [/INST] +Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen! +[INST] Do you have mayonnaise recipes? [/INST]""" + +llm = Llama(model_path="./models/mistral-7b-instruct-v0.1.Q5_K_M.gguf", n_ctx=2048, + n_threads=8, n_gpu_layers=35, verbose=True) + +output = llm(text, max_tokens=256, stop=["[INST]"], echo=True) + +print(output) diff --git a/install_llama_cpp_python.sh b/install_llama_cpp_python.sh new file mode 100644 index 0000000000..1ee6267a8c --- /dev/null +++ b/install_llama_cpp_python.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +# Set strict error handling +set -euo pipefail + +# Get current script directory +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)" + +echo "This script will install llama-cpp-python with GPU support" + +# Check if llama module exists, prompt to initialize submodule +if [ ! -d "$script_dir/modules/llama.cpp" ]; then + + read -p "llama.cpp module not found. Download with git? [Y/n] " response + + response=${response,,} + + if [ "$response" != "n" ]; then + + # Initialize llama submodule + git submodule update --init --recursive + + else + + echo "Exiting. llama.cpp module required." + exit 1 + + fi + +fi + +read -p "Enter CUDA install location (default /usr/local/cuda): " cuda_home + +cuda_home=${cuda_home:-/usr/local/cuda} + +if [ ! -d "$cuda_home" ]; then + echo "Error: $cuda_home is not a valid directory" + exit 1 +fi + +read -p "Append CUDA settings to ~/.bashrc? [Y/n] " response + +response=${response,,} + +if [ "$response" != "n" ]; then + + echo "export CUDA_HOME=$cuda_home" >> ~/.bashrc + echo "export PATH=\"$cuda_home/bin:\$PATH\"" >> ~/.bashrc + echo "export LLAMA_CUBLAS=on" >> ~/.bashrc + echo "export LLAMA_CPP_LIB=\"$script_dir/modules/llama.cpp/libllama.so\"" >> ~/.bashrc + + echo "Appended CUDA settings to ~/.bashrc" + +fi + +pushd "$script_dir/modules/llama.cpp" + +make clean +make libllama.so || { echo "Error compiling llama.cpp"; exit 1; } + +popd + +export LLAMA_CPP_LIB="$script_dir/modules/llama.cpp/libllama.so" + +CMAKE_ARGS="-DLLAMA_CUBLAS=on" python3 -m pip install llama-cpp-python --no-cache-dir + +echo "llama-cpp-python installed successfully" diff --git a/modules/llama.cpp b/modules/llama.cpp new file mode 160000 index 0000000000..48edda30ee --- /dev/null +++ b/modules/llama.cpp @@ -0,0 +1 @@ +Subproject commit 48edda30ee545fdac2e7a33d505382888f748bbf