forked from llm-d/llm-d-kv-cache
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMakefile
More file actions
700 lines (597 loc) · 28.5 KB
/
Makefile
File metadata and controls
700 lines (597 loc) · 28.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
SHELL := /usr/bin/env bash
# Defaults
PROJECT_NAME ?= llm-d-kv-cache
DEV_VERSION ?= 0.0.1
PROD_VERSION ?= 0.0.0
IMAGE_TAG_BASE ?= ghcr.io/llm-d/$(PROJECT_NAME)
IMG = $(IMAGE_TAG_BASE):$(DEV_VERSION)
NAMESPACE ?= hc4ai-operator
VLLM_VERSION := 0.14.0
TARGETOS ?= $(shell go env GOOS)
TARGETARCH ?= $(shell go env GOARCH)
UNAME_S := $(shell uname -s)
TOOLS_DIR := $(shell pwd)/hack/tools
CONTAINER_TOOL := $(shell { command -v docker >/dev/null 2>&1 && echo docker; } || { command -v podman >/dev/null 2>&1 && echo podman; } || echo "")
BUILDER := $(shell command -v buildah >/dev/null 2>&1 && echo buildah || echo $(CONTAINER_TOOL))
UDS_TOKENIZER_IMAGE ?= llm-d-uds-tokenizer:e2e-test
# go source files
SRC = $(shell find . -type f -name '*.go')
.PHONY: help
help: ## Print help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Python Configuration
PYTHON_VERSION := 3.12
VENV_DIR := $(shell pwd)/build/venv
VENV_BIN := $(VENV_DIR)/bin
# Attempt to find Python 3.9 executable.
PYTHON_EXE := $(shell command -v python$(PYTHON_VERSION) || command -v python3)
# Unified Python configuration detection. This block runs once.
# It prioritizes python-config, then pkg-config, for reliability.
ifeq ($(UNAME_S),Darwin)
# macOS: Find Homebrew's python-config script for the most reliable flags.
BREW_PREFIX := $(shell command -v brew >/dev/null 2>&1 && brew --prefix python@$(PYTHON_VERSION) 2>/dev/null)
PYTHON_CONFIG := $(BREW_PREFIX)/bin/python$(PYTHON_VERSION)-config
ifneq ($(shell $(PYTHON_CONFIG) --cflags 2>/dev/null),)
PYTHON_CFLAGS := $(shell $(PYTHON_CONFIG) --cflags)
# Use --ldflags --embed to get all necessary flags for linking
PYTHON_LDFLAGS := $(shell $(PYTHON_CONFIG) --ldflags --embed)
PYTHON_LIBS :=
else
$(error "Could not execute 'python$(PYTHON_VERSION)-config' from Homebrew. Please ensure Python is installed correctly with: 'brew install python@$(PYTHON_VERSION)'")
endif
else ifeq ($(UNAME_S),Linux)
# Linux: Use standard system tools to find flags.
PYTHON_CONFIG := $(shell command -v python$(PYTHON_VERSION)-config || command -v python3-config)
ifneq ($(shell $(PYTHON_CONFIG) --cflags 2>/dev/null),)
# Use python-config if available and correct
PYTHON_CFLAGS := $(shell $(PYTHON_CONFIG) --cflags)
PYTHON_LDFLAGS := $(shell $(PYTHON_CONFIG) --ldflags --embed)
PYTHON_LIBS :=
else ifneq ($(shell pkg-config --cflags python-$(PYTHON_VERSION) 2>/dev/null),)
# Fallback to pkg-config
PYTHON_CFLAGS := $(shell pkg-config --cflags python-$(PYTHON_VERSION))
PYTHON_LDFLAGS := $(shell pkg-config --libs python-$(PYTHON_VERSION))
PYTHON_LIBS :=
else
$(error "Python $(PYTHON_VERSION) development headers not found. Please install with: 'sudo apt install python$(PYTHON_VERSION)-dev' or 'sudo dnf install python$(PYTHON_VERSION)-devel'")
endif
else
$(error "Unsupported OS: $(UNAME_S)")
endif
# Final CGO flags with all dependencies
CGO_CFLAGS_FINAL := $(PYTHON_CFLAGS)
CGO_LDFLAGS_FINAL := $(PYTHON_LDFLAGS) $(PYTHON_LIBS) -ldl -lm
.PHONY: detect-python
detect-python: ## Detects Python and prints the configuration.
@printf "\033[33;1m==== Python Configuration ====\033[0m\n"
@if [ -z "$(PYTHON_EXE)" ]; then \
echo "ERROR: Python 3 not found in PATH."; \
exit 1; \
fi
@# Verify the version of the found python executable using its exit code
@if ! $(PYTHON_EXE) -c "import sys; sys.exit(0 if sys.version_info[:2] == ($(shell echo $(PYTHON_VERSION) | cut -d. -f1), $(shell echo $(PYTHON_VERSION) | cut -d. -f2)) else 1)"; then \
echo "ERROR: Found Python at '$(PYTHON_EXE)' but it is not version $(PYTHON_VERSION)."; \
echo "Please ensure 'python$(PYTHON_VERSION)' or a compatible 'python3' is in your PATH."; \
exit 1; \
fi
@echo "Python executable: $(PYTHON_EXE) ($$($(PYTHON_EXE) --version))"
@echo "Python CFLAGS: $(PYTHON_CFLAGS)"
@echo "Python LDFLAGS: $(PYTHON_LDFLAGS)"
@if [ -z "$(PYTHON_CFLAGS)" ]; then \
echo "ERROR: Python development headers not found. See installation instructions above."; \
exit 1; \
fi
@printf "\033[33;1m==============================\033[0m\n"
.PHONY: setup-venv
setup-venv: detect-python ## Sets up the Python virtual environment.
@printf "\033[33;1m==== Setting up Python virtual environment in $(VENV_DIR) ====\033[0m\n"
@if [ ! -f "$(VENV_BIN)/python" ]; then \
echo "Creating virtual environment..."; \
$(PYTHON_EXE) -m venv $(VENV_DIR) || { \
echo "ERROR: Failed to create virtual environment."; \
echo "Your Python installation may be missing the 'venv' module."; \
echo "Try: 'sudo apt install python$(PYTHON_VERSION)-venv' or 'sudo dnf install python$(PYTHON_VERSION)-devel'"; \
exit 1; \
}; \
fi
@echo "Upgrading pip..."
@$(VENV_BIN)/pip install --upgrade pip
@echo "Python virtual environment setup complete."
.PHONY: install-python-deps
install-python-deps: setup-venv ## installs dependencies.
@printf "\033[33;1m==== Setting up Python virtual environment in $(VENV_DIR) ====\033[0m\n"
@if [ ! -f "$(VENV_BIN)/python" ]; then \
echo "ERROR: Virtual environment not found. Run 'make setup-venv' first."; \
exit 1; \
fi
@if $(VENV_BIN)/python -c "import vllm" 2>/dev/null; then \
echo "vllm is already installed, skipping..."; \
exit 0; \
fi; \
echo "Installing vllm..."; \
if [ "$(TARGETOS)" = "linux" ]; then \
if [ "$(TARGETARCH)" = "amd64" ]; then \
echo "Installing vLLM pre-built wheel for x86_64..."; \
$(VENV_BIN)/pip install https://github.com/vllm-project/vllm/releases/download/v${VLLM_VERSION}/vllm-${VLLM_VERSION}+cpu-cp38-abi3-manylinux_2_35_x86_64.whl --extra-index-url https://download.pytorch.org/whl/cpu; \
elif [ "$(TARGETARCH)" = "arm64" ]; then \
echo "Installing vLLM pre-built wheel for aarch64..."; \
$(VENV_BIN)/pip install https://github.com/vllm-project/vllm/releases/download/v${VLLM_VERSION}/vllm-${VLLM_VERSION}+cpu-cp38-abi3-manylinux_2_35_aarch64.whl; \
else \
echo "Unsupported Linux architecture: $(TARGETARCH). Falling back to setup.sh..."; \
PATH=$(VENV_BIN):$$PATH ./pkg/preprocessing/chat_completions/setup.sh; \
fi; \
elif [ "$(TARGETOS)" = "darwin" ]; then \
echo "Building vLLM from source for macOS (pre-built wheels not available)..."; \
PATH=$(VENV_BIN):$$PATH ./pkg/preprocessing/chat_completions/setup.sh; \
else \
echo "Unsupported OS: $(TARGETOS)"; \
exit 1; \
fi; \
echo "Verifying vllm installation..."; \
$(VENV_BIN)/python -c "import vllm; print('✅ vllm version ' + vllm.__version__ + ' installed.')" || { \
echo "ERROR: vllm library not properly installed in venv."; \
exit 1; \
}
.PHONY: install-hf-cli
install-hf-cli:
@if command -v hf >/dev/null 2>&1; then \
echo "✅ HuggingFace CLI is already installed."; \
else \
echo "Installing HuggingFace CLI..."; \
curl -LsSf https://hf.co/cli/install.sh | bash; \
echo "✅ HuggingFace CLI installed."; \
fi
.PHONY: download-local-llama3
download-local-llama3: install-hf-cli
hf download --exclude "*safetensors" \
--local-dir ./tests/e2e/redis_mock/testdata/local-llama3 \
--revision "c5c6b5700a4178ef1fdae2ae37827382b90eb400" \
RedHatAI/Meta-Llama-3-8B-Instruct-FP8
##@ Precommit code checks
.PHONY: precommit lint tidy-go copr-fix
precommit: tidy-go lint copr-fix
tidy-go:
@echo "Tidying up go.mod and go.sum..."
@go mod tidy
lint:
@echo "==== Running linting ===="
@golangci-lint run
copr-fix:
@echo "Adding copyright headers..."
@docker run -i --rm -v $(shell pwd):/github/workspace apache/skywalking-eyes header fix
clang:
@echo "Running clang-format..."
@find kv_connectors -type f \( \
-name "*.cu" -o -name "*.cuh" -o \
-name "*.cc" -o -name "*.cpp" -o \
-name "*.hpp" -o -name "*.h" \
\) -exec clang-format -i {} +
##@ Development
# Build tag for embedded (Python/cgo) tokenizers
EMBEDDED_TAGS := embedded_tokenizers
# Common environment variables for Go tests and builds (with embedded tokenizers / Python cgo)
export CGO_ENABLED=1
export CGO_CFLAGS=$(CGO_CFLAGS_FINAL)
export CGO_LDFLAGS=$(CGO_LDFLAGS_FINAL)
export PYTHONPATH=$(shell pwd)/pkg/preprocessing/chat_completions/vllm_source:$(shell pwd)/pkg/preprocessing/chat_completions:$(VENV_DIR)/lib/python$(PYTHON_VERSION)/site-packages
.PHONY: test
test: unit-test e2e-test ## Run all tests (unit + e2e with embedded + UDS tokenizer service)
.PHONY: unit-test
unit-test: unit-test-uds ## Run unit tests (UDS tokenizer service only)
.PHONY: unit-test-uds
unit-test-uds: check-go download-zmq ## Run unit tests without embedded tokenizers (no Python required)
@printf "\033[33;1m==== Running unit tests (UDS-only, no embedded tokenizers) ====\033[0m\n"
@go test -v ./pkg/...
.PHONY: unit-test-race
unit-test-race: check-go download-zmq ## Run unit tests with Go race detector enabled
@printf "\033[33;1m==== Running unit tests with race detector ====\033[0m\n"
@go test -v -race ./pkg/...
.PHONY: unit-test-embedded
unit-test-embedded: check-go install-python-deps download-zmq ## Run unit tests with embedded tokenizers
@printf "\033[33;1m==== Running unit tests (with embedded tokenizers) ====\033[0m\n"
@go test -v -tags $(EMBEDDED_TAGS) ./pkg/...
.PHONY: e2e-test
e2e-test: e2e-test-uds ## Run end-to-end tests (UDS tokenizer service only)
.PHONY: e2e-test-embedded
e2e-test-embedded: check-go download-local-llama3 install-python-deps download-zmq ## Run end-to-end tests (requires embedded tokenizers)
@printf "\033[33;1m==== Running end-to-end tests (with embedded tokenizers) ====\033[0m\n"
@go test -v -tags $(EMBEDDED_TAGS) ./tests/...
.PHONY: image-build-uds
image-build-uds: check-container-tool ## Build the UDS tokenizer container image
@printf "\033[33;1m==== Building UDS tokenizer image $(UDS_TOKENIZER_IMAGE) ====\033[0m\n"
$(CONTAINER_TOOL) build -t $(UDS_TOKENIZER_IMAGE) services/uds_tokenizer
.PHONY: e2e-test-uds
e2e-test-uds: check-go download-zmq image-build-uds ## Run UDS tokenizer e2e tests (requires Docker or Podman)
@printf "\033[33;1m==== Running end-to-end tests (UDS tokenizer service) ====\033[0m\n"
@if [ "$(CONTAINER_TOOL)" = "podman" ]; then \
DOCKER_HOST="unix://$$XDG_RUNTIME_DIR/podman/podman.sock"; \
export TESTCONTAINERS_RYUK_DISABLED=true; \
else \
DOCKER_HOST=$$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null); \
if [ -z "$$DOCKER_HOST" ]; then \
echo "ERROR: DOCKER_HOST could not be determined. Ensure Docker is installed and a context is configured."; \
exit 1; \
fi; \
export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock; \
fi; \
DOCKER_HOST=$$DOCKER_HOST \
UDS_TOKENIZER_IMAGE=$(UDS_TOKENIZER_IMAGE) \
go test -v -count=1 -timeout 10m ./tests/e2e/uds_tokenizer/...
##@ UDS Tokenizer Python Tests
UDS_TOKENIZER_DIR := services/uds_tokenizer
UDS_TOKENIZER_VENV_DIR := $(UDS_TOKENIZER_DIR)/.venv
UDS_TOKENIZER_VENV_BIN := $(UDS_TOKENIZER_VENV_DIR)/bin
.PHONY: uds-tokenizer-install-deps
uds-tokenizer-install-deps: detect-python ## Set up venv and install UDS tokenizer dependencies
@printf "\033[33;1m==== Setting up UDS tokenizer venv and dependencies ====\033[0m\n"
@if [ ! -f "$(UDS_TOKENIZER_VENV_BIN)/python" ]; then \
echo "Creating virtual environment in $(UDS_TOKENIZER_VENV_DIR)..."; \
$(PYTHON_EXE) -m venv $(UDS_TOKENIZER_VENV_DIR); \
echo "Upgrading pip..."; \
$(UDS_TOKENIZER_VENV_BIN)/pip install --upgrade pip; \
else \
echo "Virtual environment already exists"; \
fi
@echo "Installing dependencies..."
@$(UDS_TOKENIZER_VENV_BIN)/pip install "$(UDS_TOKENIZER_DIR)[test]" --extra-index-url https://download.pytorch.org/whl/cpu
.PHONY: uds-tokenizer-service-test
uds-tokenizer-service-test: uds-tokenizer-install-deps ## Run UDS tokenizer integration tests (starts server automatically)
@printf "\033[33;1m==== Running UDS tokenizer integration tests ====\033[0m\n"
@$(UDS_TOKENIZER_VENV_BIN)/python -m pytest \
$(UDS_TOKENIZER_DIR)/tests/test_integration.py \
$(UDS_TOKENIZER_DIR)/tests/test_renderer.py \
-v --timeout=60
.PHONY: bench
bench: check-go install-python-deps download-zmq ## Run benchmarks (requires embedded tokenizers)
@printf "\033[33;1m==== Running chat template benchmarks ====\033[0m\n"
@go test -bench=. -benchmem -tags $(EMBEDDED_TAGS) ./pkg/preprocessing/chat_completions/
@printf "\033[33;1m==== Running tokenization benchmarks ====\033[0m\n"
@go test -bench=. -benchmem -tags $(EMBEDDED_TAGS) ./pkg/tokenization/
.PHONY: run
run: build-embedded ## Run the application locally
@printf "\033[33;1m==== Running application ====\033[0m\n"
@./bin/$(PROJECT_NAME)
##@ Build
.PHONY: build
build: build-uds build-embedded ## Build both UDS-only and embedded binaries
.PHONY: build-uds
build-uds: check-go download-zmq ## Build without embedded tokenizers (no Python required)
@printf "\033[33;1m==== Building (UDS-only, no embedded tokenizers) ====\033[0m\n"
@go build ./pkg/...
@echo "✅ UDS-only build succeeded"
.PHONY: build-embedded
build-embedded: check-go install-python-deps download-zmq ## Build with embedded tokenizers
@printf "\033[33;1m==== Building application binary (with embedded tokenizers) ====\033[0m\n"
@go build -tags $(EMBEDDED_TAGS) -o bin/$(PROJECT_NAME) examples/kv_events/online/main.go
@echo "✅ Built examples/kv_events/online/main.go -> bin/$(PROJECT_NAME)"
.PHONY: image-build
image-build: check-container-tool load-version-json ## Build Docker image
@printf "\033[33;1m==== Building Docker image $(IMG) ====\033[0m\n"
$(CONTAINER_TOOL) build \
--platform $(TARGETOS)/$(TARGETARCH) \
--build-arg TARGETOS=$(TARGETOS) \
--build-arg TARGETARCH=$(TARGETARCH) \
-t $(IMG) .
.PHONY: image-push
image-push: check-container-tool load-version-json ## Push Docker image $(IMG) to registry
@printf "\033[33;1m==== Pushing Docker image $(IMG) ====\033[0m\n"
$(CONTAINER_TOOL) push $(IMG)
##@ Install/Uninstall Targets
# Default install/uninstall (Docker)
install: install-docker ## Default install using Docker
@echo "Default Docker install complete."
uninstall: uninstall-docker ## Default uninstall using Docker
@echo "Default Docker uninstall complete."
### Docker Targets
.PHONY: install-docker
install-docker: check-container-tool ## Install app using $(CONTAINER_TOOL)
@echo "Starting container with $(CONTAINER_TOOL)..."
$(CONTAINER_TOOL) run -d --name $(PROJECT_NAME)-container $(IMG)
@echo "$(CONTAINER_TOOL) installation complete."
@echo "To use $(PROJECT_NAME), run:"
@echo "alias $(PROJECT_NAME)='$(CONTAINER_TOOL) exec -it $(PROJECT_NAME)-container /app/$(PROJECT_NAME)'"
.PHONY: uninstall-docker
uninstall-docker: check-container-tool ## Uninstall app from $(CONTAINER_TOOL)
@echo "Stopping and removing container in $(CONTAINER_TOOL)..."
-$(CONTAINER_TOOL) stop $(PROJECT_NAME)-container && $(CONTAINER_TOOL) rm $(PROJECT_NAME)-container
@echo "$(CONTAINER_TOOL) uninstallation complete. Remove alias if set: unalias $(PROJECT_NAME)"
### Kubernetes Targets (kubectl)
.PHONY: install-k8s
install-k8s: check-kubectl check-kustomize check-envsubst ## Install on Kubernetes
export PROJECT_NAME=${PROJECT_NAME}
export NAMESPACE=${NAMESPACE}
@echo "Creating namespace (if needed) and setting context to $(NAMESPACE)..."
kubectl create namespace $(NAMESPACE) 2>/dev/null || true
kubectl config set-context --current --namespace=$(NAMESPACE)
@echo "Deploying resources from deploy/ ..."
# Build the kustomization from deploy, substitute variables, and apply the YAML
kustomize build deploy | envsubst | kubectl apply -f -
@echo "Waiting for pod to become ready..."
sleep 5
@POD=$$(kubectl get pod -l app=$(PROJECT_NAME)-statefulset -o jsonpath='{.items[0].metadata.name}'); \
echo "Kubernetes installation complete."; \
echo "To use the app, run:"; \
echo "alias $(PROJECT_NAME)='kubectl exec -n $(NAMESPACE) -it $$POD -- /app/$(PROJECT_NAME)'"
.PHONY: uninstall-k8s
uninstall-k8s: check-kubectl check-kustomize check-envsubst ## Uninstall from Kubernetes
export PROJECT_NAME=${PROJECT_NAME}
export NAMESPACE=${NAMESPACE}
@echo "Removing resources from Kubernetes..."
kustomize build deploy | envsubst | kubectl delete --force -f - || true
POD=$$(kubectl get pod -l app=$(PROJECT_NAME)-statefulset -o jsonpath='{.items[0].metadata.name}'); \
echo "Deleting pod: $$POD"; \
kubectl delete pod "$$POD" --force --grace-period=0 || true; \
echo "Kubernetes uninstallation complete. Remove alias if set: unalias $(PROJECT_NAME)"
### OpenShift Targets (oc)
.PHONY: install-openshift
install-openshift: check-kubectl check-kustomize check-envsubst ## Install on OpenShift
exit 0
.PHONY: uninstall-openshift
uninstall-openshift: check-kubectl check-kustomize check-envsubst ## Uninstall from OpenShift
exit 0
### RBAC Targets (using kustomize and envsubst)
.PHONY: install-rbac
install-rbac: check-kubectl check-kustomize check-envsubst ## Install RBAC
@echo "Applying RBAC configuration from deploy/rbac..."
kustomize build deploy/rbac | envsubst '$$PROJECT_NAME $$NAMESPACE $$IMAGE_TAG_BASE $$VERSION' | kubectl apply -f -
.PHONY: uninstall-rbac
uninstall-rbac: check-kubectl check-kustomize check-envsubst ## Uninstall RBAC
@echo "Removing RBAC configuration from deploy/rbac..."
kustomize build deploy/rbac | envsubst '$$PROJECT_NAME $$NAMESPACE $$IMAGE_TAG_BASE $$VERSION' | kubectl delete -f - || true
##@ Version Extraction
.PHONY: version dev-registry prod-registry extract-version-info
dev-version: check-jq
@jq -r '.dev-version' .version.json
prod-version: check-jq
@jq -r '.prod-version' .version.json
dev-registry: check-jq
@jq -r '."dev-registry"' .version.json
prod-registry: check-jq
@jq -r '."prod-registry"' .version.json
extract-version-info: check-jq
@echo "DEV_VERSION=$$(jq -r '."dev-version"' .version.json)"
@echo "PROD_VERSION=$$(jq -r '."prod-version"' .version.json)"
@echo "DEV_IMAGE_TAG_BASE=$$(jq -r '."dev-registry"' .version.json)"
@echo "PROD_IMAGE_TAG_BASE=$$(jq -r '."prod-registry"' .version.json)"
##@ Load Version JSON
.PHONY: load-version-json
load-version-json: check-jq
@if [ "$(DEV_VERSION)" = "0.0.1" ]; then \
DEV_VERSION=$$(jq -r '."dev-version"' .version.json); \
PROD_VERSION=$$(jq -r '."dev-version"' .version.json); \
echo "Loaded DEV_VERSION from .version.json: $$DEV_VERSION"; \
echo "Loaded PROD_VERSION from .version.json: $$PROD_VERSION"; \
export DEV_VERSION; \
export PROD_VERSION; \
fi && \
CURRENT_DEFAULT="ghcr.io/llm-d/$(PROJECT_NAME)"; \
if [ "$(IMAGE_TAG_BASE)" = "$$CURRENT_DEFAULT" ]; then \
IMAGE_TAG_BASE=$$(jq -r '."dev-registry"' .version.json); \
echo "Loaded IMAGE_TAG_BASE from .version.json: $$IMAGE_TAG_BASE"; \
export IMAGE_TAG_BASE; \
fi && \
echo "Final values: DEV_VERSION=$$DEV_VERSION, PROD_VERSION=$$PROD_VERSION, IMAGE_TAG_BASE=$$IMAGE_TAG_BASE"
.PHONY: env
env: load-version-json ## Print environment variables
@echo "DEV_VERSION=$(DEV_VERSION)"
@echo "PROD_VERSION=$(PROD_VERSION)"
@echo "IMAGE_TAG_BASE=$(IMAGE_TAG_BASE)"
@echo "IMG=$(IMG)"
@echo "CONTAINER_TOOL=$(CONTAINER_TOOL)"
##@ Tools
.PHONY: check-tools
check-tools: \
check-go \
check-ginkgo \
check-golangci-lint \
check-cmake \
check-jq \
check-kustomize \
check-envsubst \
check-container-tool \
check-kubectl \
check-buildah \
check-podman
@echo "All required tools are installed."
.PHONY: check-go
check-go:
@command -v go >/dev/null 2>&1 || { \
echo "Go is not installed. Install it from https://golang.org/dl/"; exit 1; }
.PHONY: check-ginkgo
check-ginkgo:
@command -v ginkgo >/dev/null 2>&1 || { \
echo "ginkgo is not installed. Install with: go install github.com/onsi/ginkgo/v2/ginkgo@latest"; exit 1; }
.PHONY: check-golangci-lint
check-golangci-lint:
@command -v golangci-lint >/dev/null 2>&1 || { \
echo "golangci-lint is not installed. Install from https://golangci-lint.run/docs/welcome/install/"; exit 1; }
.PHONY: check-jq
check-jq:
@command -v jq >/dev/null 2>&1 || { \
echo "jq is not installed. Install it from https://stedolan.github.io/jq/download/"; exit 1; }
.PHONY: check-kustomize
check-kustomize:
@command -v kustomize >/dev/null 2>&1 || { \
echo "kustomize is not installed. Install it from https://kubectl.docs.kubernetes.io/installation/kustomize/"; exit 1; }
.PHONY: check-envsubst
check-envsubst:
@command -v envsubst >/dev/null 2>&1 || { \
echo "envsubst is not installed. It is part of gettext."; \
echo "Try: sudo apt install gettext OR brew install gettext"; exit 1; }
.PHONY: check-container-tool
check-container-tool:
@command -v $(CONTAINER_TOOL) >/dev/null 2>&1 || { \
echo "$(CONTAINER_TOOL) is not installed."; \
echo "Try: sudo apt install $(CONTAINER_TOOL) OR brew install $(CONTAINER_TOOL)"; exit 1; }
.PHONY: check-kubectl
check-kubectl:
@command -v kubectl >/dev/null 2>&1 || { \
echo "kubectl is not installed. Install it from https://kubernetes.io/docs/tasks/tools/"; exit 1; }
.PHONY: check-builder
check-builder:
@if [ -z "$(BUILDER)" ]; then \
echo "No container builder tool (buildah, docker, or podman) found."; \
exit 1; \
else \
echo "Using builder: $(BUILDER)"; \
fi
.PHONY: check-podman
check-podman:
@command -v podman >/dev/null 2>&1 || { \
echo "Podman is not installed. You can install it with:"; \
echo "sudo apt install podman OR brew install podman"; exit 1; }
check-cmake:
@command -v cmake >/dev/null 2>&1 || { \
echo "CMake is not installed. Install it with your system package manager."; exit 1; }
##@ Alias checking
.PHONY: check-alias
check-alias: check-container-tool
@echo "Checking alias functionality for container '$(PROJECT_NAME)-container'..."
@if ! $(CONTAINER_TOOL) exec $(PROJECT_NAME)-container /app/$(PROJECT_NAME) --help >/dev/null 2>&1; then \
echo "The container '$(PROJECT_NAME)-container' is running, but the alias might not work."; \
echo "Try: $(CONTAINER_TOOL) exec -it $(PROJECT_NAME)-container /app/$(PROJECT_NAME)"; \
else \
echo "Alias is likely to work: alias $(PROJECT_NAME)='$(CONTAINER_TOOL) exec -it $(PROJECT_NAME)-container /app/$(PROJECT_NAME)'"; \
fi
.PHONY: print-namespace
print-namespace: ## Print the current namespace
@echo "$(NAMESPACE)"
.PHONY: print-project-name
print-project-name: ## Print the current project name
@echo "$(PROJECT_NAME)"
.PHONY: clean
clean: ## Clean build artifacts
@printf "\033[33;1m==== Cleaning build artifacts ====\033[0m\n"
@rm -rf build/
@echo "Build artifacts cleaned."
.PHONY: install-hooks
install-hooks: ## Install git hooks
git config core.hooksPath hooks
##@ gRPC Code Generation
.PHONY: generate-grpc-go
generate-grpc-go: check-protoc ## Generate gRPC code from protobuf definitions for Go client
@echo "Generating gRPC code from protobuf definitions for Go client..."
@mkdir -p api/tokenizerpb api/indexerpb
@protoc --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative api/tokenizerpb/tokenizer.proto
@protoc --go_out=. --go-grpc_out=. --go_opt=paths=source_relative --go-grpc_opt=paths=source_relative api/indexerpb/indexer.proto
@echo "✅ gRPC Go code generated successfully"
.PHONY: generate-grpc-python
generate-grpc-python: check-grpc-tools ## Generate gRPC code from protobuf definitions for Python server
@echo "Generating gRPC code from protobuf definitions for Python server..."
@mkdir -p services/uds_tokenizer/tokenizerpb
@$(VENV_BIN)/python -m grpc_tools.protoc -Iapi --python_out=services/uds_tokenizer --grpc_python_out=services/uds_tokenizer api/tokenizerpb/tokenizer.proto
@echo "✅ gRPC Python code generated successfully"
.PHONY: generate-grpc
generate-grpc: generate-grpc-go generate-grpc-python ## Generate gRPC code for both client and server
# Ensure protoc is available before generating gRPC code
.PHONY: check-protoc
check-protoc:
@command -v protoc >/dev/null 2>&1 || { \
echo "protoc is not installed. Install it from https://grpc.io/docs/protoc-installation/"; exit 1; }
# Ensure grpc_tools is available before generating gRPC Python code
.PHONY: check-grpc-tools
check-grpc-tools: install-python-deps
@echo "Checking if grpc_tools is installed..."
@if ! $(VENV_BIN)/python -c "import grpc_tools" 2>/dev/null; then \
echo "grpc_tools is not installed. Installing from requirements..."; \
$(VENV_BIN)/pip install grpcio-tools; \
fi
@echo "✅ grpc_tools is available"
##@ ZMQ Setup
.PHONY: download-zmq
download-zmq: ## Install ZMQ dependencies based on OS/ARCH
@echo "Checking if ZMQ is already installed..."
@if pkg-config --exists libzmq; then \
echo "✅ ZMQ is already installed."; \
else \
echo "Installing ZMQ dependencies..."; \
if [ "$(TARGETOS)" = "linux" ]; then \
if [ -x "$$(command -v apt)" ]; then \
apt update && apt install -y libzmq3-dev pkg-config; \
elif [ -x "$$(command -v dnf)" ]; then \
dnf install -y zeromq-devel pkgconfig; \
else \
echo "Unsupported Linux package manager. Install libzmq manually."; \
exit 1; \
fi; \
elif [ "$(TARGETOS)" = "darwin" ]; then \
if [ -x "$$(command -v brew)" ]; then \
brew install zeromq pkg-config; \
else \
echo "Homebrew is not installed and is required to install zeromq. Install it from https://brew.sh/"; \
exit 1; \
fi; \
else \
echo "Unsupported OS: $(TARGETOS). Install libzmq manually - check https://zeromq.org/download/ for guidance."; \
exit 1; \
fi; \
echo "✅ ZMQ dependencies installed."; \
fi
##@ Examples
UDS_TOKENIZER_GRPC_PORT ?= 50051
UDS_TOKENIZER_HEALTH_PORT ?= 8082
# Define a template for building examples
define BUILD_EXAMPLE_TEMPLATE
$(1): $$(SRC) | check-go
@echo "Building $$@..."
@mkdir -p $$(dir $$@)
@go build -o $$@ $(2)
@echo "✅ Built $$@"
endef
# Generate build rules for simple examples (single main.go)
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/offline,examples/kv_events/offline/main.go))
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/online,examples/kv_events/online/main.go))
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/valkey,examples/valkey_example/main.go))
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/kv_cache_index,examples/kv_cache_index/main.go))
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/kv_cache_index_service/client,examples/kv_cache_index_service/client/main.go))
$(eval $(call BUILD_EXAMPLE_TEMPLATE,bin/examples/kv_cache_index_service/server,./examples/kv_cache_index_service/server))
.PHONY: build-examples
build-examples: bin/examples/offline bin/examples/online bin/examples/valkey bin/examples/kv_cache_index bin/examples/kv_cache_index_service/server bin/examples/kv_cache_index_service/client ## Build all example binaries
@echo "✅ All examples built successfully!"
# Allow passing the example binary as a positional make goal, e.g.
# make run-example offline
# If a positional example goal is provided, use it; otherwise fall back to default.
EXAMPLE_FROM_GOALS := $(word 2,$(MAKECMDGOALS))
ifeq ($(EXAMPLE_FROM_GOALS),)
EXAMPLE ?= bin/examples/offline
else
EXAMPLE := bin/examples/$(EXAMPLE_FROM_GOALS)
endif
# Allow short example names to appear on the command line without being
# interpreted as missing targets.
EXAMPLE_SHORTS := offline online valkey kv_cache_index kv_cache_index_service
.PHONY: $(EXAMPLE_SHORTS)
$(EXAMPLE_SHORTS):
.PHONY: start-tokenizer
start-tokenizer: check-container-tool ## Start the UDS tokenizer container; requires image-build-uds to have been run first
@printf "\033[33;1m==== Starting UDS tokenizer container ====\033[0m\n"
@$(CONTAINER_TOOL) run -d --rm --name uds-tokenizer-example --network host \
-e GRPC_PORT=$(UDS_TOKENIZER_GRPC_PORT) \
-e PROBE_PORT=$(UDS_TOKENIZER_HEALTH_PORT) \
$(UDS_TOKENIZER_IMAGE)
@printf "Waiting for tokenizer to be ready"
@for i in $$(seq 1 30); do \
if curl -sf http://localhost:$(UDS_TOKENIZER_HEALTH_PORT)/healthz > /dev/null 2>&1; then \
printf " ready!\n"; break; \
fi; \
if [ $$i -eq 30 ]; then \
printf " timeout!\n"; \
$(CONTAINER_TOOL) stop uds-tokenizer-example 2>/dev/null || true; \
exit 1; \
fi; \
printf "."; sleep 2; \
done
.PHONY: stop-tokenizer
stop-tokenizer: ## Stop and remove the UDS tokenizer container
@$(CONTAINER_TOOL) stop uds-tokenizer-example 2>/dev/null || true
@$(CONTAINER_TOOL) rm -f uds-tokenizer-example 2>/dev/null || true
.PHONY: run-example-only
run-example-only: $(EXAMPLE) ## Run the example binary only (tokenizer must already be running via start-tokenizer)
@printf "\033[33;1m==== Running example $(EXAMPLE) ====\033[0m\n"
@TOKENIZER_ENDPOINT=localhost:$(UDS_TOKENIZER_GRPC_PORT) ./$(EXAMPLE)
.PHONY: run-example
run-example: ## Run the example with UDS tokenizer in Docker (e.g., make run-example offline); requires image-build-uds to have been run first
@$(MAKE) --no-print-directory start-tokenizer
@$(MAKE) --no-print-directory run-example-only; status=$$?; \
$(MAKE) --no-print-directory stop-tokenizer; \
exit $$status