Skip to content

Commit

Permalink
Use FilePath for cost model paths (IntersectMBO#3429)
Browse files Browse the repository at this point in the history
* Use FilePath for cost model paths

* updateMaterialzed

* Remove filepath dependency

* updateMaterialzed

* Address PR comments

* updateMaterialzed
  • Loading branch information
Kenneth MacKenzie authored Jun 25, 2021
1 parent 0c66016 commit 2ae7a02
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions plutus-core/cost-model/budgeting-bench/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Main (main) where

import PlutusCore as PLC
import qualified PlutusCore.DataFilePaths as DFP
import PlutusCore.Evaluation.Machine.ExMemory
import PlutusCore.MkPlc
import UntypedPlutusCore as UPLC
Expand All @@ -19,7 +20,6 @@ import qualified Hedgehog.Internal.Gen as HH
import qualified Hedgehog.Internal.Tree as HH
import qualified Hedgehog.Range as HH.Range
import System.Directory
import System.FilePath
import System.Random (StdGen, getStdGen, randomR)

type PlainTerm = UPLC.Term Name DefaultUni DefaultFun ()
Expand Down Expand Up @@ -325,14 +325,11 @@ benchNop3 gen =
main :: IO ()
main = do
gen <- System.Random.getStdGen -- We use the initial state of gen repeatedly below, but that doesn't matter.
let dataDir = "cost-model" </> "data"
csvFile = dataDir </> "benching.csv"
backupFile = dataDir </> "benching.csv.backup"
createDirectoryIfMissing True dataDir
csvExists <- doesFileExist csvFile
if csvExists then renameFile csvFile backupFile else pure ()

defaultMainWith (defaultConfig { C.csvFile = Just csvFile }) $
createDirectoryIfMissing True DFP.costModelDataDir
csvExists <- doesFileExist DFP.benchingResultsFile
if csvExists then renameFile DFP.benchingResultsFile DFP.backupBenchingResultsFile else pure ()

defaultMainWith (defaultConfig { C.csvFile = Just DFP.benchingResultsFile }) $
[benchNop1 gen, benchNop2 gen, benchNop3 gen]
<> (benchTwoIntegers gen <$> [ AddInteger
, MultiplyInteger
Expand All @@ -349,3 +346,4 @@ main = do
, LtByteString
])
<> [benchVerifySignature]

6 changes: 4 additions & 2 deletions plutus-core/cost-model/create-cost-model/CostModelCreation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Foreign.R
import H.Prelude (MonadR, Region, r)
import Language.R


-- | Convert milliseconds represented as a float to picoseconds represented as a
-- CostingInteger. We round up to be sure we don't underestimate anything.
msToPs :: Double -> CostingInteger
Expand Down Expand Up @@ -75,8 +74,11 @@ costModelsR = do
source("cost-model/data/models.R")
modelFun("cost-model/data/benching.csv")
|]
-- TODO use btraverse instead
-- Unfortunately we can't use the paths defined in DataFilePaths inside [r|...].
-- The above code may not work on Windows because of that, but we only ever
-- want to run this on a Linux reference machine anyway.
bsequence $ bmap (\name -> let n = getConst name in Compose $ fmap Const $ [r| list_hs[[n_hs]] |]) builtinCostModelNames
-- TODO ^ use btraverse instead

-- Creates the cost model from the csv benchmarking files
createBuiltinCostModel :: IO BuiltinCostModel
Expand Down
4 changes: 3 additions & 1 deletion plutus-core/cost-model/create-cost-model/UpdateCostModel.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Main where

import PlutusCore.DataFilePaths

import Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy as BSL

Expand All @@ -10,4 +12,4 @@ import CostModelCreation
main :: IO ()
main = do
model <- createBuiltinCostModel
BSL.writeFile "cost-model/data/builtinCostModel.json" $ encodePretty' (defConfig { confCompare = \_ _-> EQ }) model
BSL.writeFile builtinCostModelFile $ encodePretty' (defConfig { confCompare = \_ _-> EQ }) model
2 changes: 1 addition & 1 deletion plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ library
PlutusCore.Constant
PlutusCore.Constant.Dynamic.Emit
PlutusCore.Core
PlutusCore.DataFilePaths
PlutusCore.DeBruijn
PlutusCore.Default
PlutusCore.Error
Expand Down Expand Up @@ -437,7 +438,6 @@ benchmark cost-model-budgeting-bench
bytestring -any,
criterion -any,
directory -any,
filepath -any,
hedgehog -any,
random -any

Expand Down
32 changes: 32 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/DataFilePaths.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- | Various file paths used in plutus-core, currently all to do with the cost
-- model.

-- Note that a couple of these paths are also used inside an inline-r splice in
-- CostModelCreation.hs but we have to use literal strings there, not the
-- versions defined here. Those strings will also have to be updated if things
-- change here.

module PlutusCore.DataFilePaths
where

import System.FilePath

costModelDataDir :: FilePath
costModelDataDir = "cost-model" </> "data"

-- A literal version of this is also used in CostModelCreation.hs
modelFile :: FilePath
modelFile = costModelDataDir </> "models" <.> "R"

-- A literal version of this is also used in CostModelCreation.hs
benchingResultsFile :: FilePath
benchingResultsFile = costModelDataDir </> "benching" <.> "csv"

backupBenchingResultsFile :: FilePath
backupBenchingResultsFile = benchingResultsFile <.> "backup"

builtinCostModelFile :: FilePath
builtinCostModelFile = costModelDataDir </> "builtinCostModel" <.> "json"

cekMachineCostsFile :: FilePath
cekMachineCostsFile = costModelDataDir </> "cekMachineCosts" <.> "json"
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import Data.Aeson.THReader

import PlutusCore.Constant

import qualified PlutusCore.DataFilePaths as DFP
import PlutusCore.Default
import PlutusCore.Evaluation.Machine.BuiltinCostModel
import PlutusCore.Evaluation.Machine.CostModelInterface
Expand All @@ -29,11 +30,10 @@ import PlutusCore.Evaluation.Machine.MachineParameters
import UntypedPlutusCore.Evaluation.Machine.Cek.CekMachineCosts
import UntypedPlutusCore.Evaluation.Machine.Cek.Internal


-- | The default cost model for built-in functions.
defaultBuiltinCostModel :: BuiltinCostModel
defaultBuiltinCostModel =
$$(readJSONFromFile "cost-model/data/builtinCostModel.json")
$$(readJSONFromFile DFP.builtinCostModelFile)

-- Use this one when you've changed the type of `CostModel` and you can't load the json.
-- Then rerun
Expand All @@ -45,7 +45,7 @@ defaultBuiltinCostModel =
-- | Default costs for CEK machine instructions.
defaultCekMachineCosts :: CekMachineCosts
defaultCekMachineCosts =
$$(readJSONFromFile "cost-model/data/cekMachineCosts.json")
$$(readJSONFromFile DFP.cekMachineCostsFile)

defaultCekCostModel :: CostModel CekMachineCosts BuiltinCostModel
defaultCekCostModel = CostModel defaultCekMachineCosts defaultBuiltinCostModel
Expand Down

0 comments on commit 2ae7a02

Please sign in to comment.