1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Facebook, Inc. and its affiliates
// SPDX-License-Identifier: MIT OR Apache-2.0

use crate::{
    arith::ArithAlgebra,
    core::{CoreAlgebra, HasDims},
    graph::{Config1, ConfigN, Graph, Value},
    linked::LinkedAlgebra,
    store::GradientStore,
    Check, Eval, Number,
};

/// Element-wise arithmetic operations with a constant value.
pub trait ConstArithAlgebra<Value, Const> {
    /// Return a value with the same shape as `v` but filled with constant `c`.
    fn setc(&mut self, v: &Value, c: Const) -> Value;

    /// Element-wise addition of a constant `v + c`.
    fn addc(&mut self, v: &Value, c: Const) -> Value;

    /// Element-wise multiplication by a constant `v * c`.
    fn mulc(&mut self, v: &Value, c: Const) -> Value;

    /// Element-wise exponentiation by a constant `v ^ c`.
    fn powc(&mut self, v: &Value, c: Const) -> Value;
}

#[cfg(feature = "arrayfire")]
mod af_arith {
    use super::*;
    use arrayfire as af;

    impl<T, C> ConstArithAlgebra<af::Array<T>, C> for Eval
    where
        Self: CoreAlgebra<af::Array<T>, Value = af::Array<T>>,
        T: af::HasAfEnum
            + af::ImplicitPromote<T, Output = T>
            + af::ConstGenerator<OutType = T>
            + From<C>,
    {
        fn setc(&mut self, v: &af::Array<T>, c: C) -> af::Array<T> {
            af::constant(T::from(c), v.dims())
        }

        fn addc(&mut self, v: &af::Array<T>, c: C) -> af::Array<T> {
            v + af::constant(T::from(c), v.dims())
        }

        fn mulc(&mut self, v: &af::Array<T>, c: C) -> af::Array<T> {
            v * af::constant(T::from(c), v.dims())
        }

        fn powc(&mut self, v: &af::Array<T>, c: C) -> af::Array<T> {
            af::pow(v, &af::constant(T::from(c), v.dims()), false)
        }
    }

    impl<C> ConstArithAlgebra<af::Dim4, C> for Check {
        #[inline]
        fn setc(&mut self, v: &af::Dim4, _c: C) -> af::Dim4 {
            *v
        }

        #[inline]
        fn addc(&mut self, v: &af::Dim4, _c: C) -> af::Dim4 {
            *v
        }

        #[inline]
        fn mulc(&mut self, v: &af::Dim4, _c: C) -> af::Dim4 {
            *v
        }

        #[inline]
        fn powc(&mut self, v: &af::Dim4, _c: C) -> af::Dim4 {
            *v
        }
    }
}

impl<T, C> ConstArithAlgebra<T, C> for Eval
where
    T: Number + From<C> + num::pow::Pow<C, Output = T>,
{
    #[inline]
    fn setc(&mut self, _v: &T, c: C) -> T {
        c.into()
    }

    #[inline]
    fn addc(&mut self, v: &T, c: C) -> T {
        v.add(c.into())
    }

    #[inline]
    fn mulc(&mut self, v: &T, c: C) -> T {
        v.mul(c.into())
    }

    #[inline]
    fn powc(&mut self, v: &T, c: C) -> T {
        v.pow(c)
    }
}

impl<C> ConstArithAlgebra<(), C> for Check {
    #[inline]
    fn setc(&mut self, _v: &(), _c: C) {}

    #[inline]
    fn addc(&mut self, _v: &(), _c: C) {}

    #[inline]
    fn mulc(&mut self, _v: &(), _c: C) {}

    #[inline]
    fn powc(&mut self, _v: &(), _c: C) {}
}

macro_rules! impl_graph {
    ($config:ident) => {
        impl<D, E, Dims, C> ConstArithAlgebra<Value<D>, C> for Graph<$config<E>>
        where
            E: Default
                + Clone
                + CoreAlgebra<D, Value = D>
                + ArithAlgebra<D>
                + ConstArithAlgebra<D, C>
                + LinkedAlgebra<Value<D>, D>,
            C: std::ops::Sub<C, Output = C> + num::One + Clone + 'static + Send + Sync,
            D: HasDims<Dims = Dims> + Clone + 'static + Send + Sync,
            Dims: PartialEq + std::fmt::Debug + Clone + 'static + Send + Sync,
        {
            fn setc(&mut self, v: &Value<D>, c: C) -> Value<D> {
                let result = self.eval().addc(v.data(), c);
                self.constant(result)
            }

            fn addc(&mut self, v: &Value<D>, c: C) -> Value<D> {
                let result = self.eval().addc(v.data(), c);
                self.make_node(result, vec![v.input()], {
                    let id = v.id();
                    move |graph, store, gradient| {
                        if let Some(id) = id {
                            store.add_gradient(graph, id, &gradient)?;
                        }
                        Ok(())
                    }
                })
            }

            fn mulc(&mut self, v: &Value<D>, c: C) -> Value<D> {
                let result = self.eval().mulc(v.data(), c.clone());
                self.make_node(result, vec![v.input()], {
                    let id = v.id();
                    move |graph, store, gradient| {
                        if let Some(id) = id {
                            let grad = graph.mulc(&gradient, c.clone());
                            store.add_gradient(graph, id, &grad)?;
                        }
                        Ok(())
                    }
                })
            }

            fn powc(&mut self, v: &Value<D>, c: C) -> Value<D> {
                let result = self.eval().powc(v.data(), c.clone());
                self.make_node(result, vec![v.input()], {
                    let v = v.clone();
                    move |graph, store, gradient| {
                        if let Some(id) = v.id() {
                            let v = graph.link(&v);
                            let e = graph.powc(&v, c.clone() - C::one());
                            let f = graph.mulc(&e, c.clone());
                            let grad = graph.mul(&f, &gradient)?;
                            store.add_gradient(graph, id, &grad)?;
                        }
                        Ok(())
                    }
                })
            }
        }
    };
}

impl_graph!(Config1);
impl_graph!(ConfigN);