Skip to content

Commit fad3988

Browse files
Unisayclaude
andcommitted
feat: add statistical models and cost parameters for Value builtins
Implement statistical modeling and parameter extraction: - R models for lookupCoin, valueContains, valueData, unValueData - Linear regression models with proper memory scaling - Cost model parameter extraction and conversion infrastructure 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 9f674de commit fad3988

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

plutus-core/cost-model/create-cost-model/BuiltinMemoryModels.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,10 @@ builtinMemoryModels = BuiltinCostModelBase
176176
, paramLengthOfArray = Id $ ModelOneArgumentConstantCost 10
177177
, paramListToArray = Id $ ModelOneArgumentLinearInX $ OneVariableLinearFunction 7 1
178178
, paramIndexArray = Id $ ModelTwoArgumentsConstantCost 32
179+
-- Builtin values
180+
, paramLookupCoin = Id $ ModelThreeArgumentsConstantCost 1
181+
, paramValueContains = Id $ ModelTwoArgumentsConstantCost 1
182+
, paramValueData = Id $ ModelOneArgumentConstantCost 1
183+
, paramUnValueData = Id $ ModelOneArgumentConstantCost 1
179184
}
180185
where identityFunction = OneVariableLinearFunction 0 1

plutus-core/cost-model/create-cost-model/CreateBuiltinCostModel.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ builtinCostModelNames = BuiltinCostModelBase
131131
, paramLengthOfArray = "lengthOfArrayModel"
132132
, paramListToArray = "listToArrayModel"
133133
, paramIndexArray = "indexArrayModel"
134+
-- Builtin values
135+
, paramLookupCoin = "lookupCoinModel"
136+
, paramValueContains = "valueContainsModel"
137+
, paramValueData = "valueDataModel"
138+
, paramUnValueData = "unValueDataModel"
134139
}
135140

136141

@@ -279,6 +284,11 @@ createBuiltinCostModel bmfile rfile = do
279284
paramLengthOfArray <- getParams readCF1 paramLengthOfArray
280285
paramListToArray <- getParams readCF1 paramListToArray
281286
paramIndexArray <- getParams readCF2 paramIndexArray
287+
-- Builtin values
288+
paramLookupCoin <- getParams readCF3 paramLookupCoin
289+
paramValueContains <- getParams readCF2 paramValueContains
290+
paramValueData <- getParams readCF1 paramValueData
291+
paramUnValueData <- getParams readCF1 paramUnValueData
282292

283293
pure $ BuiltinCostModelBase {..}
284294

plutus-core/cost-model/data/models.R

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ arity <- function(name) {
152152
"LengthOfArray" = 1,
153153
"ListToArray" = 1,
154154
"IndexArray" = 2,
155+
"LookupCoin" = 3,
156+
"ValueContains" = 2,
157+
"ValueData" = 1,
158+
"UnValueData" = 1,
155159
-1 ## Default for missing values
156160
)
157161
}
@@ -804,11 +808,28 @@ modelFun <- function(path) {
804808

805809
dropListModel <- linearInX ("DropList")
806810

807-
## Arrays
811+
## Arrays
808812
lengthOfArrayModel <- constantModel ("LengthOfArray")
809813
listToArrayModel <- linearInX ("ListToArray")
810814
indexArrayModel <- constantModel ("IndexArray")
811815

816+
## Values
817+
lookupCoinModel <- linearInZ ("LookupCoin")
818+
819+
## ValueContains is O(n₂ × log max(m₁, k₁)) where n₂ is the total size of the second Value
820+
## We model this as linear in the sum of sizes, which is conservative
821+
valueContainsModel <- {
822+
fname <- "ValueContains"
823+
filtered <- data %>%
824+
filter.and.check.nonempty(fname) %>%
825+
discard.upper.outliers()
826+
m <- lm(t ~ I(x_mem + y_mem), filtered)
827+
mk.result(m, "added_sizes")
828+
}
829+
830+
valueDataModel <- constantModel ("ValueData")
831+
unValueDataModel <- linearInX ("UnValueData")
832+
812833
##### Models to be returned to Haskell #####
813834

814835
models.for.adjustment <-
@@ -902,7 +923,11 @@ modelFun <- function(path) {
902923
dropListModel = dropListModel,
903924
lengthOfArrayModel = lengthOfArrayModel,
904925
listToArrayModel = listToArrayModel,
905-
indexArrayModel = indexArrayModel
926+
indexArrayModel = indexArrayModel,
927+
lookupCoinModel = lookupCoinModel,
928+
valueContainsModel = valueContainsModel,
929+
valueDataModel = valueDataModel,
930+
unValueDataModel = unValueDataModel
906931
)
907932

908933
## The integer division functions have a complex costing behaviour that requires some negative

0 commit comments

Comments
 (0)