Skip to content

introduce a new integrated "codeflash optimize" command #384

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4debe7e
introduce a new integrated "codeflash optimize" command
misrasaurabh1 Jun 26, 2025
535a9b1
Merge branch 'main' into trace-and-optimize
KRRT7 Jun 26, 2025
09bf156
Merge branch 'main' into trace-and-optimize
KRRT7 Jun 27, 2025
0b4fcb6
rank functions
KRRT7 Jun 28, 2025
059b4dc
Merge branch 'main' into trace-and-optimize
KRRT7 Jun 30, 2025
7f9a609
implement reranker
KRRT7 Jun 30, 2025
eb9e0c6
allow predict to be included
KRRT7 Jul 1, 2025
ce68cad
fix tracer for static methods
KRRT7 Jul 1, 2025
b7258a9
Merge branch 'main' into trace-and-optimize
KRRT7 Jul 1, 2025
72b51c1
⚡️ Speed up method `FunctionRanker._get_function_stats` by 51% in PR …
codeflash-ai[bot] Jul 1, 2025
67bd717
Merge pull request #466 from codeflash-ai/codeflash/optimize-pr384-20…
misrasaurabh1 Jul 1, 2025
ea16342
update tests
KRRT7 Jul 1, 2025
947ab07
don't let the AI replicate
KRRT7 Jul 2, 2025
4823ee5
Merge branch 'main' into trace-and-optimize
KRRT7 Jul 2, 2025
faebe9b
ruff
KRRT7 Jul 2, 2025
a0e57ba
mypy-ruff
KRRT7 Jul 2, 2025
fd1e492
silence test collection warnings
KRRT7 Jul 2, 2025
f7c8a6b
Update function_ranker.py
KRRT7 Jul 2, 2025
35059a9
Update workload.py
KRRT7 Jul 2, 2025
f74d947
update CI
KRRT7 Jul 2, 2025
9addd95
update cov numbers
KRRT7 Jul 2, 2025
70cecaf
rank only, change formula
KRRT7 Jul 3, 2025
96acfc7
per module ranking
KRRT7 Jul 3, 2025
e5e1ff0
update tests
KRRT7 Jul 3, 2025
eba8cb8
move to env utils, pre-commit
KRRT7 Jul 3, 2025
9955081
Merge branch 'main' of https://github.com/codeflash-ai/codeflash into…
KRRT7 Jul 3, 2025
692f46e
Merge branch 'main' into trace-and-optimize
KRRT7 Jul 3, 2025
e2e6803
add markers
KRRT7 Jul 3, 2025
4560b8b
Merge branch 'main' into trace-and-optimize
KRRT7 Jul 3, 2025
39e0859
Update cli.py
KRRT7 Jul 3, 2025
c09f32e
Revert "Update cli.py"
KRRT7 Jul 3, 2025
60922b8
allow args for the optimize command too
KRRT7 Jul 3, 2025
bf6313f
fix parsing
KRRT7 Jul 4, 2025
87f44a2
fix parsing
KRRT7 Jul 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions codeflash/cli_cmds/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,34 @@ def parse_args() -> Namespace:
init_actions_parser.set_defaults(func=install_github_actions)

trace_optimize = subparsers.add_parser("optimize", help="Trace and optimize a Python project.")

from codeflash.tracer import main as tracer_main

trace_optimize.set_defaults(func=tracer_main)

trace_optimize.add_argument(
"--max-function-count",
type=int,
default=100,
help="The maximum number of times to trace a single function. More calls to a function will not be traced. Default is 100.",
)
trace_optimize.add_argument(
"--timeout",
type=int,
help="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.",
)
trace_optimize.add_argument(
"--output",
type=str,
default="codeflash.trace",
help="The file to save the trace to. Default is codeflash.trace.",
)
trace_optimize.add_argument(
"--config-file-path",
type=str,
help="The path to the pyproject.toml file which stores the Codeflash config. This is auto-discovered by default.",
)

parser.add_argument("--file", help="Try to optimize only this file")
parser.add_argument("--function", help="Try to optimize only this function within the given file path")
parser.add_argument(
Expand Down
35 changes: 26 additions & 9 deletions codeflash/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from codeflash.verification.verification_utils import get_test_file_path

if TYPE_CHECKING:
from argparse import Namespace
from types import FrameType, TracebackType


Expand Down Expand Up @@ -798,7 +799,7 @@ def runctx(self, cmd: str, global_vars: dict[str, Any], local_vars: dict[str, An
return self


def main() -> ArgumentParser:
def main(args: Namespace | None = None) -> ArgumentParser:
parser = ArgumentParser(allow_abbrev=False)
parser.add_argument("-o", "--outfile", dest="outfile", help="Save trace to <outfile>", default="codeflash.trace")
parser.add_argument("--only-functions", help="Trace only these functions", nargs="+", default=None)
Expand All @@ -824,18 +825,34 @@ def main() -> ArgumentParser:
)
parser.add_argument("--trace-only", action="store_true", help="Trace and create replay tests only, don't optimize")

if not sys.argv[1:]:
parser.print_usage()
sys.exit(2)
if args is not None:
parsed_args = args
parsed_args.outfile = getattr(args, "output", "codeflash.trace")
parsed_args.only_functions = getattr(args, "only_functions", None)
parsed_args.max_function_count = getattr(args, "max_function_count", 100)
parsed_args.tracer_timeout = getattr(args, "timeout", None)
parsed_args.codeflash_config = getattr(args, "config_file_path", None)
parsed_args.trace_only = getattr(args, "trace_only", False)
parsed_args.module = False

if getattr(args, "disable", False):
console.rule("Codeflash: Tracer disabled by --disable option", style="bold red")
return parser

unknown_args = []
else:
if not sys.argv[1:]:
parser.print_usage()
sys.exit(2)

args, unknown_args = parser.parse_known_args()
sys.argv[:] = unknown_args
parsed_args, unknown_args = parser.parse_known_args()
sys.argv[:] = unknown_args

# The script that we're profiling may chdir, so capture the absolute path
# to the output file at startup.
if args.outfile is not None:
args.outfile = Path(args.outfile).resolve()
outfile = args.outfile
if parsed_args.outfile is not None:
parsed_args.outfile = Path(parsed_args.outfile).resolve()
outfile = parsed_args.outfile

if len(unknown_args) > 0:
if args.module:
Expand Down
Loading