Skip to content

Commit bf2bddd

Browse files
authored
Merge pull request #15 from gotzmann/server
Server Mode
2 parents ea45a8a + e274511 commit bf2bddd

13 files changed

Lines changed: 1179 additions & 452 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.env
2+
*.bin
23
.idea
34
.vscode
45
*.pprof

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
TARGET = llama
2-
VERSION = $(shell cat VERSION)
2+
# $(shell cat VERSION)
3+
VERSION = v1.4.0
34
OS = linux
45
ARCH = amd64
56
PACKAGE = github.com/gotzmann/$(TARGET)
@@ -140,3 +141,9 @@ fp16:
140141
pprof:
141142
go tool pprof -pdf cpu.pprof > cpu.pdf
142143

144+
.PHONY: builds
145+
builds:
146+
GOOS=windows GOARCH=amd64 go build -o ./builds/llama-go-$(VERSION).exe -ldflags "-s -w" main.go
147+
GOOS=darwin GOARCH=amd64 go build -o ./builds/llama-go-$(VERSION)-macos -ldflags "-s -w" main.go
148+
GOOS=linux GOARCH=amd64 go build -o ./builds/llama-go-$(VERSION)-linux -ldflags "-s -w" main.go
149+

README.md

Lines changed: 158 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,198 @@
22

33
![](./assets/images/terminal.png?raw=true)
44

5-
## The Goal
5+
## Motivation
66

7-
We dream of a world where ML hackers are able to grok with **REALLY BIG GPT** models without having GPU clusters consuming a shit tons of **$$$** - using only machines in their own homelabs.
7+
We dream of a world where fellow ML hackers are grokking **REALLY BIG GPT** models in their homelabs without having GPU clusters consuming a shit tons of **$$$**.
88

