onnx-runtime-ep-cpu 0.1.0-dev.4

CPU execution provider for the ORT 2.0 runtime
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! ONNX `Unique` (opset 11+) over flattened elements or slices along an axis.

use std::cmp::Ordering;

use onnx_runtime_ep_api::{EpError, Kernel, KernelFactory, Result, TensorMut, TensorView};
use onnx_runtime_ir::{Attribute, DataType, Node};

use super::{elem_size, to_dense_bytes, write_dense_bytes};
use crate::dtype::unsupported_dtype;
use crate::strided::numel;

pub struct UniqueKernel {
    axis: Option<i64>,
    sorted: bool,
}

pub struct UniqueFactory;

impl KernelFactory for UniqueFactory {
    fn create(&self, node: &Node, _input_shapes: &[Vec<usize>]) -> Result<Box<dyn Kernel>> {
        let axis = optional_int_attr(node, "axis")?;
        let sorted = match optional_int_attr(node, "sorted")?.unwrap_or(1) {
            0 => false,
            1 => true,
            value => {
                return Err(EpError::KernelFailed(format!(
                    "Unique: `sorted` must be 0 or 1, got {value}"
                )));
            }
        };
        Ok(Box::new(UniqueKernel { axis, sorted }))
    }
}

impl Kernel for UniqueKernel {
    fn execute(&self, inputs: &[TensorView], outputs: &mut [TensorMut]) -> Result<()> {
        if inputs.len() != 1 || !(1..=4).contains(&outputs.len()) {
            return Err(EpError::KernelFailed(format!(
                "Unique: expected 1 input and 1..=4 outputs, got {} inputs and {} outputs",
                inputs.len(),
                outputs.len()
            )));
        }

        let input = &inputs[0];
        ensure_supported_dtype(input.dtype)?;

        let element_size = elem_size(input.dtype)?;
        let dense = to_dense_bytes(input)?;
        let plan = unique_plan(
            &dense,
            input.dtype,
            input.shape,
            element_size,
            self.axis,
            self.sorted,
        )?;

        let expected_y_shape = match plan.axis {
            Some(axis) => {
                let mut shape = input.shape.to_vec();
                shape[axis] = plan.first_indices.len();
                shape
            }
            None => vec![plan.first_indices.len()],
        };
        validate_output(&outputs[0], input.dtype, &expected_y_shape, "Y")?;
        let y = gather_y(
            &dense,
            input.shape,
            element_size,
            plan.axis,
            &plan.first_indices,
        );
        write_dense_bytes(&mut outputs[0], &y)?;

        let unique_shape = [plan.first_indices.len()];
        if outputs.len() >= 2 {
            validate_output(&outputs[1], DataType::Int64, &unique_shape, "indices")?;
            write_i64(&mut outputs[1], &plan.first_indices)?;
        }
        if outputs.len() >= 3 {
            let inverse_shape = [plan.inverse_indices.len()];
            validate_output(
                &outputs[2],
                DataType::Int64,
                &inverse_shape,
                "inverse_indices",
            )?;
            write_i64(&mut outputs[2], &plan.inverse_indices)?;
        }
        if outputs.len() == 4 {
            validate_output(&outputs[3], DataType::Int64, &unique_shape, "counts")?;
            write_i64(&mut outputs[3], &plan.counts)?;
        }
        Ok(())
    }

    fn supports_strided_input(&self, input_idx: usize) -> bool {
        input_idx == 0
    }
}

struct UniquePlan {
    axis: Option<usize>,
    first_indices: Vec<usize>,
    inverse_indices: Vec<usize>,
    counts: Vec<usize>,
}

fn unique_plan(
    dense: &[u8],
    dtype: DataType,
    shape: &[usize],
    element_size: usize,
    axis: Option<i64>,
    sorted: bool,
) -> Result<UniquePlan> {
    let axis = axis
        .map(|axis| normalize_axis(axis, shape.len()))
        .transpose()?;
    let item_count = axis.map_or_else(|| numel(shape), |axis| shape[axis]);
    let items = make_items(dense, shape, element_size, axis);

    let (first_indices, inverse_indices, counts) = unique_groups(&items, dtype, item_count, sorted);

    Ok(UniquePlan {
        axis,
        first_indices,
        inverse_indices,
        counts,
    })
}

