-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
232 lines (213 loc) · 7 KB
/
Copy pathMakefile
File metadata and controls
232 lines (213 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
DOCKER_API_TAG := notgnoshi/api
DOCKER_RESEARCH_TAG := notgnoshi/research
DOCKER_BUILD_TRIGGER := .docker-build-trigger
REPO_INIT_TRIGGER := .repo-init-trigger
JUPYTER_PORT := 8888
# Use bash shell so that we use the `time` builtin rather than /usr/bin/time.
SHELL := bash
# The repository root directory inside the docker container
WORKSPACE := /workspaces/research
# The model configuration file. The path must be relative to the repository root.
CONFIG := data/models/gpt2/gpt2.jsonc
## View this help message
.PHONY: help
help:
@# Taken from https://gist.github.com/prwhite/8168133#gistcomment-2749866
@awk '{ \
if ($$0 ~ /^.PHONY: [a-zA-Z\-\_0-9]+$$/) { \
helpCommand = substr($$0, index($$0, ":") + 2); \
if (helpMessage) { \
printf "\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
} else if ($$0 ~ /^[a-zA-Z\-\_0-9.]+:/) { \
helpCommand = substr($$0, 0, index($$0, ":")); \
if (helpMessage) { \
printf "\033[36m%-20s\033[0m %s\n", \
helpCommand, helpMessage; \
helpMessage = ""; \
} \
# Handle multi-line comments \
} else if ($$0 ~ /^##/) { \
if (helpMessage) { \
helpMessage = helpMessage"\n "substr($$0, 3); \
} else { \
helpMessage = substr($$0, 3); \
} \
# Handle section headings.\
} else { \
if (helpMessage) { \
# Remove leading space \
helpMessage = substr(helpMessage, 2); \
print "\n"helpMessage \
} \
helpMessage = ""; \
} \
}' \
$(MAKEFILE_LIST)
## Build the docker image necessary to run the deliverables
## Build the docker image for this repository. Takes about 3-4 CPU years and more than a few spare bytes.
.PHONY: docker-build
docker-build: $(DOCKER_BUILD_TRIGGER)
$(DOCKER_BUILD_TRIGGER): Dockerfile Dockerfile-api
docker build --tag $(DOCKER_API_TAG) --file=Dockerfile-api .
time docker build --tag $(DOCKER_RESEARCH_TAG) .
touch $(DOCKER_BUILD_TRIGGER)
## Force a container rebuild. Docker can still choose to cache as many things as it wants. That's fine.
.PHONY: docker-rebuild
docker-rebuild:
rm -f $(DOCKER_BUILD_TRIGGER)
$(MAKE) docker-build
## Run the project deliverables
## Train and serialize the default Markov LM.
## Define CONFIG to specify which model to use.
## E.g., make CONFIG=data/models/markov-character.jsonc
.PHONY: train
train: $(REPO_INIT_TRIGGER)
time docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
$(WORKSPACE)/haikulib/scripts/haiku.py --train --config=$(WORKSPACE)/$(CONFIG)
## Generate haiku using the deserialized default Markov LM.
## Define CONFIG to specify which model to use.
## E.g., make CONFIG=data/models/markov-character.jsonc
.PHONY: generate
generate: $(REPO_INIT_TRIGGER)
time docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
$(WORKSPACE)/haikulib/scripts/haiku.py --generate --config=$(WORKSPACE)/$(CONFIG)
## Utilities
## Initialize the repository data
.PHONY: init-data
init-data: $(REPO_INIT_TRIGGER)
$(REPO_INIT_TRIGGER): $(DOCKER_BUILD_TRIGGER)
$(REPO_INIT_TRIGGER): haikulib/scripts/initialize.py
$(REPO_INIT_TRIGGER): haikulib/data/initialization.py
time docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
$(WORKSPACE)/haikulib/scripts/initialize.py
touch $(REPO_INIT_TRIGGER)
## Run Jupyter Lab from the Docker image.
## Uses actual witchcraft to open Jupyter Lab in a webbrowser.
.PHONY: jupyter
jupyter: $(REPO_INIT_TRIGGER)
docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--publish $(JUPYTER_PORT):$(JUPYTER_PORT) \
$(DOCKER_RESEARCH_TAG) \
jupyter lab --ip=0.0.0.0 --no-browser --port=$(JUPYTER_PORT) $(WORKSPACE) 2>&1 \
| tee --output-error=warn /dev/tty \
| grep --only-matching --max-count=1 "http://127\.0\.0\.1:$(JUPYTER_PORT)/?token=[0-9a-f]*" \
| xargs xdg-open
## Run unit tests.
.PHONY: check
check: $(REPO_INIT_TRIGGER)
docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
pytest
## Use the transformers example script to fine-tune GPT-2 on haiku.
.PHONY: gpt2-train
gpt2-train: $(REPO_INIT_TRIGGER)
cut -d , -f2 data/haiku.csv | tail -n +2 | sed 's|^\(.*\)$$|^ \1 $$|g' | shuf > data/raw.txt
head -n -5000 data/raw.txt > data/train.txt
tail -n 5000 data/raw.txt > data/eval.txt
docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
python3 $(WORKSPACE)/haikulib/scripts/run_language_modeling.py \
--overwrite_output_dir \
--cache_dir=$(WORKSPACE)/data/cache \
--output_dir=$(WORKSPACE)/data/models/gpt2-orig \
--model_type=gpt2 \
--model_name_or_path=gpt2 \
--line_by_line \
--seed=$(shell echo $$RANDOM) \
--no_cuda \
--do_train \
--train_data_file=$(WORKSPACE)/data/train.txt \
--do_eval \
--eval_data_file=$(WORKSPACE)/data/eval.txt
## Use the transformers example scripts to generate text using GPT-2.
.PHONY: gpt2-generate
gpt2-generate:
docker run \
--user $(shell id -u):$(shell id -g) \
--rm \
--interactive \
--tty \
--mount "type=bind,source=$(shell pwd),target=$(WORKSPACE)" \
--workdir=$(WORKSPACE) \
$(DOCKER_RESEARCH_TAG) \
python3 $(WORKSPACE)/haikulib/scripts/run_generation.py \
--model_type=gpt2 \
--model_name_or_path=$(WORKSPACE)/data/models/gpt2-orig \
--prompt="^ i" \
--stop_token="$$" \
--seed=$(shell echo $$RANDOM) \
--no_cuda \
--num_return_sequences=20
## Run the REST API container in the foreground, with live reloading on source changes.
## With jwilder/nginx-proxy running, visit http://api.localhost to access.
## Otherwise, replace '--expose 80' with '--publish 8080:80' and visit http://localhost:8080
.PHONY: api-dev
api-dev: $(REPO_INIT_TRIGGER)
docker run \
--interactive \
--tty \
--name research-api \
--rm \
--mount "type=bind,source=$(shell pwd),target=/app" \
--workdir=/app \
--expose 80 \
--env HTTPS_METHOD=nohttps \
--env VIRTUAL_HOST=api.localhost \
$(DOCKER_API_TAG) /start-reload.sh
## Run the REST API container in the background.
.PHONY: api
api: $(REPO_INIT_TRIGGER)
docker run \
--interactive \
--tty \
--detach \
--restart unless-stopped \
--name research-api \
--mount "type=bind,source=$(shell pwd),target=/app" \
--workdir=/app \
--expose 80 \
--env WEB_CONCURRENCY=1 \
--env HTTPS_METHOD=nohttps \
--env VIRTUAL_HOST=api.localhost \
$(DOCKER_API_TAG)