Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
102 commits
Select commit Hold shift + click to select a range
0a57afa
first pass at wrapper deco behavioral
KRRT7 Aug 26, 2025
c9aaaad
don't reapply too early
KRRT7 Aug 26, 2025
ad1d085
feedback
KRRT7 Aug 27, 2025
14353d4
Update codeflash_wrap_decorator.py
KRRT7 Aug 27, 2025
3c05f97
Update function_optimizer.py
KRRT7 Aug 28, 2025
bafad49
Update codeflash_wrap_decorator.py
KRRT7 Aug 28, 2025
745a3f7
test gen for async too
KRRT7 Aug 29, 2025
5a47237
Update aiservice.py
KRRT7 Aug 29, 2025
1ab1886
create path object once
KRRT7 Aug 29, 2025
af96274
use sqlite vs pickle
KRRT7 Aug 29, 2025
4dfda09
no dependencies on codeflash
KRRT7 Aug 29, 2025
2a2a3a3
add tests for async wrapper
KRRT7 Aug 29, 2025
b569d44
linting
KRRT7 Aug 29, 2025
4514dd6
address feedback re: using existing logic from tracer & cf capture
KRRT7 Aug 29, 2025
fb9555e
test
KRRT7 Aug 30, 2025
66f7a84
add unit tests
KRRT7 Aug 30, 2025
aa872b5
be consistent
KRRT7 Aug 30, 2025
0319803
Update codeflash_wrap_decorator.py
KRRT7 Aug 30, 2025
9bbe988
some async changes
KRRT7 Aug 31, 2025
1fe7c04
Update test_add_runtime_comments.py
KRRT7 Aug 31, 2025
91b8902
Update codeflash/code_utils/codeflash_wrap_decorator.py
misrasaurabh1 Sep 2, 2025
0f066c0
Merge branch 'granular-async-instrumentation' of https://github.com/c…
KRRT7 Sep 2, 2025
69fa701
mirror tests
KRRT7 Sep 3, 2025
862415e
add support for async tests in edit_generated_tests
KRRT7 Sep 3, 2025
febf2b6
add tests with run_and_parse_tests
KRRT7 Sep 3, 2025
61aade1
Update test_async_run_and_parse_tests.py
KRRT7 Sep 3, 2025
5bf5c20
⚡️ Speed up method `CommentMapper.visit_ClassDef` by 197% in PR #687 …
codeflash-ai[bot] Sep 3, 2025
b2bf9fa
add missing imports
KRRT7 Sep 3, 2025
ef74bb5
Update edit_generated_tests.py
KRRT7 Sep 3, 2025
e119085
Merge pull request #706 from codeflash-ai/codeflash/optimize-pr687-20…
KRRT7 Sep 3, 2025
600dafc
⚡️ Speed up method `CommentMapper.visit_FunctionDef` by 84% in PR #68…
codeflash-ai[bot] Sep 3, 2025
1142de5
linting / formatting
KRRT7 Sep 3, 2025
14280fa
Merge pull request #709 from codeflash-ai/codeflash/optimize-pr687-20…
KRRT7 Sep 3, 2025
3ca6fad
linter
KRRT7 Sep 3, 2025
73d03ae
add E2E
KRRT7 Sep 3, 2025
0af669f
Update workload.py
KRRT7 Sep 3, 2025
1ebc616
Update workload.py
KRRT7 Sep 3, 2025
d8a6ab2
temp
KRRT7 Sep 3, 2025
7bcd136
go
KRRT7 Sep 3, 2025
2429801
restore original code if not found for async
KRRT7 Sep 3, 2025
a364075
basic async critic - WIP
KRRT7 Sep 8, 2025
a535fa2
for AsyncFunctionDef too
KRRT7 Sep 8, 2025
075a2ae
add unit tests
KRRT7 Sep 8, 2025
4fac1e2
increase test coverage
KRRT7 Sep 8, 2025
8039ec4
coverage utils
KRRT7 Sep 8, 2025
0a296e2
more unit tests
KRRT7 Sep 8, 2025
189fe12
linting
KRRT7 Sep 8, 2025
878699a
Delete e2e-async.yaml
KRRT7 Sep 8, 2025
52a5a0c
Merge branch 'granular-async-instrumentation' of https://github.com/c…
KRRT7 Sep 8, 2025
ce421d4
Revert "basic async critic - WIP"
KRRT7 Sep 9, 2025
a6072b9
Merge branch 'granular-async-instrumentation' of https://github.com/c…
KRRT7 Sep 9, 2025
b189f1b
Merge pull request #719 from codeflash-ai/async-support-for
KRRT7 Sep 9, 2025
07cc91b
use async event loop timer
KRRT7 Sep 10, 2025
60b356c
disable crosshair for concolic
KRRT7 Sep 11, 2025
7e4ecb3
add debugs
KRRT7 Sep 11, 2025
ab500c1
Update codeflash.code-workspace
KRRT7 Sep 13, 2025
db82269
Optimize compare_test_results
codeflash-ai[bot] Sep 13, 2025
324f607
Merge pull request #732 from codeflash-ai/codeflash/optimize-pr687-20…
KRRT7 Sep 13, 2025
f98dae6
Update parse_test_output.py
KRRT7 Sep 16, 2025
96211b8
add tests_project_root to the wrappers
KRRT7 Sep 16, 2025
f872d5d
Update codeflash.code-workspace
KRRT7 Sep 16, 2025
ee1d386
Revert "add tests_project_root to the wrappers"
KRRT7 Sep 16, 2025
b858881
Update test_runner.py
KRRT7 Sep 16, 2025
a42c63a
Delete test_extract_test_context_from_frame.py
KRRT7 Sep 17, 2025
fadbdf7
better impl
KRRT7 Sep 17, 2025
1034ee8
Update parse_test_output.py
KRRT7 Sep 17, 2025
4ae0f93
calculate throughput
KRRT7 Sep 17, 2025
fb66ff5
add unit tests
KRRT7 Sep 17, 2025
2190963
fix wrapper validation test
KRRT7 Sep 17, 2025
2a3c975
fix linting
KRRT7 Sep 17, 2025
9c53948
cleanup
KRRT7 Sep 17, 2025
e5c4562
Update function_optimizer.py
KRRT7 Sep 17, 2025
04d31b5
optimized candidate result oo
KRRT7 Sep 17, 2025
8e516fc
critic
KRRT7 Sep 18, 2025
d7f9bbd
update tests again
KRRT7 Sep 18, 2025
58e5592
Update codeflash_wrap_decorator.py
KRRT7 Sep 18, 2025
a7a06ea
Update test_async_wrapper_sqlite_validation.py
KRRT7 Sep 22, 2025
d1b40d0
check.
KRRT7 Sep 22, 2025
a947ea0
go
KRRT7 Sep 22, 2025
2e0f38f
throughput
KRRT7 Sep 22, 2025
d3eefca
perfstdout
KRRT7 Sep 22, 2025
039c5ba
calculate throughtput for baseline
KRRT7 Sep 22, 2025
aba3dcb
Update function_optimizer.py
KRRT7 Sep 22, 2025
d3afd8a
Update critic.py
KRRT7 Sep 22, 2025
971470e
adjust critic
KRRT7 Sep 22, 2025
00a8981
update tests
KRRT7 Sep 22, 2025
1e4b3ba
pre-commit fixes
KRRT7 Sep 22, 2025
654055d
unbound local
KRRT7 Sep 22, 2025
540b8aa
Optimize AsyncCallInstrumenter.visit_ClassDef
codeflash-ai[bot] Sep 22, 2025
7aa30a8
Merge pull request #744 from codeflash-ai/codeflash/optimize-pr739-20…
KRRT7 Sep 22, 2025
fecdc94
Update .pre-commit-config.yaml
KRRT7 Sep 22, 2025
eccdeff
Update pre-commit.yaml
KRRT7 Sep 22, 2025
8b1b2be
Update .pre-commit-config.yaml
KRRT7 Sep 22, 2025
515b976
Delete pre-commit.yaml
KRRT7 Sep 22, 2025
30c8988
give throughput context to explanations
KRRT7 Sep 22, 2025
d8849a5
logger.debug
KRRT7 Sep 23, 2025
47384e2
add end to end test
KRRT7 Sep 23, 2025
43615bd
Create e2e-async.yaml
KRRT7 Sep 23, 2025
9aa34d9
Merge pull request #752 from codeflash-ai/end-to-end-test
KRRT7 Sep 23, 2025
b221be4
Merge pull request #739 from codeflash-ai/get-throughput-from-output
KRRT7 Sep 24, 2025
b6b4b63
only for debug
KRRT7 Sep 26, 2025
7bbb1e7
adjust tests
KRRT7 Sep 26, 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
159 changes: 159 additions & 0 deletions codeflash/code_utils/codeflash_wrap_decorator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
from __future__ import annotations

