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
use super::TchOps;
use crate::{element::TchElement, LibTorch, LibTorchDevice, TchTensor};
use burn_tensor::{backend::Backend, ops::BoolTensorOps, Data, Reader, Shape};
use std::ops::Range;

impl<E: TchElement> BoolTensorOps<Self> for LibTorch<E> {
    fn bool_from_data<const D: usize>(
        data: Data<bool, D>,
        device: &LibTorchDevice,
    ) -> TchTensor<bool, D> {
        TchTensor::from_data(data, (*device).into())
    }

    fn bool_shape<const D: usize>(tensor: &TchTensor<bool, D>) -> Shape<D> {
        tensor.shape()
    }

    fn bool_repeat<const D: usize>(
        tensor: TchTensor<bool, D>,
        dim: usize,
        times: usize,
    ) -> TchTensor<bool, D> {
        TchOps::repeat(tensor, dim, times)
    }

    fn bool_into_data<const D: usize>(tensor: TchTensor<bool, D>) -> Reader<Data<bool, D>> {
        let shape = Self::bool_shape(&tensor);
        let tensor = Self::bool_reshape(tensor.clone(), Shape::new([shape.num_elements()]));
        let values: Result<Vec<bool>, tch::TchError> = tensor.tensor.shallow_clone().try_into();

        Reader::Concrete(Data::new(values.unwrap(), shape))
    }

    fn bool_to_device<const D: usize>(
        tensor: TchTensor<bool, D>,
        device: &LibTorchDevice,
    ) -> TchTensor<bool, D> {
        TchOps::to_device(tensor, device)
    }

    fn bool_reshape<const D1: usize, const D2: usize>(
        tensor: TchTensor<bool, D1>,
        shape: Shape<D2>,
    ) -> TchTensor<bool, D2> {
        TchOps::reshape(tensor, shape)
    }

    fn bool_device<const D: usize>(tensor: &TchTensor<bool, D>) -> LibTorchDevice {
        tensor.tensor.device().into()
    }

    fn bool_empty<const D: usize>(
        shape: Shape<D>,
        device: &<LibTorch<E> as Backend>::Device,
    ) -> TchTensor<bool, D> {
        let tensor = tch::Tensor::empty(
            shape.dims.map(|a| a as i64),
            (tch::Kind::Bool, (*device).into()),
        );

        TchTensor::new(tensor)
    }

    fn bool_slice<const D1: usize, const D2: usize>(
        tensor: TchTensor<bool, D1>,
        ranges: [Range<usize>; D2],
    ) -> TchTensor<bool, D1> {
        TchOps::slice(tensor, ranges)
    }

    fn bool_slice_assign<const D1: usize, const D2: usize>(
        tensor: TchTensor<bool, D1>,
        ranges: [std::ops::Range<usize>; D2],
        value: TchTensor<bool, D1>,
    ) -> TchTensor<bool, D1> {
        TchOps::slice_assign(tensor, ranges, value)
    }

    fn bool_cat<const D: usize>(
        tensors: Vec<TchTensor<bool, D>>,
        dim: usize,
    ) -> TchTensor<bool, D> {
        TchOps::cat(tensors, dim)
    }

    fn bool_equal<const D: usize>(
        lhs: TchTensor<bool, D>,
        rhs: TchTensor<bool, D>,
    ) -> TchTensor<bool, D> {
        TchOps::equal(lhs, rhs)
    }

    fn bool_not<const D: usize>(tensor: TchTensor<bool, D>) -> TchTensor<bool, D> {
        tensor.unary_ops(
            |mut tensor| tensor.eq_(0).to_kind(tch::Kind::Bool),
            |tensor| tensor.eq(0),
        )
    }

    fn bool_into_int<const D: usize>(tensor: TchTensor<bool, D>) -> TchTensor<i64, D> {
        let tensor = tensor.tensor.to_kind(tch::Kind::Int64);
        TchTensor::new(tensor)
    }

    fn bool_into_float<const D: usize>(tensor: TchTensor<bool, D>) -> TchTensor<E, D> {
        let tensor = tensor.tensor.to_kind(E::KIND);
        TchTensor::new(tensor)
    }

    fn bool_swap_dims<const D: usize>(
        tensor: <LibTorch<E> as Backend>::BoolTensorPrimitive<D>,
        dim1: usize,
        dim2: usize,
    ) -> <LibTorch<E> as Backend>::BoolTensorPrimitive<D> {
        TchOps::swap_dims(tensor, dim1, dim2)
    }

    fn bool_narrow<const D: usize>(
        tensor: TchTensor<bool, D>,
        dim: usize,
        start: usize,
        length: usize,
    ) -> TchTensor<bool, D> {
        TchOps::narrow(tensor, dim, start, length)
    }

    fn bool_chunk<const D: usize>(
        tensor: TchTensor<bool, D>,
        chunks: usize,
        dim: usize,
    ) -> Vec<TchTensor<bool, D>> {
        TchOps::chunk(tensor, chunks, dim)
    }
}