Repository navigation
Expand file tree
/
Copy pathMakefile
More file actions
243 lines (182 loc) · 11.8 KB
/
Copy pathMakefile
File metadata and controls
243 lines (182 loc) · 11.8 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
MODEL ?= model.safetensors
PYTHON ?= python3
NASM ?= nasm
TOKENIZER ?= weights/tokenizer.json
OS_BUNDLE ?= build/lfm-boot.bundle
FONT_PYTHON ?= python3
OS_IMAGE ?= build/lfm-os.iso
.PHONY: all verify verify-threads verify-generation verify-tokenizer verify-chat benchmark benchmark-threads benchmark-generation FORCE
all: build/lfm-next
build:
mkdir -p $@
# Re-evaluate the header when MODEL is changed, including older checkpoints.
build/layout.inc: FORCE layout.py | build
$(PYTHON) layout.py "$(MODEL)" $@.tmp
cmp -s $@.tmp $@ || cp $@.tmp $@
rm -f $@.tmp
build/main.o: main.asm platform_linux.asm kernels.asm attention.asm engine.asm util.asm expmath.asm expmath_data.inc threads.asm tokenizer.asm chat.asm layers.inc frequencies.inc build/layout.inc build/tokenizer.inc
$(NASM) -f elf64 -Ibuild/ main.asm -o $@
build/lfm-next: build/main.o build/tokenizer.bin
$(LD) -static -z noexecstack -o $@ $<
build/logits.o: main.asm platform_linux.asm kernels.asm attention.asm engine.asm util.asm expmath.asm expmath_data.inc threads.asm tokenizer.asm chat.asm layers.inc frequencies.inc build/layout.inc build/tokenizer.inc
$(NASM) -f elf64 -DDUMP_LOGITS -Ibuild/ main.asm -o $@
build/lfm-logits: build/logits.o
$(LD) -static -z noexecstack -o $@ $<
verify: build/lfm-next build/lfm-logits
$(PYTHON) verify.py "$(MODEL)" build/lfm-next build/lfm-logits
build/scalar.o: main.asm platform_linux.asm kernels.asm attention.asm engine.asm util.asm expmath.asm expmath_data.inc threads.asm tokenizer.asm chat.asm layers.inc frequencies.inc build/layout.inc build/tokenizer.inc
$(NASM) -f elf64 -DFORCE_SCALAR -Ibuild/ main.asm -o $@
build/lfm-scalar: build/scalar.o build/tokenizer.bin
$(LD) -static -z noexecstack -o $@ $<
benchmark: build/lfm-next build/lfm-scalar
$(PYTHON) benchmark.py "$(MODEL)" build/lfm-scalar build/lfm-next --output build/benchmark.json
verify-threads: build/lfm-logits
$(PYTHON) verify_threads.py "$(MODEL)" build/lfm-logits
benchmark-threads: build/lfm-next
$(PYTHON) benchmark.py "$(MODEL)" build/lfm-next build/lfm-next --threads 2 3 4 6 8 12 --repeats 5 --output build/thread-benchmark.json
verify-generation: build/lfm-next
$(PYTHON) verify_generation.py "$(MODEL)" build/lfm-next
benchmark-generation: build/lfm-next
$(PYTHON) benchmark_generation.py "$(MODEL)" build/lfm-next --output build/generation-benchmark.json
verify-tokenizer: build/lfm-next
$(PYTHON) verify_tokenizer.py "$(TOKENIZER)" build/lfm-next --checkpoint "$(MODEL)"
verify-chat: build/lfm-next
$(PYTHON) verify_chat.py "$(MODEL)" build/lfm-next
# Grouped targets keep the binary and its offsets from the same tokenizer build.
build/tokenizer.bin build/tokenizer.inc &: build_tokenizer.py $(TOKENIZER) | build
$(PYTHON) build_tokenizer.py "$(TOKENIZER)" build/tokenizer.bin build/tokenizer.inc
.PHONY: evaluate-quality
evaluate-quality: build/lfm-next build/lfm-logits
$(PYTHON) evaluate_quality.py "$(MODEL)"
.PHONY: os os-run os-image verify-os
os: build/lfm-os.elf $(OS_BUNDLE)
os-image: $(OS_IMAGE)
# Boot options baked into the ISOs. OS_TOKENS, OS_CONTEXT and OS_THREADS apply to both images;
# when unset, each model keeps its own defaults.
OS_KEYMAP ?= us
OS_OPTIONS = mode=chat tokens=$(or $(OS_TOKENS),512) context=$(or $(OS_CONTEXT),4096) threads=$(or $(OS_THREADS),6) keymap=$(OS_KEYMAP)
# A stamp rewritten only when the options change rebuilds the image.
$(OS_IMAGE).options: FORCE
@mkdir -p $(dir $@)
@echo '$(OS_OPTIONS)' | cmp -s - $@ || echo '$(OS_OPTIONS)' > $@
$(OS_IMAGE): build/lfm-os.elf $(OS_BUNDLE) $(OS_IMAGE).options os/image.py os/FONT-LICENSE.txt os/CONSOLE-FONTS-LICENSE.txt os/KEYMAPS-LICENSE.txt LICENSE
$(PYTHON) os/image.py build/lfm-os.elf "$(OS_BUNDLE)" "$(OS_IMAGE)" --title "Pithos CORE LFM2.5-350M" --subtitle "LFM2.5-350M · BF16" \
--license os/CONSOLE-FONTS-LICENSE.txt --license os/KEYMAPS-LICENSE.txt --license LICENSE \
--options "$(OS_OPTIONS)"
os-run: os
$(PYTHON) os/run.py "$(OS_BUNDLE)"
verify-os: os build/lfm-os-logits.elf build/lfm-next build/lfm-logits build/lfm-scalar
$(PYTHON) os/verify.py "$(MODEL)" "$(OS_BUNDLE)"
.PHONY: verify-platform
verify-platform: os $(OS_IMAGE) build/lfm-os-logits.elf build/lfm-next build/lfm-logits
$(PYTHON) os/verify_platform.py "$(MODEL)" "$(OS_BUNDLE)" "$(OS_IMAGE)"
.PHONY: benchmark-os
benchmark-os: os
$(PYTHON) os/benchmark.py "$(OS_BUNDLE)"
$(OS_BUNDLE) build/boot_bundle.inc &: os/bundle.py $(MODEL) build/tokenizer.bin build/layout.inc | build
$(PYTHON) os/bundle.py "$(MODEL)" build/tokenizer.bin "$(OS_BUNDLE)" build/boot_bundle.inc
build/os-boot.o: os/boot.asm | build
$(NASM) -f elf64 os/boot.asm -o $@
build/console-font.bin: os/font.py | build
$(FONT_PYTHON) os/font.py $@
build/os-main.o: main.asm os/platform.asm os/multiboot.asm os/smp.asm os/ui.asm os/keymaps.inc kernels.asm attention.asm engine.asm util.asm expmath.asm expmath_data.inc tokenizer.asm chat.asm layers.inc frequencies.inc build/layout.inc build/tokenizer.inc build/boot_bundle.inc build/pithos-font.bin
$(NASM) -f elf64 -DBARE_METAL -Ibuild/ main.asm -o $@
build/lfm-os.elf64: build/os-boot.o build/os-main.o os/linker.ld
$(LD) -m elf_x86_64 -T os/linker.ld -z max-page-size=0x1000 -o $@ build/os-boot.o build/os-main.o
build/lfm-os.elf: build/lfm-os.elf64
objcopy -O elf32-i386 $< $@
build/os-logits.o: main.asm os/platform.asm os/multiboot.asm os/smp.asm os/ui.asm os/keymaps.inc kernels.asm attention.asm engine.asm util.asm expmath.asm expmath_data.inc tokenizer.asm chat.asm layers.inc frequencies.inc build/layout.inc build/tokenizer.inc build/boot_bundle.inc build/pithos-font.bin
$(NASM) -f elf64 -DBARE_METAL -DDUMP_LOGITS -Ibuild/ main.asm -o $@
build/lfm-os-logits.elf64: build/os-boot.o build/os-logits.o os/linker.ld
$(LD) -m elf_x86_64 -T os/linker.ld -z max-page-size=0x1000 -o $@ build/os-boot.o build/os-logits.o
build/lfm-os-logits.elf: build/lfm-os-logits.elf64
objcopy -O elf32-i386 $< $@
# LFM3-1.7B GGUF runtime. The tokenizer tables sit beside the executable.
LFM3_MODEL ?= LFM3-1.7B-Q4_K_M.gguf
LFM3_TOKENIZER ?= lfm3-ref/tokenizer.json
LFM3_SOURCES = lfm3/main.asm lfm3/kernels.asm engine.asm server.asm lfm3/frequencies.inc platform_linux.asm threads.asm \
tokenizer.asm chat.asm util.asm expmath.asm expmath_data.inc \
build/lfm3/layout.inc build/lfm3/lfm3-layers.inc build/lfm3/tokenizer.inc
.PHONY: lfm3
lfm3: build/lfm3/lfm3-next
build/lfm3:
mkdir -p $@
build/lfm3/layout.inc build/lfm3/lfm3-layers.inc &: FORCE lfm3/layout.py | build/lfm3
$(PYTHON) lfm3/layout.py "$(LFM3_MODEL)" build/lfm3/layout.inc.tmp build/lfm3/lfm3-layers.inc.tmp
cmp -s build/lfm3/layout.inc.tmp build/lfm3/layout.inc || cp build/lfm3/layout.inc.tmp build/lfm3/layout.inc
cmp -s build/lfm3/lfm3-layers.inc.tmp build/lfm3/lfm3-layers.inc || cp build/lfm3/lfm3-layers.inc.tmp build/lfm3/lfm3-layers.inc
rm -f build/lfm3/layout.inc.tmp build/lfm3/lfm3-layers.inc.tmp
build/lfm3/tokenizer.bin build/lfm3/tokenizer.inc &: build_tokenizer.py $(LFM3_TOKENIZER) | build/lfm3
$(PYTHON) build_tokenizer.py "$(LFM3_TOKENIZER)" build/lfm3/tokenizer.bin build/lfm3/tokenizer.inc --vocab 128000 --hash-bits 19
build/lfm3/main.o: $(LFM3_SOURCES)
$(NASM) -f elf64 -Ibuild/lfm3/ lfm3/main.asm -o $@
build/lfm3/lfm3-next: build/lfm3/main.o build/lfm3/tokenizer.bin
$(LD) -static -z noexecstack -o $@ $<
build/lfm3/logits.o: $(LFM3_SOURCES)
$(NASM) -f elf64 -DDUMP_LOGITS -Ibuild/lfm3/ lfm3/main.asm -o $@
build/lfm3/lfm3-logits: build/lfm3/logits.o build/lfm3/tokenizer.bin
$(LD) -static -z noexecstack -o $@ $<
# Diagnostic build: the embedding and every normalized block output, then the logits.
build/lfm3/layers.o: $(LFM3_SOURCES)
$(NASM) -f elf64 -DDUMP_LAYERS -DDUMP_LOGITS -Ibuild/lfm3/ lfm3/main.asm -o $@
build/lfm3/lfm3-layers: build/lfm3/layers.o build/lfm3/tokenizer.bin
$(LD) -static -z noexecstack -o $@ $<
LFM3_REF ?= lfm3-ref
.PHONY: verify-lfm3 verify-lfm3-tokenizer
verify-lfm3: build/lfm3/lfm3-next build/lfm3/lfm3-logits build/lfm3/lfm3-layers
$(PYTHON) lfm3/verify.py "$(LFM3_MODEL)" "$(LFM3_REF)" build/lfm3/lfm3-next build/lfm3/lfm3-logits build/lfm3/lfm3-layers
verify-lfm3-tokenizer: build/lfm3/lfm3-next
$(PYTHON) lfm3/verify_tokenizer.py "$(LFM3_TOKENIZER)" build/lfm3/lfm3-next
# Pithos CORE: the LFM3 runtime as a standalone Multiboot kernel, with the GGUF
# and tokenizer tables in one boot module.
LFM3_BUNDLE ?= build/lfm3/lfm3-boot.bundle
LFM3_OS_IMAGE ?= build/lfm3/pithos-core.iso
LFM3_OS_SOURCES = $(LFM3_SOURCES) os/platform.asm os/multiboot.asm os/smp.asm os/ui.asm os/keymaps.inc \
build/lfm3/boot_bundle.inc build/pithos-font.bin
# Console glyphs (DejaVu, GNU Unifont, Noto Color Emoji) and the Pithos mark.
build/pithos-font.bin: os/ui_font.py os/assets/pithos-mark.png | build
$(FONT_PYTHON) os/ui_font.py os/assets/pithos-mark.png $@
# Regenerating the layouts needs ckbcomp (console-setup) and the XKB data.
.PHONY: keymaps
keymaps:
$(FONT_PYTHON) os/keymaps.py os/keymaps.inc
.PHONY: lfm3-os lfm3-os-image verify-lfm3-os
lfm3-os: build/lfm3/lfm3-os.elf $(LFM3_BUNDLE)
lfm3-os-image: $(LFM3_OS_IMAGE)
$(LFM3_BUNDLE) build/lfm3/boot_bundle.inc &: os/bundle.py $(LFM3_MODEL) build/lfm3/tokenizer.bin build/lfm3/layout.inc | build/lfm3
$(PYTHON) os/bundle.py "$(LFM3_MODEL)" build/lfm3/tokenizer.bin "$(LFM3_BUNDLE)" build/lfm3/boot_bundle.inc
build/lfm3/os-main.o: $(LFM3_OS_SOURCES)
$(NASM) -f elf64 -DBARE_METAL -Ibuild/lfm3/ -Ibuild/ lfm3/main.asm -o $@
build/lfm3/os-logits.o: $(LFM3_OS_SOURCES)
$(NASM) -f elf64 -DBARE_METAL -DDUMP_LOGITS -Ibuild/lfm3/ -Ibuild/ lfm3/main.asm -o $@
build/lfm3/lfm3-os.elf64: build/os-boot.o build/lfm3/os-main.o os/linker.ld
$(LD) -m elf_x86_64 -T os/linker.ld -z max-page-size=0x1000 -o $@ build/os-boot.o build/lfm3/os-main.o
build/lfm3/lfm3-os-logits.elf64: build/os-boot.o build/lfm3/os-logits.o os/linker.ld
$(LD) -m elf_x86_64 -T os/linker.ld -z max-page-size=0x1000 -o $@ build/os-boot.o build/lfm3/os-logits.o
build/lfm3/%.elf: build/lfm3/%.elf64
objcopy -O elf32-i386 $< $@
LFM3_KEYMAP ?= $(OS_KEYMAP)
LFM3_OS_OPTIONS = mode=chat tokens=$(or $(OS_TOKENS),1024) context=$(or $(OS_CONTEXT),8192) threads=$(or $(OS_THREADS),6) keymap=$(LFM3_KEYMAP)
# The API server entry: the network kernel, entered under UEFI with the
# firmware's boot services kept, so its Ethernet driver carries the API.
# OS_PORT, OS_APIKEY and OS_IP (a static address instead of DHCP) are optional;
# OS_MENU_TIMEOUT is how long the boot menu waits before the chat starts.
LFM3_SERVER_OPTIONS = $(LFM3_OS_OPTIONS) port=$(or $(OS_PORT),8080)$(if $(OS_APIKEY), apikey=$(OS_APIKEY))$(if $(OS_IP), ip=$(OS_IP))
LFM3_MENU_TIMEOUT = $(or $(OS_MENU_TIMEOUT),10)
LFM3_IMAGE_STAMP = $(LFM3_OS_OPTIONS) | $(LFM3_SERVER_OPTIONS) | $(LFM3_MENU_TIMEOUT)
$(LFM3_OS_IMAGE).options: FORCE
@mkdir -p $(dir $@)
@echo '$(LFM3_IMAGE_STAMP)' | cmp -s - $@ || echo '$(LFM3_IMAGE_STAMP)' > $@
$(LFM3_OS_IMAGE): build/lfm3/lfm3-os.elf build/lfm3/lfm3-os-net.elf $(LFM3_BUNDLE) $(LFM3_OS_IMAGE).options os/image.py os/FONT-LICENSE.txt os/CONSOLE-FONTS-LICENSE.txt os/KEYMAPS-LICENSE.txt LICENSE
$(PYTHON) os/image.py build/lfm3/lfm3-os.elf "$(LFM3_BUNDLE)" "$(LFM3_OS_IMAGE)" --title "Pithos CORE - chat" \
--license os/CONSOLE-FONTS-LICENSE.txt --license os/KEYMAPS-LICENSE.txt --license LICENSE \
--options "$(LFM3_OS_OPTIONS)" --server-kernel build/lfm3/lfm3-os-net.elf \
--server-options "$(LFM3_SERVER_OPTIONS)" --timeout $(LFM3_MENU_TIMEOUT)
build/os-boot-net.o: os/boot.asm | build
$(NASM) -f elf64 -DEFI_NET os/boot.asm -o $@
build/lfm3/os-net-main.o: $(LFM3_OS_SOURCES) server.asm os/efi.asm os/net.asm
$(NASM) -f elf64 -DBARE_METAL -DEFI_NET -Ibuild/lfm3/ -Ibuild/ lfm3/main.asm -o $@
build/lfm3/lfm3-os-net.elf64: build/os-boot-net.o build/lfm3/os-net-main.o os/linker.ld
$(LD) -m elf_x86_64 -T os/linker.ld -z max-page-size=0x1000 -o $@ build/os-boot-net.o build/lfm3/os-net-main.o
verify-lfm3-os: lfm3-os build/lfm3/lfm3-os-logits.elf build/lfm3/lfm3-next build/lfm3/lfm3-logits
$(PYTHON) lfm3/verify_os.py "$(LFM3_MODEL)" "$(LFM3_BUNDLE)"