fn unique_groups(
    items: &[Vec<u8>],
    dtype: DataType,
    item_count: usize,
    sorted: bool,
) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
    unique_groups_by(item_count, sorted, |a, b| {
        compare_items(dtype, &items[a], &items[b])
    })
}

fn unique_groups_by(
    item_count: usize,
    sorted: bool,
    compare: impl Fn(usize, usize) -> Ordering,
) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
    let mut order: Vec<usize> = (0..item_count).collect();
    order.sort_unstable_by(|&a, &b| compare(a, b).then_with(|| a.cmp(&b)));

    let mut first_indices = Vec::new();
    let mut inverse_indices = vec![0; item_count];
    let mut counts = Vec::new();
    let mut previous_index: Option<usize> = None;
    for &index in &order {
        let new_group =
            previous_index.is_none_or(|previous| compare(previous, index) != Ordering::Equal);
        if new_group {
            first_indices.push(index);
            counts.push(0);
        } else if index < *first_indices.last().unwrap() {
            *first_indices.last_mut().unwrap() = index;
        }
        let group = counts.len() - 1;
        inverse_indices[index] = group;
        counts[group] += 1;
        previous_index = Some(index);
    }

    if !sorted {
        let mut group_order: Vec<usize> = (0..first_indices.len()).collect();
        group_order.sort_unstable_by_key(|&group| first_indices[group]);
        let mut sorted_to_unsorted = vec![0; group_order.len()];
        for (unsorted, &sorted) in group_order.iter().enumerate() {
            sorted_to_unsorted[sorted] = unsorted;
        }
        first_indices = group_order
            .iter()
            .map(|&group| first_indices[group])
            .collect();
        counts = group_order.iter().map(|&group| counts[group]).collect();
        for group in &mut inverse_indices {
            *group = sorted_to_unsorted[*group];
        }
    }

    (first_indices, inverse_indices, counts)
}

fn make_items(
    dense: &[u8],
    shape: &[usize],
    element_size: usize,
    axis: Option<usize>,
) -> Vec<Vec<u8>> {
    let Some(axis) = axis else {
        return dense
            .chunks_exact(element_size)
            .map(<[u8]>::to_vec)
            .collect();
    };
    let axis_len = shape[axis];
    let inner: usize = shape[axis + 1..].iter().product();
    let outer: usize = shape[..axis].iter().product();
    let block_bytes = inner * element_size;
    let mut items = vec![Vec::with_capacity(outer * block_bytes); axis_len];
    for outer_index in 0..outer {
        for (axis_index, item) in items.iter_mut().enumerate() {
            let start = (outer_index * axis_len + axis_index) * block_bytes;
            item.extend_from_slice(&dense[start..start + block_bytes]);
        }
    }
    items
}

fn gather_y(
    dense: &[u8],
    shape: &[usize],
    element_size: usize,
    axis: Option<usize>,
    first_indices: &[usize],
) -> Vec<u8> {
    let Some(axis) = axis else {
        let mut output = Vec::with_capacity(first_indices.len() * element_size);
        for &index in first_indices {
            let start = index * element_size;
            output.extend_from_slice(&dense[start..start + element_size]);
        }
        return output;
    };
    let axis_len = shape[axis];
    let inner: usize = shape[axis + 1..].iter().product();
    let outer: usize = shape[..axis].iter().product();
    let block_bytes = inner * element_size;
    let mut output = Vec::with_capacity(outer * first_indices.len() * block_bytes);
    for outer_index in 0..outer {
        for &axis_index in first_indices {
            let start = (outer_index * axis_len + axis_index) * block_bytes;
            output.extend_from_slice(&dense[start..start + block_bytes]);
        }
    }
    output
}

fn compare_items(dtype: DataType, a: &[u8], b: &[u8]) -> Ordering {
    let size = dtype.byte_size();
    for (a, b) in a.chunks_exact(size).zip(b.chunks_exact(size)) {
        let ordering = compare_element(dtype, a, b);
        if ordering != Ordering::Equal {
            return ordering;
        }
    }
    Ordering::Equal
}

