backprop icon indicating copy to clipboard operation
backprop copied to clipboard

Backpropagating through "stateful" operations.

Open jh14778 opened this issue 3 years ago • 4 comments

I am trying to use this library with a third party C library, but I need to carry some additional context to perform the computations.

My issue boils down to how do I integrate library calls like:

-- Haskell wrapper function signature 
zero :: (Int, Int) -> StateT IO Context Matrix
one :: (Int, Int) -> StateT IO Context Matrix
add :: Matrix -> Matrix -> StateT IO Context Matrix
matMul :: Matrix -> Matrix -> StateT IO Context Matrix

-- FFI signature 
zero :: Ptr Context -> CInt -> CInt -> IO (Ptr Matrix)
one :: Ptr Context -> CInt -> CInt -> IO (Ptr Matrix)
add :: Ptr Context -> Ptr Matrix -> Ptr Matrix -> IO (Ptr Matrix)
matmul :: Ptr Context -> Ptr Matrix -> Ptr Matrix -> IO (Ptr Matrix)

There are no standalone functions to zero, one, or add a Matrix without carrying the Context.

jh14778 avatar Aug 28 '20 11:08 jh14778

I've only recently started using backprop, but pretty much like ad it assumes that the computation to be differentiated is pure. I can't quite follow the internals of the library well enough to be able to tell why that is the case, though.

ocramz avatar Sep 09 '20 06:09 ocramz

I suspected that was the case.

My library calls are "pure" as far as the computed values are concerned. Unfortunately, there are observable side-effects on the context (unsafePerformIO might not be acceptable here either).

Perhaps this is more of a feature request. I'm happy for this to be closed, if there's no plan for this to be supported.

jh14778 avatar Sep 09 '20 11:09 jh14778

Maybe the MVar trick will do:

import Control.Concurrent
import Control.Monad.State
import Data.Tuple
import System.IO.Unsafe

type Context = Int
type Matrix = [[Double]]
newtype Ctx = Ctx { unCtx :: MVar Context }

newCtx :: IO Context -> IO Ctx
newCtx ioc = fmap Ctx $ newMVar =<< ioc

zero :: (Int, Int) -> StateT Context IO Matrix
zero xy@(x,y) = do
    modify' succ
    s <- get
    lift $ print $ (s, xy)
    pure $ replicate y $ replicate x $ 0

zero' :: Ctx -> (Int, Int) -> Matrix
zero' ctx xy = wrap ctx $ zero xy

wrap :: Ctx -> StateT Context IO a -> a
wrap ctx ma = unsafePerformIO $
    modifyMVar (unCtx ctx) $ fmap swap . runStateT ma

main :: IO ()
main = do
  ctx <- newCtx $ pure 0
  print $ zero' ctx (2,3)
  print $ zero' ctx (1,1)

ivanovs-4 avatar Sep 09 '20 12:09 ivanovs-4

@jh14778 @ivanovs-4 I just recalled : you could also wrap your effects inside ABP , which will provide the Backprop instance for you : https://hackage.haskell.org/package/backprop-0.2.6.4/docs/Numeric-Backprop.html#t:ABP

ocramz avatar Dec 20 '20 10:12 ocramz