forked from quantumaikr/quant.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
163 lines (138 loc) · 4.69 KB
/
Makefile
File metadata and controls
163 lines (138 loc) · 4.69 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
# TurboQuant.cpp — Standalone Makefile (no CMake needed)
#
# Usage:
# make # build quant + tq_convert
# make quant # inference tool only
# make test # build and run tests (requires Google Test)
# make clean # remove build artifacts
#
# Cross-platform:
# Linux/gcc: make CC=gcc
# macOS/clang: make (auto-detects Apple Silicon)
# macOS+Metal: make METAL=1 (enables Metal GPU backend)
# Windows/mingw: make CC=x86_64-w64-mingw32-gcc TARGET=quant.exe
#
# Options:
# DEBUG=1 — debug build (-g -O0 -fsanitize=address)
# METAL=1 — enable Metal GPU backend (macOS only)
# NEON=1 — force NEON (auto-detected on arm64)
# AVX2=1 — force AVX2 (auto-detected on x86_64)
CC ?= cc
AR ?= ar
CFLAGS ?= -std=c11 -Wall -Wextra -Wpedantic -Wno-unused-parameter
LDFLAGS ?= -lm -lpthread
# Optimization
ifdef DEBUG
CFLAGS += -g -O0 -fsanitize=address -fsanitize=undefined
LDFLAGS += -fsanitize=address -fsanitize=undefined
else
CFLAGS += -O2
endif
# Include paths
CFLAGS += -Iinclude
# Auto-detect architecture
UNAME_M := $(shell uname -m)
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_M),arm64)
CFLAGS += -mcpu=native
NEON ?= 1
endif
ifeq ($(UNAME_M),aarch64)
CFLAGS += -mcpu=native
NEON ?= 1
endif
ifeq ($(UNAME_M),x86_64)
CFLAGS += -march=native
AVX2 ?= 1
endif
# Apple Accelerate framework (macOS)
ifeq ($(UNAME_S),Darwin)
LDFLAGS += -framework Accelerate
CFLAGS += -DTQ_HAS_ACCELERATE=1 -DACCELERATE_NEW_LAPACK=1
endif
# ============================================================
# Source files
# ============================================================
SRC_CORE := $(wildcard src/core/*.c)
SRC_CACHE := $(wildcard src/cache/*.c)
SRC_CPU := $(wildcard src/backend/cpu/*.c)
SRC_ENGINE := $(wildcard src/engine/*.c)
SRC_LIB := $(SRC_CORE) $(SRC_CACHE) $(SRC_CPU) $(SRC_ENGINE)
OBJ_LIB := $(SRC_LIB:.c=.o)
# Metal backend (macOS only, optional)
ifdef METAL
ifeq ($(UNAME_S),Darwin)
SRC_METAL_OBJ := $(wildcard src/backend/metal/*.m)
OBJ_METAL := $(SRC_METAL_OBJ:.m=.o)
OBJ_LIB += $(OBJ_METAL)
CFLAGS += -DTQ_HAS_METAL=1
LDFLAGS += -framework Metal -framework Foundation
OBJCFLAGS = $(CFLAGS) -fobjc-arc
endif
endif
# ============================================================
# Targets
# ============================================================
.PHONY: all clean test
all: quant tq_convert
# Static library
libturboquant.a: $(OBJ_LIB)
$(AR) rcs $@ $^
# Main tools
quant: tools/quant.c libturboquant.a
$(CC) $(CFLAGS) -o $@ $< -L. -lturboquant $(LDFLAGS)
tq_convert: tools/tq_convert.c libturboquant.a
$(CC) $(CFLAGS) -o $@ $< -L. -lturboquant $(LDFLAGS)
# ============================================================
# Compile rules
# ============================================================
%.o: %.c
$(CC) $(CFLAGS) -c -o $@ $<
# Objective-C (Metal dispatch)
%.o: %.m
$(CC) $(OBJCFLAGS) -c -o $@ $<
# ============================================================
# Test (lightweight — no Google Test dependency)
# ============================================================
test: quant
@echo "=== Quick sanity test ==="
@echo "Building..."
@echo "Running quant --info on test..."
@if [ -f model.tqm ]; then \
./quant model.tqm --info && echo "PASS: model loads" || echo "FAIL"; \
else \
echo "SKIP: no model.tqm found (download a model first)"; \
fi
@echo "=== Done ==="
# ============================================================
# Clean
# ============================================================
clean:
rm -f $(OBJ_LIB) $(OBJ_METAL) libturboquant.a quant tq_convert
rm -f src/**/*.o
# ============================================================
# Help
# ============================================================
help:
@echo "TurboQuant.cpp Makefile"
@echo ""
@echo "Targets:"
@echo " make Build quant + tq_convert"
@echo " make quant Build inference tool only"
@echo " make clean Remove build artifacts"
@echo " make test Quick sanity test"
@echo " make help Show this help"
@echo ""
@echo "Options:"
@echo " CC=gcc Use gcc instead of default cc"
@echo " DEBUG=1 Debug build with ASan"
@echo " METAL=1 Enable Metal GPU (macOS only)"
@echo " AVX2=1 Force AVX2 (x86_64)"
@echo " NEON=1 Force NEON (arm64)"
@echo ""
@echo "Examples:"
@echo " make # default build"
@echo " make CC=gcc # Linux gcc build"
@echo " make METAL=1 # macOS with Metal GPU"
@echo " make DEBUG=1 # debug + sanitizers"
@echo " make CC=x86_64-w64-mingw32-gcc # cross-compile for Windows"