ella_tensor/ops/
cmp.rs

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
use crate::{shape::NdimMax, Shape, Tensor, TensorValue};

use super::{binary_op, unary_op, TensorOp};

pub trait TensorCompare<Rhs> {
    type Output;

    fn eq(&self, other: &Rhs) -> Self::Output;
    fn ne(&self, other: &Rhs) -> Self::Output;
    fn lt(&self, other: &Rhs) -> Self::Output;
    fn gt(&self, other: &Rhs) -> Self::Output;
    fn lte(&self, other: &Rhs) -> Self::Output;
    fn gte(&self, other: &Rhs) -> Self::Output;
}

macro_rules! impl_tensor_compare {
    ($([$op:ident $kernel:tt])+) => {
        // Implement scalar compare
        impl<T1, T2, S> TensorCompare<Tensor<T2, S>> for T1
            where T1: TensorOp<T2>,
                T2: TensorValue,
                S: Shape,
                T1::Unmasked: PartialOrd<T2::Unmasked>,
                T1::Output<bool>: TensorValue,
        {
            type Output = Tensor<T1::Output<bool>, S>;

            $(
                fn $op(&self, other: &Tensor<T2, S>) -> Self::Output {
                    unary_op(&other, |t| T1::apply(self.clone(), t, |a, b| a $kernel b))
                }
            )+
        }

        // Implement tensor compare
        impl<T1, T2, S1, S2> TensorCompare<Tensor<T2, S2>> for Tensor<T1, S1>
            where T1: TensorOp<T2>,
                T2: TensorValue,
                S1: Shape + NdimMax<S2>,
                S2: Shape,
                T1::Unmasked: PartialOrd<T2::Unmasked>,
                T1::Output<bool>: TensorValue,
        {
            type Output = Tensor<T1::Output<bool>, <S1 as NdimMax<S2>>::Output>;

            $(
            fn $op(&self, other: &Tensor<T2, S2>) -> Self::Output {
                binary_op(self, other, |a, b| a.apply(b, |a, b| a $kernel b))
            }
            )+
        }

        // Implement tensor compare by reference
        impl<T1, T2, S1, S2> TensorCompare<Tensor<T2, S2>> for &Tensor<T1, S1>
            where T1: TensorOp<T2>,
                T2: TensorValue,
                S1: Shape + NdimMax<S2>,
                S2: Shape,
                T1::Unmasked: PartialOrd<T2::Unmasked>,
                T1::Output<bool>: TensorValue,
        {
            type Output = Tensor<T1::Output<bool>, <S1 as NdimMax<S2>>::Output>;

            $(
            fn $op(&self, other: &Tensor<T2, S2>) -> Self::Output {
                binary_op(self, other, |a, b| a.apply(b, |a, b| a $kernel b))
            }
            )+
        }
    };
}

impl_tensor_compare!(
    [eq ==]
    [ne !=]
    [lt <]
    [gt >]
    [lte <=]
    [gte >=]
);

impl<T, S> Tensor<T, S>
where
    T: TensorValue,
    S: Shape,
{
    pub fn eq<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.eq(self)
    }

    pub fn ne<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.ne(self)
    }

    pub fn lt<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.gte(self)
    }

    pub fn gt<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.lte(self)
    }

    pub fn lte<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.gt(self)
    }

    pub fn gte<C>(&self, other: C) -> C::Output
    where
        C: TensorCompare<Self>,
    {
        other.lt(self)
    }

    // pub fn minimum(&self, other: )
}