Skip to content

Commit 1625b2a

Browse files
committed
Update docs for codeflash optimize
1 parent a54be51 commit 1625b2a

File tree

5 files changed

+83
-74
lines changed

5 files changed

+83
-74
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ It uses advanced LLMs to generate multiple optimization ideas for your code, tes
1717
How to use Codeflash -
1818
- Optimize an entire existing codebase by running `codeflash --all`
1919
- Automate optimizing all __future__ code you will write by installing Codeflash as a GitHub action.
20-
- Optimize a Python workflow `python myscript.py` end-to-end by running `python -m codeflash.tracer -o benchmark.trace myscript.py`
20+
- Optimize a Python workflow `python myscript.py` end-to-end by running `codeflash optimize myscript.py`
2121

22-
Codeflash is used by top engineering teams at [Pydantic](https://github.com/pydantic/pydantic/pulls?q=is%3Apr+author%3Amisrasaurabh1+is%3Amerged), [Langflow](https://github.com/langflow-ai/langflow/issues?q=state%3Aclosed%20is%3Apr%20author%3Amisrasaurabh1), [Albumentations](https://github.com/albumentations-team/albumentations/issues?q=state%3Amerged%20is%3Apr%20author%3Akrrt7%20OR%20state%3Amerged%20is%3Apr%20author%3Aaseembits93%20) and many others to ship performant, expert level code.
22+
Codeflash is used by top engineering teams at [Pydantic](https://github.com/pydantic/pydantic/pulls?q=is%3Apr+author%3Amisrasaurabh1+is%3Amerged), [Langflow](https://github.com/langflow-ai/langflow/issues?q=state%3Aclosed%20is%3Apr%20author%3Amisrasaurabh1), [Roboflow](https://github.com/roboflow/inference/pulls?q=is%3Apr+is%3Amerged+codeflash+sort%3Acreated-asc), [Albumentations](https://github.com/albumentations-team/albumentations/issues?q=state%3Amerged%20is%3Apr%20author%3Akrrt7%20OR%20state%3Amerged%20is%3Apr%20author%3Aaseembits93%20) and many others to ship performant, expert level code.
2323

24-
Codeflash is great at optimizing AI Agents, Computer Vision algorithms, numerical code, backend code or anything else you might write with Python.
24+
Codeflash is great at optimizing AI Agents, Computer Vision algorithms, PyTorch code, numerical code, backend code or anything else you might write with Python.
2525

2626

2727
## Installation
@@ -50,6 +50,10 @@ Add codeflash as a development time dependency if you are using package managers
5050
codeflash --all
5151
```
5252
This can take a while to run for a large codebase, but it will keep opening PRs as it finds optimizations.
53+
3. Optimize a script:
54+
```
55+
codeflash optimize myscript.py
56+
```
5357

5458
## Documentation
5559
For detailed installation and usage instructions, visit our documentation at [docs.codeflash.ai](https://docs.codeflash.ai)

docs/docs/getting-started/local-installation.md

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ codeflash --all # optimize the entire repo
145145
Make sure:
146146
- ✅ Your virtual environment is activated
147147
- ✅ All project dependencies are installed
148-
- ✅ You're running `codeflash` from your project root
149148
150149
#### 🧪 "No optimizations found" or debugging issues
151150
Use the `--verbose` flag for detailed output:
@@ -165,18 +164,10 @@ Verify:
165164
- 🔍 Tests are discoverable by your test framework
166165
- 📝 Test files follow naming conventions (`test_*.py` for pytest)
167166
168-
#### ⚙️ Configuration issues
169-
Check your `pyproject.toml`:
170-
```toml
171-
[tool.codeflash]
172-
module = "my_package"
173-
test-framework = "pytest"
174-
tests = "tests/"
175-
```
176167
177168
### Next Steps
178169
179170
- Learn about [Codeflash Concepts](/codeflash-concepts/how-codeflash-works)
180-
- Explore [optimization workflows](/optimizing-with-codeflash/one-function)
171+
- Explore [Optimization workflows](/optimizing-with-codeflash/one-function)
181172
- Set up [GitHub Actions integration](/getting-started/codeflash-github-actions)
182173
- Read [configuration options](/configuration) for advanced setups

docs/docs/intro.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ This is a great way to ensure that your code, your team's code and your AI Agent
2727

2828
<!--- TODO: Add links to the relevant sections of the documentation and style the table --->
2929

30-
| Feature | Description |
31-
|-----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
32-
| [Optimize a single function](optimizing-with-codeflash/one-function) | Basic unit of optimization by asking Codeflash to optimize a particular function |
33-
| [Optimize all code in a repo](optimizing-with-codeflash/codeflash-all) | Codeflash discovers all functions in a repo and optimizes all of them! |
34-
| [Optimize every new pull request](optimizing-with-codeflash/optimize-prs) | Codeflash runs as a GitHub action and GitHub app and reviews all new code for Optimizations |
35-
| [Optimize a whole workflow by Tracing it](optimizing-with-codeflash/trace-and-optimize) | End to end optimization for all the functions called in a workflow, by tracing to collect real inputs seen during execution and ensuring correctness and performance optimization with those inputs |
30+
| Feature | Description |
31+
|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
32+
| [Optimize a single function](optimizing-with-codeflash/one-function) | `codeflash --file path.py --function myfunctionpy` Basic unit of optimization by asking Codeflash to optimize a particular function
33+
| [Optimize an entire workflow](optimizing-with-codeflash/trace-and-optimize) | `codeflash optimize myscript.py` End to end optimization for all the functions called in a workflow, by tracing to collect real inputs to ensure correctness and e2e performance optimization |
34+
| [Optimize all code in a repo](optimizing-with-codeflash/codeflash-all) | `codeflash --all` Codeflash discovers all functions in a repo and optimizes all of them! |
35+
| [Optimize every new pull request](optimizing-with-codeflash/optimize-prs) | `codeflash init-actions` Codeflash runs as a GitHub action and GitHub app and reviews all new code for Optimizations |
36+
3637

3738
## How to use these docs
3839

docs/docs/optimizing-with-codeflash/benchmarking.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
sidebar_position: 5
33
---
4-
# Using Benchmarks
4+
# Using Benchmarks in CI
55

66
Codeflash is able to determine the impact of an optimization on predefined benchmarks, when used in benchmark mode.
77

Lines changed: 67 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,97 @@
11
---
22
sidebar_position: 4
33
---
4-
# Optimize Workflows End-to-End by Tracing them.
4+
# Optimize Workflows End-to-End.
55

6-
Codeflash supports optimizing an entire Python script end to end by tracing the execution of the script and generating Replay Tests. Tracing means following the execution of a script and capturing the inputs to all the functions called, so they can be replayed again while optimizing. These Replay Tests can be used to optimize all the functions called in the script.
6+
Codeflash supports optimizing an entire Python script end-to-end by tracing the script's execution and generating Replay Tests. Tracing follows the execution of a script, profiles it and captures inputs to all called functions, allowing them to be replayed during optimization. Codeflash uses these Replay Tests to optimize all functions called in the script, starting from the most important ones.
77

8-
## Motivation for Tracing a workflow
8+
To optimize a script, `python myscript.py`, replace `python` with `codeflash optimize` and run the following command:
99

10-
One of the hard problems with optimizing new code is verifying correctness and performance gains.
11-
The way Codeflash verifies correctness and performance gains is by running the function under optimization against a set of test cases.
12-
These test cases can be part of your existing unit test suite or generated by Codeflash. However, it's tedious to write test cases for every function you want to optimize, plus it's hard to come up with test cases for complex inputs and inputs that cover all edge cases.
13-
Additionally, running the function with these test cases is not a great way to verify performance gains as the test cases might not be representative of the real-world usage of the function.
10+
```bash
11+
codeflash optimize myscript.py
12+
```
13+
14+
To optimize code within pytest tests that you normally run like `python -m pytest tests/`, use this command:
1415

15-
Codeflash Tracer solves these issues.
16+
```bash
17+
codeflash optimize -m pytest tests/
18+
```
1619

17-
## What is Codeflash Tracer?
18-
Codeflash Tracer is a tool that traces the execution of your workflow and generates a set of test cases that are derived from how your code is actually run.
20+
This powerful command creates high-quality optimizations, making it ideal when you need to optimize a workflow or script. The initial tracing process is slow, so try to limit your script's runtime to under 1 minute for best results. If your workflow is longer, consider breaking it into smaller sections and optimizing them separately with limited but representative data.
21+
22+
## What is the codeflash optimize command?
23+
24+
`codeflash optimize` does everything that an expert engineer would do while optimizing a workflow. It profiles your code, traces the execution of your workflow and generates a set of test cases that are derived from how your code is actually run.
1925
Codeflash Tracer works by recording the inputs of your functions as they are called in your codebase. These inputs are then used to generate test cases that are representative of the real-world usage of your functions.
2026
We call these generated test cases "Replay Tests" because they replay the inputs that were recorded during the tracing phase.
2127

2228
Then, Codeflash Optimizer can use these replay tests to verify correctness and calculate accurate performance gains for the optimized functions.
23-
Using Replay Tests, Codeflash can verify that the optimized function produces the same output as the original function and also measure the performance gains of the optimized function on the real-world inputs.
24-
This way you can be _sure_ that the optimized function causes no changes of behavior for the traced workflow and also, that it is faster than the original function.
29+
Using Replay Tests, Codeflash can verify that the optimized functions produce the same output as the original function and also measure the performance gains of the optimized function on the real-world inputs.
30+
This way you can be *sure* that the optimized function causes no changes of behavior for the traced workflow and also, that it is faster than the original function.
2531

26-
## Using Codeflash Tracer
32+
## Using codeflash optimize
2733

28-
Codeflash Tracer can be used in two ways:
34+
Codeflash Tracer can be used in three ways:
2935

30-
1. **As a command line module -**
36+
1. **As an integrated command**
3137

32-
You can use Codeflash Tracer as a module when you run Python.
33-
If you run a Python script as follows -
38+
If you run a Python script as follows
39+
3440
```bash
3541
python path/to/your/file.py --your_options
3642
```
37-
You can trace the execution of the script by running -
43+
44+
You can start tracing and optimizing your code with the following command
45+
3846
```bash
39-
python -m codeflash.tracer -o codeflash.trace path/to/your/file.py --your_options
47+
codeflash optimize path/to/your/file.py --your_options
4048
```
4149

42-
So adding a `-m codeflash.tracer -o codeflash.trace` before your script will trace the execution of the script and save the trace to a file called `codeflash.trace`.
43-
If your script itself runs as a module, you can run it as follows -
50+
The above command should suffice in most situations. You can add a argument like `codeflash optimize -o trace_file_path.trace` if you want to customize the trace file location. Otherwise, it defaults to `codeflash.trace` in the cwd.
51+
52+
2. **Trace and optimize as two separate steps**
53+
54+
If you want more control over the tracing and optimization process. You can trace first and then optimize with the replay tests later. Each replay test is associated with a trace file.
55+
56+
To first create just the trace file, run
57+
58+
```python
59+
codeflash optimize -o trace_file.trace --trace-only path/to/your/file.py --your_options
60+
```
61+
62+
This will create a replay test file. To optimize with the replay test, run the
63+
4464
```bash
45-
python -m codeflash.tracer -o codeflash.trace -m path.to.your.module --your_options
65+
codeflash --replay-test /path/to/test_replay_test_0.py
4666
```
47-
More Options:
48-
- `--max-function-count`: The maximum number of times to trace a single function. More calls to a function will not be traced. Default is 100.
67+
68+
More Options:
4969
- `--tracer-timeout`: The maximum time in seconds to trace the entire workflow. Default is indefinite. This is useful while tracing really long workflows.
50-
2. **As a Context Manager -**
51-
52-
You can also use Codeflash Tracer as a context manager in your codebase.
53-
You can wrap the code you want to trace in a `with` statement as follows -
54-
```python
55-
from codeflash.tracer import Tracer
56-
57-
with Tracer(output="codeflash.trace"):
58-
# Your code here
59-
```
60-
This is useful to only trace and optimize a part of your executable, not the entire script.
61-
Sometimes, if using the tracer as a module fails, then the Context Manager can also be used to trace the code sections.
62-
63-
More Options:
70+
3. **As a Context Manager -**
71+
72+
To trace only very specific sections of your codeflash, You can also use the Codeflash Tracer as a context manager.
73+
You can wrap the code you want to trace in a `with` statement as follows -
74+
75+
```python
76+
from codeflash.tracer import Tracer
77+
78+
with Tracer(output="codeflash.trace"):
79+
# Your code here
80+
```
81+
82+
Sometimes, if using the tracer as a module fails, then the Context Manager can also be used to trace the code sections. This also is much faster than tracing the whole script.
83+
84+
After this finishes, you can optimize using the replay tests
85+
86+
```bash
87+
codeflash --replay-test /path/to/test_replay_test_0.py
88+
```
89+
90+
More Options to the Tracer:
91+
6492
- `disable`: If set to `True`, the tracer will not trace the code. Default is `False`.
6593
- `max_function_count`: The maximum number of times to trace a single function. More calls to a function will not be traced. Default is 100.
6694
- `timeout`: The maximum time in seconds to trace the entire workflow. Default is indefinite. This is useful while tracing really long workflows, to not wait indefinitely.
6795
- `output`: The file to save the trace to. Default is `codeflash.trace`.
6896
- `config_file_path`: The path to the `pyproject.toml` file which stores the Codeflash config. This is auto-discovered by default.
69-
You can also disable the tracer in the code by setting the `disable=True` option in the `Tracer` constructor.
70-
71-
## Optimizing with Replay Tests
72-
After the tracing phase is complete, the tracer will generate a trace file as well as a Replay Test file.
73-
The path of the generated replay test is printed on the console after the tracing is complete. It will be located in your tests directory and have a name like `test_file_getting_traced__replay_test_0.py`.
74-
The Replay Test file is a Python test file that when run will call the traced functions with the recorded inputs, i.e. replay them.
75-
Now Codeflash Optimizer can use these Replay Tests to verify correctness and calculate performance gains of the optimized functions.
76-
77-
To optimize all the functions traced, you can run the following command -
78-
```bash
79-
codeflash --replay-test tests/test_file_getting_traced__replay_test_0.py
80-
```
81-
Codeflash will auto-discover all the functions that were traced, and use the replay tests, plus will discover existing unit tests and generate more tests to get the best optimizations.
82-
Codeflash will open pull requests with the optimized functions as it finds them, which should speed up your end to end workflow!
83-
84-
97+
You can also disable the tracer in the code by setting the `disable=True` option in the `Tracer` constructor.

0 commit comments

Comments
 (0)