import gc
import inspect
import os
import pickle
import time
from functools import wraps
from pathlib import Path
from typing import Any, Callable, TypeVar

from codeflash.code_utils.code_utils import get_run_tmp_file

F = TypeVar("F", bound=Callable[..., Any])


def extract_test_context_from_frame() -> tuple[str, str | None, str]:
frame = inspect.currentframe()
try:
while frame:
frame = frame.f_back
if frame and frame.f_code.co_name.startswith("test_"):
test_name = frame.f_code.co_name
test_module_name = frame.f_globals.get("__name__", "unknown_module")
test_class_name = None
if "self" in frame.f_locals:
test_class_name = frame.f_locals["self"].__class__.__name__

return test_module_name, test_class_name, test_name
raise RuntimeError("No test function found in call stack")
finally:
del frame


def codeflash_behavior_async(func: F) -> F:
function_name = func.__name__
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
loop_index = int(os.environ.get("CODEFLASH_LOOP_INDEX", "1"))

@wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
test_module_name, test_class_name, test_name = extract_test_context_from_frame()

test_id = f"{test_module_name}:{test_class_name}:{test_name}:{line_id}:{loop_index}"

if not hasattr(async_wrapper, "index"):
async_wrapper.index = {}
if test_id in async_wrapper.index:
async_wrapper.index[test_id] += 1
else:
async_wrapper.index[test_id] = 0

