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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
use crate::internal::*;
use tract_core::ops::math::*;

macro_rules! activation {
    ($op: ident, $wire:expr) => {
        impl_dyn_hash!($op);

        impl Expansion for $op {
            fn name(&self) -> Cow<str> {
                stringify!($op).into()
            }

            op_hir!();

            fn rules<'r, 'p: 'r, 's: 'r>(
                &'s self,
                s: &mut Solver<'r>,
                inputs: &'p [TensorProxy],
                outputs: &'p [TensorProxy],
            ) -> InferenceResult {
                simple_unary_rules(s, inputs, outputs)
            }

            fn wire(
                &self,
                name: &str,
                model: &mut TypedModel,
                inputs: &[OutletId],
            ) -> TractResult<TVec<OutletId>> {
                let wire: fn(
                    &$op,
                    &str,
                    &mut TypedModel,
                    &[OutletId],
                ) -> TractResult<TVec<OutletId>> = $wire;
                (wire)(self, name, model, inputs)
            }
        }
    };
}

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct Clip(
    #[educe(Hash(method = "hash_opt_f32"))] Option<f32>,
    #[educe(Hash(method = "hash_opt_f32"))] Option<f32>,
);

activation!(Clip, |op, name: &str, model: &mut TypedModel, inputs| {
    let mut wire: TVec<OutletId> = inputs.into();
    if let Some(low) = op.0 {
        let low = broadcast_scalar(low, model, inputs)?;
        wire = model.wire_node(name.to_string() + ".low", max::unary(low), &wire)?;
    }
    if let Some(high) = op.1 {
        let high = broadcast_scalar(high, model, inputs)?;
        wire = model.wire_node(name.to_string() + ".high", min::unary(high), &wire)?;
    }
    Ok(wire)
});

#[derive(Debug, Clone, new, Hash)]
pub struct Softplus;

activation!(Softplus, |_op, name: &str, model: &mut TypedModel, inputs| {
    let one = broadcast_scalar(1.0, model, inputs)?;
    let wire = model.wire_node(name.to_string() + ".exp", exp(), inputs)?;
    let wire = model.wire_node(name.to_string() + ".plus_one", add::unary(one), &wire)?;
    let wire = model.wire_node(name.to_string() + ".ln", ln(), &wire)?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Hash)]
pub struct Softsign;

