|
| 1 | +use crate::{dimension::Dimension, try_unsafe, util::Result, Rank}; |
| 2 | +use openvino_sys::{ |
| 3 | + ov_dimension_t, ov_partial_shape_create, ov_partial_shape_create_dynamic, |
| 4 | + ov_partial_shape_create_static, ov_partial_shape_free, ov_partial_shape_is_dynamic, |
| 5 | + ov_partial_shape_t, ov_rank_t, |
| 6 | +}; |
| 7 | + |
| 8 | +use std::convert::TryInto; |
| 9 | + |
| 10 | +/// See [`PartialShape`](https://docs.openvino.ai/2023.3/api/c_cpp_api/group__ov__partial__shape__c__api.html). |
| 11 | +pub struct PartialShape { |
| 12 | + instance: ov_partial_shape_t, |
| 13 | +} |
| 14 | + |
| 15 | +impl Drop for PartialShape { |
| 16 | + /// Drops the `PartialShape` instance and frees the associated memory. |
| 17 | + fn drop(&mut self) { |
| 18 | + unsafe { ov_partial_shape_free(std::ptr::addr_of_mut!(self.instance)) } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl PartialShape { |
| 23 | + /// Get the pointer to the underlying OpenVINO partial shape. |
| 24 | + #[allow(dead_code)] |
| 25 | + pub(crate) fn instance(&self) -> ov_partial_shape_t { |
| 26 | + self.instance |
| 27 | + } |
| 28 | + |
| 29 | + /// Create a new partial shape object from `ov_partial_shape_t`. |
| 30 | + pub(crate) fn new_from_instance(instance: ov_partial_shape_t) -> Self { |
| 31 | + Self { instance } |
| 32 | + } |
| 33 | + |
| 34 | + /// Creates a new `PartialShape` instance with a static rank and dynamic dimensions. |
| 35 | + pub fn new(rank: i64, dimensions: &[Dimension]) -> Result<Self> { |
| 36 | + let mut partial_shape = ov_partial_shape_t { |
| 37 | + rank: ov_rank_t { min: 0, max: 0 }, |
| 38 | + dims: std::ptr::null_mut(), |
| 39 | + }; |
| 40 | + try_unsafe!(ov_partial_shape_create( |
| 41 | + rank, |
| 42 | + dimensions.as_ptr().cast::<ov_dimension_t>(), |
| 43 | + std::ptr::addr_of_mut!(partial_shape) |
| 44 | + ))?; |
| 45 | + Ok(Self { |
| 46 | + instance: partial_shape, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + /// Creates a new `PartialShape` instance with a dynamic rank and dynamic dimensions. |
| 51 | + pub fn new_dynamic(rank: Rank, dimensions: &[Dimension]) -> Result<Self> { |
| 52 | + let mut partial_shape = ov_partial_shape_t { |
| 53 | + rank: ov_rank_t { min: 0, max: 0 }, |
| 54 | + dims: std::ptr::null_mut(), |
| 55 | + }; |
| 56 | + try_unsafe!(ov_partial_shape_create_dynamic( |
| 57 | + rank.instance(), |
| 58 | + dimensions.as_ptr().cast::<ov_dimension_t>(), |
| 59 | + std::ptr::addr_of_mut!(partial_shape) |
| 60 | + ))?; |
| 61 | + Ok(Self { |
| 62 | + instance: partial_shape, |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + /// Creates a new `PartialShape` instance with a static rank and static dimensions. |
| 67 | + pub fn new_static(rank: i64, dimensions: &[i64]) -> Result<Self> { |
| 68 | + let mut partial_shape = ov_partial_shape_t { |
| 69 | + rank: ov_rank_t { min: 0, max: 0 }, |
| 70 | + dims: std::ptr::null_mut(), |
| 71 | + }; |
| 72 | + try_unsafe!(ov_partial_shape_create_static( |
| 73 | + rank, |
| 74 | + dimensions.as_ptr(), |
| 75 | + std::ptr::addr_of_mut!(partial_shape) |
| 76 | + ))?; |
| 77 | + Ok(Self { |
| 78 | + instance: partial_shape, |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + /// Returns the rank of the partial shape. |
| 83 | + pub fn get_rank(&self) -> Rank { |
| 84 | + let rank = self.instance.rank; |
| 85 | + Rank::new_from_instance(rank) |
| 86 | + } |
| 87 | + |
| 88 | + /// Returns the dimensions of the partial shape. |
| 89 | + pub fn get_dimensions(&self) -> &[Dimension] { |
| 90 | + if self.instance.dims.is_null() { |
| 91 | + &[] |
| 92 | + } else { |
| 93 | + unsafe { |
| 94 | + std::slice::from_raw_parts( |
| 95 | + self.instance.dims.cast::<Dimension>(), |
| 96 | + self.instance.rank.max.try_into().unwrap(), |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /// Returns `true` if the partial shape is dynamic. |
| 103 | + pub fn is_dynamic(&self) -> bool { |
| 104 | + unsafe { ov_partial_shape_is_dynamic(self.instance) } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(test)] |
| 109 | +mod tests { |
| 110 | + use crate::LoadingError; |
| 111 | + |
| 112 | + use super::*; |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn test_new_partial_shape() { |
| 116 | + openvino_sys::library::load() |
| 117 | + .map_err(LoadingError::SystemFailure) |
| 118 | + .unwrap(); |
| 119 | + |
| 120 | + let dimensions = vec![ |
| 121 | + Dimension::new(0, 1), |
| 122 | + Dimension::new(1, 2), |
| 123 | + Dimension::new(2, 3), |
| 124 | + Dimension::new(3, 4), |
| 125 | + ]; |
| 126 | + |
| 127 | + let shape = PartialShape::new(4, &dimensions).unwrap(); |
| 128 | + assert_eq!(shape.get_rank().get_min(), 4); |
| 129 | + assert_eq!(shape.get_rank().get_max(), 4); |
| 130 | + assert!(shape.is_dynamic()); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_new_dynamic_partial_shape() { |
| 135 | + openvino_sys::library::load() |
| 136 | + .map_err(LoadingError::SystemFailure) |
| 137 | + .unwrap(); |
| 138 | + |
| 139 | + let dimensions = vec![Dimension::new(1, 1), Dimension::new(2, 2)]; |
| 140 | + |
| 141 | + let shape = PartialShape::new_dynamic(Rank::new(0, 2), &dimensions).unwrap(); |
| 142 | + assert!(shape.is_dynamic()); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn test_new_static_partial_shape() { |
| 147 | + openvino_sys::library::load() |
| 148 | + .map_err(LoadingError::SystemFailure) |
| 149 | + .unwrap(); |
| 150 | + |
| 151 | + let dimensions = vec![1, 2]; |
| 152 | + |
| 153 | + let shape = PartialShape::new_static(2, &dimensions).unwrap(); |
| 154 | + assert!(!shape.is_dynamic()); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_get_dimensions() { |
| 159 | + openvino_sys::library::load() |
| 160 | + .map_err(LoadingError::SystemFailure) |
| 161 | + .unwrap(); |
| 162 | + |
| 163 | + let dimensions = vec![ |
| 164 | + Dimension::new(0, 1), |
| 165 | + Dimension::new(1, 2), |
| 166 | + Dimension::new(2, 3), |
| 167 | + Dimension::new(3, 4), |
| 168 | + ]; |
| 169 | + |
| 170 | + let shape = PartialShape::new(4, &dimensions).unwrap(); |
| 171 | + |
| 172 | + let dims = shape.get_dimensions(); |
| 173 | + |
| 174 | + assert_eq!(dims, &dimensions); |
| 175 | + } |
| 176 | +} |
0 commit comments