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
use crate::tensor_ops::cpu_kernels::BinaryDerivative;

use num_traits::Float;

impl<F: Float + std::fmt::Debug> BinaryDerivative<F> for super::HuberErrorKernelOp<F> {
    const HAS_CONST_DF: bool = false;
    #[inline(always)]
    fn f(&self, &x: &F, &y: &F) -> F {
        let half = F::from(0.5).unwrap();
        if (x - y).abs() < self.delta {
            (x - y).powi(2) * half
        } else {
            (x - y).abs() * self.delta - half * self.delta * self.delta
        }
    }

    #[inline(always)]
    fn dfdx(&self, &x: &F, &y: &F) -> F {
        if (x - y) == F::zero() {
            F::zero()
        } else if (x - y).abs() < self.delta {
            x - y
        } else {
            (x - y).signum() * self.delta
        }
    }

    #[inline(always)]
    fn dfdy(&self, &x: &F, &y: &F) -> F {
        if (x - y) == F::zero() {
            F::zero()
        } else if (x - y).abs() < self.delta {
            y - x
        } else {
            (y - x).signum() * self.delta
        }
    }
}