Skip to content

The cost algebra — signature reference ​

Signature-level reference for mlopt_core.cost. The conceptual account — why the substrate ships three trust levels of cost — is the three costs; the frozen-name list is the manifest slice.

dag_cost — the shared-subtree-honest price ​

python
dag_cost(term, cost_fn, memo=None) -> float

Walks the program graph — a shared subtree is paid once, so a DAG is never billed as a tree. cost_fn is any (term, memo=None) -> float callable (the CostFn port convention); memo threads the shared-subterm cache. This is the ranking price: static weights, no tensors executed, no claim.

The shipped models ​

familynamesshape
countingcount_cost(term, memo=None), flops_cost(term, memo=None)bare cost fns
parametersparam_bytes_cost(term, memo=None), param_bytes_cost_for(source_tensors=None, by_bytes=False)storage pricing; the _for variant binds the source tensors
backend-awarebackend_cost(cost_fn, supported_ops) -> CostFnwraps a base fn — members using ops outside supported_ops price +inf, so search never commits to a form the sink cannot lower
executorexecutor_cost_for(profile=None, *, lowering="generic", base="roofline") -> CostFnper-executor pricing, calibrated to a target profile
fusedfused_cost_for(profile=None) -> CostFnfusion-aware pricing
lowering-awarelowering_aware_cost_for(profile=None, *, lowerings=LOWERINGS, base="roofline") -> CostFnprices across the LOWERINGS family (("generic", "batched_scan", "compiled"))
depth / rooflinedepth_cost, depth_cost_for(profile=None); roofline_cost, roofline_cost_for(profile=None, *, peak_flops=None, peak_bw=None, launch_s=None)critical-path and roofline pricing
launchlaunch_aware_cost(term, memo=None)launch-overhead pricing
fusion regionsfusion_regions(term, memo=None) -> tuple[frozenset, ...], fusion_member_key(term, memo=None) -> tuple[int, int]the fusible-partition analysis the fused models price over
configurableCostModel(op_weights=None, weight_coeff=1.0, matmul_coeff=2.0)a callable weighted-FLOPs model for EGraph.extract_best

The _for family are factories — they return a CostFn bound to a target profile or lowering; the bare names are ready-made cost fns. A profile is a mlopt_core.profile.TargetProfile-like object or dict; None is the built-in profile.

What a cost can and cannot say ​

dag_cost ranking says "probably cheaper" — it has no notion of memory traffic, kernel fusion, or launch overhead (a fused sdpa looks expensive on paper and is fast under Inductor). Measured pay and the certificate are the domain side's job — see the three costs.

Source: mlopt_core/cost/.