Skip to content

Commit abd107e

Browse files
authored
feat: export write_trace in pdl.pdl (#966)
Signed-off-by: Louis Mandel <[email protected]>
1 parent b96450f commit abd107e

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

src/pdl/pdl.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
from .pdl_lazy import PdlDict
2323
from .pdl_parser import parse_dict, parse_file, parse_str
2424
from .pdl_runner import exec_docker
25-
from .pdl_utils import validate_scope
25+
from .pdl_utils import ( # pylint: disable=unused-import # noqa: F401
26+
validate_scope,
27+
write_trace,
28+
)
2629

2730

2831
class InterpreterConfig(TypedDict, total=False):

src/pdl/pdl_interpreter.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@
103103
deserialize,
104104
ensure_context,
105105
)
106-
from .pdl_dumper import as_json, block_to_dict # noqa: E402
107106
from .pdl_lazy import PdlConst, PdlDict, PdlLazy, PdlList, lazy_apply # noqa: E402
108107
from .pdl_llms import LitellmModel # noqa: E402
109108
from .pdl_location_utils import append, get_loc_string # noqa: E402
@@ -119,6 +118,7 @@
119118
replace_contribute_value,
120119
stringify,
121120
value_of_expr,
121+
write_trace,
122122
)
123123

124124
empty_scope: ScopeType = PdlDict({"pdl_context": DependentContext([])})
@@ -202,25 +202,6 @@ def generate(
202202
return 0
203203

204204

205-
def write_trace(
206-
trace_file: str | Path,
207-
trace: BlockType,
208-
):
209-
"""Write the execution trace into a file.
210-
211-
Args:
212-
trace_file: File to save the execution trace.
213-
trace: Execution trace.
214-
"""
215-
try:
216-
d: Any = block_to_dict(trace, json_compatible=True)
217-
d = as_json(d)
218-
with open(trace_file, "w", encoding="utf-8") as fp:
219-
json.dump(d, fp)
220-
except Exception as e:
221-
print(f"Failure generating the trace: {str(e)}", file=sys.stderr)
222-
223-
224205
def process_prog(
225206
state: InterpreterState,
226207
scope: ScopeType,

src/pdl/pdl_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import fnmatch
22
import json
3+
import sys
4+
from pathlib import Path
35
from typing import Any, Generator, Generic, Sequence, TypeVar
46

57
from .pdl_ast import (
8+
BlockType,
69
ContributeTarget,
710
ContributeValue,
811
ExpressionType,
912
FunctionBlock,
1013
LocalizedExpression,
1114
get_sampling_defaults,
1215
)
16+
from .pdl_dumper import as_json, block_to_dict
1317

1418
GeneratorWrapperYieldT = TypeVar("GeneratorWrapperYieldT")
1519
GeneratorWrapperSendT = TypeVar("GeneratorWrapperSendT")
@@ -196,3 +200,22 @@ def validate_pdl_model_defaults(model_defaults: list[dict[str, dict[str, Any]]])
196200
f"invalid defaults {glob_defaults} for model matcher {model_glob}"
197201
)
198202
assert isinstance(glob_defaults, dict)
203+
204+
205+
def write_trace(
206+
trace_file: str | Path,
207+
trace: BlockType,
208+
):
209+
"""Write the execution trace into a file.
210+
211+
Args:
212+
trace_file: File to save the execution trace.
213+
trace: Execution trace.
214+
"""
215+
try:
216+
d: Any = block_to_dict(trace, json_compatible=True)
217+
d = as_json(d)
218+
with open(trace_file, "w", encoding="utf-8") as fp:
219+
json.dump(d, fp)
220+
except Exception as e:
221+
print(f"Failure generating the trace: {str(e)}", file=sys.stderr)

0 commit comments

Comments
 (0)