#[repr(C)]pub struct MLMultiArray { /* private fields */ }
MLMultiArray
only.Expand description
Use MLMultiArray
to store a multi-dimensional value.
Unlike MLShapedArray
or MLTensor
, MLMultiArray
can be used in Obj-C code. Unlike MLTensor
, MLMultiArray
is
always backed by a concrete storage.
The object has properties to define the interpretation of the storage.
.dataType
defines the interpretation of raw bytes into a numeric scalar value. For example,
MLMultiArrayDataTypeFloat32
means the backing storage uses IEEE 754 Float32 encoding.
.shape
defines the multi-dimensional space. For example, 30 x 20 image with three color components (Red, Green,
Blue) could be defined using the shape [3, 20, 30]
.
.strides
defines the offset addressing of the scalar for a given coordinates. For example, the image above might
use [640, 32, 1]
as the strides
. Then, the scalar at (1, 10, 15) is stored at 640 * 1 + 32 * 10 + 1 * 15
, or
975th scalar in the storage. In general, the scalar offset for coordinates index
and strides strides
is:
scalarOffset = sum_d index[d]*strides[d]
The backing storage can be a heap allocated buffer or CVPixelBuffer. Though CVPixelBuffer backing supports limited
data types, MLModel
could share the storage with backend hardware such as Apple Neural Engine without copy.
See also Apple’s documentation
Implementations§
Source§impl MLMultiArray
impl MLMultiArray
Sourcepub unsafe fn dataPointer(&self) -> NonNull<c_void>
👎Deprecated: Use getBytesWithHandler or getMutableBytesWithHandler instead. For Swift, use withUnsafeBytes or withUnsafeMutableBytes.
pub unsafe fn dataPointer(&self) -> NonNull<c_void>
Unsafe pointer to underlying buffer holding the data
Sourcepub unsafe fn dataType(&self) -> MLMultiArrayDataType
pub unsafe fn dataType(&self) -> MLMultiArrayDataType
Scalar’s data type.
Sourcepub unsafe fn shape(&self) -> Retained<NSArray<NSNumber>>
pub unsafe fn shape(&self) -> Retained<NSArray<NSNumber>>
Shape of the multi-dimensional space that this instance represents.
Sourcepub unsafe fn strides(&self) -> Retained<NSArray<NSNumber>>
pub unsafe fn strides(&self) -> Retained<NSArray<NSNumber>>
Strides.
It defines the offset of the scalar of a given coordinate index in the storage, which is:
scalarOffset = sum_d index[d]*strides[d]
Sourcepub unsafe fn count(&self) -> NSInteger
pub unsafe fn count(&self) -> NSInteger
Count of total number of addressable scalars.
The value is same as product_d shape[d]
.
Sourcepub unsafe fn pixelBuffer(&self) -> Option<Retained<CVPixelBuffer>>
Available on crate feature objc2-core-video
only.
pub unsafe fn pixelBuffer(&self) -> Option<Retained<CVPixelBuffer>>
objc2-core-video
only.Returns the backing pixel buffer if exists, otherwise nil.
Source§impl MLMultiArray
Methods declared on superclass NSObject
.
impl MLMultiArray
Methods declared on superclass NSObject
.
Source§impl MLMultiArray
Creation.
impl MLMultiArray
Creation.
Sourcepub unsafe fn initWithShape_dataType_error(
this: Allocated<Self>,
shape: &NSArray<NSNumber>,
data_type: MLMultiArrayDataType,
) -> Result<Retained<Self>, Retained<NSError>>
pub unsafe fn initWithShape_dataType_error( this: Allocated<Self>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, ) -> Result<Retained<Self>, Retained<NSError>>
Creates the object.
The contents of the object are left uninitialized; the client must initialize it.
The scalars will use the first-major contiguous layout.
- Parameters:
- shape: The shape
- dataType: The data type
- error: Filled with error information on error.
Sourcepub unsafe fn initWithShape_dataType_strides(
this: Allocated<Self>,
shape: &NSArray<NSNumber>,
data_type: MLMultiArrayDataType,
strides: &NSArray<NSNumber>,
) -> Retained<Self>
pub unsafe fn initWithShape_dataType_strides( this: Allocated<Self>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, strides: &NSArray<NSNumber>, ) -> Retained<Self>
Creates the object with specified strides.
The contents of the object are left uninitialized; the client must initialize it.
let shape = [2, 3];
let strides = [4, 1]
let multiArray = MLMultiArray(shape: shape, dataType: .float32, strides: strides)
XCTAssertEqual(multiArray.shape, shape as [NSNumber])
XCTAssertEqual(multiArray.strides, strides as [NSNumber])
NSArray
<NSNumber
*> *shape =
@
[
@
2,
@
3];
NSArray
<NSNumber
*> *strides =
@
[
@
4,
@
1];
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeFloat32
strides:strides];
XCTAssertEqualObjects(multiArray.shape, shape);
XCTAssertEqualObjects(multiArray.strides, strides);
- Parameters:
- shape: The shape
- dataType: The data type
- strides: The strides.
Sourcepub unsafe fn initWithDataPointer_shape_dataType_strides_deallocator_error(
this: Allocated<Self>,
data_pointer: NonNull<c_void>,
shape: &NSArray<NSNumber>,
data_type: MLMultiArrayDataType,
strides: &NSArray<NSNumber>,
deallocator: Option<&DynBlock<dyn Fn(NonNull<c_void>)>>,
) -> Result<Retained<Self>, Retained<NSError>>
Available on crate feature block2
only.
pub unsafe fn initWithDataPointer_shape_dataType_strides_deallocator_error( this: Allocated<Self>, data_pointer: NonNull<c_void>, shape: &NSArray<NSNumber>, data_type: MLMultiArrayDataType, strides: &NSArray<NSNumber>, deallocator: Option<&DynBlock<dyn Fn(NonNull<c_void>)>>, ) -> Result<Retained<Self>, Retained<NSError>>
block2
only.Creates the object with existing data without copy.
Use this initializer to reference the existing buffer as the storage without copy.
int32_t *buffer = malloc(sizeof(int32_t) * 2 * 3 * 4);
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithDataPointer:buffer
shape:
@
[
@
2,
@
3,
@
4]
dataType:MLMultiArrayDataTypeInt32
strides:
@
[
@
12,
@
4,
@
1]
deallocator:^(void *bytes) { free(bytes); }
error:NULL];
- Parameters:
- dataPointer: The pointer to the buffer.
- shape: The shape
- dataType: The data type
- strides: The strides.
- deallocator: Block to be called on the deallocation of the instance.
- error: Filled with error information on error.
Sourcepub unsafe fn initWithPixelBuffer_shape(
this: Allocated<Self>,
pixel_buffer: &CVPixelBuffer,
shape: &NSArray<NSNumber>,
) -> Retained<Self>
Available on crate feature objc2-core-video
only.
pub unsafe fn initWithPixelBuffer_shape( this: Allocated<Self>, pixel_buffer: &CVPixelBuffer, shape: &NSArray<NSNumber>, ) -> Retained<Self>
objc2-core-video
only.Create by wrapping a pixel buffer.
Use this initializer to create an IOSurface backed MLMultiArray, which can reduce the inference latency by avoiding the buffer copy.
The instance will own the pixel buffer and release it on the deallocation.
The pixel buffer’s pixel format type must be OneComponent16Half. As such, MLMultiArray’s data type will be MLMultiArrayDataTypeFloat16.
CVPixelBufferRef pixelBuffer = NULL;
NSDictionary* pixelBufferAttributes =
@
{
(id)kCVPixelBufferIOSurfacePropertiesKey:
@
{}
};
// Since shape == [2, 3, 4], width is 4 (= shape[2]) and height is 6 (= shape[0] * shape[1]).
CVPixelBufferCreate(kCFAllocatorDefault, 4, 6, kCVPixelFormatType_OneComponent16Half, (__bridge CFDictionaryRef)pixelBufferAttributes,
&pixelBuffer
);
MLMultiArray *multiArray = [[MLMultiArray alloc] initWithPixelBuffer:pixelBuffer shape:
@
[
@
2,
@
3,
@
4]];
- Parameters:
- pixelBuffer: The pixel buffer to be owned by the instance.
- shape: The shape of the MLMultiArray. The last dimension of
shape
must match the pixel buffer’s width. The product of the rest of the dimensions must match the height.
Source§impl MLMultiArray
ScopedBufferAccess.
impl MLMultiArray
ScopedBufferAccess.
Sourcepub unsafe fn getBytesWithHandler(
&self,
handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger) + '_>,
)
Available on crate feature block2
only.
pub unsafe fn getBytesWithHandler( &self, handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger) + '_>, )
block2
only.Get the underlying buffer pointer to read.
The buffer pointer is valid only within the block.
MLMultiArray * A = [[MLMultiArray alloc] initWithShape:
@
[
@
3,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
A[
@
[
@
1,
@
2]] =
@
42;
[A getBytesWithHandler:^(const void *bytes, NSInteger size) {
const int32_t *scalarBuffer = (const int32_t *)bytes;
const int strideY = A.strides[0].intValue;
// Print 42
NSLog(
"
Scalar at (1, 2): %d", scalarBuffer[1 * strideY + 2]);
}];
- Parameters:
- handler: The block to receive the buffer pointer and its size in bytes.
Sourcepub unsafe fn getMutableBytesWithHandler(
&self,
handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger, NonNull<NSArray<NSNumber>>) + '_>,
)
Available on crate feature block2
only.
pub unsafe fn getMutableBytesWithHandler( &self, handler: &DynBlock<dyn Fn(NonNull<c_void>, NSInteger, NonNull<NSArray<NSNumber>>) + '_>, )
block2
only.Get the underlying buffer pointer to mutate.
The buffer pointer is valid only within the block.
Use strides
parameter passed in the block because the method may switch to a new backing buffer with different strides.
MLMultiArray * A = [[MLMultiArray alloc] initWithShape:
@
[
@
3,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
[A getMutableBytesWithHandler:^(void *bytes, NSInteger __unused size, NSArray
<NSNumber
*> *strides) {
int32_t *scalarBuffer = (int32_t *)bytes;
const int strideY = strides[0].intValue;
scalarBuffer[1 * strideY + 2] = 42; // Set 42 at A[1, 2]
}];
- Parameters:
- handler: The block to receive the buffer pointer, size in bytes, and strides.
Source§impl MLMultiArray
Concatenating.
impl MLMultiArray
Concatenating.
Sourcepub unsafe fn multiArrayByConcatenatingMultiArrays_alongAxis_dataType(
multi_arrays: &NSArray<MLMultiArray>,
axis: NSInteger,
data_type: MLMultiArrayDataType,
) -> Retained<Self>
pub unsafe fn multiArrayByConcatenatingMultiArrays_alongAxis_dataType( multi_arrays: &NSArray<MLMultiArray>, axis: NSInteger, data_type: MLMultiArrayDataType, ) -> Retained<Self>
Concatenate MLMultiArrays to form a new MLMultiArray.
All the source MLMultiArrays must have a same shape except the specified axis. The resultant MLMultiArray has the same shape as inputs except this axis, which dimension will be the sum of all the input dimensions of the axis.
For example,
// Swift
let A = try MLMultiArray(shape: [2, 3], dataType: .int32)
let B = try MLMultiArray(shape: [2, 2], dataType: .int32)
let C = MLMultiArray(concatenating: [A, B], axis: 1, dataType: .int32)
assert(C.shape == [2, 5])
// Obj-C
MLMultiArray *A = [[MLMultiArray alloc] initWithShape:
@
[
@
2,
@
3] dataType:MLMultiArrayDataTypeInt32 error:NULL];
MLMultiArray *B = [[MLMultiArray alloc] initWithShape:
@
[
@
2,
@
2] dataType:MLMultiArrayDataTypeInt32 error:NULL];
MLMultiArray *C = [MLMultiArray multiArrayByConcatenatingMultiArrays:
@
[A, B] alongAxis:1 dataType:MLMultiArrayDataTypeInt32];
assert(C.shape ==
@
[
@
2,
@
5])
Numeric data will be up or down casted as needed.
The method raises NSInvalidArgumentException if the shapes of input multi arrays are not compatible for concatenation.
- Parameters:
- multiArrays: Array of MLMultiArray instances to be concatenated.
- axis: Axis index with which the concatenation will performed. The value is wrapped by the dimension of the axis. For example, -1 is the last axis.
- dataType: The data type of the resultant MLMultiArray.
Source§impl MLMultiArray
NSNumberDataAccess.
impl MLMultiArray
NSNumberDataAccess.
Sourcepub unsafe fn objectAtIndexedSubscript(
&self,
idx: NSInteger,
) -> Retained<NSNumber>
pub unsafe fn objectAtIndexedSubscript( &self, idx: NSInteger, ) -> Retained<NSNumber>
Get a value by its linear index (assumes C-style index ordering)
Sourcepub unsafe fn objectForKeyedSubscript(
&self,
key: &NSArray<NSNumber>,
) -> Retained<NSNumber>
pub unsafe fn objectForKeyedSubscript( &self, key: &NSArray<NSNumber>, ) -> Retained<NSNumber>
Get a value by its multidimensional index (NSArray <NSNumber *>)
Sourcepub unsafe fn setObject_atIndexedSubscript(
&self,
obj: &NSNumber,
idx: NSInteger,
)
pub unsafe fn setObject_atIndexedSubscript( &self, obj: &NSNumber, idx: NSInteger, )
Set a value by its linear index (assumes C-style index ordering)
Sourcepub unsafe fn setObject_forKeyedSubscript(
&self,
obj: &NSNumber,
key: &NSArray<NSNumber>,
)
pub unsafe fn setObject_forKeyedSubscript( &self, obj: &NSNumber, key: &NSArray<NSNumber>, )
Set a value by subindicies (NSArray <NSNumber *>)
Source§impl MLMultiArray
Transferring.
impl MLMultiArray
Transferring.
Sourcepub unsafe fn transferToMultiArray(
&self,
destination_multi_array: &MLMultiArray,
)
pub unsafe fn transferToMultiArray( &self, destination_multi_array: &MLMultiArray, )
Transfer the contents to the destination multi-array.
Numeric data will be up or down casted as needed. It can transfer to a multi-array with different layout (strides).
let sourceMultiArray: MLMultiArray = ... // shape is [2, 3] and data type is Float64
let newStrides = [4, 1]
let destinationMultiArray = MLMultiArray(shape: [2, 3],
dataType: .float32,
strides: newStrides)
sourceMultiArray.transfer(to: destinationMultiArray)
NSArray
<NSNumber
*> *shape =
@
[
@
2,
@
3];
NSArray
<NSNumber
*> *sourceStrides =
@
[
@
3,
@
1];
NSArray
<NSNumber
*> *destinationStrides =
@
[
@
4,
@
1];
MLMultiArray *source = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeDouble
strides:sourceStrides];
// Initialize source...
MLMultiArray *destination = [[MLMultiArray alloc] initWithShape:shape
dataType:MLMultiArrayDataTypeFloat32
strides:destinationStrides];
[source transferToMultiArray:destination];
- Parameters:
- destinationMultiArray: The transfer destination.
Methods from Deref<Target = NSObject>§
Sourcepub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !
Handle messages the object doesn’t recognize.
See Apple’s documentation for details.
Methods from Deref<Target = AnyObject>§
Sourcepub fn class(&self) -> &'static AnyClass
pub fn class(&self) -> &'static AnyClass
Dynamically find the class of this object.
§Panics
May panic if the object is invalid (which may be the case for objects
returned from unavailable init
/new
methods).
§Example
Check that an instance of NSObject
has the precise class NSObject
.
use objc2::ClassType;
use objc2::runtime::NSObject;
let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Sourcepub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
👎Deprecated: this is difficult to use correctly, use Ivar::load
instead.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &Twhere
T: Encode,
Ivar::load
instead.Use Ivar::load
instead.
§Safety
The object must have an instance variable with the given name, and it
must be of type T
.
See Ivar::load_ptr
for details surrounding this.
Sourcepub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
pub fn downcast_ref<T>(&self) -> Option<&T>where
T: DowncastTarget,
Attempt to downcast the object to a class of type T
.
This is the reference-variant. Use Retained::downcast
if you want
to convert a retained object to another type.
§Mutable classes
Some classes have immutable and mutable variants, such as NSString
and NSMutableString
.
When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.
So using this method to convert a NSString
to a NSMutableString
,
while not unsound, is generally frowned upon unless you created the
string yourself, or the API explicitly documents the string to be
mutable.
See Apple’s documentation on mutability and on
isKindOfClass:
for more details.
§Generic classes
Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.
You can, however, safely downcast to generic collections where all the
type-parameters are AnyObject
.
§Panics
This works internally by calling isKindOfClass:
. That means that the
object must have the instance method of that name, and an exception
will be thrown (if CoreFoundation is linked) or the process will abort
if that is not the case. In the vast majority of cases, you don’t need
to worry about this, since both root objects NSObject
and
NSProxy
implement this method.
§Examples
Cast an NSString
back and forth from NSObject
.
use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};
let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();
Try (and fail) to cast an NSObject
to an NSString
.
use objc2_foundation::{NSObject, NSString};
let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());
Try to cast to an array of strings.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();
This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.
Downcast when processing each element instead.
use objc2_foundation::{NSArray, NSObject, NSString};
let arr = NSArray::from_retained_slice(&[NSObject::new()]);
for elem in arr {
if let Some(data) = elem.downcast_ref::<NSString>() {
// handle `data`
}
}
Trait Implementations§
Source§impl AsRef<AnyObject> for MLMultiArray
impl AsRef<AnyObject> for MLMultiArray
Source§impl AsRef<MLMultiArray> for MLMultiArray
impl AsRef<MLMultiArray> for MLMultiArray
Source§impl AsRef<NSObject> for MLMultiArray
impl AsRef<NSObject> for MLMultiArray
Source§impl Borrow<AnyObject> for MLMultiArray
impl Borrow<AnyObject> for MLMultiArray
Source§impl Borrow<NSObject> for MLMultiArray
impl Borrow<NSObject> for MLMultiArray
Source§impl ClassType for MLMultiArray
impl ClassType for MLMultiArray
Source§const NAME: &'static str = "MLMultiArray"
const NAME: &'static str = "MLMultiArray"
Source§type ThreadKind = <<MLMultiArray as ClassType>::Super as ClassType>::ThreadKind
type ThreadKind = <<MLMultiArray as ClassType>::Super as ClassType>::ThreadKind
Source§impl Debug for MLMultiArray
impl Debug for MLMultiArray
Source§impl Deref for MLMultiArray
impl Deref for MLMultiArray
Source§impl Hash for MLMultiArray
impl Hash for MLMultiArray
Source§impl Message for MLMultiArray
impl Message for MLMultiArray
Source§impl NSObjectProtocol for MLMultiArray
impl NSObjectProtocol for MLMultiArray
Source§fn isEqual(&self, other: Option<&AnyObject>) -> bool
fn isEqual(&self, other: Option<&AnyObject>) -> bool
Source§fn hash(&self) -> usize
fn hash(&self) -> usize
Source§fn isKindOfClass(&self, cls: &AnyClass) -> bool
fn isKindOfClass(&self, cls: &AnyClass) -> bool
Source§fn is_kind_of<T>(&self) -> bool
fn is_kind_of<T>(&self) -> bool
isKindOfClass
directly, or cast your objects with AnyObject::downcast_ref