hpt 0.1.3

High Performance Tensor (HPT) - A fast, efficient, and user-friendly tensor computation library for Rust
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
use crate::tensor_base::_Tensor;
use crate::Tensor;
use hpt_allocator::traits::Allocator;
use hpt_allocator::traits::AllocatorOutputRetrive;
use hpt_allocator::Cpu;
use hpt_common::error::base::TensorError;
use hpt_common::error::shape::ShapeError;
use hpt_iterator::iterator_traits::ParStridedIteratorSimd;
use hpt_iterator::iterator_traits::ParStridedIteratorSimdZip;
use hpt_iterator::iterator_traits::ParStridedIteratorZip;
use hpt_iterator::TensorIterator;
use hpt_traits::ops::creation::TensorCreator;
use hpt_traits::tensor::CommonBounds;
use hpt_traits::tensor::TensorInfo;
use hpt_traits::tensor::TensorLike;
use hpt_types::dtype::TypeCommon;
use rayon::iter::{
    IndexedParallelIterator, IntoParallelRefIterator, IntoParallelRefMutIterator, ParallelIterator,
};
use std::borrow::Borrow;

/// Performs a binary operation on two tensors with optional SIMD optimization and an output tensor.
///
/// This method applies a binary function element-wise on two tensors (`lhs` and `rhs`) and returns
/// a new tensor with the result. Optionally, SIMD (Single Instruction, Multiple Data) can be used
/// for vectorized operations if the sizes of the underlying data vectors align. Additionally,
/// the user can provide an output tensor to store the result, allowing in-place computations
/// and reducing memory allocations.
///
/// # Arguments
///
/// * `lhs` - A reference to the left-hand side tensor involved in the binary operation.
/// * `rhs` - A reference to the right-hand side tensor involved in the binary operation.
/// * `f` - A binary function applied to elements of the tensors during the operation. This function
///   is used when SIMD is not applicable.
/// * `f2` - A binary function that operates on vectorized data (SIMD). This function is used when
///   SIMD is applicable.
/// * `out` - An optional output tensor that, if provided, will store the result of the operation.
///   If not provided, a new tensor will be created to hold the result.
///
/// # Returns
///
/// Returns a `Result` containing a new tensor with the result of the binary operation. If any error occurs
/// (e.g., shape mismatch or allocation issues), an `anyhow::Result` with an error message is returned.
///
/// # SIMD Optimization
///
/// If the vector sizes of the input tensors match and SIMD is enabled, the `f2` function is applied to
/// perform vectorized operations for faster computation. If not, the scalar function `f` is applied to each element.
#[track_caller]
pub(crate) fn binary_fn_with_out_simd<A, B, O, K, F, F2, const DEVICE: usize, Al>(
    lhs: &_Tensor<A, Cpu, DEVICE, Al>,
    rhs: &_Tensor<B, Cpu, DEVICE, Al>,
    f: F,
    f2: F2,
    out: Option<O>,
) -> std::result::Result<_Tensor<K, Cpu, DEVICE, Al>, TensorError>
where
    A: CommonBounds,
    B: CommonBounds,
    O: Borrow<_Tensor<K, Cpu, DEVICE, Al>>,
    K: CommonBounds,
    F: Fn(A, B) -> K + Sync + Send + Copy,
    F2: Fn(<A as TypeCommon>::Vec, <B as TypeCommon>::Vec) -> <K as TypeCommon>::Vec
        + Sync
        + Send
        + Copy,
    Al: Allocator,
    Al::Output: AllocatorOutputRetrive,
{
    use hpt_types::traits::*;
    use rayon::slice::{ParallelSlice, ParallelSliceMut};
    if lhs.size() == 1 {
        let val = lhs.as_raw()[0];
        let val_vec = <A as TypeCommon>::Vec::splat(val);
        let mut res = if let Some(out) = out {
            ShapeError::check_inplace_out_layout_valid(rhs.shape(), &out.borrow().layout())?;
            let out: &_Tensor<K, Cpu, DEVICE, Al> = out.borrow();
            out.clone()
        } else {
            _Tensor::<K, Cpu, DEVICE, Al>::empty(rhs.shape())?
        };
        if rhs.is_contiguous() {
            if <A as TypeCommon>::Vec::SIZE == <B as TypeCommon>::Vec::SIZE
                && <B as TypeCommon>::Vec::SIZE == <K as TypeCommon>::Vec::SIZE
            {
                let remain = res.size() % <A as TypeCommon>::Vec::SIZE;
                res.as_raw_mut()
                    .par_chunks_exact_mut(<A as TypeCommon>::Vec::SIZE)
                    .zip(rhs.as_raw().par_chunks_exact(<A as TypeCommon>::Vec::SIZE))
                    .for_each(|(a, b)| {
                        let inp = unsafe { <B as TypeCommon>::Vec::from_ptr(b.as_ptr()) };
                        let res: *const K = f2(val_vec, inp).as_ptr();
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                res,
                                a.as_mut_ptr(),
                                <A as TypeCommon>::Vec::SIZE,
                            );
                        }
                    });
                if remain > 0 {
                    let ret_size = res.size();
                    res.as_raw_mut()[ret_size - remain..]
                        .iter_mut()
                        .zip(rhs.as_raw()[ret_size - remain..].iter())
                        .for_each(|(a, b)| {
                            *a = f(val, *b);
                        });
                }
            } else {
                res.as_raw_mut()
                    .par_chunks_exact_mut(<K as TypeCommon>::Vec::SIZE)
                    .zip(rhs.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                    .for_each(|(a, b)| {
                        a.iter_mut().zip(b.iter()).for_each(|(a, b)| {
                            *a = f(val, *b);
                        });
                    });
                let remain = res.size() % <K as TypeCommon>::Vec::SIZE;
                if remain > 0 {
                    let ret_size = res.size();
                    res.as_raw_mut()[ret_size - remain..]
                        .iter_mut()
                        .zip(rhs.as_raw()[ret_size - remain..].iter())
                        .for_each(|(a, b)| {
                            *a = f(val, *b);
                        });
                }
            }
        } else {
            res.par_iter_mut().zip(rhs.par_iter()).for_each(|(a, b)| {
                *a = f(val, b);
            });
        }
        Ok(res)
    } else if rhs.size() == 1 {
        let val = rhs.as_raw()[0];
        let val_vec = <B as TypeCommon>::Vec::splat(val);
        let mut res = if let Some(out) = out {
            ShapeError::check_inplace_out_layout_valid(lhs.shape(), &out.borrow().layout())?;
            let out: &_Tensor<K, Cpu, DEVICE, Al> = out.borrow();
            out.clone()
        } else {
            _Tensor::<K, Cpu, DEVICE, Al>::empty(lhs.shape())?
        };
        if lhs.is_contiguous() {
            if <A as TypeCommon>::Vec::SIZE == <B as TypeCommon>::Vec::SIZE
                && <B as TypeCommon>::Vec::SIZE == <K as TypeCommon>::Vec::SIZE
            {
                let remain = res.size() % <A as TypeCommon>::Vec::SIZE;
                res.as_raw_mut()
                    .par_chunks_exact_mut(<A as TypeCommon>::Vec::SIZE)
                    .zip(lhs.as_raw().par_chunks_exact(<A as TypeCommon>::Vec::SIZE))
                    .for_each(|(a, lhs)| {
                        let inp = unsafe { <A as TypeCommon>::Vec::from_ptr(lhs.as_ptr()) };
                        let res: *const K = f2(inp, val_vec).as_ptr();
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                res,
                                a.as_mut_ptr(),
                                <A as TypeCommon>::Vec::SIZE,
                            );
                        }
                    });
                if remain > 0 {
                    let ret_size = res.size();
                    res.as_raw_mut()[ret_size - remain..]
                        .iter_mut()
                        .zip(lhs.as_raw()[ret_size - remain..].iter())
                        .for_each(|(a, lhs)| {
                            *a = f(*lhs, val);
                        });
                }
            } else {
                res.as_raw_mut()
                    .par_chunks_exact_mut(<K as TypeCommon>::Vec::SIZE)
                    .zip(lhs.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                    .for_each(|(a, lhs)| {
                        a.iter_mut().zip(lhs.iter()).for_each(|(a, lhs)| {
                            *a = f(*lhs, val);
                        });
                    });
                let remain = res.size() % <K as TypeCommon>::Vec::SIZE;
                if remain > 0 {
                    let ret_size = res.size();
                    res.as_raw_mut()[ret_size - remain..]
                        .iter_mut()
                        .zip(lhs.as_raw()[ret_size - remain..].iter())
                        .for_each(|(a, lhs)| {
                            *a = f(*lhs, val);
                        });
                }
            }
        } else {
            res.par_iter_mut().zip(lhs.par_iter()).for_each(|(a, lhs)| {
                *a = f(lhs, val);
            });
        }
        Ok(res)
    } else {
        if rhs.is_contiguous() && lhs.is_contiguous() && rhs.shape() == lhs.shape() {
            let mut ret = if let Some(out) = out {
                ShapeError::check_inplace_out_layout_valid(rhs.shape(), &out.borrow().layout())?;
                let out: &_Tensor<K, Cpu, DEVICE, Al> = out.borrow();
                out.clone()
            } else {
                _Tensor::<K, Cpu, DEVICE, Al>::empty(rhs.shape())?
            };
            if <A as TypeCommon>::Vec::SIZE == <B as TypeCommon>::Vec::SIZE
                && <B as TypeCommon>::Vec::SIZE == <K as TypeCommon>::Vec::SIZE
            {
                let remain = ret.size() % <K as TypeCommon>::Vec::SIZE;
                ret.as_raw_mut()
                    .par_chunks_exact_mut(<K as TypeCommon>::Vec::SIZE)
                    .zip(lhs.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                    .zip(rhs.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                    .for_each(|((ret, lhs), rhs)| {
                        let a = unsafe { <A as TypeCommon>::Vec::from_ptr(lhs.as_ptr()) };
                        let b = unsafe { <B as TypeCommon>::Vec::from_ptr(rhs.as_ptr()) };
                        let res = f2(a, b);
                        unsafe {
                            std::ptr::copy_nonoverlapping(
                                res.as_ptr(),
                                ret.as_mut_ptr(),
                                <K as TypeCommon>::Vec::SIZE,
                            );
                        }
                    });
                if remain > 0 {
                    let ret_size = ret.size();
                    ret.as_raw_mut()[ret_size - remain..]
                        .iter_mut()
                        .zip(lhs.as_raw()[ret_size - remain..].iter())
                        .zip(rhs.as_raw()[ret_size - remain..].iter())
                        .for_each(|((a, &lhs), &rhs)| {
                            *a = f(lhs, rhs);
                        });
                }
            } else {
                let min_len: usize =
                    ret.size() / (((rayon::current_num_threads() as f64) * 1.3) as usize);
                ret.as_raw_mut()
                    .par_iter_mut()
                    .with_min_len(min_len)
                    .zip(lhs.as_raw().par_iter().with_min_len(min_len))
                    .zip(rhs.as_raw().par_iter().with_min_len(min_len))
                    .for_each(|((ret, &lhs), &rhs)| {
                        *ret = f(lhs, rhs);
                    });
            }
            Ok(ret)
        } else {
            let output_shape = lhs.layout().broadcast(rhs.layout())?;
            let mut res = if let Some(out) = out {
                ShapeError::check_inplace_out_layout_valid(
                    output_shape.shape(),
                    &out.borrow().layout(),
                )?;
                let out: &_Tensor<K, Cpu, DEVICE, Al> = out.borrow();
                out.clone()
            } else {
                _Tensor::<K, Cpu, DEVICE, Al>::empty(output_shape.shape())?
            };
            let iter = res
                .par_iter_mut_simd()
                .zip(lhs.par_iter_simd())
                .zip(rhs.par_iter_simd());
            ParStridedIteratorSimd::for_each(
                iter,
                |((x, y), z)| {
                    *x = f(y, z);
                },
                |((x, y), z)| {
                    x.write_unaligned(f2(y, z));
                },
            );
            Ok(res)
        }
    }
}

/// Perform binary operation with output tensor
#[track_caller]
pub fn binary_with_out<A, B, O, K, F, F2, const DEVICE: usize, Al>(
    lhs: &Tensor<A, Cpu, DEVICE, Al>,
    rhs: &Tensor<B, Cpu, DEVICE, Al>,
    f: F,
    f2: F2,
    out: Option<O>,
) -> std::result::Result<Tensor<K, Cpu, DEVICE, Al>, TensorError>
where
    A: CommonBounds,
    B: CommonBounds,
    O: Borrow<Tensor<K, Cpu, DEVICE, Al>>,
    K: CommonBounds,
    F: Fn(A, B) -> K + Sync + Send + Copy,
    F2: Fn(<A as TypeCommon>::Vec, <B as TypeCommon>::Vec) -> <K as TypeCommon>::Vec
        + Sync
        + Send
        + Copy,
    Al: Allocator,
    Al::Output: AllocatorOutputRetrive,
{
    let out: Option<_Tensor<K, Cpu, DEVICE, Al>> = out.map(|x| x.borrow().inner.as_ref().clone());
    Ok(binary_fn_with_out_simd(lhs.inner.as_ref(), rhs.inner.as_ref(), f, f2, out)?.into())
}

#[track_caller]
pub(crate) fn binary_fn_with_out_simd_3<A, B, C, O, K, F, F2, const DEVICE: usize, Al>(
    a: &_Tensor<A, Cpu, DEVICE, Al>,
    b: &_Tensor<B, Cpu, DEVICE, Al>,
    c: &_Tensor<C, Cpu, DEVICE, Al>,
    f: F,
    f2: F2,
    out: Option<O>,
) -> std::result::Result<_Tensor<K, Cpu, DEVICE, Al>, TensorError>
where
    A: CommonBounds,
    B: CommonBounds,
    C: CommonBounds,
    O: Borrow<_Tensor<K, Cpu, DEVICE, Al>>,
    K: CommonBounds,
    F: Fn(A, B, C) -> K + Sync + Send + Copy,
    F2: Fn(
            <A as TypeCommon>::Vec,
            <B as TypeCommon>::Vec,
            <C as TypeCommon>::Vec,
        ) -> <K as TypeCommon>::Vec
        + Sync
        + Send
        + Copy,
    Al: Allocator,
    Al::Output: AllocatorOutputRetrive,
{
    use hpt_types::traits::*;
    use rayon::slice::{ParallelSlice, ParallelSliceMut};
    if b.is_contiguous() && a.is_contiguous() && b.shape() == a.shape() {
        let mut ret = if let Some(out) = out {
            ShapeError::check_inplace_out_layout_valid(b.shape(), &out.borrow().layout())?;
            let out: &_Tensor<K, Cpu, DEVICE, Al> = out.borrow();
            out.clone()
        } else {
            _Tensor::<K, Cpu, DEVICE, Al>::empty(b.shape())?
        };
        if <A as TypeCommon>::Vec::SIZE == <B as TypeCommon>::Vec::SIZE
            && <B as TypeCommon>::Vec::SIZE == <K as TypeCommon>::Vec::SIZE
            && <B as TypeCommon>::Vec::SIZE == <C as TypeCommon>::Vec::SIZE
        {
            let remain = ret.size() % <K as TypeCommon>::Vec::SIZE;
            ret.as_raw_mut()
                .par_chunks_exact_mut(<K as TypeCommon>::Vec::SIZE)
                .zip(a.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                .zip(b.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                .zip(c.as_raw().par_chunks_exact(<K as TypeCommon>::Vec::SIZE))
                .for_each(|(((ret, a), b), c)| {
                    let a = unsafe { <A as TypeCommon>::Vec::from_ptr(a.as_ptr()) };
                    let b = unsafe { <B as TypeCommon>::Vec::from_ptr(b.as_ptr()) };
                    let c = unsafe { <C as TypeCommon>::Vec::from_ptr(c.as_ptr()) };
                    let res = f2(a, b, c);
                    unsafe {
                        std::ptr::copy_nonoverlapping(
                            res.as_ptr(),
                            ret.as_mut_ptr(),
                            <K as TypeCommon>::Vec::SIZE,
                        );
                    }
                });
            if remain > 0 {
                let ret_size = ret.size();
                ret.as_raw_mut()[ret_size - remain..]
                    .iter_mut()
                    .zip(a.as_raw()[ret_size - remain..].iter())
                    .zip(b.as_raw()[ret_size - remain..].iter())
                    .zip(c.as_raw()[ret_size - remain..].iter())
                    .for_each(|(((a, &lhs), &rhs), &c)| {
                        *a = f(lhs, rhs, c);
                    });
            }
        } else {
            let min_len: usize =
                ret.size() / (((rayon::current_num_threads() as f64) * 1.3) as usize);
            ret.as_raw_mut()
                .par_iter_mut()
                .with_min_len(min_len)
                .zip(a.as_raw().par_iter().with_min_len(min_len))
                .zip(b.as_raw().par_iter().with_min_len(min_len))
                .zip(c.as_raw().par_iter().with_min_len(min_len))
                .for_each(|(((ret, &lhs), &rhs), &c)| {
                    *ret = f(lhs, rhs, c);
                });
        }
        Ok(ret)
    } else {
        let ret = a
            .par_iter()
            .zip(b.par_iter())
            .zip(c.par_iter())
            .strided_map(|(res, ((x, y), z))| {
                *res = f(x, y, z);
            })
            .collect::<_Tensor<K, Cpu, DEVICE, Al>>();
        Ok(ret)
    }
}