Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,9 @@ jobs:
# intentionally not a production or default-CI dependency.
python bench/opencv_comparison/test_report.py
# Fail the build if the .pyi type stubs drift from the runtime API.
python -m mypy.stubtest spatialrust --ignore-missing-stub
python -m mypy.stubtest spatialrust --ignore-missing-stub \
--allowlist crates/spatialrust-py/stubtest_allowlist.txt \
--ignore-unused-allowlist

vision-platform-conformance:
name: Vision conformance (${{ matrix.os }})
Expand Down
28 changes: 7 additions & 21 deletions crates/spatialrust-ai/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,10 @@ impl ModelSession for MockSession {
self.info.validate_inputs(&inputs)?;
// Output is always a newly allocated host `TensorBuffer`.
if options.output_copy == CopyPolicy::Forbid {
return Err(AiError::CopyRequired {
direction: "output host",
name: "depth".into(),
});
return Err(AiError::CopyRequired { direction: "output host", name: "depth".into() });
}
match self.profile {
MockProfile::SyntheticDepth => {
run_synthetic_depth(inputs, options.input_copy)
}
MockProfile::SyntheticDepth => run_synthetic_depth(inputs, options.input_copy),
}
}
}
Expand Down Expand Up @@ -110,10 +105,7 @@ impl MockProfile {
}
}

