-
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
Showing that random-fu is x4 faster than mwc and thus monad-bayes #321
Closed
+155
−29
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
use nix | ||
# Make sure you have direnv >= 2.30 | ||
use flake --extra-experimental-features nix-command --extra-experimental-features flakes | ||
# use flake --extra-experimental-features nix-command --extra-experimental-features flakes |
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,49 @@ | ||
( | ||
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 = ./.;} | ||
) | ||
.defaultNix | ||
Comment on lines
-1
to
-14
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. I guess this was also not intentional? |
||
{ mkDerivation, abstract-par, base, brick, containers, criterion | ||
, directory, foldl, free, histogram-fill, hspec, ieee754 | ||
, integration, lens, lib, linear, log-domain, math-functions | ||
, matrix, monad-coroutine, monad-extras, mtl, mwc-random | ||
, optparse-applicative, pipes, pretty-simple, primitive, process | ||
, QuickCheck, random, safe, scientific, statistics, text, time | ||
, transformers, typed-process, vector, vty | ||
}: | ||
mkDerivation { | ||
pname = "monad-bayes"; | ||
version = "1.2.0"; | ||
src = ./.; | ||
isLibrary = true; | ||
isExecutable = true; | ||
libraryHaskellDepends = [ | ||
base brick containers foldl free histogram-fill ieee754 integration | ||
lens linear log-domain math-functions matrix monad-coroutine | ||
monad-extras mtl mwc-random pipes pretty-simple primitive random | ||
safe scientific statistics text vector vty | ||
]; | ||
executableHaskellDepends = [ | ||
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 safe scientific statistics text time transformers | ||
typed-process vector vty | ||
]; | ||
testHaskellDepends = [ | ||
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 safe scientific statistics text time transformers | ||
typed-process vector vty | ||
]; | ||
benchmarkHaskellDepends = [ | ||
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 safe scientific statistics text time transformers | ||
typed-process vector vty | ||
]; | ||
homepage = "http://github.com/tweag/monad-bayes#readme"; | ||
description = "A library for probabilistic programming"; | ||
license = lib.licenses.mit; | ||
mainProgram = "example"; | ||
} |
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,50 @@ | ||
{-# OPTIONS_GHC -Wall #-} | ||
{-# OPTIONS_GHC -Wno-type-defaults #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
-- From random | ||
import System.Random.Stateful | ||
|
||
-- From mwc-random | ||
import qualified System.Random.MWC.Distributions as MWC | ||
|
||
-- From random-fu | ||
import Data.Random | ||
|
||
-- From monad-bayes | ||
import qualified Control.Monad.Bayes.Class as MonadBayes | ||
import Control.Monad.Bayes.Sampler.Strict | ||
|
||
import Criterion.Main | ||
import Control.DeepSeq (NFData) | ||
import Foreign | ||
|
||
|
||
main :: IO () | ||
main = do | ||
stdGen <- newIOGenM =<< newStdGen | ||
defaultMain | ||
[ bgroup "dists" | ||
[ bgroup "StdGen" (dists (stdGen :: (IOGenM StdGen))) | ||
] | ||
] | ||
|
||
dists :: StatefulGen g IO => g -> [Benchmark] | ||
dists gen = | ||
[ doubleSuite gen "stdNormal" (Normal 0.0 1.0) | ||
] | ||
|
||
doubleSuite :: (Distribution d Double, StatefulGen g IO) => g -> String -> d Double -> Benchmark | ||
doubleSuite = suite | ||
|
||
suite :: (Storable t, Num t, Distribution d t, NFData t, StatefulGen g IO) => g -> String -> d t -> Benchmark | ||
suite gen name var = bgroup name | ||
[ bench "single sample" $ nfIO $ do | ||
sampleFrom gen var | ||
, bench "single sample MWC" $ nfIO $ do | ||
MWC.normal 0.0 1.0 gen | ||
, bench "single sample monad bayes" $ nfIO $ do | ||
sampleWith (MonadBayes.normal 0.0 1.0) gen | ||
] | ||
|
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,54 @@ | ||
( | ||
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 { | ||
random-fu = super.haskell.lib.dontCheck (hself.callCabal2nixWithOptions "random-fu" (builtins.fetchGit { | ||
url = "https://github.com/haskell-numerics/random-fu"; | ||
rev = "b5cf96c6d904faa6adcf010a88f3ee117b289a4f"; | ||
}) "--subpath random-fu" { }); | ||
rvar = super.haskell.lib.dontCheck (hself.callCabal2nixWithOptions "rvar" (builtins.fetchGit { | ||
url = "https://github.com/haskell-numerics/random-fu"; | ||
rev = "b5cf96c6d904faa6adcf010a88f3ee117b289a4f"; | ||
}) "--subpath rvar" { }); | ||
monad-bayes = super.haskell.lib.dontCheck ( | ||
super.haskell.lib.doJailbreak ( | ||
super.haskell.lib.disableLibraryProfiling (hself.callPackage ./. { }) | ||
) | ||
); | ||
}; | ||
}; | ||
}; | ||
|
||
in | ||
|
||
{ nixpkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/nixpkgs-23.05-darwin.tar.gz") | ||
{ | ||
overlays = [ myHaskellPackageOverlay ]; | ||
config.allowBroken = true; | ||
} | ||
}: | ||
|
||
let | ||
|
||
pkgs = nixpkgs; | ||
|
||
haskellDeps = ps: with ps; [ | ||
base criterion deepseq | ||
mersenne-random-pure64 | ||
monad-bayes | ||
MonadRandom mtl mwc-random random random-fu stateref vector | ||
]; | ||
|
||
in | ||
|
||
pkgs.stdenv.mkDerivation { | ||
name = "simple-shell"; | ||
|
||
buildInputs = [ | ||
pkgs.cabal-install | ||
pkgs.cabal2nix | ||
(pkgs.myHaskellPackages.ghcWithPackages haskellDeps) | ||
pkgs.darwin.apple_sdk.frameworks.Cocoa | ||
]; | ||
} |
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.
Is that change intentional?