Skip to content

Commit d441307

Browse files
committed
Initial commit after moving over from llama.cpp repo
0 parents  commit d441307

File tree

13 files changed

+1272
-0
lines changed

13 files changed

+1272
-0
lines changed

.gitignore

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# setup.py is auto-generated by poetry
2+
setup.py
3+
4+
models/*
5+
# Byte-compiled / optimized / DLL files
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
10+
# C extensions
11+
*.so
12+
13+
# Distribution / packaging
14+
.Python
15+
build/
16+
develop-eggs/
17+
dist/
18+
downloads/
19+
eggs/
20+
.eggs/
21+
lib/
22+
lib64/
23+
parts/
24+
sdist/
25+
var/
26+
wheels/
27+
share/python-wheels/
28+
*.egg-info/
29+
.installed.cfg
30+
*.egg
31+
MANIFEST
32+
33+
# PyInstaller
34+
# Usually these files are written by a python script from a template
35+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
36+
*.manifest
37+
*.spec
38+
39+
# Installer logs
40+
pip-log.txt
41+
pip-delete-this-directory.txt
42+
43+
# Unit test / coverage reports
44+
htmlcov/
45+
.tox/
46+
.nox/
47+
.coverage
48+
.coverage.*
49+
.cache
50+
nosetests.xml
51+
coverage.xml
52+
*.cover
53+
*.py,cover
54+
.hypothesis/
55+
.pytest_cache/
56+
cover/
57+
58+
# Translations
59+
*.mo
60+
*.pot
61+
62+
# Django stuff:
63+
*.log
64+
local_settings.py
65+
db.sqlite3
66+
db.sqlite3-journal
67+
68+
# Flask stuff:
69+
instance/
70+
.webassets-cache
71+
72+
# Scrapy stuff:
73+
.scrapy
74+
75+
# Sphinx documentation
76+
docs/_build/
77+
78+
# PyBuilder
79+
.pybuilder/
80+
target/
81+
82+
# Jupyter Notebook
83+
.ipynb_checkpoints
84+
85+
# IPython
86+
profile_default/
87+
ipython_config.py
88+
89+
# pyenv
90+
# For a library or package, you might want to ignore these files since the code is
91+
# intended to run in multiple environments; otherwise, check them in:
92+
# .python-version
93+
94+
# pipenv
95+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
96+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
97+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
98+
# install all needed dependencies.
99+
#Pipfile.lock
100+
101+
# poetry
102+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
103+
# This is especially recommended for binary packages to ensure reproducibility, and is more
104+
# commonly ignored for libraries.
105+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
106+
#poetry.lock
107+
108+
# pdm
109+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
110+
#pdm.lock
111+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
112+
# in version control.
113+
# https://pdm.fming.dev/#use-with-ide
114+
.pdm.toml
115+
116+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
117+
__pypackages__/
118+
119+
# Celery stuff
120+
celerybeat-schedule
121+
celerybeat.pid
122+
123+
# SageMath parsed files
124+
*.sage.py
125+
126+
# Environments
127+
.env
128+
.venv
129+
env/
130+
venv/
131+
ENV/
132+
env.bak/
133+
venv.bak/
134+
135+
# Spyder project settings
136+
.spyderproject
137+
.spyproject
138+
139+
# Rope project settings
140+
.ropeproject
141+
142+
# mkdocs documentation
143+
/site
144+
145+
# mypy
146+
.mypy_cache/
147+
.dmypy.json
148+
dmypy.json
149+
150+
# Pyre type checker
151+
.pyre/
152+
153+
# pytype static type analyzer
154+
.pytype/
155+
156+
# Cython debug symbols
157+
cython_debug/
158+
159+
# PyCharm
160+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
161+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
162+
# and can be added to the global gitignore or merged into this file. For a more nuclear
163+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
164+
#.idea/

CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CmakeLists for building python bindings
2+
cmake_minimum_required(VERSION 3.0)
3+
4+
project(llamacpp)
5+
6+
set(CMAKE_CXX_STANDARD 11)
7+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
8+
set(CMAKE_CXX_EXTENSIONS OFF)
9+
10+
set(GGML_USE_ACCELERATE 1)
11+
find_package(pybind11 REQUIRED)
12+
13+
add_library(llamacpp MODULE src/PyLlama.cpp ../llama.cpp ../ggml.c ../utils.cpp)
14+
15+
target_include_directories(llamacpp PRIVATE ../)
16+
17+
target_link_libraries(llamacpp PRIVATE pybind11::module pybind11::lto pybind11::windows_extras)
18+
add_link_options(-no_fixup_chains)
19+
pybind11_extension(llamacpp)
20+
21+
if(NOT MSVC AND NOT ${CMAKE_BUILD_TYPE} MATCHES Debug|RelWithDebInfo)
22+
# Strip unnecessary sections of the binary on Linux/macOS
23+
pybind11_strip(llamacpp)
24+
endif()
25+
26+
set_target_properties(llamacpp PROPERTIES CXX_VISIBILITY_PRESET "hidden"
27+
CUDA_VISIBILITY_PRESET "hidden")

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Building the Python bindings
2+
3+
### macOS
4+
5+
`brew install pybind11`
6+
7+
## Install python package
8+
9+
### From PyPI
10+
11+
```
12+
pip install llamacpp
13+
```
14+
15+
### From source
16+
17+
```
18+
poetry install
19+
```
20+
21+
## Get the model weights
22+
23+
You will need to obtain the weights for LLaMA yourself. There are a few torrents floating around as well as some huggingface repositories (e.g https://huggingface.co/nyanko7/LLaMA-7B/). Once you have them, copy them into the models folder.
24+
25+
```
26+
ls ./models
27+
65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
28+
```
29+
30+
Convert the weights to GGML format using `llamacpp-convert`. Then use `llamacpp-quantize` to quantize them into INT4. For example, for the 7B parameter model, run
31+
32+
```
33+
llamacpp-convert ./models/7B/ 1
34+
llamacpp-quantize ./models/7B/
35+
```
36+
37+
## Run this demo script
38+
39+
```
40+
import llamacpp
41+
import os
42+
43+
model_path = "./models/7B/ggml-model-q4_0.bin"
44+
params = llamacpp.gpt_params(model_path,
45+
"Hi, I'm a llama.",
46+
4096,
47+
40,
48+
0.1,
49+
0.7,
50+
2.0)
51+
model = llamacpp.PyLLAMA(model_path, params)
52+
model.predict("Hello, I'm a llama.", 10)
53+
```
54+
55+
## ToDo
56+
57+
- [x] Use poetry to build package
58+
- [x] Add command line entry point for quantize script
59+
- [x] Publish wheel to PyPI
60+
- [ ] Add chat interface based on tinygrad

build.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from setuptools_cpp import CMakeExtension, ExtensionBuilder, Pybind11Extension
2+
from typing import Any, Dict
3+
4+
5+
def build(setup_kwargs: Dict[str, Any]) -> None:
6+
ext_modules = [
7+
CMakeExtension(f"llamacpp.llamacpp", sourcedir="./python"),
8+
]
9+
10+
setup_kwargs.update(
11+
{
12+
"ext_modules": ext_modules,
13+
"cmdclass": dict(build_ext=ExtensionBuilder),
14+
"zip_safe": False,
15+
}
16+
)

llamacpp/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Expose the bindings in module
2+
from .llamacpp import gpt_params, PyLLAMA, llama_model_quantize

0 commit comments

Comments
 (0)