-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
422 lines (340 loc) · 15 KB
/
Makefile
File metadata and controls
422 lines (340 loc) · 15 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
.PHONY: help install install-dev test test-unit test-integration test-comparison
.PHONY: lint format type-check quality train-baseline train-free compare
.PHONY: generate-data clean clean-checkpoints clean-data clean-all
.PHONY: setup-env publish-test publish clean-dist build check-package
.PHONY: docs-install docs-build docs-serve docs-serve-dev docs-deploy docs-clean docs-check
.PHONY: version-bump-patch version-bump-minor version-bump-major
.PHONY: release-patch release-minor release-major validate-release
# Color output
BLUE := \033[0;34m
GREEN := \033[0;32m
YELLOW := \033[1;33m
NC := \033[0m # No Color
# Project variables
PROJECT_NAME := free-transformer
PYTHON_VERSION := 3.12
SRC_DIR := src/free_transformer
TEST_DIR := tests
EXAMPLES_DIR := examples
DATA_DIR := data
CHECKPOINT_DIR := checkpoints
# UV commands
UV := uv
UV_RUN := $(UV) run
UV_PIP := $(UV) pip
##@ General
help: ## Display this help message
@echo "$(BLUE)Free Transformer - Makefile Commands$(NC)"
@echo ""
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make $(GREEN)<target>$(NC)\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " $(GREEN)%-20s$(NC) %s\n", $$1, $$2 } /^##@/ { printf "\n$(BLUE)%s$(NC)\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
##@ Setup
setup-env: ## Create UV environment and install Python version
@echo "$(BLUE)Setting up UV environment...$(NC)"
@$(UV) python install $(PYTHON_VERSION)
@echo "$(PYTHON_VERSION)" > .python-version
@$(UV) venv --python $(PYTHON_VERSION)
@echo "$(GREEN)Environment created successfully!$(NC)"
@echo "Activate with: source .venv/bin/activate"
install: ## Install package dependencies
@echo "$(BLUE)Installing dependencies...$(NC)"
@$(UV_PIP) install -e .
@echo "$(GREEN)Installation complete!$(NC)"
install-dev: ## Install package with development dependencies
@echo "$(BLUE)Installing with development dependencies...$(NC)"
@$(UV_PIP) install -e ".[dev]"
@echo "$(GREEN)Development installation complete!$(NC)"
install-all: setup-env install-dev docs-install ## Full setup: create env and install all dependencies
@echo "$(GREEN)Full installation complete!$(NC)"
sync: ## Sync dependencies with uv.lock
@echo "$(BLUE)Syncing dependencies...$(NC)"
@$(UV) sync
@echo "$(GREEN)Dependencies synced!$(NC)"
##@ Code Quality
lint: ## Run linting with ruff
@echo "$(BLUE)Running linter...$(NC)"
@$(UV_RUN) ruff check $(SRC_DIR) $(TEST_DIR) $(EXAMPLES_DIR)
format: ## Format code with black and isort
@echo "$(BLUE)Formatting code...$(NC)"
@$(UV_RUN) black $(SRC_DIR) $(TEST_DIR) $(EXAMPLES_DIR)
@$(UV_RUN) isort $(SRC_DIR) $(TEST_DIR) $(EXAMPLES_DIR)
@echo "$(GREEN)Code formatted!$(NC)"
format-check: ## Check code formatting without changes
@echo "$(BLUE)Checking code format...$(NC)"
@$(UV_RUN) black --check $(SRC_DIR) $(TEST_DIR) $(EXAMPLES_DIR)
@$(UV_RUN) isort --check-only $(SRC_DIR) $(TEST_DIR) $(EXAMPLES_DIR)
type-check: ## Run type checking with mypy
@echo "$(BLUE)Running type checker...$(NC)"
@$(UV_RUN) mypy $(SRC_DIR)
quality: lint type-check format-check ## Run all quality checks
##@ Testing
test: ## Run all tests
@echo "$(BLUE)Running all tests...$(NC)"
@$(UV_RUN) pytest $(TEST_DIR) -v --cov=$(SRC_DIR) --cov-report=html --cov-report=term
@echo "$(GREEN)Tests complete! Coverage report: htmlcov/index.html$(NC)"
test-unit: ## Run unit tests only
@echo "$(BLUE)Running unit tests...$(NC)"
@$(UV_RUN) pytest $(TEST_DIR)/unit -v
test-integration: ## Run integration tests only
@echo "$(BLUE)Running integration tests...$(NC)"
@$(UV_RUN) pytest $(TEST_DIR)/integration -v
test-comparison: ## Run Transformer vs Free Transformer comparison
@echo "$(BLUE)Running comparison tests...$(NC)"
@$(UV_RUN) pytest $(TEST_DIR)/test_comparison.py -v -s
test-fast: ## Run tests without coverage (faster)
@echo "$(BLUE)Running fast tests...$(NC)"
@$(UV_RUN) pytest $(TEST_DIR) -v -x
##@ Data
generate-data: ## Generate synthetic training data
@echo "$(BLUE)Generating synthetic data...$(NC)"
@mkdir -p $(DATA_DIR)
@$(UV_RUN) python $(EXAMPLES_DIR)/generate_data.py \
--output-dir $(DATA_DIR) \
--vocab-size 10000 \
--seq-length 512 \
--num-train 50000 \
--num-val 5000 \
--seed 42
@echo "$(GREEN)Data generated in $(DATA_DIR)/$(NC)"
generate-data-small: ## Generate small dataset for testing
@echo "$(BLUE)Generating small test dataset...$(NC)"
@mkdir -p $(DATA_DIR)
@$(UV_RUN) python $(EXAMPLES_DIR)/generate_data.py \
--output-dir $(DATA_DIR) \
--vocab-size 1000 \
--seq-length 128 \
--num-train 1000 \
--num-val 100 \
--seed 42
@echo "$(GREEN)Small dataset generated!$(NC)"
##@ Training
train-baseline: ## Train baseline Transformer
@echo "$(BLUE)Training baseline Transformer...$(NC)"
@mkdir -p $(CHECKPOINT_DIR)/baseline
@$(UV_RUN) python $(EXAMPLES_DIR)/train_baseline.py \
--config configs/baseline.yaml \
--output-dir $(CHECKPOINT_DIR)/baseline
@echo "$(GREEN)Baseline training complete!$(NC)"
train-free: ## Train Free Transformer
@echo "$(BLUE)Training Free Transformer...$(NC)"
@mkdir -p $(CHECKPOINT_DIR)/free
@$(UV_RUN) python $(EXAMPLES_DIR)/train_free.py \
--config configs/free_transformer.yaml \
--output-dir $(CHECKPOINT_DIR)/free
@echo "$(GREEN)Free Transformer training complete!$(NC)"
train-baseline-fsdp: ## Train baseline with FSDP (multi-GPU)
@echo "$(BLUE)Training baseline with FSDP...$(NC)"
@mkdir -p $(CHECKPOINT_DIR)/baseline_fsdp
@$(UV_RUN) torchrun --nproc_per_node=auto \
$(EXAMPLES_DIR)/train_baseline.py \
--config configs/baseline.yaml \
--use-fsdp \
--output-dir $(CHECKPOINT_DIR)/baseline_fsdp
train-free-fsdp: ## Train Free Transformer with FSDP (multi-GPU)
@echo "$(BLUE)Training Free Transformer with FSDP...$(NC)"
@mkdir -p $(CHECKPOINT_DIR)/free_fsdp
@$(UV_RUN) torchrun --nproc_per_node=auto \
$(EXAMPLES_DIR)/train_free.py \
--config configs/free_transformer.yaml \
--use-fsdp \
--output-dir $(CHECKPOINT_DIR)/free_fsdp
train-all: train-baseline train-free ## Train both models sequentially
##@ Evaluation
compare: ## Compare baseline and Free Transformer
@echo "$(BLUE)Comparing models...$(NC)"
@$(UV_RUN) python $(EXAMPLES_DIR)/eval_compare.py \
--baseline-checkpoint $(CHECKPOINT_DIR)/baseline/model_final.pt \
--free-checkpoint $(CHECKPOINT_DIR)/free/model_final.pt \
--data-dir $(DATA_DIR) \
--output-dir results/comparison
@echo "$(GREEN)Comparison complete! Results in results/comparison/$(NC)"
evaluate-baseline: ## Evaluate baseline model
@echo "$(BLUE)Evaluating baseline model...$(NC)"
@$(UV_RUN) python $(EXAMPLES_DIR)/eval_compare.py \
--baseline-checkpoint $(CHECKPOINT_DIR)/baseline/model_final.pt \
--data-dir $(DATA_DIR) \
--output-dir results/baseline
evaluate-free: ## Evaluate Free Transformer
@echo "$(BLUE)Evaluating Free Transformer...$(NC)"
@$(UV_RUN) python $(EXAMPLES_DIR)/eval_compare.py \
--free-checkpoint $(CHECKPOINT_DIR)/free/model_final.pt \
--data-dir $(DATA_DIR) \
--output-dir results/free
##@ Quick Start
quick-start: install-dev generate-data-small test-fast ## Quick start: setup + small data + tests
@echo "$(GREEN)Quick start complete!$(NC)"
@echo "$(YELLOW)Next steps:$(NC)"
@echo " 1. Run 'make train-baseline' to train baseline model"
@echo " 2. Run 'make train-free' to train Free Transformer"
@echo " 3. Run 'make compare' to compare models"
@echo " 4. Run 'make docs-serve' to view documentation"
demo: generate-data-small train-baseline train-free compare ## Full demo pipeline
@echo "$(GREEN)Demo complete!$(NC)"
##@ Cleanup
clean: ## Remove Python cache files
@echo "$(BLUE)Cleaning Python cache...$(NC)"
@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
@find . -type f -name "*.pyc" -delete
@find . -type f -name "*.pyo" -delete
@find . -type f -name "*.egg-info" -exec rm -rf {} + 2>/dev/null || true
@rm -rf .pytest_cache .mypy_cache .ruff_cache htmlcov .coverage
@echo "$(GREEN)Cache cleaned!$(NC)"
clean-checkpoints: ## Remove all checkpoints
@echo "$(BLUE)Removing checkpoints...$(NC)"
@rm -rf $(CHECKPOINT_DIR)
@echo "$(GREEN)Checkpoints removed!$(NC)"
clean-data: ## Remove generated data
@echo "$(BLUE)Removing generated data...$(NC)"
@rm -rf $(DATA_DIR)
@echo "$(GREEN)Data removed!$(NC)"
clean-results: ## Remove evaluation results
@echo "$(BLUE)Removing results...$(NC)"
@rm -rf results
@echo "$(GREEN)Results removed!$(NC)"
clean-all: clean clean-checkpoints clean-data clean-results docs-clean ## Remove everything (cache, checkpoints, data, docs)
@echo "$(GREEN)Full cleanup complete!$(NC)"
clean-env: ## Remove virtual environment
@echo "$(YELLOW)Warning: This will remove the .venv directory$(NC)"
@read -p "Continue? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
rm -rf .venv; \
echo "$(GREEN)Environment removed!$(NC)"; \
fi
##@ Docker
docker-build: ## Build Docker image for GPU
@echo "$(BLUE)Building Docker image for GPU...$(NC)"
@docker build -t $(PROJECT_NAME):demo .
@echo "$(GREEN)Docker image built!$(NC)"
docker-build-cpu: ## Build Docker image for CPU
@echo "$(BLUE)Building Docker image for CPU...$(NC)"
@docker build -f Dockerfile.cpu -t $(PROJECT_NAME):cpu .
@echo "$(GREEN)CPU Docker image built!$(NC)"
docker-demo: ## Run demo using Docker Compose
@echo "$(BLUE)Running Free Transformer demo...$(NC)"
@docker-compose up free-transformer-demo
@echo "$(GREEN)Demo complete!$(NC)"
docker-interactive: ## Start interactive Docker container
@echo "$(BLUE)Starting interactive container...$(NC)"
@docker-compose up -d free-transformer-interactive
@docker-compose exec free-transformer-interactive bash
docker-run-gpu: ## Run container with GPU support
@echo "$(BLUE)Running Docker container with GPU...$(NC)"
@docker run --gpus all -it --rm \
-v $(PWD)/data:/workspace/data \
-v $(PWD)/checkpoints:/workspace/checkpoints \
$(PROJECT_NAME):demo
docker-run-cpu: ## Run container with CPU only
@echo "$(BLUE)Running Docker container with CPU...$(NC)"
@docker run -it --rm \
-v $(PWD)/data:/workspace/data \
-v $(PWD)/checkpoints:/workspace/checkpoints \
$(PROJECT_NAME):cpu
docker-clean: ## Clean Docker containers and images
@echo "$(BLUE)Cleaning Docker resources...$(NC)"
@docker-compose down --remove-orphans
@docker rmi $(PROJECT_NAME):demo $(PROJECT_NAME):cpu 2>/dev/null || true
@echo "$(GREEN)Docker resources cleaned!$(NC)"
##@ Documentation
docs-install: ## Install documentation dependencies
@echo "$(BLUE)Installing documentation dependencies...$(NC)"
@$(UV_PIP) install -e ".[docs]"
@echo "$(GREEN)Documentation dependencies installed!$(NC)"
docs-build: ## Build documentation
@echo "$(BLUE)Building documentation...$(NC)"
@$(UV_RUN) mkdocs build
@echo "$(GREEN)Documentation built in site/$(NC)"
docs-serve: ## Serve documentation locally
@echo "$(BLUE)Serving documentation (will find available port)$(NC)"
@$(UV_RUN) python scripts/serve_docs.py
docs-serve-dev: ## Serve documentation with auto-reload
@echo "$(BLUE)Serving documentation with auto-reload (will find available port)$(NC)"
@$(UV_RUN) python scripts/serve_docs.py --dev
docs-serve-port: ## Serve documentation on specific port (usage: make docs-serve-port PORT=8001)
@echo "$(BLUE)Serving documentation at http://127.0.0.1:$(or $(PORT),8000)$(NC)"
@$(UV_RUN) mkdocs serve --dev-addr 127.0.0.1:$(or $(PORT),8000)
docs-deploy: ## Deploy documentation to GitHub Pages
@echo "$(BLUE)Deploying documentation to GitHub Pages...$(NC)"
@$(UV_RUN) mkdocs gh-deploy --force
@echo "$(GREEN)Documentation deployed!$(NC)"
docs-clean: ## Clean documentation build
@echo "$(BLUE)Cleaning documentation build...$(NC)"
@rm -rf site/
@echo "$(GREEN)Documentation build cleaned!$(NC)"
docs-check: ## Check documentation for issues
@echo "$(BLUE)Checking documentation...$(NC)"
@$(UV_RUN) mkdocs build --strict
@echo "$(GREEN)Documentation check passed!$(NC)"
##@ CI/CD
ci: quality test docs-check ## Run CI pipeline locally
@echo "$(GREEN)CI checks passed!$(NC)"
pre-commit: format lint test-fast ## Run pre-commit checks
@echo "$(GREEN)Pre-commit checks passed!$(NC)"
##@ Info
info: ## Display project information
@echo "$(BLUE)Project Information$(NC)"
@echo " Name: $(PROJECT_NAME)"
@echo " Python: $(PYTHON_VERSION)"
@echo " UV version: $$($(UV) --version)"
@echo ""
@echo "$(BLUE)Directory Structure:$(NC)"
@echo " Source: $(SRC_DIR)"
@echo " Tests: $(TEST_DIR)"
@echo " Examples: $(EXAMPLES_DIR)"
@echo " Data: $(DATA_DIR)"
@echo " Checkpoints: $(CHECKPOINT_DIR)"
check-gpu: ## Check GPU availability
@echo "$(BLUE)Checking GPU...$(NC)"
@$(UV_RUN) python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}'); print(f'GPU count: {torch.cuda.device_count()}'); [print(f'GPU {i}: {torch.cuda.get_device_name(i)}') for i in range(torch.cuda.device_count())]"
##@ Publishing
clean-dist: ## Clean distribution files
@echo "$(BLUE)Cleaning distribution files...$(NC)"
@rm -rf dist/ build/ *.egg-info/
@echo "$(GREEN)Distribution files cleaned!$(NC)"
build: clean-dist ## Build distribution packages
@echo "$(BLUE)Building package...$(NC)"
@$(UV) build
@echo "$(GREEN)Build complete! Packages in dist/$(NC)"
@echo "$(YELLOW)Files created:$(NC)"
@ls -la dist/
check-package: build ## Check package for PyPI compliance
@echo "$(BLUE)Checking package...$(NC)"
@$(UV_RUN) twine check dist/*
@echo "$(GREEN)Package check complete!$(NC)"
validate-release: ## Comprehensive pre-release validation
@echo "$(BLUE)Running comprehensive release validation...$(NC)"
@$(UV_RUN) python scripts/validate_package.py
publish-test: check-package ## Publish to Test PyPI
@echo "$(BLUE)Publishing to Test PyPI...$(NC)"
@$(UV_RUN) twine upload --repository testpypi dist/*
@echo "$(GREEN)Published to Test PyPI!$(NC)"
@echo "$(YELLOW)Test installation:$(NC)"
@echo " pip install --index-url https://test.pypi.org/simple/ free-transformer"
publish: check-package ## Publish to PyPI
@echo "$(YELLOW)Warning: Publishing to production PyPI$(NC)"
@echo "$(YELLOW)Current version: $$(grep '^version = ' pyproject.toml | cut -d'"' -f2)$(NC)"
@read -p "Continue? [y/N] " -n 1 -r; \
echo; \
if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
$(UV_RUN) twine upload dist/*; \
echo "$(GREEN)Published to PyPI!$(NC)"; \
echo "$(YELLOW)Installation:$(NC)"; \
echo " pip install free-transformer"; \
fi
version-bump-patch: ## Bump patch version (0.1.0 -> 0.1.1)
@echo "$(BLUE)Bumping patch version...$(NC)"
@$(UV_RUN) python scripts/bump_version.py patch
@echo "$(GREEN)Version bumped!$(NC)"
version-bump-minor: ## Bump minor version (0.1.0 -> 0.2.0)
@echo "$(BLUE)Bumping minor version...$(NC)"
@$(UV_RUN) python scripts/bump_version.py minor
@echo "$(GREEN)Version bumped!$(NC)"
version-bump-major: ## Bump major version (0.1.0 -> 1.0.0)
@echo "$(BLUE)Bumping major version...$(NC)"
@$(UV_RUN) python scripts/bump_version.py major
@echo "$(GREEN)Version bumped!$(NC)"
release-patch: version-bump-patch build publish ## Release new patch version
@echo "$(GREEN)Patch release complete!$(NC)"
release-minor: version-bump-minor build publish ## Release new minor version
@echo "$(GREEN)Minor release complete!$(NC)"
release-major: version-bump-major build publish ## Release new major version
@echo "$(GREEN)Major release complete!$(NC)"