Skip to content

Commit cc206a5

Browse files
authored
feat: save factor score in the trace (#1350)
Signed-off-by: Louis Mandel <[email protected]>
1 parent c274f93 commit cc206a5

File tree

5 files changed

+36
-0
lines changed

5 files changed

+36
-0
lines changed

pdl-live-react/src/pdl_ast.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,15 @@ export type Contribute19 = ContributeElement[]
447447
export type ExpectationsType19 = ExpectationType[]
448448
export type PdlIsLeaf19 = true
449449
export type Kind19 = "factor"
450+
/**
451+
* Score to condition the model.
452+
*
453+
*/
450454
export type Factor = LocalizedExpression | number | string
455+
/**
456+
* Allow to raise the Resampling exception during the execution of the block.
457+
*
458+
*/
451459
export type Resample = boolean
452460
/**
453461
* Indicate if the block contributes to the result and background context.
@@ -3368,6 +3376,7 @@ export interface FactorBlock {
33683376
kind?: Kind19
33693377
factor: Factor
33703378
resample?: Resample
3379+
pdl__score?: number | null
33713380
}
33723381
/**
33733382
* Set of definitions executed before the execution of the block.

src/pdl/pdl-schema.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,18 @@
14251425
"type": "string"
14261426
}
14271427
],
1428+
"description": "Score to condition the model.\n ",
14281429
"title": "Factor"
14291430
},
14301431
"resample": {
14311432
"default": true,
1433+
"description": "Allow to raise the Resampling exception during the execution of the block.\n ",
14321434
"title": "Resample",
14331435
"type": "boolean"
1436+
},
1437+
"pdl__score": {
1438+
"$ref": "#/$defs/OptionalFloat",
1439+
"default": null
14341440
}
14351441
},
14361442
"required": [
@@ -4029,6 +4035,16 @@
40294035
}
40304036
]
40314037
},
4038+
"OptionalFloat": {
4039+
"anyOf": [
4040+
{
4041+
"type": "number"
4042+
},
4043+
{
4044+
"type": "null"
4045+
}
4046+
]
4047+
},
40324048
"OptionalInt": {
40334049
"anyOf": [
40344050
{

src/pdl/pdl_ast.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ def _ensure_lower(value):
4141
"""Optional string."""
4242
OptionalInt = TypeAliasType("OptionalInt", Optional[int])
4343
"""Optional integer."""
44+
OptionalFloat = TypeAliasType("OptionalFloat", Optional[float])
45+
"""Optional floating point number."""
4446
OptionalBool = TypeAliasType("OptionalBool", Optional[bool])
4547
"""Optional Boolean."""
4648
OptionalBoolOrStr = TypeAliasType("OptionalBoolOrStr", Optional[Union[bool, str]])
@@ -1093,7 +1095,13 @@ class FactorBlock(LeafBlock):
10931095

10941096
kind: Literal[BlockKind.FACTOR] = BlockKind.FACTOR
10951097
factor: ExpressionType[float]
1098+
"""Score to condition the model.
1099+
"""
10961100
resample: bool = True
1101+
"""Allow to raise the Resampling exception during the execution of the block.
1102+
"""
1103+
# Field for internal use
1104+
pdl__score: OptionalFloat = None
10971105

10981106

10991107
class AggregatorConfig(BaseModel):

src/pdl/pdl_dumper.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def block_to_dict( # noqa: C901
229229
d["factor"] = expr_to_dict(block.factor, json_compatible)
230230
if not block.resample:
231231
d["resample"] = block.resample
232+
if block.pdl__score is not None:
233+
d["pdl__score"] = block.pdl__score
232234
case AggregatorBlock():
233235
d["aggregator"] = aggregator_to_dict(block.aggregator)
234236
case IfBlock():

src/pdl/pdl_interpreter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,7 @@ def loop_body(iidx, items):
11381138
weight, trace = process_expr_of(
11391139
block, "factor", scope, append(loc, "factor")
11401140
)
1141+
trace.pdl__score = weight
11411142
state.score.ref += weight
11421143
result = PdlConst("")
11431144
background = DependentContext([])

0 commit comments

Comments
 (0)