{-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveTraversable #-} {-# LANGUAGE GADTs #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} module Numeric.ADDual.Internal where import Control.Monad (when) import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.State.Strict import Data.IORef import Data.Int import Data.Proxy import Data.Typeable import qualified Data.Vector.Storable as VS import qualified Data.Vector.Storable.Mutable as VSM import Foreign.C.Types import Foreign.ForeignPtr import Foreign.Ptr import Foreign.Storable import GHC.Stack import GHC.Exts (withDict) import System.IO.Unsafe import System.IO (hPutStrLn, stderr) -- TODO: type roles on 's' debug :: Bool debug = toEnum 0 foreign import ccall unsafe "ad_dual_hs_backpropagate_double" c_backpropagate_double :: Ptr CDouble -> Int64 -> Int64 -> Ptr () -> IO () -- TODO: full vjp (just some more Traversable mess) -- TODO: if non-scalar output types are allowed, ensure that all its scalar components are WHNF evaluated before we backpropagate {-# NOINLINE gradient' #-} gradient' :: forall a f. (Traversable f, Num a, Storable a, Typeable a) => HasCallStack => Show a -- TODO: remove => (forall s. Taping s a => f (Dual s a) -> Dual s a) -> f a -> a -> (a, f a) gradient' f inp topctg = unsafePerformIO $ do when debug $ hPutStrLn stderr "Preparing input" let (inp', starti) = runState (traverse (\x -> state (\i -> (Dual x i, i + 1))) inp) 0 idref <- newIORef starti -- The first chunk starts after the input IDs. vec1 <- VSM.unsafeNew 128 taperef <- newIORef (MLog idref (Chunk starti vec1) SLNil) when debug $ hPutStrLn stderr "Running function" let !(Dual result outi) = withDict @(Taping () a) taperef $ f @() inp' when debug $ hPutStrLn stderr $ "result = " ++ show result ++ "; outi = " ++ show outi MLog _ lastChunk tapeTail <- readIORef taperef when debug $ do tapestr <- showTape (tapeTail `Snoc` lastChunk) hPutStrLn stderr $ "tape = " ++ tapestr "" when debug $ hPutStrLn stderr "Backpropagating" accums <- VSM.new (outi+1) VSM.write accums outi topctg let backpropagate i chunk@(Chunk ci0 vec) tape | i >= ci0 = do ctg <- VSM.read accums i Contrib i1 dx i2 dy <- VSM.read vec (i - ci0) when (i1 /= -1) $ VSM.modify accums (+ ctg*dx) i1 when (i2 /= -1) $ VSM.modify accums (+ ctg*dy) i2 backpropagate (i-1) chunk tape | otherwise = case tape of SLNil -> return () -- reached end of tape we should loop over tape' `Snoc` chunk' -> backpropagate i chunk' tape' backpropagate_via_c :: Ptr CDouble -> Int -> Chunk Double -> Snoclist (Chunk Double) -> IO () backpropagate_via_c accums_ptr i (Chunk ci0 vec) tape = do let (vec_fptr, _) = VSM.unsafeToForeignPtr0 vec withForeignPtr vec_fptr $ \vec_ptr -> c_backpropagate_double accums_ptr (fromIntegral ci0) (fromIntegral i) (castPtr @(Contrib Double) @() vec_ptr) case tape of SLNil -> return () tape' `Snoc` chunk' -> backpropagate_via_c accums_ptr (ci0 - 1) chunk' tape' case (eqT @a @Double, sizeOf (undefined :: Int)) of (Just Refl, 8) -> do let (accums_fptr, _) = VSM.unsafeToForeignPtr0 accums withForeignPtr accums_fptr $ \accums_ptr -> backpropagate_via_c (castPtr @Double @CDouble accums_ptr) outi lastChunk tapeTail _ -> backpropagate outi lastChunk tapeTail when debug $ do accums' <- VS.freeze accums hPutStrLn stderr $ "accums = " ++ show accums' when debug $ hPutStrLn stderr "Reconstructing gradient" let readDeriv = do i <- get d <- lift $ VSM.read accums i put (i+1) return d grad <- evalStateT (traverse (\_ -> readDeriv) inp) 0 return (result, grad) data Snoclist a = SLNil | Snoc !(Snoclist a) !a deriving (Show, Eq, Ord, Functor, Foldable, Traversable) data Contrib a = Contrib {-# UNPACK #-} !Int !a -- ^ ID == -1 -> no contribution {-# UNPACK #-} !Int !a -- ^ idem deriving (Show) instance Storable a => Storable (Contrib a) where sizeOf _ = 2 * (sizeOf (undefined :: Int) + sizeOf (undefined :: a)) alignment _ = alignment (undefined :: Int) peek ptr = Contrib <$> peek (castPtr ptr) <*> peekByteOff (castPtr ptr) (sizeOf (undefined :: Int)) <*> peekByteOff (castPtr ptr) (sizeOf (undefined :: Int) + sizeOf (undefined :: a)) <*> peekByteOff (castPtr ptr) (2 * sizeOf (undefined :: Int) + sizeOf (undefined :: a)) poke ptr (Contrib i1 dx i2 dy) = do poke (castPtr ptr) i1 pokeByteOff (castPtr ptr) (sizeOf (undefined :: Int)) dx pokeByteOff (castPtr ptr) (sizeOf (undefined :: Int) + sizeOf (undefined :: a)) i2 pokeByteOff (castPtr ptr) (2 * sizeOf (undefined :: Int) + sizeOf (undefined :: a)) dy data Chunk a = Chunk {-# UNPACK #-} !Int -- ^ First ID in this chunk {-# UNPACK #-} !(VSM.IOVector (Contrib a)) data MLog s a = MLog !(IORef Int) -- ^ next ID to generate {-# UNPACK #-} !(Chunk a) -- ^ current running chunk !(Snoclist (Chunk a)) -- ^ tape showChunk :: (Storable a, Show a) => Chunk a -> IO ShowS showChunk (Chunk ci0 vec) = do vec' <- VS.freeze vec return (showString ("Chunk " ++ show ci0 ++ " ") . shows vec') showTape :: (Storable a, Show a) => Snoclist (Chunk a) -> IO ShowS showTape SLNil = return (showString "SLNil") showTape (tape `Snoc` chunk) = do s1 <- showTape tape s2 <- showChunk chunk return (s1 . showString " `Snoc` " . s2) -- | This class does not have any instances defined, on purpose. You'll get one -- magically when you differentiate. class Taping s a where getTape :: IORef (MLog s a) data Dual s a = Dual !a {-# UNPACK #-} !Int -- ^ -1 if this is a constant instance Eq a => Eq (Dual s a) where Dual x _ == Dual y _ = x == y instance Ord a => Ord (Dual s a) where compare (Dual x _) (Dual y _) = compare x y instance (Num a, Storable a, Taping s a) => Num (Dual s a) where Dual x i1 + Dual y i2 = mkDual (x + y) i1 1 i2 1 Dual x i1 - Dual y i2 = mkDual (x - y) i1 1 i2 (-1) Dual x i1 * Dual y i2 = mkDual (x * y) i1 y i2 x negate (Dual x i1) = mkDual (negate x) i1 (-1) (-1) 0 abs (Dual x i1) = mkDual (abs x) i1 (x * signum x) (-1) 0 signum (Dual x _) = Dual (signum x) (-1) fromInteger n = Dual (fromInteger n) (-1) instance (Fractional a, Storable a, Taping s a) => Fractional (Dual s a) where Dual x i1 / Dual y i2 = mkDual (x / y) i1 (recip y) i2 (-x/(y*y)) recip (Dual x i1) = mkDual (recip x) i1 (-1/(x*x)) (-1) 0 fromRational r = Dual (fromRational r) (-1) instance (Floating a, Storable a, Taping s a) => Floating (Dual s a) where pi = Dual pi (-1) exp (Dual x i1) = mkDual (exp x) i1 (exp x) (-1) 0 log (Dual x i1) = mkDual (log x) i1 (recip x) (-1) 0 sqrt (Dual x i1) = mkDual (sqrt x) i1 (recip (2*sqrt x)) (-1) 0 -- d/dx (x ^ y) = d/dx (e ^ (y ln x)) = e ^ (y ln x) * d/dx (y ln x) = e ^ (y ln x) * y/x -- d/dy (x ^ y) = d/dy (e ^ (y ln x)) = e ^ (y ln x) * d/dy (y ln x) = e ^ (y ln x) * ln x Dual x i1 ** Dual y i2 = let z = x ** y in mkDual z i1 (z * y/x) i2 (z * log x) logBase = undefined ; sin = undefined ; cos = undefined ; tan = undefined asin = undefined ; acos = undefined ; atan = undefined ; sinh = undefined cosh = undefined ; tanh = undefined ; asinh = undefined ; acosh = undefined atanh = undefined constant :: a -> Dual s a constant x = Dual x (-1) mkDual :: forall a s. (Storable a, Taping s a) => a -> Int -> a -> Int -> a -> Dual s a mkDual res i1 dx i2 dy = Dual res (writeTapeUnsafe @a (Proxy @s) i1 dx i2 dy) data WriteTapeAction a = WTANewvec (VSM.IOVector (Contrib a)) | WTAOldTape (Snoclist (Chunk a)) -- This NOINLINE really doesn't seem to matter for performance, so let's be safe {-# NOINLINE writeTapeUnsafe #-} writeTapeUnsafe :: forall a s proxy. (Storable a, Taping s a) => proxy s -> Int -> a -> Int -> a -> Int writeTapeUnsafe _ i1 dx i2 dy = unsafePerformIO $ writeTapeIO (Proxy @s) i1 dx i2 dy writeTapeIO :: forall a s proxy. (Storable a, Taping s a) => HasCallStack => proxy s -> Int -> a -> Int -> a -> IO Int writeTapeIO _ i1 dx i2 dy = do MLog idref (Chunk ci0 vec) _ <- readIORef (getTape @s) let n = VSM.length vec i <- atomicModifyIORef' idref (\i -> (i + 1, i)) let idx = i - ci0 if | idx < n -> do VSM.write vec idx (Contrib i1 dx i2 dy) return i -- check if we'd fit in the next chunk (overwhelmingly likely) | let newlen = 3 * n `div` 2 , idx < n + newlen -> do newvec <- VSM.unsafeNew newlen action <- atomicModifyIORef' (getTape @s) $ \(MLog idref' chunk@(Chunk ci0' vec') tape) -> if | ci0 == ci0' -> -- Likely (certain when single-threaded): no race condition, -- we get the chance to put the new chunk in place. (MLog idref' (Chunk (ci0 + n) newvec) (tape `Snoc` chunk), WTANewvec newvec) | i < ci0' + VSM.length vec' -> -- Race condition; need to write to appropriate position in this vector. (MLog idref' chunk tape, WTANewvec vec') | i < ci0' -> -- Very unlikely; need to write to old chunk in tape. (MLog idref' chunk tape, WTAOldTape tape) | otherwise -> -- We got an ID so far in the future that it doesn't even fit -- in the next chunk. But that can't happen, because we're -- only in this branch if the ID would have fit in the next -- chunk in the first place! error "writeTape: impossible" case action of WTANewvec vec' -> do when debug $ hPutStrLn stderr $ "writeTapeIO: new vec of size " ++ show (VSM.length vec') VSM.write vec' (idx - n) (Contrib i1 dx i2 dy) WTAOldTape tape -> let go SLNil = error "writeTape: no appropriate tape chunk?" go (tape' `Snoc` Chunk ci0' vec') -- The first comparison here is technically unnecessary, but -- I'm not courageous enough to remove it. | ci0' <= i, i < ci0' + VSM.length vec' = VSM.write vec' (i - ci0') (Contrib i1 dx i2 dy) | otherwise = go tape' in go tape return i -- there's a tremendous amount of competition, let's just try again | otherwise -> writeTapeIO (Proxy @s) i1 dx i2 dy