-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
56 lines (44 loc) · 1.54 KB
/
Copy pathMakefile
File metadata and controls
56 lines (44 loc) · 1.54 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
# learn-llm —— 纯 C 实现的 Qwen3-0.6B 推理
#
# make 编译 ./qwen3
# make ref 用 PyTorch 导出对拍所需的参考数值
# make test 编译并运行四组对拍测试(需先 make ref)
# make clean 清掉全部生成物
# make BACKEND=gcd|naive 换 matmul 后端
CC := cc
CFLAGS := -std=c11 -O2 -g -Wall -Wextra -Wshadow -Wconversion -Wno-sign-conversion
LDFLAGS := -lm
# matmul 后端: accelerate(Apple BLAS)| gcd(GCD 多线程)| naive(可移植的朴素循环)
BACKEND ?= accelerate
ifeq ($(BACKEND),accelerate)
CFLAGS += -DUSE_ACCELERATE -DACCELERATE_NEW_LAPACK
LDFLAGS += -framework Accelerate
else ifeq ($(BACKEND),gcd)
CFLAGS += -DUSE_GCD
endif
MODEL ?= ../models/Qwen3-0.6B
PYTHON ?= python
# 除 main.c 外的所有源文件,主程序和测试共用
LIB_SRC := $(filter-out src/main.c,$(wildcard src/*.c))
LIB_OBJ := $(LIB_SRC:.c=.o)
TESTS := test_ops test_layer test_forward test_tokenizer
.PHONY: all test ref clean
all: qwen3
qwen3: $(LIB_OBJ) src/main.o
$(CC) $^ -o $@ $(LDFLAGS)
$(TESTS): %: $(LIB_OBJ) tests/%.o
$(CC) $^ -o $@ $(LDFLAGS)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
ref:
@for c in ops layer tok forward; do $(PYTHON) tests/dump_ref.py $$c || exit 1; done
test: $(TESTS)
@./test_ops $(MODEL)
@echo
@./test_layer $(MODEL)
@echo
@./test_forward $(MODEL)
@echo
@./test_tokenizer $(MODEL) ref/tok_cases.txt
clean:
rm -rf $(LIB_OBJ) src/main.o tests/*.o qwen3 $(TESTS) ref __pycache__ tests/__pycache__