activation!(Softsign, |_op, name: &str, model: &mut TypedModel, inputs| {
    let one = broadcast_scalar(1.0, model, inputs)?;
    let x_abs = model.wire_node(name.to_string() + ".abs", abs(), inputs)?;
    let denum = model.wire_node(name.to_string() + ".plus_one", add::unary(one), &x_abs)?;
    let wire =
        model.wire_node(name.to_string() + ".div", div::bin_typed(), &[inputs[0], denum[0]])?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct Elu(#[educe(Hash(method = "hash_f32"))] pub f32);

activation!(Elu, |op, name: &str, model: &mut TypedModel, inputs| {
    let zero = broadcast_scalar(0.0, model, inputs)?;
    let minus_one = broadcast_scalar(-1.0, model, inputs)?;
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let x_exp = model.wire_node(name.to_string() + ".exp", exp(), inputs)?;
    let minus_one =
        model.wire_node(name.to_string() + ".minus_one", add::unary(minus_one), &x_exp)?;
    let neg = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), &minus_one)?;
    let test = model.wire_node(
        name.to_string() + ".test",
        tract_core::ops::logic::lesser::unary(zero),
        &[inputs[0]],
    )?;
    let wire = model.wire_node(
        name.to_string() + ".iff",
        tract_core::ops::logic::Iff,
        &[test[0], inputs[0], neg[0]],
    )?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct HardSigmoid(
    #[educe(Hash(method = "hash_f32"))] pub f32,
    #[educe(Hash(method = "hash_f32"))] pub f32,
);

activation!(HardSigmoid, |op, name: &str, model: &mut TypedModel, inputs| {
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let beta = broadcast_scalar(op.1, model, inputs)?;
    let one = broadcast_scalar(1.0, model, inputs)?;
    let zero = broadcast_scalar(0.0, model, inputs)?;
    let wire = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), inputs)?;
    let wire = model.wire_node(name.to_string() + ".add_beta", add::unary(beta), &wire)?;
    let wire = model.wire_node(name.to_string() + ".sat-one", min::unary(one), &wire)?;
    let wire = model.wire_node(name.to_string() + ".sat-zero", max::unary(zero), &wire)?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct LeakyRelu(#[educe(Hash(method = "hash_f32"))] pub f32);

activation!(LeakyRelu, |op, name: &str, model: &mut TypedModel, inputs| {
    let zero = broadcast_scalar(0.0, model, inputs)?;
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let neg = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), &inputs)?;
    let test = model.wire_node(
        name.to_string() + ".test",
        tract_core::ops::logic::lesser::unary(zero),
        &[inputs[0]],
    )?;
    let wire = model.wire_node(
        name.to_string() + ".iff",
        tract_core::ops::logic::Iff,
        &[test[0], inputs[0], neg[0]],
    )?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct ParametricSoftplus(
    #[educe(Hash(method = "hash_f32"))] pub f32,
    #[educe(Hash(method = "hash_f32"))] pub f32,
);

activation!(ParametricSoftplus, |op, name: &str, model: &mut TypedModel, inputs| {
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let beta = broadcast_scalar(op.1, model, inputs)?;
    let one = broadcast_scalar(1.0, model, inputs)?;
    let wire = model.wire_node(name.to_string() + ".mul_beta", mul::unary(beta), inputs)?;
    let wire = model.wire_node(name.to_string() + ".exp", exp(), &wire)?;
    let wire = model.wire_node(name.to_string() + ".plus_one", add::unary(one), &wire)?;
    let wire = model.wire_node(name.to_string() + ".ln", ln(), &wire)?;
    let wire = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), &wire)?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct ScaledTanh(
    #[educe(Hash(method = "hash_f32"))] pub f32,
    #[educe(Hash(method = "hash_f32"))] pub f32,
);

activation!(ScaledTanh, |op, name: &str, model: &mut TypedModel, inputs| {
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let beta = broadcast_scalar(op.1, model, inputs)?;
    let wire = model.wire_node(name.to_string() + ".mul_beta", mul::unary(beta), inputs)?;
    let wire = model.wire_node(name.to_string() + ".tanh", tanh(), &wire)?;
    let wire = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), &wire)?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct Selu(
    #[educe(Hash(method = "hash_f32"))] pub f32,
    #[educe(Hash(method = "hash_f32"))] pub f32,
);

activation!(Selu, |op, name: &str, model: &mut TypedModel, inputs| {
    let zero = broadcast_scalar(0.0, model, inputs)?;
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let minus_alpha = broadcast_scalar(-op.0, model, inputs)?;
    let gamma = broadcast_scalar(op.1, model, inputs)?;
    let wire = model.wire_node(name.to_string() + ".exp", exp(), &inputs)?;
    let wire = model.wire_node(name.to_string() + ".mul_alpha", mul::unary(alpha), &wire)?;
    let wire = model.wire_node(name.to_string() + ".sub_alpha", add::unary(minus_alpha), &wire)?;
    let test = model.wire_node(
        name.to_string() + ".test",
        tract_core::ops::logic::lesser::unary(zero),
        &[inputs[0]],
    )?;
    let wire = model.wire_node(
        name.to_string() + ".iff",
        tract_core::ops::logic::Iff,
        &[test[0], inputs[0], wire[0]],
    )?;
    let wire = model.wire_node(name.to_string() + ".mul_gamma", mul::unary(gamma), &wire)?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct Shrink(
    #[educe(Hash(method = "hash_f32"))] pub f32,
    #[educe(Hash(method = "hash_f32"))] pub f32,
);

activation!(Shrink, |op, name: &str, model: &mut TypedModel, inputs| {
    let bias = broadcast_scalar(op.0, model, inputs)?;
    let lambda = broadcast_scalar(op.1, model, inputs)?;
    let minus_bias = broadcast_scalar(-op.0, model, inputs)?;
    let minus_lambda = broadcast_scalar(-op.1, model, inputs)?;
    let zero =
        model.add_const(name.to_string() + ".zero", broadcast_scalar(0.0, model, inputs)?)?;
    let test_pos = model.wire_node(
        name.to_string() + ".test_pos",
        tract_core::ops::logic::lesser::unary(lambda),
        &inputs,
    )?;
    let pos = model.wire_node(
        name.to_string() + ".pos",
        tract_core::ops::math::add::unary(minus_bias),
        &inputs,
    )?;
    let test_neg = model.wire_node(
        name.to_string() + ".test_neg",
        tract_core::ops::logic::greater::unary(minus_lambda),
        &inputs,
    )?;
    let neg = model.wire_node(
        name.to_string() + ".neg",
        tract_core::ops::math::add::unary(bias),
        &inputs,
    )?;
    let wire = model.wire_node(
        name.to_string() + ".if_pos",
        tract_core::ops::logic::Iff,
        &[test_pos[0], pos[0], zero],
    )?;
    let wire = model.wire_node(
        name.to_string() + ".if_neg",
        tract_core::ops::logic::Iff,
        &[test_neg[0], neg[0], wire[0]],
    )?;
    Ok(wire)
});

#[derive(Debug, Clone, new, Educe)]
#[educe(Hash)]
pub struct ThresholdRelu(#[educe(Hash(method = "hash_f32"))] pub f32);

activation!(ThresholdRelu, |op, name: &str, model: &mut TypedModel, inputs| {
    let zero =
        model.add_const(name.to_string() + ".zero", broadcast_scalar(0.0, model, inputs)?)?;
    let alpha = broadcast_scalar(op.0, model, inputs)?;
    let test = model.wire_node(
        name.to_string() + ".test",
        tract_core::ops::logic::lesser::unary(alpha),
        &[inputs[0]],
    )?;
    let wire = model.wire_node(
        name.to_string() + ".iff",
        tract_core::ops::logic::Iff,
        &[test[0], inputs[0], zero],
    )?;
    Ok(wire)
});

fn simple_unary_rules<'r, 'p: 'r, 's: 'r>(
    s: &mut Solver<'r>,
    inputs: &'p [TensorProxy],
    outputs: &'p [TensorProxy],
) -> InferenceResult {
    check_input_arity(&inputs, 1)?;
    check_output_arity(&outputs, 1)?;
    s.equals(&inputs[0].datum_type, &outputs[0].datum_type)?;
    s.equals(&inputs[0].shape, &outputs[0].shape)?;
    Ok(())
}

pub fn broadcast_scalar(
    f: f32,
    model: &TypedModel,
    inputs: &[OutletId],
) -> TractResult<Arc<Tensor>> {
    let fact = model.outlet_fact(inputs[0])?;
    let mut tensor = tensor0(f).cast_to_dt(fact.datum_type)?.into_owned();
    while tensor.rank() < fact.rank() {
        tensor.insert_axis(0)?;
    }
    Ok(tensor.into_arc_tensor())
}