-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A benchmark of random-fu (replacing mwc-random) #323
Draft
idontgetoutmuch
wants to merge
2
commits into
tweag:master
Choose a base branch
from
idontgetoutmuch:use-random-fu
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
( | ||
import | ||
( | ||
let | ||
lock = builtins.fromJSON (builtins.readFile ./flake.lock); | ||
in | ||
fetchTarball { | ||
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; | ||
sha256 = lock.nodes.flake-compat.locked.narHash; | ||
} | ||
) | ||
{src = ./.;} | ||
) | ||
.shellNix | ||
let | ||
|
||
myHaskellPackageOverlay = self: super: { | ||
myHaskellPackages = super.haskellPackages.override { | ||
overrides = hself: hsuper: rec { | ||
}; | ||
}; | ||
}; | ||
|
||
in | ||
|
||
{ nixpkgs ? import <nixpkgs> { overlays = [ myHaskellPackageOverlay ]; }, compiler ? "default", doBenchmark ? false }: | ||
|
||
|
||
let | ||
|
||
pkgs = nixpkgs; | ||
|
||
haskellDeps = ps: with ps; [ | ||
abstract-par base brick containers criterion directory foldl free | ||
histogram-fill hspec ieee754 integration lens linear log-domain | ||
math-functions matrix monad-coroutine monad-extras mtl mwc-random | ||
optparse-applicative pipes pretty-simple primitive process | ||
QuickCheck random random-fu safe scientific statistics text time transformers | ||
typed-process vector vty | ||
]; | ||
|
||
in | ||
|
||
pkgs.stdenv.mkDerivation { | ||
name = "whatever"; | ||
|
||
buildInputs = [ | ||
pkgs.libintlOrEmpty | ||
pkgs.cabal-install | ||
(pkgs.myHaskellPackages.ghcWithPackages haskellDeps) | ||
pkgs.darwin.apple_sdk.frameworks.Cocoa | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,106 @@ | ||||||||
{-# LANGUAGE ApplicativeDo #-} | ||||||||
{-# LANGUAGE DerivingStrategies #-} | ||||||||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||||||||
{-# LANGUAGE ImportQualifiedPost #-} | ||||||||
|
||||||||
-- | | ||||||||
-- Module : Control.Monad.Bayes.Sampler | ||||||||
-- Description : Pseudo-random sampling monads | ||||||||
-- Copyright : (c) Adam Scibior, 2015-2020 | ||||||||
-- License : MIT | ||||||||
-- Maintainer : [email protected] | ||||||||
-- Stability : experimental | ||||||||
-- Portability : GHC | ||||||||
-- | ||||||||
-- 'SamplerIO' and 'SamplerST' are instances of 'MonadDistribution'. Apply a 'MonadFactor' | ||||||||
-- transformer to obtain a 'MonadMeasure' that can execute probabilistic models. | ||||||||
module Control.Monad.Bayes.Sampler.StrictFu | ||||||||
( SamplerT (..), | ||||||||
SamplerIO, | ||||||||
SamplerST, | ||||||||
sampleIO, | ||||||||
sampleIOfixed, | ||||||||
sampleWith, | ||||||||
sampleSTfixed, | ||||||||
sampleMean, | ||||||||
sampler, | ||||||||
) | ||||||||
where | ||||||||
|
||||||||
import Control.Foldl qualified as F hiding (random) | ||||||||
import Control.Monad.Bayes.Class | ||||||||
( MonadDistribution | ||||||||
( bernoulli, | ||||||||
beta, | ||||||||
-- categorical, | ||||||||
gamma, | ||||||||
-- geometric, | ||||||||
normal, | ||||||||
random, | ||||||||
uniform | ||||||||
), | ||||||||
) | ||||||||
import Control.Monad.Reader (ReaderT (..)) | ||||||||
import Control.Monad.ST (ST) | ||||||||
import Control.Monad.State | ||||||||
import Numeric.Log (Log (ln)) | ||||||||
import Data.Random qualified as RF | ||||||||
import Data.Random.Distribution.Beta qualified as RF | ||||||||
import Data.Random.Distribution.Bernoulli qualified as RF | ||||||||
import Data.Random.Distribution.Uniform as RF | ||||||||
import System.Random.Stateful (IOGenM (..), STGenM, StatefulGen, StdGen, initStdGen, mkStdGen, newIOGenM, newSTGenM) | ||||||||
|
||||||||
|
||||||||
-- | The sampling interpretation of a probabilistic program | ||||||||
-- Here m is typically IO or ST | ||||||||
newtype SamplerT g m a = SamplerT {runSamplerT :: ReaderT g m a} deriving (Functor, Applicative, Monad, MonadIO) | ||||||||
|
||||||||
-- | convenient type synonym to show specializations of SamplerT | ||||||||
-- to particular pairs of monad and RNG | ||||||||
type SamplerIO = SamplerT (IOGenM StdGen) IO | ||||||||
|
||||||||
-- | convenient type synonym to show specializations of SamplerT | ||||||||
-- to particular pairs of monad and RNG | ||||||||
type SamplerST s = SamplerT (STGenM StdGen s) (ST s) | ||||||||
|
||||||||
instance StatefulGen g m => MonadDistribution (SamplerT g m) where | ||||||||
random = SamplerT (ReaderT $ RF.runRVar $ RF.stdUniform) | ||||||||
|
||||||||
uniform a b = SamplerT (ReaderT $ RF.runRVar $ RF.doubleUniform a b) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
normal m s = SamplerT (ReaderT $ RF.runRVar $ RF.normal m s) | ||||||||
gamma shape scale = SamplerT (ReaderT $ RF.runRVar $ RF.gamma shape scale) | ||||||||
beta a b = SamplerT (ReaderT $ RF.runRVar $ RF.beta a b) | ||||||||
|
||||||||
bernoulli p = SamplerT (ReaderT $ RF.runRVar $ RF.bernoulli p) | ||||||||
-- categorical ps = error "categorical" | ||||||||
-- geometric p = error "geometric" | ||||||||
|
||||||||
-- | Sample with a random number generator of your choice e.g. the one | ||||||||
-- from `System.Random`. | ||||||||
-- | ||||||||
-- >>> import Control.Monad.Bayes.Class | ||||||||
-- >>> import System.Random.Stateful hiding (random) | ||||||||
-- >>> newIOGenM (mkStdGen 1729) >>= sampleWith random | ||||||||
-- 4.690861245089605e-2 | ||||||||
sampleWith :: SamplerT g m a -> g -> m a | ||||||||
sampleWith (SamplerT m) = runReaderT m | ||||||||
|
||||||||
-- | initialize random seed using system entropy, and sample | ||||||||
sampleIO, sampler :: SamplerIO a -> IO a | ||||||||
sampleIO x = initStdGen >>= newIOGenM >>= sampleWith x | ||||||||
sampler = sampleIO | ||||||||
|
||||||||
-- | Run the sampler with a fixed random seed | ||||||||
sampleIOfixed :: SamplerIO a -> IO a | ||||||||
sampleIOfixed x = newIOGenM (mkStdGen 1729) >>= sampleWith x | ||||||||
|
||||||||
-- | Run the sampler with a fixed random seed | ||||||||
sampleSTfixed :: SamplerST s b -> ST s b | ||||||||
sampleSTfixed x = newSTGenM (mkStdGen 1729) >>= sampleWith x | ||||||||
|
||||||||
sampleMean :: [(Double, Log Double)] -> Double | ||||||||
sampleMean samples = | ||||||||
let z = F.premap (ln . exp . snd) F.sum | ||||||||
w = (F.premap (\(x, y) -> x * ln (exp y)) F.sum) | ||||||||
s = (/) <$> w <*> z | ||||||||
in F.fold s samples |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are calling polymorphic code (the inner
do
blocks) on particular types. This means that additionally to the actual computation, type class dictionary lookup is also performed. This can have a performance impact. Maybe this can be improved by adding aSPECIALISE
pragma to theMonadDistribution
instance definitions of both sampler modules. See https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/pragmas.html#specialize-pragma.I imagine a change like this:
This is for the MWC sampler, and a similar pragma can be added to the
random-fu
sampler. Hopefully, both will be faster and more comparable then.