mlx_rs/linalg.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
//! Linear algebra operations.
use crate::error::{Exception, Result};
use crate::utils::guard::Guarded;
use crate::utils::{IntoOption, VectorArray};
use crate::{Array, Stream, StreamOrDevice};
use mlx_internal_macros::default_device;
use smallvec::SmallVec;
use std::f64;
use std::ffi::CString;
/// Order of the norm
///
/// See [`norm`] for more details.
#[derive(Debug, Clone, Copy)]
pub enum Ord<'a> {
/// String representation of the order
Str(&'a str),
/// Order of the norm
P(f64),
}
impl Default for Ord<'_> {
fn default() -> Self {
Ord::Str("fro")
}
}
impl std::fmt::Display for Ord<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Ord::Str(s) => write!(f, "{}", s),
Ord::P(p) => write!(f, "{}", p),
}
}
}
impl<'a> From<&'a str> for Ord<'a> {
fn from(value: &'a str) -> Self {
Ord::Str(value)
}
}
impl From<f64> for Ord<'_> {
fn from(value: f64) -> Self {
Ord::P(value)
}
}
impl<'a> IntoOption<Ord<'a>> for &'a str {
fn into_option(self) -> Option<Ord<'a>> {
Some(Ord::Str(self))
}
}
impl<'a> IntoOption<Ord<'a>> for f64 {
fn into_option(self) -> Option<Ord<'a>> {
Some(Ord::P(self))
}
}
/// Compute p-norm of an [`Array`]
#[default_device]
pub fn norm_p_device<'a>(
array: impl AsRef<Array>,
ord: f64,
axes: impl IntoOption<&'a [i32]>,
keep_dims: impl Into<Option<bool>>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let keep_dims = keep_dims.into().unwrap_or(false);
match axes.into_option() {
Some(axes) => Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm_p(
res,
array.as_ref().as_ptr(),
ord,
axes.as_ptr(),
axes.len(),
keep_dims,
stream.as_ref().as_ptr(),
)
}),
None => Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm_p(
res,
array.as_ref().as_ptr(),
ord,
std::ptr::null(),
0,
keep_dims,
stream.as_ref().as_ptr(),
)
}),
}
}
/// Matrix or vector norm.
#[default_device]
pub fn norm_ord_device<'a>(
array: impl AsRef<Array>,
ord: &'a str,
axes: impl IntoOption<&'a [i32]>,
keep_dims: impl Into<Option<bool>>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let ord = CString::new(ord).map_err(|e| Exception::custom(format!("{}", e)))?;
let keep_dims = keep_dims.into().unwrap_or(false);
match axes.into_option() {
Some(axes) => Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm_ord(
res,
array.as_ref().as_ptr(),
ord.as_ptr(),
axes.as_ptr(),
axes.len(),
keep_dims,
stream.as_ref().as_ptr(),
)
}),
None => Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm_ord(
res,
array.as_ref().as_ptr(),
ord.as_ptr(),
std::ptr::null(),
0,
keep_dims,
stream.as_ref().as_ptr(),
)
}),
}
}
/// Matrix or vector norm.
///
/// For values of `ord < 1`, the result is, strictly speaking, not a
/// mathematical norm, but it may still be useful for various numerical
/// purposes.
///
/// The following norms can be calculated:
///
/// ord | norm for matrices | norm for vectors
/// ----- | ---------------------------- | --------------------------
/// None | Frobenius norm | 2-norm
/// 'fro' | Frobenius norm | --
/// inf | max(sum(abs(x), axis-1)) | max(abs(x))
/// -inf | min(sum(abs(x), axis-1)) | min(abs(x))
/// 0 | -- | sum(x !- 0)
/// 1 | max(sum(abs(x), axis-0)) | as below
/// -1 | min(sum(abs(x), axis-0)) | as below
/// 2 | 2-norm (largest sing. value) | as below
/// -2 | smallest singular value | as below
/// other | -- | sum(abs(x)**ord)**(1./ord)
///
/// > Nuclear norm and norms based on singular values are not yet implemented.
///
/// The Frobenius norm is given by G. H. Golub and C. F. Van Loan, *Matrix Computations*,
/// Baltimore, MD, Johns Hopkins University Press, 1985, pg. 15
///
/// The nuclear norm is the sum of the singular values.
///
/// Both the Frobenius and nuclear norm orders are only defined for
/// matrices and produce a fatal error when `array.ndim != 2`
///
/// # Params
///
/// - `array`: input array
/// - `ord`: order of the norm, see table
/// - `axes`: axes that hold 2d matrices
/// - `keep_dims`: if `true` the axes which are normed over are left in the result as dimensions
/// with size one
#[default_device]
pub fn norm_device<'a>(
array: impl AsRef<Array>,
ord: impl IntoOption<Ord<'a>>,
axes: impl IntoOption<&'a [i32]>,
keep_dims: impl Into<Option<bool>>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let ord = ord.into_option();
let axes = axes.into_option();
let keep_dims = keep_dims.into().unwrap_or(false);
match (ord, axes) {
// If axis and ord are both unspecified, computes the 2-norm of flatten(x).
(None, None) => {
let axes_ptr = std::ptr::null(); // mlx-c already handles the case where axes is null
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm(
res,
array.as_ref().as_ptr(),
axes_ptr,
0,
keep_dims,
stream.as_ref().as_ptr(),
)
})
}
// If axis is not provided but ord is, then x must be either 1D or 2D.
//
// Frobenius norm is only supported for matrices
(Some(Ord::Str(ord)), None) => norm_ord_device(array, ord, axes, keep_dims, stream),
(Some(Ord::P(p)), None) => norm_p_device(array, p, axes, keep_dims, stream),
// If axis is provided, but ord is not, then the 2-norm (or Frobenius norm for matrices) is
// computed along the given axes. At most 2 axes can be specified.
(None, Some(axes)) => Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_norm(
res,
array.as_ref().as_ptr(),
axes.as_ptr(),
axes.len(),
keep_dims,
stream.as_ref().as_ptr(),
)
}),
// If both axis and ord are provided, then the corresponding matrix or vector
// norm is computed. At most 2 axes can be specified.
(Some(Ord::Str(ord)), Some(axes)) => norm_ord_device(array, ord, axes, keep_dims, stream),
(Some(Ord::P(p)), Some(axes)) => norm_p_device(array, p, axes, keep_dims, stream),
}
}
/// The QR factorization of the input matrix. Returns an error if the input is not valid.
///
/// This function supports arrays with at least 2 dimensions. The matrices which are factorized are
/// assumed to be in the last two dimensions of the input.
///
/// Evaluation on the GPU is not yet implemented.
///
/// # Params
///
/// - `array`: input array
///
/// # Example
///
/// ```rust
/// use mlx_rs::{Array, StreamOrDevice, linalg::*};
///
/// let a = Array::from_slice(&[2.0f32, 3.0, 1.0, 2.0], &[2, 2]);
///
/// let (q, r) = qr_device(&a, StreamOrDevice::cpu()).unwrap();
///
/// let q_expected = Array::from_slice(&[-0.894427, -0.447214, -0.447214, 0.894427], &[2, 2]);
/// let r_expected = Array::from_slice(&[-2.23607, -3.57771, 0.0, 0.447214], &[2, 2]);
///
/// assert!(q.all_close(&q_expected, None, None, None).unwrap().item::<bool>());
/// assert!(r.all_close(&r_expected, None, None, None).unwrap().item::<bool>());
/// ```
#[default_device]
pub fn qr_device(a: impl AsRef<Array>, stream: impl AsRef<Stream>) -> Result<(Array, Array)> {
<(Array, Array)>::try_from_op(|(res_0, res_1)| unsafe {
mlx_sys::mlx_linalg_qr(res_0, res_1, a.as_ref().as_ptr(), stream.as_ref().as_ptr())
})
}
/// The Singular Value Decomposition (SVD) of the input matrix. Returns an error if the input is not
/// valid.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the function iterates over all indices of the first a.ndim - 2 dimensions and for
/// each combination SVD is applied to the last two indices.
///
/// Evaluation on the GPU is not yet implemented.
///
/// # Params
///
/// - `array`: input array
///
/// # Example
///
/// ```rust
/// use mlx_rs::{Array, StreamOrDevice, linalg::*};
///
/// let a = Array::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2]);
/// let (u, s, vt) = svd_device(&a, StreamOrDevice::cpu()).unwrap();
/// let u_expected = Array::from_slice(&[-0.404554, 0.914514, -0.914514, -0.404554], &[2, 2]);
/// let s_expected = Array::from_slice(&[5.46499, 0.365966], &[2]);
/// let vt_expected = Array::from_slice(&[-0.576048, -0.817416, -0.817415, 0.576048], &[2, 2]);
/// assert!(u.all_close(&u_expected, None, None, None).unwrap().item::<bool>());
/// assert!(s.all_close(&s_expected, None, None, None).unwrap().item::<bool>());
/// assert!(vt.all_close(&vt_expected, None, None, None).unwrap().item::<bool>());
/// ```
#[default_device]
pub fn svd_device(
array: impl AsRef<Array>,
stream: impl AsRef<Stream>,
) -> Result<(Array, Array, Array)> {
let v = VectorArray::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_svd(res, array.as_ref().as_ptr(), stream.as_ref().as_ptr())
})?;
let vals: SmallVec<[Array; 3]> = v.try_into_values()?;
let mut iter = vals.into_iter();
let u = iter.next().unwrap();
let s = iter.next().unwrap();
let vt = iter.next().unwrap();
Ok((u, s, vt))
}
/// Compute the inverse of a square matrix. Returns an error if the input is not valid.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the inverse is computed for each matrix in the last two dimensions of `a`.
///
/// Evaluation on the GPU is not yet implemented.
///
/// # Params
///
/// - `a`: input array
///
/// # Example
///
/// ```rust
/// use mlx_rs::{Array, StreamOrDevice, linalg::*};
///
/// let a = Array::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2]);
/// let a_inv = inv_device(&a, StreamOrDevice::cpu()).unwrap();
/// let expected = Array::from_slice(&[-2.0, 1.0, 1.5, -0.5], &[2, 2]);
/// assert!(a_inv.all_close(&expected, None, None, None).unwrap().item::<bool>());
/// ```
#[default_device]
pub fn inv_device(a: impl AsRef<Array>, stream: impl AsRef<Stream>) -> Result<Array> {
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_inv(res, a.as_ref().as_ptr(), stream.as_ref().as_ptr())
})
}
/// Compute the Cholesky decomposition of a real symmetric positive semi-definite matrix.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the Cholesky decomposition is computed for each matrix in the last two dimensions of
/// `a`.
///
/// If the input matrix is not symmetric positive semi-definite, behaviour is undefined.
///
/// # Params
///
/// - `a`: input array
/// - `upper`: If `true`, return the upper triangular Cholesky factor. If `false`, return the lower
/// triangular Cholesky factor. Default: `false`.
#[default_device]
pub fn cholesky_device(
a: impl AsRef<Array>,
upper: Option<bool>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let upper = upper.unwrap_or(false);
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_cholesky(res, a.as_ref().as_ptr(), upper, stream.as_ref().as_ptr())
})
}
/// Compute the inverse of a real symmetric positive semi-definite matrix using it’s Cholesky decomposition.
///
/// Please see the python documentation for more details.
#[default_device]
pub fn cholesky_inv_device(
a: impl AsRef<Array>,
upper: Option<bool>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let upper = upper.unwrap_or(false);
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_cholesky_inv(res, a.as_ref().as_ptr(), upper, stream.as_ref().as_ptr())
})
}
/// Compute the cross product of two arrays along a specified axis.
///
/// The cross product is defined for arrays with size 2 or 3 in the specified axis. If the size is 2
/// then the third value is assumed to be zero.
#[default_device]
pub fn cross_device(
a: impl AsRef<Array>,
b: impl AsRef<Array>,
axis: Option<i32>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let axis = axis.unwrap_or(-1);
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_cross(
res,
a.as_ref().as_ptr(),
b.as_ref().as_ptr(),
axis,
stream.as_ref().as_ptr(),
)
})
}
/// Compute the eigenvalues and eigenvectors of a complex Hermitian or real symmetric matrix.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the eigenvalues and eigenvectors are computed for each matrix in the last two
/// dimensions.
#[default_device]
pub fn eigh_device(
a: impl AsRef<Array>,
uplo: Option<&str>,
stream: impl AsRef<Stream>,
) -> Result<(Array, Array)> {
let a = a.as_ref();
let uplo =
CString::new(uplo.unwrap_or("L")).map_err(|e| Exception::custom(format!("{}", e)))?;
<(Array, Array) as Guarded>::try_from_op(|(res_0, res_1)| unsafe {
mlx_sys::mlx_linalg_eigh(
res_0,
res_1,
a.as_ptr(),
uplo.as_ptr(),
stream.as_ref().as_ptr(),
)
})
}
/// Compute the eigenvalues of a complex Hermitian or real symmetric matrix.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the eigenvalues are computed for each matrix in the last two dimensions.
#[default_device]
pub fn eigvalsh_device(
a: impl AsRef<Array>,
uplo: Option<&str>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let a = a.as_ref();
let uplo =
CString::new(uplo.unwrap_or("L")).map_err(|e| Exception::custom(format!("{}", e)))?;
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_eigvalsh(res, a.as_ptr(), uplo.as_ptr(), stream.as_ref().as_ptr())
})
}
/// Compute the (Moore-Penrose) pseudo-inverse of a matrix.
#[default_device]
pub fn pinv_device(a: impl AsRef<Array>, stream: impl AsRef<Stream>) -> Result<Array> {
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_pinv(res, a.as_ref().as_ptr(), stream.as_ref().as_ptr())
})
}
/// Compute the inverse of a triangular square matrix.
///
/// This function supports arrays with at least 2 dimensions. When the input has more than two
/// dimensions, the inverse is computed for each matrix in the last two dimensions of a.
#[default_device]
pub fn tri_inv_device(
a: impl AsRef<Array>,
upper: Option<bool>,
stream: impl AsRef<Stream>,
) -> Result<Array> {
let upper = upper.unwrap_or(false);
Array::try_from_op(|res| unsafe {
mlx_sys::mlx_linalg_tri_inv(res, a.as_ref().as_ptr(), upper, stream.as_ref().as_ptr())
})
}
#[cfg(test)]
mod tests {
use float_eq::assert_float_eq;
use super::*;
// The tests below are adapted from the swift bindings tests
// and they are not exhaustive. Additional tests should be added
// to cover the error cases
#[test]
fn test_norm_no_axes() {
let a = Array::from_iter(0..9, &[9]) - 4;
let b = a.reshape(&[3, 3]).unwrap();
assert_float_eq!(
norm(&a, None, None, None).unwrap().item::<f32>(),
7.74597,
abs <= 0.001
);
assert_float_eq!(
norm(&b, None, None, None).unwrap().item::<f32>(),
7.74597,
abs <= 0.001
);
assert_float_eq!(
norm(&b, "fro", None, None).unwrap().item::<f32>(),
7.74597,
abs <= 0.001
);
assert_float_eq!(
norm(&a, f64::INFINITY, None, None).unwrap().item::<f32>(),
4.0,
abs <= 0.001
);
assert_float_eq!(
norm(&b, f64::INFINITY, None, None).unwrap().item::<f32>(),
9.0,
abs <= 0.001
);
assert_float_eq!(
norm(&a, f64::NEG_INFINITY, None, None)
.unwrap()
.item::<f32>(),
0.0,
abs <= 0.001
);
assert_float_eq!(
norm(&b, f64::NEG_INFINITY, None, None)
.unwrap()
.item::<f32>(),
2.0,
abs <= 0.001
);
assert_float_eq!(
norm(&a, 1.0, None, None).unwrap().item::<f32>(),
20.0,
abs <= 0.001
);
assert_float_eq!(
norm(&b, 1.0, None, None).unwrap().item::<f32>(),
7.0,
abs <= 0.001
);
assert_float_eq!(
norm(&a, -1.0, None, None).unwrap().item::<f32>(),
0.0,
abs <= 0.001
);
assert_float_eq!(
norm(&b, -1.0, None, None).unwrap().item::<f32>(),
6.0,
abs <= 0.001
);
}
#[test]
fn test_norm_axis() {
let c = Array::from_slice(&[1, 2, 3, -1, 1, 4], &[2, 3]);
let result = norm(&c, None, &[0][..], None).unwrap();
let expected = Array::from_slice(&[1.41421, 2.23607, 5.0], &[3]);
assert!(result
.all_close(&expected, None, None, None)
.unwrap()
.item::<bool>());
}
#[test]
fn test_norm_axes() {
let m = Array::from_iter(0..8, &[2, 2, 2]);
let result = norm(&m, None, &[1, 2][..], None).unwrap();
let expected = Array::from_slice(&[3.74166, 11.225], &[2]);
assert!(result
.all_close(&expected, None, None, None)
.unwrap()
.item::<bool>());
}
#[test]
fn test_qr() {
let a = Array::from_slice(&[2.0f32, 3.0, 1.0, 2.0], &[2, 2]);
let (q, r) = qr_device(&a, StreamOrDevice::cpu()).unwrap();
let q_expected = Array::from_slice(&[-0.894427, -0.447214, -0.447214, 0.894427], &[2, 2]);
let r_expected = Array::from_slice(&[-2.23607, -3.57771, 0.0, 0.447214], &[2, 2]);
assert!(q
.all_close(&q_expected, None, None, None)
.unwrap()
.item::<bool>());
assert!(r
.all_close(&r_expected, None, None, None)
.unwrap()
.item::<bool>());
}
// The tests below are adapted from the c++ tests
#[test]
fn test_svd() {
// eval_gpu is not implemented yet.
let stream = StreamOrDevice::cpu();
// 0D and 1D returns error
let a = Array::from_float(0.0);
assert!(svd_device(&a, &stream).is_err());
let a = Array::from_slice(&[0.0, 1.0], &[2]);
assert!(svd_device(&a, &stream).is_err());
// Unsupported types returns error
let a = Array::from_slice(&[0, 1], &[1, 2]);
assert!(svd_device(&a, &stream).is_err());
// TODO: wait for random
}
#[test]
fn test_inv() {
// eval_gpu is not implemented yet.
let stream = StreamOrDevice::cpu();
// 0D and 1D returns error
let a = Array::from_float(0.0);
assert!(inv_device(&a, &stream).is_err());
let a = Array::from_slice(&[0.0, 1.0], &[2]);
assert!(inv_device(&a, &stream).is_err());
// Unsupported types returns error
let a = Array::from_slice(&[1, 2, 3, 4, 5, 6], &[2, 3]);
assert!(inv_device(&a, &stream).is_err());
// TODO: wait for random
}
#[test]
fn test_cholesky() {
// eval_gpu is not implemented yet.
let stream = StreamOrDevice::cpu();
// 0D and 1D returns error
let a = Array::from_float(0.0);
assert!(cholesky_device(&a, None, &stream).is_err());
let a = Array::from_slice(&[0.0, 1.0], &[2]);
assert!(cholesky_device(&a, None, &stream).is_err());
// Unsupported types returns error
let a = Array::from_slice(&[0, 1, 1, 2], &[2, 2]);
assert!(cholesky_device(&a, None, &stream).is_err());
// Non-square returns error
let a = Array::from_slice(&[1, 2, 3, 4, 5, 6], &[2, 3]);
assert!(cholesky_device(&a, None, &stream).is_err());
// TODO: wait for random
}
}