Skip to main content

MatrixDescriptor

Struct MatrixDescriptor 

Source
pub struct MatrixDescriptor {
    pub rows: usize,
    pub columns: usize,
    pub matrices: usize,
    pub row_bytes: usize,
    pub matrix_bytes: usize,
    pub data_type: u32,
}
Expand description

Plain-Rust configuration for MPSMatrixDescriptor.

Fields§

§rows: usize§columns: usize§matrices: usize§row_bytes: usize§matrix_bytes: usize§data_type: u32

Implementations§

Source§

impl MatrixDescriptor

Source

pub const fn with_strides( rows: usize, columns: usize, matrices: usize, row_bytes: usize, matrix_bytes: usize, data_type: u32, ) -> Self

Construct a matrix descriptor with explicit row and matrix strides.

Source

pub fn contiguous(rows: usize, columns: usize, data_type: u32) -> Option<Self>

Construct a single contiguous matrix descriptor for a supported data type.

Examples found in repository?
examples/02_matrix_multiply.rs (line 26)
20fn main() {
21    let device = MetalDevice::system_default().expect("no Metal device available");
22    let queue = device
23        .new_command_queue()
24        .expect("failed to create command queue");
25
26    let left_desc = MatrixDescriptor::contiguous(2, 3, data_type::FLOAT32)
27        .expect("FLOAT32 matrix descriptor should be supported");
28    let right_desc = MatrixDescriptor::contiguous(3, 2, data_type::FLOAT32)
29        .expect("FLOAT32 matrix descriptor should be supported");
30    let result_desc = MatrixDescriptor::contiguous(2, 2, data_type::FLOAT32)
31        .expect("FLOAT32 matrix descriptor should be supported");
32
33    let left_buffer = device
34        .new_buffer(
35            left_desc.matrix_bytes,
36            resource_options::STORAGE_MODE_SHARED,
37        )
38        .expect("failed to allocate left buffer");
39    let right_buffer = device
40        .new_buffer(
41            right_desc.matrix_bytes,
42            resource_options::STORAGE_MODE_SHARED,
43        )
44        .expect("failed to allocate right buffer");
45    let result_buffer = device
46        .new_buffer(
47            result_desc.matrix_bytes,
48            resource_options::STORAGE_MODE_SHARED,
49        )
50        .expect("failed to allocate result buffer");
51
52    let left_values = [1.0_f32, 2.0, 3.0, 4.0, 5.0, 6.0];
53    let right_values = [7.0_f32, 8.0, 9.0, 10.0, 11.0, 12.0];
54    let zeros = [0.0_f32; 4];
55
56    let _ = left_buffer.write_bytes(as_bytes(&left_values));
57    let _ = right_buffer.write_bytes(as_bytes(&right_values));
58    let _ = result_buffer.write_bytes(as_bytes(&zeros));
59
60    let left =
61        Matrix::new_with_buffer(&left_buffer, left_desc).expect("failed to wrap left matrix");
62    let right =
63        Matrix::new_with_buffer(&right_buffer, right_desc).expect("failed to wrap right matrix");
64    let result =
65        Matrix::new_with_buffer(&result_buffer, result_desc).expect("failed to wrap result matrix");
66
67    let gemm = MatrixMultiplication::new_simple(&device, 2, 2, 3)
68        .expect("failed to create matrix multiplication kernel");
69    let command_buffer = queue
70        .new_command_buffer()
71        .expect("failed to allocate command buffer");
72    gemm.encode(&command_buffer, &left, &right, &result);
73    command_buffer.commit();
74    command_buffer.wait_until_completed();
75
76    let output = read_f32s(&result_buffer, 4);
77    let expected = [58.0_f32, 64.0, 139.0, 154.0];
78    for (actual, expected_value) in output.iter().zip(expected) {
79        assert!(
80            (actual - expected_value).abs() < 1.0e-4,
81            "unexpected matrix multiply result: {output:?}"
82        );
83    }
84
85    println!("matmul smoke passed: {output:?}");
86}
Source

pub fn recommended_row_bytes(columns: usize, data_type: u32) -> usize

Query MPS’s recommended row stride for a matrix width.

Trait Implementations§

Source§

impl Clone for MatrixDescriptor

Source§

fn clone(&self) -> MatrixDescriptor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MatrixDescriptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for MatrixDescriptor

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.