9-
The code of the project is based on the legendary **[ggml.cpp](https://github.com/ggerganov/llama.cpp)** framework of Georgi Gerganov written in C++
9+
The code of the project is based on the legendary **[ggml.cpp](https://github.com/ggerganov/llama.cpp)** framework of Georgi Gerganov written in C++ with the same attitude to performance and elegance.
1010

11-
We hope using our beloved Golang instead of *soo-powerful* but *too-low-level* language will allow much greater adoption of the **NoGPU** ideas.
12-
13-
The V1 supports only FP32 math, so you'll need at least 32GB RAM to work even with the smallest **LLaMA-7B** model. As a preliminary step you should have binary files converted from original LLaMA model locally.
11+
We hope using Golang instead of *soo-powerful* but *too-low-level* language will allow much greater adoption.
1412

1513
## V0 Roadmap
1614

17-
- [x] Run tensor math in pure Golang based on C++ source
15+
- [x] Tensor math in pure Golang
1816
- [x] Implement LLaMA neural net architecture and model loading
19-
- [x] Run smaller LLaMA-7B model
20-
- [x] Be sure Go inference works EXACT SAME way as C++
21-
- [x] Let Go shine! Enable multi-threading and boost performance
17+
- [x] Test with smaller LLaMA-7B model
18+
- [x] Be sure Go inference works exactly same way as C++
19+
- [x] Let Go shine! Enable multi-threading and messaging to boost performance
2220

23-
## V1 Roadmap
21+
## V1 Roadmap - Spring'23
2422

2523
- [x] Cross-patform compatibility with Mac, Linux and Windows
26-
- [x] Release first stable version for ML hackers
27-
- [x] Support bigger LLaMA models: 13B, 30B, 65B
28-
- [x] ARM NEON support on Apple Silicon (modern Macs) and ARM servers
29-
- [x] Performance boost with x64 AVX2 support for Intel and AMD
24+
- [x] Release first stable version for ML hackers - v1.0
25+
- [x] Enable bigger LLaMA models: 13B, 30B, 65B - v1.1
26+
- [x] ARM NEON support on Apple Silicon (modern Macs) and ARM servers - v1.2
27+
- [x] Performance boost with x64 AVX2 support for Intel and AMD - v1.2
28+
- [x] Better memory use and GC optimizations - v1.3
29+
- [x] Introduce Server Mode (embedded REST API) for use in real projects - v1.4
30+
- [x] Release converted models for free access over the Internet - v1.4
31+
- [ ] INT8 quantization to allow x4 bigger models fit same memory
32+
- [ ] Benchmark LLaMA.go against some mainstream Python / C++ frameworks
33+
- [ ] Enable some popular models of LLaMA family: Vicuna, Alpaca, etc
3034
- [ ] Speed-up AVX2 with memory aligned tensors
31-
- [ ] INT8 quantization to allow x4 bigger models fit the same memory
32-
- [ ] Enable interactive mode for real-time chat with GPT
33-
- [ ] Allow automatic download converted model weights from the Internet
35+
- [ ] Extensive logging for production monitoring
36+
- [ ] Interactive mode for real-time chat with GPT
37+
38+
## V2 Roadmap - Summer'23
39+
40+
- [ ] Automatic CPU / GPU features detection
3441
- [ ] Implement metrics for RAM and CPU usage
35-
- [ ] Server Mode for use in Clouds as part of Microservice Architecture
42+
- [ ] Standalone GUI or web interface for better access to framework
43+
- [ ] Support popular open models: Open Assistant, StableLM, BLOOM, Anthropic, etc.
44+
- [ ] AVX512 support - yet another performance boost for AMD Epyc and Intel Sapphire Rapids
45+
- [ ] Nvidia GPUs support (CUDA or Tensor Cores)
3646

37-
## V2 Roadmap
47+
## V3 Roadmap - Fall'23
3848

3949
- [ ] Allow plugins and external APIs for complex projects
40-
- [ ] AVX512 support - yet another performance boost for AMD Epyc
41-
- [ ] FP16 and BF16 support when hardware support there
42-
- [ ] Support INT4 and GPTQ quantization
50+
- [ ] Allow model training and fine-tuning
51+
- [ ] Speed up execution on GPU cards and clusters
52+
- [ ] FP16 and BF16 math if hardware support is there
53+
- [ ] INT4 and GPTQ quantization
54+
- [ ] AMD Radeon GPUs support with OpenCL
55+
56+
## How to Run?
57+
58+
First, obtain and convert original LLaMA models on your own, or just download ready-to-rock ones:
59+
60+
**LLaMA-7B:** [llama-7b-fp32.bin](https://nogpu.com/llama-7b-fp32.bin)
61+
62+
**LLaMA-13B:** [llama-7b-fp32.bin](https://nogpu.com/llama-7b-fp32.bin)
63+
64+
Both models store FP32 weights, so you'll needs at least 32Gb of RAM (not VRAM or GPU RAM) for LLaMA-7B. Double to 64Gb for LLaMA-13B.
4365

44-
## How to Run
66+
Next, build app binary from sources (see instructions below), or just download already built one:
67+
68+
**Windows:** [llama-go-v1.4.0.exe](./builds/llama-go-v1.4.0.exe)
69+
70+
**MacOS:** [llama-go-v1.4.0-macos](./builds/llama-go-v1.4.0-macos)
71+
72+
**Linux:** [llama-go-v1.4.0-linux](./builds/llama-go-v1.4.0-linux)
73+
74+
So now you have both executable and model, go try it for yourself:
4575

4676
```shell
47-
go run main.go \
48-
--model ~/models/7B/ggml-model-f32.bin \
49-
--temp 0.80 \
50-
--context 128 \
51-
--predict 128 \
52-
--prompt "Why Golang is so popular?"
77+
llama-go-v1.4.0-macos \
78+
--model ~/models/llama-7b-fp32.bin \
79+
--prompt "Why Golang is so popular?" \
5380
```
5481

55-
Or build it with Makefile and then run binary.
56-
57-
## Useful CLI parameters:
82+
## Useful command line flags:
5883

5984
```shell
6085
--prompt Text prompt from user to feed the model input
61-
--model Path and file name of converted .bin LLaMA model
86+
--model Path and file name of converted .bin LLaMA model [ llama-7b-fp32.bin, etc ]
87+
--server Start in Server Mode acting as REST API endpoint
88+
--host Host to allow requests from in Server Mode [ localhost by default ]
89+
--port Port listen to in Server Mode [ 8080 by default ]
90+
--pods Maximum pods or units of parallel execution allowed in Server Mode [ 1 by default ]
6291
--threads Adjust to the number of CPU cores you want to use [ all cores by default ]
63-
--predict Number of tokens to predict [ 64 by default ]
64-
--context Context size in tokens [ 64 by default ]
65-
--temp Model temperature hyper parameter [ 0.8 by default ]
66-
--silent Hide welcome logo and other output [ show by default ]
92+
--context Context size in tokens [ 1024 by default ]
93+
--predict Number of tokens to predict [ 512 by default ]
94+
--temp Model temperature hyper parameter [ 0.5 by default ]
95+
--silent Hide welcome logo and other output [ shown by default ]
6796
--chat Chat with user in interactive mode instead of compute over static prompt
68-
--profile Profe CPU performance while running and store results to [cpu.pprof] file
97+
--profile Profe CPU performance while running and store results to cpu.pprof file
6998
--avx Enable x64 AVX2 optimizations for Intel and AMD machines
7099
--neon Enable ARM NEON optimizations for Apple Macs and ARM server
71100
```
72101
102+
## Going Production
103+
104+
LLaMA.go embeds standalone HTTP server exposing REST API. To enable it, run app with special flags:
105+
106+
```shell
107+
llama-go-v1.4.0-macos \
108+
--model ~/models/llama-7b-fp32.bin \
109+
--server \
110+
--host 127.0.0.1 \
111+
--port 8080 \
112+
--pods 4 \
113+
--threads 4
114+
```
115+
116+
Depending on the model size, how many CPU cores available there, how many requests you want to process in parallel, how fast you'd like to get answers, choose **pods** and **threads** parameters wisely.
117+
118+
**Pods** is a number of inference instances that might run in parallel.
119+
120+
**Threads** parameter sets how many cores will be used for tensor math within a pod.
121+
122+
So for example if you have machine with 16 hardware cores capable running 32 hyper-threads in parallel, you might end up with something like that:
123+
124+
```shell
125+
--server --pods 4 --threads 8
126+
```
127+
128+
When there is no free pod to handle arriving request, it will be placed into the waiting queue and started when some pod gets job finished.
129+
130+
# REST API examples
131+
132+
## Place new job
133+
134+
Send POST request (with Postman) to your server address with JSON containing unique UUID v4 and prompt:
135+
136+
```json
137+
{
138+
"id": "5fb8ebd0-e0c9-4759-8f7d-35590f6c9fc3",
139+
"prompt": "Why Golang is so popular?"
140+
}
141+
```
142+
143+
## Check job status
144+
145+
Send GET request (with Postman or browser) to URL like http://host:port/jobs/status/:id
146+
147+
```shell
148+
GET http://localhost:8080/jobs/status/5fb8ebd0-e0c9-4759-8f7d-35590f6c9fcb
149+
```
150+
151+
## Get the results
152+
153+
Send GET request (with Postman or browser) to URL like http://host:port/jobs/:id
154+
155+
```shell
156+
GET http://localhost:8080/jobs/5fb8ebd0-e0c9-4759-8f7d-35590f6c9fcb
157+
```
158+
159+
# How to build
160+
161+
First, install **Golang** and **git** (you'll need to download installers in case of Windows).
162+
163+
```shell
164+
brew install git
165+
brew install golang
166+
```
167+
168+
Then clone the repo and enter the project folder:
169+
170+
```
171+
git clone https://github.com/gotzmann/llama.go.git
172+
cd llama.go
173+
```
174+
175+
Some Go magic to install external dependencies:
176+
177+
```
178+
go tidy
179+
go vendor
180+
```
181+
182+
Now we are ready to build the binary from the source code:
183+
184+
```shell
185+
go build -o llama-go-v1.exe -ldflags "-s -w" main.go
186+
```
187+
73188
## FAQ
74189
75-
**1] Where might I get original LLaMA model files?**
190+
**1) From where I might obtain original LLaMA models?**
76191
77-
Contact Meta directly or look around for some torrent alternatives
192+
Contact Meta directly or just look around for some torrent alternatives.
78193
79-
**2] How to convert original LLaMA files into supported format?**
194+
**2) How to convert original LLaMA files into supported format?**
80195
81-
Youl'll need original FP16 files placed into **models** directory, then convert with command:
196+
Place original PyTorch FP16 files into **models** directory, then convert with command:
82197
83198
```shell
84199
python3 ./scripts/convert.py ~/models/LLaMA/7B/ 0

assets/images/terminal.png

-5.82 KB
Loading

builds/llama-go-v1.4.0-linux

7.72 MB
Binary file not shown.

builds/llama-go-v1.4.0-macos

8.22 MB
Binary file not shown.

builds/llama-go-v1.4.0.exe

7.99 MB
Binary file not shown.

go.mod

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ module github.com/gotzmann/llama.go
33
go 1.20
44

55
require (
6+
github.com/gofiber/fiber/v2 v2.44.0
7+
github.com/google/uuid v1.3.0
8+
github.com/gotzmann/llama.go/llama v0.0.0-20230412160549-c20730f209a3
69
github.com/gotzmann/llama.go/ml v0.0.0-20230412160549-c20730f209a3
710
github.com/jessevdk/go-flags v1.5.0
811
github.com/mattn/go-colorable v0.1.13
@@ -14,11 +17,20 @@ require (
1417
)
1518

1619
require (
20+
github.com/andybalholm/brotli v1.0.5 // indirect
1721
github.com/felixge/fgprof v0.9.3 // indirect
1822
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
19-
github.com/mattn/go-isatty v0.0.17 // indirect
23+
github.com/klauspost/compress v1.16.3 // indirect
24+
github.com/mattn/go-isatty v0.0.18 // indirect
2025
github.com/mattn/go-runewidth v0.0.14 // indirect
26+
github.com/philhofer/fwd v1.1.2 // indirect
2127
github.com/rivo/uniseg v0.2.0 // indirect
22-
golang.org/x/sys v0.6.0 // indirect
28+
github.com/savsgio/dictpool v0.0.0-20221023140959-7bf2e61cea94 // indirect
29+
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
30+
github.com/tinylib/msgp v1.1.8 // indirect
31+
github.com/valyala/bytebufferpool v1.0.0 // indirect
32+
github.com/valyala/fasthttp v1.45.0 // indirect
33+
github.com/valyala/tcplisten v1.0.0 // indirect
34+
golang.org/x/sys v0.7.0 // indirect
2335
golang.org/x/term v0.6.0 // indirect
2436
)

0 commit comments

Comments
 (0)