-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
300 lines (242 loc) · 10.1 KB
/
Copy pathMakefile
File metadata and controls
300 lines (242 loc) · 10.1 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# Tiny AgenC -- a character-level autoregressive transformer in plain C.
#
# make build ./tiny-agenc
# make check run gradient checks and teaching integration tests
# make check-foundations/data/mat run focused chapter checks
# make check-forward/backward/optimizer run focused math checks
# make check-model run the model contract and full-model math checks
# make check-sampling run controlled temperature and context witnesses
# make smoke run the tiny train, save, load, and sample smoke test
# make check-cli exercise the public CLI and scene-level data split
# make overfit prove the assembled learner can memorize one batch
# make bigram-baseline compare held-out loss with a count model
# make check-book check local links and objective prose rules
# make check-evidence verify the evidence manifest and recorded metrics
# make check-metadata verify release-version metadata stays synchronized
# make check-install stage and exercise the installed program and model
# make check-checkpoint exercise durable-save metadata and failure paths
# make check-sanitizers run core checks under AddressSanitizer and UBSan
# make check-ndebug compile and test the model with assertions disabled
# make corpus clean the committed raw NIGHT GRID corpus without network access
# make generate-corpus extend raw data with Ollama, then clean it
# make validation-data split the cleaned corpus at scene boundaries
# make data fetch and verify the pinned Shakespeare comparison corpus
# make install install the program, model, licenses, and artifact metadata
# make clean remove build products while preserving build/migration
#
# OpenMP is on by default; build single-threaded with `make OPENMP=0`.
# -ffast-math lets the compiler reassociate reductions and apply more
# aggressive floating-point optimizations. The measured tradeoff is
# recorded in book/logs/dev-measurements.md. The portable default does
# not tune for the build host. `make NATIVE=1` adds -march=native; the
# recorded 5,000-step evidence used that setting.
CC ?= cc
OPENMP ?= 1
NATIVE ?= 0
OPTFLAGS ?= -O3 -ffast-math
CFLAGS += -std=c11 $(OPTFLAGS) -Wall -Wextra -Werror
LDLIBS += -lacl -lm
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
DATADIR ?= $(PREFIX)/share
DOCDIR ?= $(DATADIR)/doc/tiny-agenc
MODELDIR ?= $(DATADIR)/tiny-agenc
DESTDIR ?=
ifeq ($(OPENMP),1)
CFLAGS += -fopenmp
else ifeq ($(OPENMP),0)
CFLAGS += -Wno-unknown-pragmas
else
$(error OPENMP must be 0 or 1)
endif
ifeq ($(NATIVE),1)
CFLAGS += -march=native
else ifneq ($(NATIVE),0)
$(error NATIVE must be 0 or 1)
endif
SRC := src/util.c src/rng.c src/tokenizer.c src/dataset.c src/ops.c \
src/param.c src/model.c src/model_parameters.c src/model_memory.c \
src/model_forward.c src/model_backward.c src/model_sampling.c \
src/checkpoint.c
BUILD ?= build/openmp-$(OPENMP)
OBJ := $(SRC:src/%.c=$(BUILD)/%.o)
HEADERS := $(wildcard src/*.h)
BUILD_CONFIG := $(BUILD)/.build-config
INSTALL_DOCS := CHANGELOG.md CITATION.cff EVIDENCE.md EVIDENCE.sha256 \
LICENSE MODEL_CARD.md NOTICE README.md VERSION
# The public target intentionally relinks. Mode-specific directories and
# the configuration stamp below prevent stale objects when the compiler,
# flags, OpenMP mode, or native-code setting changes.
tiny-agenc: $(BUILD)/main.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/gradcheck: $(BUILD)/gradcheck.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/integration: $(BUILD)/integration.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/overfit: $(BUILD)/overfit.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/bigram: $(BUILD)/bigram.o $(BUILD)/util.o $(BUILD)/rng.o
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/sampling: $(BUILD)/sampling.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
$(BUILD)/model-invalid: $(BUILD)/model-invalid.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)
CHECKPOINT_WRAP_FLAGS := \
-Wl,--wrap=acl_cmp \
-Wl,--wrap=acl_set_fd \
-Wl,--wrap=close \
-Wl,--wrap=fchmod \
-Wl,--wrap=fchown \
-Wl,--wrap=fclose \
-Wl,--wrap=fflush \
-Wl,--wrap=fmemopen \
-Wl,--wrap=fsetxattr \
-Wl,--wrap=fsync \
-Wl,--wrap=renameat \
-Wl,--wrap=renameat2
$(BUILD)/checkpoint-failures: $(BUILD)/checkpoint-failures.o $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) $(CHECKPOINT_WRAP_FLAGS) \
-o $@ $^ $(LDLIBS)
build/split-order: scripts/split-order.c src/rng.c src/util.c src/rng.h \
src/util.h $(BUILD_CONFIG)
mkdir -p build
$(CC) $(CPPFLAGS) $(CFLAGS) -Wno-unknown-pragmas -Isrc \
-o $@ scripts/split-order.c src/rng.c src/util.c $(LDLIBS)
$(BUILD)/%.o: src/%.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
$(BUILD)/gradcheck.o: tests/gradcheck.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/integration.o: tests/integration.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/overfit.o: tests/overfit.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/bigram.o: tests/bigram.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/sampling.o: tests/sampling.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/model-invalid.o: tests/model_invalid.c $(HEADERS) $(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD)/checkpoint-failures.o: tests/checkpoint_failures.c $(HEADERS) \
$(BUILD_CONFIG) | $(BUILD)
$(CC) $(CPPFLAGS) $(CFLAGS) -Isrc -c -o $@ $<
$(BUILD):
mkdir -p $@
$(BUILD_CONFIG): FORCE | $(BUILD)
@temporary="[email protected].$$$$"; \
trap 'rm -f -- "$$temporary"' EXIT HUP INT TERM; \
{ \
printf '%s\n' \
"CC=$(CC)" \
"CPPFLAGS=$(CPPFLAGS)" \
"CFLAGS=$(CFLAGS)" \
"LDFLAGS=$(LDFLAGS)" \
"LDLIBS=$(LDLIBS)" \
"OPENMP=$(OPENMP)" \
"NATIVE=$(NATIVE)"; \
$(CC) --version 2>/dev/null | sed -n '1p'; \
} > "$$temporary"; \
if test -r "$@" && cmp -s "$$temporary" "$@"; then \
rm -f -- "$$temporary"; \
else \
mv -f -- "$$temporary" "$@"; \
fi; \
trap - EXIT HUP INT TERM
FORCE:
check-gradient: $(BUILD)/gradcheck
./$(BUILD)/gradcheck
check-integration: $(BUILD)/integration
./$(BUILD)/integration
check: check-gradient check-integration check-checkpoint
check-checkpoint: $(BUILD)/checkpoint-failures
./$(BUILD)/checkpoint-failures
check-foundations: $(BUILD)/integration
./$(BUILD)/integration foundations
check-data: $(BUILD)/integration
./$(BUILD)/integration data
check-mat: $(BUILD)/integration
./$(BUILD)/integration mat
check-forward: $(BUILD)/integration
./$(BUILD)/integration forward
check-parallel: $(BUILD)/integration
./$(BUILD)/integration parallel
check-backward: $(BUILD)/gradcheck
./$(BUILD)/gradcheck backward
check-optimizer: $(BUILD)/gradcheck $(BUILD)/integration
./$(BUILD)/gradcheck optimizer
./$(BUILD)/integration optimizer
check-model: $(BUILD)/gradcheck $(BUILD)/integration
./$(BUILD)/gradcheck model
./$(BUILD)/integration model
check-sampling: $(BUILD)/sampling
./$(BUILD)/sampling
check-cli: tiny-agenc build/split-order
bash scripts/smoke-cli.sh ./tiny-agenc
smoke: check-cli check-sampling $(BUILD)/integration
./$(BUILD)/integration smoke
overfit: $(BUILD)/overfit
./$(BUILD)/overfit
bigram-baseline: validation-data $(BUILD)/bigram
./$(BUILD)/bigram data/cyberpunk.train.txt data/cyberpunk.val.txt
check-book:
bash scripts/check-book.sh
check-labs:
env CC="$(CC)" OPENMP="$(OPENMP)" bash scripts/check-labs.sh
check-evidence: validation-data tiny-agenc $(BUILD)/bigram
bash scripts/check-evidence.sh ./tiny-agenc ./$(BUILD)/bigram
check-metadata:
bash scripts/check-release-metadata.sh
check-install: tiny-agenc tiny-agenc.bin $(INSTALL_DOCS)
@set -eu; \
staging=$$(mktemp -d); \
trap 'rm -rf -- "$$staging"' EXIT HUP INT TERM; \
$(MAKE) --no-print-directory DESTDIR="$$staging" PREFIX=/usr install; \
bash scripts/check-install.sh "$$staging/usr"
check-sanitizers:
$(MAKE) OPENMP=0 NATIVE=0 BUILD=build/sanitizers \
OPTFLAGS="-O1 -g -fno-omit-frame-pointer -fsanitize=address,undefined" \
LDFLAGS="-fsanitize=address,undefined" \
check check-sampling overfit
check-invalid-construction: $(BUILD)/model-invalid
./$(BUILD)/model-invalid
check-ndebug:
$(MAKE) --no-print-directory OPENMP=0 NATIVE=0 BUILD=build/ndebug \
CPPFLAGS="$(CPPFLAGS) -DNDEBUG" \
check-model check-invalid-construction
check-all: check check-parallel check-cli check-sampling overfit \
check-book check-labs check-evidence check-metadata check-ndebug
$(MAKE) --no-print-directory check-install
corpus:
bash scripts/clean-corpus.sh
generate-corpus:
bash scripts/gen-corpus.sh
bash scripts/clean-corpus.sh
validation-data: corpus build/split-order
bash scripts/split-corpus.sh
data:
bash scripts/get-shakespeare.sh
install: tiny-agenc tiny-agenc.bin $(INSTALL_DOCS)
install -d "$(DESTDIR)$(BINDIR)" "$(DESTDIR)$(DOCDIR)" \
"$(DESTDIR)$(MODELDIR)"
install -m 0755 tiny-agenc "$(DESTDIR)$(BINDIR)/tiny-agenc"
install -m 0644 $(INSTALL_DOCS) "$(DESTDIR)$(DOCDIR)"
install -m 0644 tiny-agenc.bin "$(DESTDIR)$(MODELDIR)/tiny-agenc.bin"
clean:
@if test -d build; then \
find build -mindepth 1 -maxdepth 1 ! -name migration \
-exec rm -rf -- {} +; \
fi
rm -f tiny-agenc
.PHONY: tiny-agenc $(BUILD)/gradcheck $(BUILD)/integration $(BUILD)/overfit \
$(BUILD)/bigram $(BUILD)/sampling $(BUILD)/model-invalid \
$(BUILD)/checkpoint-failures \
build/split-order \
check-gradient \
check-integration check-checkpoint check check-foundations check-data check-mat \
check-parallel \
check-forward check-backward check-optimizer check-model \
check-sampling check-cli \
smoke overfit \
bigram-baseline \
check-book check-labs check-evidence check-metadata check-install \
check-sanitizers check-invalid-construction check-ndebug check-all \
corpus generate-corpus validation-data data install clean FORCE