Optimize with the CLI
Drive Weco yourself from the terminal — write an evaluation script, configure the CLI, and run an optimization on your own code without an AI agent.
If you'd rather run Weco yourself instead of letting an AI assistant set it up, here's how to configure and run an optimization from the terminal.
First, make sure the CLI is installed and you're logged in (see Manual Installation if you don't have the weco command yet):
weco loginUsing Weco comes down to two things:
- What to optimize: the code file(s) you want to improve (a function, module, or script).
- The goal: an evaluation script that measures and prints the performance of your code (e.g.
speedup: 2.5).
Select code to optimize
Weco can optimize code in any programming language (a GPU kernel, an ML model, a prompt, and so on) — either a single file (--source) or up to 10 files together (--sources).
For example, a simple PyTorch module:
# File: module.py
import torch
import torch.nn as nn
class Model(nn.Module):
"""
Model that performs a matrix multiplication, division, summation, and scaling.
"""
def __init__(self, input_size, hidden_size, scaling_factor):
super(Model, self).__init__()
self.weight = nn.Parameter(torch.randn(hidden_size, input_size))
self.scaling_factor = scaling_factor
def forward(self, x):
x = torch.matmul(x, self.weight.T)
x = x / 2
x = torch.sum(x, dim=1, keepdim=True)
x = x * self.scaling_factor
return xDefine the goal of the optimization
You need an evaluation script that:
- Evaluates the code you picked in Step 1 with a quantitative metric.
- Prints the metric name and value to the console (like
speedup: 2.5) so Weco can read it.
# File: evaluate.py
# Load the baseline model and the optimized model...
# Prepare test data...
# Evaluate performance
baseline_time = benchmark(baseline_model, test_inputs)
optimized_time = benchmark(optimized_model, test_inputs)
speedup = baseline_time / optimized_time
# Print the metric for Weco to read
print(f"speedup: {speedup}")Pro tip: See Build great eval scripts for better optimization results.
Run Weco on your code
weco run --source <path-to-your-code-to-optimize> \
--eval-command <command-to-run-the-evaluation-script> \
--metric <metric-name> \
--goal <maximize/minimize> \
--steps <number-of-optimization-iterations> \
--additional-instructions <additional-instructions-for-the-LLM-to-follow>weco run --source <path-to-your-code-to-optimize> ^
--eval-command <command-to-run-the-evaluation-script> ^
--metric <metric-name> ^
--goal <maximize/minimize> ^
--steps <number-of-optimization-iterations> ^
--additional-instructions <additional-instructions-for-the-LLM-to-follow>weco run --source <path-to-your-code-to-optimize> `
--eval-command <command-to-run-the-evaluation-script> `
--metric <metric-name> `
--goal <maximize/minimize> `
--steps <number-of-optimization-iterations> `
--additional-instructions <additional-instructions-for-the-LLM-to-follow>Weco iteratively optimizes the file(s) specified by --source (or --sources). At each iteration it:
- Generates a new version of the code and writes it to the file(s).
- Runs the evaluation script specified by
--eval-commandto measure performance. - Checks whether the metric specified by
--metricimproved, and proposes further changes.
After all iterations (--steps) complete, the file holds the best-performing version found.
If your code spans multiple files, pass them all and Weco makes coordinated changes across file boundaries:
weco run --sources model.py utils.py config.py \
--eval-command "python evaluate.py" \
--metric accuracy \
--goal maximizeFor the full set of commands and options, see the CLI Reference.
Weco modifies your source file(s) in place. Use version control (or a separate branch/worktree) and review the optimized code carefully before deploying it.
Inspect and share the results
Besides the status in your terminal, you can watch the optimization in the dashboard in real time and share the results with your team. See Watch, share & manage runs.
What's next?
- Build great eval scripts — get better results with well-designed benchmarks
- CLI Reference — every command and option
- FAQ — answers to common questions