Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .envrc
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that change intentional?

# use flake --extra-experimental-features nix-command --extra-experimental-features flakes
63 changes: 49 additions & 14 deletions default.nix
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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";
}
50 changes: 50 additions & 0 deletions samplePerformance.hs
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
]

68 changes: 54 additions & 14 deletions shell.nix
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
];
}