Skip to content

Commit

Permalink
support running tests with --no-default-features
Browse files Browse the repository at this point in the history
  • Loading branch information
decahedron1 committed Feb 1, 2025
1 parent 13be8d8 commit 0ea636a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ mod tests {

#[test]
fn test_lora() -> crate::Result<()> {
let model = Session::builder()?.commit_from_file("tests/data/lora_model.onnx")?;
let lora = Adapter::from_file("tests/data/adapter.orl", None)?;
let model = std::fs::read("tests/data/lora_model.onnx").expect("");
let session = Session::builder()?.commit_from_memory(&model)?;
let lora = std::fs::read("tests/data/adapter.orl").expect("");
let lora = Adapter::from_memory(&lora, None)?;

let mut run_options = RunOptions::new()?;
run_options.add_adapter(&lora)?;

let output: Tensor<f32> = model
let output: Tensor<f32> = session
.run_with_options(crate::inputs![Tensor::<f32>::from_array(([4, 4], vec![1.0; 16]))?], &run_options)?
.remove("output")
.expect("")
Expand All @@ -187,7 +189,8 @@ mod tests {

#[test]
fn test_lora_from_memory() -> crate::Result<()> {
let model = Session::builder()?.commit_from_file("tests/data/lora_model.onnx")?;
let model = std::fs::read("tests/data/lora_model.onnx").expect("");
let session = Session::builder()?.commit_from_memory(&model)?;

let lora_bytes = std::fs::read("tests/data/adapter.orl").expect("");
let lora = Adapter::from_memory(&lora_bytes, None)?;
Expand All @@ -196,7 +199,7 @@ mod tests {
let mut run_options = RunOptions::new()?;
run_options.add_adapter(&lora)?;

let output: Tensor<f32> = model
let output: Tensor<f32> = session
.run_with_options(crate::inputs![Tensor::<f32>::from_array(([4, 4], vec![1.0; 16]))?], &run_options)?
.remove("output")
.expect("")
Expand Down
10 changes: 9 additions & 1 deletion src/io_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,16 +269,19 @@ mod tests {
use core::cmp::Ordering;

use image::{ImageBuffer, Luma, Pixel};
#[cfg(feature = "ndarray")]
use ndarray::{Array2, Array4, Axis};

#[cfg(feature = "ndarray")]
use crate::tensor::ArrayExtensions;
use crate::{
Result,
memory::{AllocationDevice, AllocatorType, MemoryInfo, MemoryType},
session::Session,
tensor::ArrayExtensions,
value::{Tensor, TensorValueTypeMarker, Value}
};

#[cfg(feature = "ndarray")]
fn get_image() -> Array4<f32> {
let image_buffer: ImageBuffer<Luma<u8>, Vec<u8>> = image::open("tests/data/mnist_5.jpg").expect("failed to load image").to_luma8();
ndarray::Array::from_shape_fn((1, 1, 28, 28), |(_, c, j, i)| {
Expand All @@ -288,6 +291,7 @@ mod tests {
})
}

#[cfg(feature = "ndarray")]
fn extract_probabilities<T: TensorValueTypeMarker>(output: &Value<T>) -> Result<Vec<(usize, f32)>> {
let mut probabilities: Vec<(usize, f32)> = output
.try_extract_tensor()?
Expand All @@ -302,6 +306,7 @@ mod tests {

// not terribly useful since CI is CPU-only, but it at least ensures the API won't segfault or something silly
#[test]
#[cfg(all(feature = "ndarray", feature = "fetch-models"))]
fn test_mnist_input_bound() -> Result<()> {
let session = Session::builder()?.commit_from_url("https://parcel.pyke.io/v2/cdn/assetdelivery/ortrsv2/ex_models/mnist.onnx")?;

Expand All @@ -319,6 +324,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "ndarray", feature = "fetch-models"))]
fn test_mnist_input_output_bound() -> Result<()> {
let session = Session::builder()?.commit_from_url("https://parcel.pyke.io/v2/cdn/assetdelivery/ortrsv2/ex_models/mnist.onnx")?;

Expand All @@ -338,6 +344,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "ndarray", feature = "fetch-models"))]
fn test_send_iobinding() -> Result<()> {
let session = Session::builder()?.commit_from_url("https://parcel.pyke.io/v2/cdn/assetdelivery/ortrsv2/ex_models/mnist.onnx")?;

Expand All @@ -362,6 +369,7 @@ mod tests {
}

#[test]
#[cfg(all(feature = "ndarray", feature = "fetch-models"))]
fn test_mnist_clear_bounds() -> Result<()> {
let session = Session::builder()?.commit_from_url("https://parcel.pyke.io/v2/cdn/assetdelivery/ortrsv2/ex_models/mnist.onnx")?;

Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ pub mod __private {
pub extern crate core;
}

#[cfg(all(test, not(feature = "fetch-models")))]
compile_error!("`cargo test --features fetch-models`!!1!");

pub mod adapter;
pub mod environment;
pub mod error;
Expand Down
3 changes: 2 additions & 1 deletion src/operator/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ impl Operator for CustomOpTwo {

#[test]
fn test_custom_ops() -> crate::Result<()> {
let model = std::fs::read("tests/data/custom_op_test.onnx").expect("");
let session = Session::builder()?
.with_operators(OperatorDomain::new("test.customop")?.add(CustomOpOne)?.add(CustomOpTwo)?)?
.commit_from_file("tests/data/custom_op_test.onnx")?;
.commit_from_memory(&model)?;

let allocator = session.allocator();
let mut value1 = Tensor::<f32>::new(allocator, [3, 5])?;
Expand Down
2 changes: 2 additions & 0 deletions src/session/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ mod tests {
use crate::value::{DynTensor, Tensor};

#[test]
#[cfg(feature = "std")]
fn test_hashmap_static_keys() -> crate::Result<()> {
let v: Vec<f32> = vec![1., 2., 3., 4., 5.];
let shape = vec![v.len() as i64];
Expand All @@ -138,6 +139,7 @@ mod tests {
}

#[test]
#[cfg(feature = "std")]
fn test_hashmap_string_keys() -> crate::Result<()> {
let v: Vec<f32> = vec![1., 2., 3., 4., 5.];
let shape = vec![v.len() as i64];
Expand Down
2 changes: 0 additions & 2 deletions src/value/impl_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ impl<K: IntoTensorElementType + Debug + Clone + Hash + Eq + 'static, V: IntoTens
}

impl<K: IntoTensorElementType + Debug + Clone + Hash + Eq, V: PrimitiveTensorElementType + Debug + Clone> Value<MapValueType<K, V>> {
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn extract_raw_map(&self) -> Vec<(K, V)> {
self.try_extract_raw_map().expect("Failed to extract map")
}
Expand Down
1 change: 1 addition & 0 deletions src/value/impl_tensor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub(crate) fn calculate_tensor_size(shape: &[i64]) -> usize {
mod tests {
use alloc::sync::Arc;

#[cfg(feature = "ndarray")]
use ndarray::{ArcArray1, Array1, CowArray};

use super::Tensor;
Expand Down
2 changes: 1 addition & 1 deletion src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ mod tests {
let value = Sequence::new([Map::<String, f32>::new(map_contents)?])?;

for map in value.extract_sequence(&Allocator::default()) {
let map = map.extract_map();
let map = map.extract_raw_map().into_iter().collect::<std::collections::HashMap<_, _>>();
assert_eq!(map["meaning"], 42.0);
assert_eq!(map["pi"], core::f32::consts::PI);
}
Expand Down

0 comments on commit 0ea636a

Please sign in to comment.