3535--
3636-- @since 0.2.7.0
3737module Numeric.Backprop.Internal (
38- BVar ,
39- W ,
38+ -- * Exported for re-use
39+ BVar (.. ),
40+ W (.. ),
4041 backpropWithN ,
4142 evalBPN ,
4243 constVar ,
@@ -66,6 +67,23 @@ module Numeric.Backprop.Internal (
6667 -- * Debug
6768 debugSTN ,
6869 debugIR ,
70+
71+ -- * Only used internally
72+ TapeNode (.. ),
73+ SomeTapeNode (.. ),
74+ BRef (.. ),
75+ Runner (.. ),
76+ initWengert ,
77+ insertNode ,
78+ bvConst ,
79+ forceBVar ,
80+ forceInpRef ,
81+ forceSomeTapeNode ,
82+ forceTapeNode ,
83+ fillWengert ,
84+ bumpMaybe ,
85+ initRunner ,
86+ gradRunner ,
6987) where
7088
7189import Control.DeepSeq
@@ -207,17 +225,20 @@ ofNum = OF (const 1)
207225--
208226-- See "Numeric.Backprop#liftops" and documentation for
209227-- 'Numeric.Backprop.liftOp' for more information.
210- data BVar s a = BV
211- { _bvRef :: ! (BRef s )
212- , _bvVal :: ! a
213- }
228+ data BVar s a
229+ = -- | @since 0.2.7.1
230+ BV
231+ { _bvRef :: ! (BRef s )
232+ , _bvVal :: ! a
233+ }
214234
215235-- | @since 0.1.5.1
216236deriving instance Typeable (BVar s a )
217237
218238-- | @since 0.2.6.3
219239instance X. IsoHKD (BVar s ) a
220240
241+ -- | @since 0.2.7.1
221242data BRef (s :: Type )
222243 = BRInp ! Int
223244 | BRIx ! Int
@@ -240,6 +261,7 @@ forceBVar :: BVar s a -> ()
240261forceBVar (BV r ! _) = force r `seq` ()
241262{-# INLINE forceBVar #-}
242263
264+ -- | @since 0.2.7.1
243265data InpRef :: Type -> Type where
244266 IR ::
245267 { _irIx :: ! (BVar s b )
@@ -248,6 +270,7 @@ data InpRef :: Type -> Type where
248270 } ->
249271 InpRef a
250272
273+ -- | @since 0.2.7.1
251274forceInpRef :: InpRef a -> ()
252275forceInpRef (IR v ! _ ! _) = forceBVar v `seq` ()
253276{-# INLINE forceInpRef #-}
@@ -256,23 +279,27 @@ forceInpRef (IR v !_ !_) = forceBVar v `seq` ()
256279debugIR :: InpRef a -> String
257280debugIR IR {.. } = show (_bvRef _irIx)
258281
282+ -- | @since 0.2.7.1
259283data TapeNode :: Type -> Type where
260284 TN ::
261285 { _tnInputs :: ! (Rec InpRef as )
262286 , _tnGrad :: ! (a -> Rec Identity as )
263287 } ->
264288 TapeNode a
265289
290+ -- | @since 0.2.7.1
266291forceTapeNode :: TapeNode a -> ()
267292forceTapeNode (TN inps ! _) = VR. rfoldMap forceInpRef inps `seq` ()
268293{-# INLINE forceTapeNode #-}
269294
295+ -- | @since 0.2.7.1
270296data SomeTapeNode :: Type where
271297 STN ::
272298 { _stnNode :: ! (TapeNode a )
273299 } ->
274300 SomeTapeNode
275301
302+ -- | @since 0.2.7.1
276303forceSomeTapeNode :: SomeTapeNode -> ()
277304forceSomeTapeNode (STN n) = forceTapeNode n
278305
@@ -285,12 +312,16 @@ debugSTN (STN TN{..}) = show . VR.rfoldMap ((: []) . debugIR) $ _tnInputs
285312--
286313-- For the end user, one can just imagine @'Reifies' s 'W'@ as a required
287314-- constraint on @s@ that allows backpropagation to work.
288- newtype W = W { wRef :: IORef (Int , [SomeTapeNode ])}
315+ newtype W
316+ = -- | @since 0.2.7.1
317+ W { wRef :: IORef (Int , [SomeTapeNode ])}
289318
319+ -- | @since 0.2.7.1
290320initWengert :: IO W
291321initWengert = W <$> newIORef (0 , [] )
292322{-# INLINE initWengert #-}
293323
324+ -- | @since 0.2.7.1
294325insertNode ::
295326 TapeNode a ->
296327 -- | val
@@ -659,11 +690,13 @@ coerceVar ::
659690 BVar s b
660691coerceVar v@ (BV r x) = forceBVar v `seq` BV r (coerce x)
661692
693+ -- | @since 0.2.7.1
662694data Runner s = R
663695 { _rDelta :: ! (MV. MVector s (Maybe Any ))
664696 , _rInputs :: ! (MV. MVector s (Maybe Any ))
665697 }
666698
699+ -- | @since 0.2.7.1
667700initRunner ::
668701 (Int , [SomeTapeNode ]) ->
669702 (Int , [Maybe Any ]) ->
@@ -677,6 +710,7 @@ initRunner (n, stns) (nx, xs) = do
677710 return $ R delts inps
678711{-# INLINE initRunner #-}
679712
713+ -- | @since 0.2.7.1
680714gradRunner ::
681715 forall b s .
682716 () =>
@@ -709,6 +743,7 @@ gradRunner o R{..} (n, stns) = do
709743 {-# INLINE propagate #-}
710744{-# INLINE gradRunner #-}
711745
746+ -- | @since 0.2.7.1
712747bumpMaybe ::
713748 -- | val
714749 a ->
0 commit comments