codeflash_test_index = async_wrapper.index[test_id]
invocation_id = f"{line_id}_{codeflash_test_index}"
test_stdout_tag = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{loop_index}:{invocation_id}"

print(f"!$######{test_stdout_tag}######$!")

exception = None
gc.disable()
try:
counter = time.perf_counter_ns()
ret = func(*args, **kwargs)

if inspect.isawaitable(ret):
counter = time.perf_counter_ns()
return_value = await ret
else:
return_value = ret

codeflash_duration = time.perf_counter_ns() - counter
except Exception as e:
codeflash_duration = time.perf_counter_ns() - counter
exception = e
finally:
gc.enable()

print(f"!######{test_stdout_tag}######!")

iteration = os.environ.get("CODEFLASH_TEST_ITERATION", "0")

codeflash_run_tmp_dir = get_run_tmp_file(Path()).as_posix()

output_file = Path(codeflash_run_tmp_dir) / f"test_return_values_{iteration}.bin"

with output_file.open("ab") as f:
pickled_values = (
pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
)
_test_name = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{line_id}".encode(
"ascii"
)

f.write(len(_test_name).to_bytes(4, byteorder="big"))
f.write(_test_name)
f.write(codeflash_duration.to_bytes(8, byteorder="big"))
f.write(len(pickled_values).to_bytes(4, byteorder="big"))
f.write(pickled_values)
f.write(loop_index.to_bytes(8, byteorder="big"))
f.write(len(invocation_id).to_bytes(4, byteorder="big"))
f.write(invocation_id.encode("ascii"))

if exception:
raise exception
return return_value

return async_wrapper


def codeflash_performance_async(func: F) -> F:
function_name = func.__name__
line_id = f"{func.__name__}_{func.__code__.co_firstlineno}"
loop_index = int(os.environ.get("CODEFLASH_LOOP_INDEX", "1"))

@wraps(func)
async def async_wrapper(*args: Any, **kwargs: Any) -> Any: # noqa: ANN401
test_module_name, test_class_name, test_name = extract_test_context_from_frame()

test_id = f"{test_module_name}:{test_class_name}:{test_name}:{line_id}:{loop_index}"

if not hasattr(async_wrapper, "index"):
async_wrapper.index = {}
if test_id in async_wrapper.index:
async_wrapper.index[test_id] += 1
else:
async_wrapper.index[test_id] = 0

codeflash_test_index = async_wrapper.index[test_id]
invocation_id = f"{line_id}_{codeflash_test_index}"
test_stdout_tag = f"{test_module_name}:{(test_class_name + '.' if test_class_name else '')}{test_name}:{function_name}:{loop_index}:{invocation_id}"

print(f"!$######{test_stdout_tag}######$!")

exception = None
gc.disable()
try:
counter = time.perf_counter_ns()
ret = func(*args, **kwargs)

if inspect.isawaitable(ret):
counter = time.perf_counter_ns()
return_value = await ret
else:
return_value = ret

codeflash_duration = time.perf_counter_ns() - counter
except Exception as e:
codeflash_duration = time.perf_counter_ns() - counter
exception = e
finally:
gc.enable()

print(f"!######{test_stdout_tag}:{codeflash_duration}######!")

if exception:
raise exception
return return_value

return async_wrapper
Loading
Loading