Skip to content

Commit

Permalink
polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarriba committed Oct 15, 2024
1 parent 7e9b683 commit a7726d8
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 86 deletions.
80 changes: 0 additions & 80 deletions crates/kornia-core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,46 +82,6 @@ where
self.buffer.into_vec()
}

/// Creates a new tensor storage from an existing raw pointer with the given allocator.
///
/// # Arguments
///
/// * `ptr` - The existing raw pointer to the tensor data.
/// * `len` - The number of elements in the tensor storage.
/// * `alloc` - A reference to the allocator used to allocate the tensor storage.
///
/// # Safety
///
/// The pointer must be properly aligned and have the correct length.
//pub unsafe fn from_ptr(ptr: *mut T, len: usize, alloc: &A) -> Self {
// // create the buffer
// //let buffer = Buffer::from_custom_allocation(
// // NonNull::new_unchecked(ptr as *mut u8),
// // len * std::mem::size_of::<T>(),
// // Arc::new(()),
// //);

// //// create tensor storage
// //Self {
// // data: buffer.into(),
// // alloc: Arc::new(alloc.clone()),
// // marker: PhantomData,
// //}
// let ptr = unsafe { NonNull::new_unchecked(ptr as *mut u8) };
// let layout = Layout::array::<T>(len).unwrap();
// let alloc = Arc::new(alloc);
// let buffer = TensorBuffer {
// alloc: alloc.clone(),
// layout,
// ptr,
// };
// Self {
// buffer,
// alloc: alloc.clone(), // NOTE: redundant
// marker: PhantomData,
// }
//}

/// Returns the capacity of the tensor storage.
#[inline]
pub fn capacity(&self) -> usize {
Expand Down Expand Up @@ -294,28 +254,6 @@ mod tests {
Ok(())
}

//#[test]
//fn test_tensor_storage_from_cpu_ptr() -> Result<(), TensorAllocatorError> {
// let len = 1024;
// let layout = Layout::array::<u8>(len).unwrap();
// let allocator = CpuAllocator;

// // Allocate CPU memory
// let ptr = allocator.alloc(layout)?;

// // Wrap the existing CPU pointer in a `TensorStorage`
// let storage = unsafe { TensorStorage::from_ptr(ptr, len, &allocator) };

// // Use the `TensorStorage` as needed
// assert_eq!(storage.len(), len);
// assert!(!storage.is_empty());

// // Deallocate CPU memory
// allocator.dealloc(ptr, layout);

// Ok(())
//}

#[test]
fn test_tensor_storage_into_vec() {
let original_vec = vec![1, 2, 3, 4, 5];
Expand Down Expand Up @@ -396,24 +334,6 @@ mod tests {
}
assert_eq!(*allocator.bytes_allocated.borrow(), 0);

// TensorStorage::from_ptr()
// TensorStorage::from_ptr() does not take ownership of buffer. So the memory should not be
// deallocated when the TensorStorage goes out of scope.
// In this case, the memory will be deallocated when the vector goes out of scope.
//{
// let mut original_vec = Vec::<u8>::with_capacity(len);
// let original_ptr = original_vec.as_ptr();
// {
// let storage = unsafe {
// TensorStorage::<u8, _>::from_ptr(original_vec.as_mut_ptr(), len, &allocator)
// };
// assert_eq!(*allocator.bytes_allocated.borrow(), 0);

// assert_eq!(storage.as_ptr(), original_ptr);
// }
// assert_eq!(*allocator.bytes_allocated.borrow(), 0);
//}

Ok(())
}

Expand Down
7 changes: 1 addition & 6 deletions crates/kornia-core/src/view.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use crate::{
//get_strides_from_shape, storage::TensorStorage, SafeTensorType, Tensor, TensorAllocator,
get_strides_from_shape,
storage::TensorStorage,
CpuAllocator,
Tensor,
TensorAllocator,
get_strides_from_shape, storage::TensorStorage, CpuAllocator, Tensor, TensorAllocator,
};

/// A view into a tensor.
Expand Down

0 comments on commit a7726d8

Please sign in to comment.