Skip to content
Merged
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
75 changes: 68 additions & 7 deletions qdp/qdp-core/tests/memory_safety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

// Memory safety tests: DLPack lifecycle, RAII, Arc reference counting

use qdp_core::QdpEngine;
use qdp_core::{Precision, QdpEngine};

mod common;

Expand All @@ -37,12 +37,16 @@ fn test_memory_pressure() {
let data = common::create_test_data(1024);

for i in 0..100 {
let ptr = engine.encode(&data, 10, "amplitude")
let ptr = engine
.encode(&data, 10, "amplitude")
.expect("Encoding should succeed");

unsafe {
let managed = &mut *ptr;
let deleter = managed.deleter.take().expect("Deleter missing in pressure test!");
let deleter = managed
.deleter
.take()
.expect("Deleter missing in pressure test!");
deleter(ptr);
}

Expand Down Expand Up @@ -87,7 +91,7 @@ fn test_multiple_concurrent_states() {

#[test]
#[cfg(target_os = "linux")]
fn test_dlpack_tensor_metadata() {
fn test_dlpack_tensor_metadata_default() {
println!("Testing DLPack tensor metadata...");

let engine = match QdpEngine::new(0) {
Expand All @@ -114,15 +118,72 @@ fn test_dlpack_tensor_metadata() {
assert_eq!(stride, 1, "Stride for 1D contiguous array should be 1");

assert_eq!(tensor.dtype.code, 5, "Should be complex type (code=5)");
assert_eq!(tensor.dtype.bits, 128, "Should be 128 bits (2x64-bit floats)");
assert_eq!(tensor.dtype.bits, 64, "Should be 64 bits (2x32-bit floats)");

println!("PASS: DLPack metadata verified");
println!(" ndim: {}", tensor.ndim);
println!(" shape: {}", shape);
println!(" stride: {}", stride);
println!(" dtype: code={}, bits={}", tensor.dtype.code, tensor.dtype.bits);
println!(
" dtype: code={}, bits={}",
tensor.dtype.code, tensor.dtype.bits
);

let deleter = managed
.deleter
.take()
.expect("Deleter missing in metadata test!");
deleter(ptr);
}
}

#[test]
#[cfg(target_os = "linux")]
fn test_dlpack_tensor_metadata_f64() {
println!("Testing DLPack tensor metadata...");

let engine = match QdpEngine::new_with_precision(0, Precision::Float64) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I changed

Ok(e) => e,
Err(_) => return,
};

let data = common::create_test_data(1024);
let ptr = engine.encode(&data, 10, "amplitude").unwrap();

unsafe {
let managed = &mut *ptr;
let tensor = &managed.dl_tensor;

let deleter = managed.deleter.take().expect("Deleter missing in metadata test!");
assert_eq!(tensor.ndim, 1, "Should be 1D tensor");
assert!(!tensor.data.is_null(), "Data pointer should be valid");
assert!(!tensor.shape.is_null(), "Shape pointer should be valid");
assert!(!tensor.strides.is_null(), "Strides pointer should be valid");

let shape = *tensor.shape;
assert_eq!(shape, 1024, "Shape should be 1024 (2^10)");

let stride = *tensor.strides;
assert_eq!(stride, 1, "Stride for 1D contiguous array should be 1");

assert_eq!(tensor.dtype.code, 5, "Should be complex type (code=5)");
assert_eq!(
tensor.dtype.bits, 128,
"Should be 128 bits (2x64-bit floats)"
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is the failed test

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking as we change the precision to float32 by default, should we set the expected value to 2x32 rather than change the precision back to float64?

Copy link

@400Ping 400Ping Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see this, working on float32 right now.


println!("PASS: DLPack metadata verified");
println!(" ndim: {}", tensor.ndim);
println!(" shape: {}", shape);
println!(" stride: {}", stride);
println!(
" dtype: code={}, bits={}",
tensor.dtype.code, tensor.dtype.bits
);

let deleter = managed
.deleter
.take()
.expect("Deleter missing in metadata test!");
deleter(ptr);
}
}
Loading