|
| 1 | +# Makefile for running MetaMathQA experiments. |
| 2 | + |
| 3 | +# --- Configuration --- |
| 4 | +PYTHON := python |
| 5 | +RUN_SCRIPT := run.py |
| 6 | +EXPERIMENTS_DIR := experiments |
| 7 | +RESULTS_DIR := results |
| 8 | + |
| 9 | +# --- Automatic Experiment and Result Discovery --- |
| 10 | + |
| 11 | +# 1. Find all experiment directories by looking for adapter_config.json files. |
| 12 | +# This gives us a list like: experiments/lora/llama-3.2-3B-rank32 ... |
| 13 | +EXPERIMENT_PATHS := $(patsubst %/adapter_config.json,%,$(shell find $(EXPERIMENTS_DIR) -name "adapter_config.json")) |
| 14 | + |
| 15 | +# 2. Define a function to replace all occurrences of a character in a string. |
| 16 | +# This is needed to replicate the result naming logic from run.py (e.g., "lora/foo" -> "lora-foo"). |
| 17 | +# Usage: $(call replace-all, string, char_to_replace, replacement_char) |
| 18 | +replace-all = $(if $(findstring $(2),$(1)),$(call replace-all,$(subst $(2),$(3),$(1)),$(2),$(3)),$(1)) |
| 19 | + |
| 20 | +# 3. Define a function to convert an experiment path to its flat result file path. |
| 21 | +# e.g., "experiments/lora/llama-3.2-3B-rank32" -> "results/lora-llama-3.2-3B-rank32.json" |
| 22 | +exp_to_res = $(RESULTS_DIR)/$(call replace-all,$(patsubst $(EXPERIMENTS_DIR)/%,%,$(1)),/,-).json |
| 23 | + |
| 24 | +# 4. Generate the list of all target result files we want to build. |
| 25 | +RESULT_FILES := $(foreach exp,$(EXPERIMENT_PATHS),$(call exp_to_res,$(exp))) |
| 26 | + |
| 27 | + |
| 28 | +# --- Main Rules --- |
| 29 | + |
| 30 | +# The default 'all' target depends on all possible result files. |
| 31 | +# Running `make` or `make all` will check and run any outdated or missing experiments. |
| 32 | +all: $(RESULT_FILES) |
| 33 | + |
| 34 | + |
| 35 | +# --- Dynamic Rule Generation --- |
| 36 | + |
| 37 | +# This is the core logic. We dynamically generate a specific Makefile rule for each experiment found. |
| 38 | +# This avoids a complex pattern rule and makes the logic clearer. |
| 39 | +define EXPERIMENT_template |
| 40 | +# Input $1: The full experiment path (e.g., experiments/lora/llama-3.2-3B-rank32) |
| 41 | + |
| 42 | +# Define the rule: |
| 43 | +# The target is the result file (e.g., results/lora-llama-3.2-3B-rank32.json). |
| 44 | +# The dependencies are its config files, code changes need to be audited manually since they can |
| 45 | +# vary in degree of importance. Note that we explicitly ignore when the script fails to run |
| 46 | +# so that the other experiments still have a chance to run. |
| 47 | +$(call exp_to_res,$(1)): $(1)/adapter_config.json $(wildcard $(1)/training_params.json) |
| 48 | + @echo "---" |
| 49 | + @echo "Running experiment: $(1)" |
| 50 | + -$(PYTHON) $(RUN_SCRIPT) -v $(1) |
| 51 | + @echo "Finished: $$@" |
| 52 | + @echo "---" |
| 53 | + |
| 54 | +endef |
| 55 | + |
| 56 | +# This command iterates through every found experiment path and evaluates the template, |
| 57 | +# effectively stamping out a unique, explicit rule for each one. |
| 58 | +$(foreach exp_path,$(EXPERIMENT_PATHS),$(eval $(call EXPERIMENT_template,$(exp_path)))) |
| 59 | + |
| 60 | + |
| 61 | +# --- Utility Rules --- |
| 62 | + |
| 63 | +.PHONY: all clean list dump_rules |
| 64 | + |
| 65 | +# The 'clean' rule removes all generated results. |
| 66 | +clean: |
| 67 | + @echo "Cleaning results directory..." |
| 68 | + @([ -n "$(wildcard $(RESULTS_DIR)/*.json)" ] && rm $(RESULTS_DIR)/*.json) || exit 0 |
| 69 | + |
| 70 | +# The 'list' rule is for debugging. It shows the discovered experiments |
| 71 | +# and the result files the Makefile expects to create for them. |
| 72 | +list: |
| 73 | + @echo "Discovered experiment configurations:" |
| 74 | + @$(foreach exp,$(EXPERIMENT_PATHS),echo " - $(exp)/adapter_config.json";) |
| 75 | + @echo "\nTarget result files:" |
| 76 | + @$(foreach res,$(RESULT_FILES),echo " - $(res)";) |
| 77 | + |
| 78 | +# The 'dump_rules' rule is for debugging. It dumps all dynamically defined rules. |
| 79 | +define newline |
| 80 | + |
| 81 | + |
| 82 | +endef |
| 83 | +define DUMPED_RULES |
| 84 | + $(foreach exp_path,$(EXPERIMENT_PATHS),$(call EXPERIMENT_template,$(exp_path))) |
| 85 | +endef |
| 86 | + |
| 87 | +dump_rules: |
| 88 | + @echo -e "$(subst $(newline),\n,${DUMPED_RULES})" |
0 commit comments