packages feed

lazyppl-singlesite-0.2.0: src/LazyPPL/SingleSite/Internal.hs

{-# LANGUAGE MagicHash, UnboxedTuples #-}
{-# LANGUAGE ScopedTypeVariables #-}
module LazyPPL.SingleSite.Internal where

import LazyPPL
import LazyPPL.Internal.Sampler (randomLabel, checkedLogWeight)
import Control.Monad.Trans.Writer
import Data.Functor.Identity (Identity (..))

import Data.Monoid
import Numeric.Log
import qualified Data.Map as M

import System.Random hiding (uniform)

import Control.DeepSeq
import Control.Exception (evaluate)

import System.IO.Unsafe

import Foreign.Marshal.Alloc (alloca)
import Foreign.Marshal.Array (pokeArray)
import Foreign.Ptr (castPtr)
import Foreign.Storable (peek, sizeOf)
import GHC.Float (castWord64ToDouble)


import Data.IORef
import GHC.Exts (Ptr(..), unpackClosure#, indexArray#, sizeofArray#, Int(I#))
import GHC.Exts.Heap.Utils (dataConNames)
import GHC.Exts.Heap


{-- | Single-site Metropolis-Hastings --}

-- | A partial tree, used for examining finite evaluated subtrees of the working infinite
-- | random tree.
data PTree = PTree (Maybe Double) [Maybe PTree] deriving Show

type Site = [Int]
type Subst = M.Map Site Double

getSites :: PTree -> [Site]
getSites p = getSites' p []

getSites' :: PTree -> Site  -> [Site]
getSites' (PTree (Just v) ts) site = site : concat [ getSites' t (n:site)  | (n, Just t) <- zip [0..] ts ]
getSites' (PTree Nothing  ts) site = concat [ getSites' t (n:site) | (n, Just t) <- zip [0..] ts ]

mutateNode :: Tree -> Site -> Double -> Tree
mutateNode (Tree _ ts) []     d = Tree d ts
mutateNode (Tree v ts) (n:ns) d = Tree v $ take n ts ++ mutateNode (ts!!n) ns d : drop (n+1) ts

mutateNodes :: Tree -> Subst -> Tree
mutateNodes = M.foldrWithKey (\k d t -> mutateNode t k d)

randomElement :: RandomGen g => g -> [a] -> (g, a)
randomElement g xs = if null xs then error "0d sample space" else (g', xs !! n)
  where (n, g') = uniformR (0, length xs - 1) g
               -- ^^^^^^^^ 
               -- Depending on the version of `random` this can be either uniformR or randomR.

-- One local transition driver is shared by the legacy pure sampler and the
-- completed-evaluation interface. The callbacks own evaluation and the exact
-- decision rule; the source order remains site, replacement label, acceptance.
-- The label's generator is retained for interfaces that rebuild fresh heaps
-- without reusing already forced Double objects. It is not advanced on retry.
data LocalStep g a decision
  = NoLocalSite
  | LocalStep Site g Double a decision

localStepWith :: (Monad m, RandomGen g)
              => Maybe (g -> m (Site, g, selection))
              -> (Site -> g -> Double -> m a)
              -> (Site -> selection -> Double -> a -> m decision)
              -> g -> m (LocalStep g a decision)
localStepWith Nothing _ _ _ = pure NoLocalSite
localStepWith (Just select) evaluateCandidate decide source = do
  (site, labelSource, selection) <- select source
  let (newLabel, acceptanceSource) = randomLabel labelSource
      (label, _) = randomLabel acceptanceSource
  candidate <- evaluateCandidate site labelSource newLabel
  verdict <- decide site selection label candidate
  pure (LocalStep site labelSource label candidate verdict)

mh1 :: forall a. NFData a => Meas a -> IO [(a, Product (Log Double))]
mh1 (Meas m) = do
    g <- newStdGen
    let (gTree,g') = split g
        tree = randomTree gTree
        (x0, w0) = checkedSample (runProb (runWriterT m) tree)
        p = unsafePerformIO $ do { evaluate (rnf x0); evaluate (rnf w0) ; trunc tree }
        samples = map (\(_,_,_,_,s) -> s) $ iterate (mhStep (Meas m)) (gTree, g', M.empty, p, (x0, w0))
    return samples

-- | One transition of 'mh1'. The state holds the seed of the initial tree, the
-- proposal seed, the accepted substitutions, the evaluated sites, and the
-- current sample with its weight.
mhStep :: forall a g. (NFData a, RandomGen g) => Meas a -> (g, g, Subst, PTree, (a, Product (Log Double))) -> (g, g, Subst, PTree, (a, Product (Log Double)))
mhStep (Meas m) (treeSeed, seed, sub, ptree, (x,w)) =
    let (seed1, seed2) = split seed
        sites = getSites ptree
        select generator =
          let (next, selected) = randomElement generator sites
          in pure (reverse selected, next, ())
        evaluateCandidate site _ newLabel =
          let sub' = M.insert site newLabel sub
              tree = mutateNodes (randomTree treeSeed) sub'
              pair@(x', w') = checkedSample (runProb (runWriterT m) tree)
              partial = unsafePerformIO $ do
                evaluate (rnf x')
                evaluate (rnf w')
                trunc tree
          in pure (sub', partial, pair)
        decide site () label (_, partial, (_, w')) =
          let reverseSites = getSites partial
              alpha = fromIntegral (length sites) / fromIntegral (length reverseSites)
                    * exp (ln (getProduct w' / getProduct w))
          -- Keep the established pure API's arithmetic, strict boundary and
          -- zero-current-weight behavior. The new hooks state their own rule.
          in pure (reverse site `elem` reverseSites && (getProduct w == 0 || label < alpha))
        retained = (treeSeed, seed2, sub, ptree, (x,w))
    in case runIdentity (localStepWith (if null sites then Nothing else Just select) evaluateCandidate decide seed1) of
         NoLocalSite -> retained
         LocalStep _ _ _ (sub', partial, pair) accepted ->
           if accepted then (treeSeed, seed2, sub', partial, pair) else retained

-- A product of valid weights can still overflow to a logarithm of +Infinity.
checkedSample :: (a, Product (Log Double)) -> (a, Product (Log Double))
checkedSample (v, Product l) = (v, Product (checkedLogWeight "mh1" l))

-- Functions for truncating a tree.
getGCClosureData inspect b = do
  c <- inspect b
  case c of
    BlackholeClosure _ n -> getGCClosureData inspect n
    IndClosure _ n -> getGCClosureData inspect n
    _ -> return c

helperT :: (Box -> IO Closure) -> Box -> IO PTree
helperT inspect b = do
  c <- getGCClosureData inspect b
  case c of
    ConstrClosure _ [n,l] [] _ _ "Tree" ->
      do  n' <- getGCClosureData inspect n
          l' <- getGCClosureData inspect l
          let missing :: IO x
              missing = return $ error $ "Missing case:\n" ++ show n' ++ "\n" ++ show l'
          -- The label is a site once it is evaluated, and the children were
          -- demanded once the list is evaluated; each field is read on its own,
          -- so a node with both fields unevaluated has no site.
          label <- case n' of
            ConstrClosure {dataArgs = ds, name = "D#"} -> Just <$> doubleFromWords ds
            ThunkClosure {} -> return Nothing
            SelectorClosure {} -> return Nothing
            _ -> missing
          children <- case l' of
            ConstrClosure {name = ":"} -> helperB inspect l
            ThunkClosure {} -> return []
            _ -> missing
          return $ PTree label children
    ThunkClosure {} -> return $ PTree Nothing []
    SelectorClosure {} -> ioError (userError "trunc: selector root cannot be inspected without evaluation")
    _ -> ioError (userError ("trunc: unsupported tree closure " ++ show (tipe (info c))))

-- | The heap payload of a @D#@ closure, as machine words in host byte order.
-- Where one 'Word' fills the payload, 'castWord64ToDouble' is the documented
-- bit cast; a payload that spans narrower words is reassembled by writing the
-- words and reading the 'Double' back, which keeps the heap's byte order.
doubleFromWords :: [Word] -> IO Double
doubleFromWords ws
  | [w] <- ws, sizeOf (0 :: Word) == sizeOf (0 :: Double) =
      pure (castWord64ToDouble (fromIntegral w))
  | length ws * sizeOf (0 :: Word) == sizeOf (0 :: Double) = alloca $ \ptr -> do
      pokeArray (castPtr ptr) ws
      peek ptr
  | otherwise =
      fail "Unexpected Double heap payload size"

helperB :: (Box -> IO Closure) -> Box -> IO [Maybe PTree]
helperB inspect b = do
  c <- getGCClosureData inspect b
  case c of
    ConstrClosure _ [n,l] [] _ _ ":" ->
      do  n' <- getGCClosureData inspect n
          l' <- getGCClosureData inspect l
          case (n',l') of
            (ConstrClosure {name = "Tree"}, ConstrClosure {name = ":"}) ->
              do  n'' <- helperT inspect n
                  l'' <- helperB inspect l
                  return $ Just n'' : l''
            (ConstrClosure {name = "Tree"}, ThunkClosure {}) ->
              do  n'' <- helperT inspect n
                  return [Just n'']
            (ThunkClosure {}, ConstrClosure {name = ":"}) ->
              do  l'' <- helperB inspect l
                  return $ Nothing : l''
            (ThunkClosure {}, ThunkClosure {}) ->
              return [] -- alternatively, Nothing : []
            _ -> return $ error $ "Missing case:\n" ++ show n' ++ "\n" ++ show l'
    ThunkClosure {} -> return []

-- | Inspect a tree without evaluating it. A selector root raises an IO error:
-- resolving it could evaluate the selected field and change the observed sites.
trunc :: Tree -> IO PTree
trunc t = do
  cache <- newIORef M.empty
  helperT (getCachedClosureData cache) (asBox t)


-- Constructor descriptions are immutable during this traversal.
getCachedClosureData :: IORef (M.Map (Ptr StgInfoTable) (String, String, String)) -> Box -> IO Closure
getCachedClosureData cache (Box x) =
  case unpackClosure# x of
    (# infoAddress, heapRep, pointers #) -> do
      let infoPointer = Ptr infoAddress
          boxes = [case indexArray# pointers i of { (# p #) -> Box p }
                  | I# i <- [0 .. I# (sizeofArray# pointers) - 1]]
          names = do
            known <- readIORef cache
            case M.lookup infoPointer known of
              Just result -> return result
              Nothing -> do
                result <- dataConNames infoPointer
                writeIORef cache (M.insert infoPointer result known)
                return result
      table <- peekItbl infoPointer
      case tipe table of
        TSO -> return (UnsupportedClosure table)
        STACK -> return (UnsupportedClosure table)
        _ -> getClosureDataFromHeapRepPrim names (const (return Nothing)) table heapRep boxes