Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- reverseAD :: forall a b. (Typeable a, Typeable b) => Code Q (a -> b) -> Code Q (a -> (b, b -> a))
- reverseAD' :: forall a b. Q Structure -> Q Structure -> Code Q (a -> b) -> Code Q (a -> (b, b -> a))
- data Structure
- structureFromTypeable :: Typeable a => Proxy a -> Q Structure
- structureFromType :: Q Type -> Q Structure
- (|*|) :: a -> b -> (a, b)
- evlog :: String -> IO ()
Reverse AD
reverseAD :: forall a b. (Typeable a, Typeable b) => Code Q (a -> b) -> Code Q (a -> (b, b -> a)) Source #
Use as follows:
> :t $$(reverseAD @_ @Double [|| \(x, y) -> x * y ||]) (Double, Double) -> (Double, Double -> (Double, Double))
The active scalar type is Double
. Double
values are differentiated; Float
is currently unsupported.
Note that due to the GHC stage restriction, any data types used in a
and
b
must be defined in a separate module that is then imported into the
module where you use reverseAD
. If you get a GHC panic about $tcFoo
not
being in scope (where Foo
is your data type), this means that you violated
this stage restriction. See
GHC #21547.
Structure descriptions
The structure of a type, as used by the AD transformation. Use
structureFromTypeable
or structureFromType
to construct a Structure
.
structureFromTypeable :: Typeable a => Proxy a -> Q Structure Source #
A TypeRep
(which we can obtain from the Typeable
constraint) can be
used to construct a Type
for the type a
, on which we can then call
structureFromType
.
structureFromType :: Q Type -> Q Structure Source #
Analyse the Type
and give a Structure
that describes the type for use
in reverseAD'
.
Special methods
(|*|) :: a -> b -> (a, b) Source #
Parallel (strict) pair construction.
The definition of x |*| y
is x `
: par
` y `par
` (x, y)x
and y
are evaluated in parallel. This also means that this pair constructor is, in
a certain sense, strict.
In differentiation using reverseAD
, this function is specially interpreted
so that not only the forward pass, but also the reverse gradient pass runs
in parallel.