fn compare_element(dtype: DataType, a: &[u8], b: &[u8]) -> Ordering {
    macro_rules! compare {
        ($ty:ty) => {{
            let a = <$ty>::from_le_bytes(a.try_into().unwrap());
            let b = <$ty>::from_le_bytes(b.try_into().unwrap());
            a.cmp(&b)
        }};
    }
    macro_rules! compare_float {
        ($ty:ty) => {{
            let a = <$ty>::from_le_bytes(a.try_into().unwrap());
            let b = <$ty>::from_le_bytes(b.try_into().unwrap());
            match (a.is_nan(), b.is_nan()) {
                (true, true) => Ordering::Equal,
                (true, false) => Ordering::Greater,
                (false, true) => Ordering::Less,
                (false, false) => a.partial_cmp(&b).unwrap(),
            }
        }};
    }
    match dtype {
        DataType::Bool | DataType::Uint8 => a[0].cmp(&b[0]),
        DataType::Int8 => (a[0] as i8).cmp(&(b[0] as i8)),
        DataType::Uint16 => compare!(u16),
        DataType::Int16 => compare!(i16),
        DataType::Uint32 => compare!(u32),
        DataType::Int32 => compare!(i32),
        DataType::Uint64 => compare!(u64),
        DataType::Int64 => compare!(i64),
        DataType::Float16 => {
            let a = half::f16::from_le_bytes(a.try_into().unwrap());
            let b = half::f16::from_le_bytes(b.try_into().unwrap());
            match (a.is_nan(), b.is_nan()) {
                (true, true) => Ordering::Equal,
                (true, false) => Ordering::Greater,
                (false, true) => Ordering::Less,
                (false, false) => a.partial_cmp(&b).unwrap(),
            }
        }
        DataType::BFloat16 => {
            let a = half::bf16::from_le_bytes(a.try_into().unwrap());
            let b = half::bf16::from_le_bytes(b.try_into().unwrap());
            match (a.is_nan(), b.is_nan()) {
                (true, true) => Ordering::Equal,
                (true, false) => Ordering::Greater,
                (false, true) => Ordering::Less,
                (false, false) => a.partial_cmp(&b).unwrap(),
            }
        }
        DataType::Float32 => compare_float!(f32),
        DataType::Float64 => compare_float!(f64),
        _ => unreachable!("unsupported Unique dtype was validated"),
    }
}

fn normalize_axis(axis: i64, rank: usize) -> Result<usize> {
    let rank = i64::try_from(rank)
        .map_err(|_| EpError::KernelFailed("Unique: input rank is too large".into()))?;
    let axis = if axis < 0 { axis + rank } else { axis };
    if !(0..rank).contains(&axis) {
        return Err(EpError::KernelFailed(format!(
            "Unique: axis {axis} is out of range for rank {rank}"
        )));
    }
    Ok(axis as usize)
}

fn ensure_supported_dtype(dtype: DataType) -> Result<()> {
    match dtype {
        DataType::Bool
        | DataType::Uint8
        | DataType::Int8
        | DataType::Uint16
        | DataType::Int16
        | DataType::Uint32
        | DataType::Int32
        | DataType::Uint64
        | DataType::Int64
        | DataType::Float16
        | DataType::BFloat16
        | DataType::Float32
        | DataType::Float64 => Ok(()),
        _ => Err(unsupported_dtype("Unique", dtype)),
    }
}

fn validate_output(output: &TensorMut, dtype: DataType, shape: &[usize], name: &str) -> Result<()> {
    if output.dtype != dtype || output.shape != shape {
        return Err(EpError::KernelFailed(format!(
            "Unique: {name} must have dtype {dtype:?} and shape {shape:?}, got {:?}{:?}",
            output.dtype, output.shape
        )));
    }
    Ok(())
}

fn write_i64(output: &mut TensorMut, values: &[usize]) -> Result<()> {
    let mut bytes = Vec::with_capacity(values.len() * 8);
    for &value in values {
        let value = i64::try_from(value)
            .map_err(|_| EpError::KernelFailed("Unique: index exceeds i64 range".into()))?;
        bytes.extend_from_slice(&value.to_le_bytes());
    }
    write_dense_bytes(output, &bytes)
}

