सामग्री पर जाएँ

लेसन 18 · Experiments

DVC Experiment Queues, Grid Search and Hydra

Queue many DVC experiments, sweep parameter values with Hydra syntax, run workers in parallel with dvc queue, and compose configurations with Hydra support.

Trying ten values by typing ten commands is slow. DVC can queue experiments, run them later (one by one or in parallel), and generate many runs from one command. For larger configurations, it can also build params.yaml with Hydra.

Queue experiments

Terminal
# Read-only: add runs to the queue, then process them
dvc exp run --queue -S train.max_iter=200
dvc exp run --queue -S train.max_iter=400
dvc queue status
dvc queue start
dvc queue logs <experiment-name>

--queue only records the experiment. It starts when you run dvc queue start. Each queued run captures the workspace as it was when queued. The documentation notes that tasks usually run in order, but this is not guaranteed. Queued and temporary runs skip files ignored by Git, and untracked files should be staged first.

Run in parallel

Terminal
# Read-only: two workers
dvc queue start --jobs 2
dvc queue stop

The default is one worker. Do not start more jobs than your machine can handle: each run needs memory, and GPU work often cannot share a device. If workers are already running, a higher number raises the limit; to lower it, stop the queue first. Use dvc queue remove to drop queued tasks (for example with --queued), and dvc queue kill to end a running one.

With --queue, -S accepts Hydra's sweep syntax. A comma list creates one experiment per value, and range() creates a numeric sequence:

Terminal
# Read-only: 2 models x 3 depths = 6 queued experiments
dvc exp run --queue -S 'train.model=forest,boost' -S 'train.max_depth=range(3,6)'
dvc queue start --jobs 2

As with Python's range, the end value is not included, so range(3,6) means 3, 4 and 5; check the queue to confirm. Quote the argument so your shell does not interpret the parentheses. Sweeps with several parameters create every combination, so the count multiplies. Four parameters with five values each make 625 runs. Estimate time and cost before you queue.

Search smart: start coarse, look at the results, then refine around promising areas. Trying a random sample of combinations can beat a dense grid when only a few parameters matter.

Hydra composition

For complex setups, you may want separate config files for each dataset, model and optimizer. Hydra builds one combined configuration from pieces. Enable DVC's support:

Terminal
# Read-only: turn on Hydra composition
dvc config hydra.enabled True

Then create a conf/ folder:

TEXT
conf/
  config.yaml
  model/
    forest.yaml
    boost.yaml
  data/
    small.yaml
    full.yaml
YAML
# conf/config.yaml
defaults:
  - model: forest
  - data: small

Before each dvc exp run, DVC composes params.yaml from these files. You then choose another option in a group from the command line:

Terminal
# Read-only: swap config groups
dvc exp run -S model=boost -S data=full

By default DVC looks for conf/config.yaml. The settings hydra.config_dir, hydra.config_name and hydra.plugins_path change the locations. The documentation describes composition for experiment runs, so check the generated params.yaml and commit it with your results. A later plain dvc repro then uses the same values. Learn Hydra itself from its own documentation.

Try the idea

This stand-in expands sweep values into the list of queued runs and estimates total time, which is worth doing before you start a large queue.

Python Stand-in: expand a sweep and estimate cost

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The estimate assumes every run takes the same time and that workers do not compete for memory or a GPU. Real runs often vary.

Practical limits

  • The queue runs on your machine. It is not a cluster scheduler. For large distributed training, use a dedicated platform and DVC for versioning.
  • Check logs for failed tasks; a queue can finish with some runs failed.
  • Clear old queued tasks you no longer want.
  • Remember that picking the best of hundreds of runs inflates the apparent score. Confirm the winner on untouched data.

Common mistake: queuing a large grid whose scripts read data from an untracked local path. The temporary copies may not contain it. Track inputs with DVC or stage the files in Git first.

Manage a long queue

Before starting a long queue, do a pilot. Run one experiment normally with the largest settings you plan to use. Check the run time, memory use, disk use of outputs and the metrics file. Each experiment can add new objects to the cache, so a grid that saves a large model every time may fill your disk. Consider whether you need to keep every model, or only the metrics for most runs.

While the queue works, check dvc queue status and read the logs of any failed task. A common pattern is that one combination of parameters is invalid, for example a depth of zero. Fix the validation in your script so such values fail quickly with a clear message instead of failing after a long data load.

When the queue finishes, use dvc exp show with sorting and --only-changed to find promising areas. Then remove the experiments you will never look at again. A tidy table is far easier to reason about than hundreds of rows with generated names.

Choose between a grid and a few careful runs

A grid is useful when the search space is small and runs are cheap. For expensive training, a handful of experiments chosen from your understanding of the model often teaches more. Whatever you choose, write down the ranges you searched. "Best of 5 runs" and "best of 500 runs" carry very different weight.

Make queued runs fail early

Add a quick check at the start of each script that validates parameters and confirms that input files exist. A clear error in the first second saves hours when many runs are waiting.

After the sweep, rerun your chosen setting once on its own to confirm that you can reproduce its metrics.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What happens when you run dvc exp run --queue?

    Queued experiments run when a worker is started.
  2. 2. How many experiments does a sweep of 3 values by 4 values create?

    Every combination is queued: 3 x 4 = 12.
  3. 3. What does dvc config hydra.enabled True do?

    Hydra config groups under conf/ are combined into params.yaml.
  4. 4. Why should you be careful with the best result from a very large sweep?

    Confirm it on data you did not use for selection.

Summary

  • --queue stores experiments; dvc queue start runs them, optionally with several jobs.
  • Comma lists and range() in -S create sweeps when queued.
  • Combinations multiply quickly, so estimate first.
  • Hydra composition builds params.yaml from config groups.
  • The queue is local, and winners need confirmation.

Next: Log Metrics Live with DVCLive

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।