fn run_synthetic_depth(
inputs: NamedTensors,
input_copy: CopyPolicy,
) -> AiResult<NamedTensors> {
fn run_synthetic_depth(inputs: NamedTensors, input_copy: CopyPolicy) -> AiResult<NamedTensors> {
let input = inputs.get("images").ok_or_else(|| AiError::MissingInput("images".into()))?;
let descriptor = input.descriptor();
let shape = descriptor.shape();
Expand All @@ -131,10 +123,7 @@ fn run_synthetic_depth(
}
if !descriptor.is_c_contiguous() || descriptor.byte_offset() != 0 {
if input_copy == CopyPolicy::Forbid {
return Err(AiError::CopyRequired {
direction: "input host",
name: "images".into(),
});
return Err(AiError::CopyRequired { direction: "input host", name: "images".into() });
}
return Err(AiError::Unsupported {
backend: "mock".into(),
Expand Down Expand Up @@ -192,8 +181,8 @@ fn f32_values(tensor: &TensorBuffer) -> AiResult<Vec<f32>> {
mod tests {
use super::{MockInferenceBackend, MockProfile};
use crate::{
AiError, CopyPolicy, InferenceBackend, ModelSession as _, ModelSource, NamedTensors,
RunOptions, SessionOptions,
AiError, CopyPolicy, InferenceBackend, ModelSource, NamedTensors, RunOptions,
SessionOptions,
};
use spatialrust_tensor::{DataType, Device, TensorBuffer, TensorDescriptor};

Expand Down Expand Up @@ -222,10 +211,7 @@ mod tests {
let outputs = session
.run_with_options(
inputs,
RunOptions {
input_copy: CopyPolicy::Forbid,
output_copy: CopyPolicy::Allow,
},
RunOptions { input_copy: CopyPolicy::Forbid, output_copy: CopyPolicy::Allow },
)
.unwrap();
let depth = outputs.get("depth").unwrap();
Expand Down
51 changes: 20 additions & 31 deletions crates/spatialrust-arrow/src/cdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ pub unsafe fn import_point_cloud_c_data(
)));
}
if schema.n_children < 0 || array.n_children < 0 || schema.n_children != array.n_children {
return Err(ArrowBridgeError::SchemaMismatch(
"schema/array child counts disagree".into(),
));
return Err(ArrowBridgeError::SchemaMismatch("schema/array child counts disagree".into()));
}
let n = schema.n_children as usize;
let mut point_schema = PointSchema::new();
Expand All @@ -178,8 +176,7 @@ fn export_schema(point_schema: &PointSchema) -> ArrowBridgeResult<ExportedArrowS
.iter()
.map(export_field_schema)
.collect::<ArrowBridgeResult<Vec<_>>>()?;
let mut child_ptrs =
children.into_iter().map(Box::into_raw).collect::<Vec<*mut ArrowSchema>>();
let mut child_ptrs = children.into_iter().map(Box::into_raw).collect::<Vec<*mut ArrowSchema>>();
let child_table = child_ptrs.as_mut_ptr();
let n_children = child_ptrs.len() as i64;
// Leak the vec table into private data; release rebuilds and frees it.
Expand Down Expand Up @@ -213,18 +210,12 @@ fn export_field_schema(field: &PointField) -> ArrowBridgeResult<Box<ArrowSchema>
"Arrow C Data export currently supports scalar fields only".into(),
));
}
let format = CString::new(dtype_format(field.dtype)?).map_err(|_| {
ArrowBridgeError::InvalidConfiguration("field format contained NUL".into())
})?;
let name = CString::new(field.name.as_str()).map_err(|_| {
ArrowBridgeError::InvalidConfiguration("field name contained NUL".into())
})?;
let private = Box::new(SchemaPrivate {
format,
name,
children_table: ptr::null_mut(),
n_children: 0,
});
let format = CString::new(dtype_format(field.dtype)?)
.map_err(|_| ArrowBridgeError::InvalidConfiguration("field format contained NUL".into()))?;
let name = CString::new(field.name.as_str())
.map_err(|_| ArrowBridgeError::InvalidConfiguration("field name contained NUL".into()))?;
let private =
Box::new(SchemaPrivate { format, name, children_table: ptr::null_mut(), n_children: 0 });
Ok(Box::new(ArrowSchema {
format: private.format.as_ptr(),
name: private.name.as_ptr(),
Expand Down Expand Up @@ -429,9 +420,9 @@ fn format_dtype(format: &str) -> ArrowBridgeResult<DType> {
"S" => Ok(DType::U16),
"I" => Ok(DType::U32),
"i" => Ok(DType::I32),
other => Err(ArrowBridgeError::SchemaMismatch(format!(
"unsupported Arrow format `{other}`"
))),
other => {
Err(ArrowBridgeError::SchemaMismatch(format!("unsupported Arrow format `{other}`")))
}
}
}

Expand Down Expand Up @@ -511,11 +502,8 @@ unsafe extern "C" fn release_array(array: *mut ArrowArray) {
if !array.private_data.is_null() {
let private = Box::from_raw(array.private_data as *mut ArrayPrivate);
if !private.children_table.is_null() && private.n_children > 0 {
let children = Vec::from_raw_parts(
private.children_table,
private.n_children,
private.n_children,
);
let children =
Vec::from_raw_parts(private.children_table, private.n_children, private.n_children);
for child in children {
if !child.is_null() {
if let Some(release) = (*child).release {
Expand All @@ -526,11 +514,8 @@ unsafe extern "C" fn release_array(array: *mut ArrowArray) {
}
}
if !private.buffers_table.is_null() && private.n_buffers > 0 {
let _ = Vec::from_raw_parts(
private.buffers_table as *mut *const c_void,
private.n_buffers,
private.n_buffers,
);
let _ =
Vec::from_raw_parts(private.buffers_table, private.n_buffers, private.n_buffers);
}
drop(private);
}
Expand Down Expand Up @@ -561,7 +546,11 @@ mod tests {
.unwrap();
let (mut schema, mut array) = export_point_cloud_c_data(&cloud).unwrap();
let imported = unsafe {
import_point_cloud_c_data(schema.as_mut_ptr(), array.as_mut_ptr(), SpatialMetadata::default())
import_point_cloud_c_data(
schema.as_mut_ptr(),
array.as_mut_ptr(),
SpatialMetadata::default(),
)
}
.unwrap();
assert_eq!(imported.len(), 2);
Expand Down
8 changes: 6 additions & 2 deletions crates/spatialrust-arrow/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::ptr;
use spatialrust_core::{PointCloud, SpatialMetadata};

use crate::{
cdata::{export_point_cloud_c_data, import_point_cloud_c_data, ArrowArray, ExportedArrowSchema},
cdata::{
export_point_cloud_c_data, import_point_cloud_c_data, ArrowArray, ExportedArrowSchema,
},
ArrowBridgeError, ArrowBridgeResult,
};

Expand Down Expand Up @@ -112,7 +114,9 @@ pub unsafe fn import_point_cloud_device_array(

#[cfg(test)]
mod tests {
use super::{export_point_cloud_device_array, import_point_cloud_device_array, ArrowDeviceType};
use super::{
export_point_cloud_device_array, import_point_cloud_device_array, ArrowDeviceType,
};
use spatialrust_core::{
PointBuffer, PointBufferSet, PointCloud, SpatialMetadata, StandardSchemas,
};
Expand Down
8 changes: 4 additions & 4 deletions crates/spatialrust-arrow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

#[cfg(feature = "arrow-c-data")]
mod cdata;
#[cfg(feature = "arrow-c-data")]
mod error;
#[cfg(feature = "arrow-c-device")]
mod device;
#[cfg(feature = "arrow-c-data")]
mod error;
#[cfg(feature = "arrow-c-stream")]
mod stream;

Expand All @@ -19,12 +19,12 @@ pub use cdata::{
export_point_cloud_c_data, import_point_cloud_c_data, ArrowArray, ArrowSchema,
ExportedArrowArray, ExportedArrowSchema,
};
#[cfg(feature = "arrow-c-data")]
pub use error::{ArrowBridgeError, ArrowBridgeResult};
#[cfg(feature = "arrow-c-device")]
pub use device::{
export_point_cloud_device_array, import_point_cloud_device_array, ArrowDeviceArray,
ArrowDeviceType, ExportedArrowDeviceArray,
};
#[cfg(feature = "arrow-c-data")]
pub use error::{ArrowBridgeError, ArrowBridgeResult};
#[cfg(feature = "arrow-c-stream")]
pub use stream::{export_record_source_c_stream, ArrowArrayStream, ExportedArrowArrayStream};
30 changes: 17 additions & 13 deletions crates/spatialrust-arrow/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ use std::{

use spatialrust_records::SpatialRecordSource;

use crate::{cdata::{export_point_cloud_c_data, ArrowArray, ArrowSchema}, ArrowBridgeResult};
use crate::{
cdata::{export_point_cloud_c_data, ArrowArray, ArrowSchema},
ArrowBridgeResult,
};

/// Arrow C Stream Interface object.
#[repr(C)]
pub struct ArrowArrayStream {
/// Fills `out` with the stream schema.
pub get_schema: Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream, out: *mut ArrowSchema) -> i32>,
pub get_schema:
Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream, out: *mut ArrowSchema) -> i32>,
/// Fills `out` with the next array (`release=null` when exhausted).
pub get_next: Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream, out: *mut ArrowArray) -> i32>,
pub get_next:
Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream, out: *mut ArrowArray) -> i32>,
/// Optional last-error message.
pub get_last_error: Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream) -> *const c_char>,
pub get_last_error:
Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream) -> *const c_char>,
/// Release callback.
pub release: Option<unsafe extern "C" fn(stream: *mut ArrowArrayStream)>,
/// Implementation private data.
Expand Down Expand Up @@ -141,11 +147,9 @@ unsafe extern "C" fn stream_get_last_error(stream: *mut ArrowArrayStream) -> *co
return ptr::null();
}
match private_mut(stream) {
Ok(private) => private
.last_error
.as_ref()
.map(|value| value.as_ptr())
.unwrap_or(ptr::null()),
Ok(private) => {
private.last_error.as_ref().map(|value| value.as_ptr()).unwrap_or(ptr::null())
}
Err(_) => ptr::null(),
}
}
Expand Down Expand Up @@ -176,7 +180,9 @@ unsafe fn private_mut(stream: *mut ArrowArrayStream) -> Result<&'static mut Stre
Ok(&mut *(stream.private_data as *mut StreamPrivate))
}

fn empty_cloud(schema: &spatialrust_core::PointSchema) -> ArrowBridgeResult<spatialrust_core::PointCloud> {
fn empty_cloud(
schema: &spatialrust_core::PointSchema,
) -> ArrowBridgeResult<spatialrust_core::PointCloud> {
use spatialrust_core::{PointBuffer, PointBufferSet, PointCloud, SpatialMetadata};
let mut buffers = PointBufferSet::new();
for field in schema.fields() {
Expand Down Expand Up @@ -214,9 +220,7 @@ mod tests {
use spatialrust_core::{
PointBuffer, PointBufferSet, PointCloud, SpatialMetadata, StandardSchemas,
};
use spatialrust_records::{
MemoryChunkSource, SchemaDescriptor, SchemaVersion,
};
use spatialrust_records::{MemoryChunkSource, SchemaDescriptor, SchemaVersion};

#[test]
fn stream_yields_chunked_clouds() {
Expand Down
Loading
Loading