fn optional_int_attr(node: &Node, name: &str) -> Result<Option<i64>> {
    match node.attr(name) {
        None => Ok(None),
        Some(Attribute::Int(value)) => Ok(Some(*value)),
        Some(_) => Err(EpError::KernelFailed(format!(
            "Unique: `{name}` must be an integer"
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::kernels::testutil::Owned;

    #[test]
    fn sorts_axis_slices_lexicographically() {
        let input = Owned::f32(
            &[2, 4, 2],
            &[
                1., 1., 0., 1., 2., 1., 0., 1., 1., 1., 0., 1., 2., 1., 0., 1.,
            ],
        );
        let mut y = Owned::zeros_f32(&[2, 3, 2]);
        let mut indices = Owned::zeros(DataType::Int64, &[3]);
        let mut inverse = Owned::zeros(DataType::Int64, &[4]);
        let mut counts = Owned::zeros(DataType::Int64, &[3]);

        UniqueKernel {
            axis: Some(1),
            sorted: true,
        }
        .execute(
            &[input.view()],
            &mut [
                y.view_mut(),
                indices.view_mut(),
                inverse.view_mut(),
                counts.view_mut(),
            ],
        )
        .unwrap();

        assert_eq!(
            y.to_f32(),
            vec![0., 1., 1., 1., 2., 1., 0., 1., 1., 1., 2., 1.]
        );
        assert_eq!(indices.to_i64(), vec![1, 0, 2]);
        assert_eq!(inverse.to_i64(), vec![1, 0, 2, 0]);
        assert_eq!(counts.to_i64(), vec![2, 1, 1]);
    }

    #[test]
    fn unsorted_flattened_values_keep_first_appearance() {
        let input = Owned::f32(&[6], &[2., 1., 1., 3., 4., 3.]);
        let mut y = Owned::zeros_f32(&[4]);
        let mut indices = Owned::zeros(DataType::Int64, &[4]);
        let mut inverse = Owned::zeros(DataType::Int64, &[6]);
        let mut counts = Owned::zeros(DataType::Int64, &[4]);

        UniqueKernel {
            axis: None,
            sorted: false,
        }
        .execute(
            &[input.view()],
            &mut [
                y.view_mut(),
                indices.view_mut(),
                inverse.view_mut(),
                counts.view_mut(),
            ],
        )
        .unwrap();

        assert_eq!(y.to_f32(), vec![2., 1., 3., 4.]);
        assert_eq!(indices.to_i64(), vec![0, 1, 3, 4]);
        assert_eq!(inverse.to_i64(), vec![0, 1, 1, 2, 3, 2]);
        assert_eq!(counts.to_i64(), vec![1, 2, 2, 1]);
    }

    #[test]
    fn collapses_all_nan_payloads_and_signed_zero() {
        let first_nan = f32::from_bits(0x7fc0_0001);
        let second_nan = f32::from_bits(0x7fc0_1234);
        let input = Owned::f32(&[4], &[first_nan, second_nan, -0.0, 0.0]);
        let mut y = Owned::zeros_f32(&[2]);
        let mut indices = Owned::zeros(DataType::Int64, &[2]);
        let mut inverse = Owned::zeros(DataType::Int64, &[4]);
        let mut counts = Owned::zeros(DataType::Int64, &[2]);

        UniqueKernel {
            axis: None,
            sorted: true,
        }
        .execute(
            &[input.view()],
            &mut [
                y.view_mut(),
                indices.view_mut(),
                inverse.view_mut(),
                counts.view_mut(),
            ],
        )
        .unwrap();

        let values = y.to_f32();
        assert_eq!(values[0], -0.0);
        assert!(values[1].is_nan());
        assert_eq!(indices.to_i64(), vec![2, 0]);
        assert_eq!(inverse.to_i64(), vec![1, 1, 0, 0]);
        assert_eq!(counts.to_i64(), vec![2, 2]);
    }

    #[test]
    fn large_unique_input_uses_sort_and_linear_grouping() {
        let item_count = 50_000usize;
        let items: Vec<Vec<u8>> = (0..item_count)
            .rev()
            .map(|value| (value as u64).to_le_bytes().to_vec())
            .collect();

        let (indices, inverse, counts) = unique_groups(&items, DataType::Uint64, item_count, true);
        assert_eq!(indices.len(), item_count);
        assert_eq!(inverse.len(), item_count);
        assert!(counts.iter().all(|&count| count == 1));
        assert_eq!(indices[0], item_count - 1);
        assert_eq!(indices[item_count - 1], 0);
    }
}