jaxmg.least_squares¤
least_squares solves an overdetermined linear system using distributed QR
factorization:
The solve input \(B\) may be a vector or a matrix containing multiple columns.
The returned solution has shape (N,) or (N, K), respectively.
The RHS may have replicated or distributed columns. On grids with multiple
process columns, cuSOLVERMp requires \(B\) to span at least as many column tiles
as \(A\). Use a one-column process grid for a vector or a small number of RHS
columns; routing padding does not change this requirement.
Use least_squares for a direct solve. Use least_squares_jit_ctx when
the solve is part of a larger caller-owned jax.jit. Since cuSOLVERMp
overwrites both distributed inputs, the context interface returns a_work and
b_work as shape-compatible alias targets for donated arguments.
jaxmg.least_squares(a, b, T_A, mesh=None, matrix_specs=None, *, in_specs=None, return_status=False, pad=True, donate=True)
¤
Solve min_X ||A X - B||_2 with distributed QR factorization.
The current implementation supports overdetermined or square systems with
M >= N. For A of shape (M, N) and B of shape (M, K),
the returned solution has shape (N, K). A rank-1 B is accepted and
produces a rank-1 solution. On grids with more than one process column,
B must span at least as many column tiles as A. Use a one-column
process grid for a vector or a small number of right-hand sides.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array
|
Rank-2 input matrix sharded over a one- or two-axis device mesh. |
required |
b
|
Array
|
Rank-1 or rank-2 solve input with |
required |
T_A
|
int
|
Square cuSOLVERMp tile width. |
required |
mesh
|
Mesh or AbstractMesh
|
JAX mesh used by
|
None
|
matrix_specs
|
PartitionSpec or tuple / list[PartitionSpec]
|
Rank-2 matrix sharding. If omitted, read from the sharding of
|
None
|
in_specs
|
P | Tuple[P] | List[P] | None
|
Backwards-compatible alias for |
None
|
return_status
|
bool
|
If True return |
False
|
pad
|
bool
|
If True (default), add tile-aligned local capacity where required. |
True
|
donate
|
bool
|
If True (default), the input buffers may be reused by the native call and must not be used afterwards. Pass False to preserve them at the cost of additional memory. |
True
|
Returns:
| Type | Description |
|---|---|
Union[Array, Tuple[Array, Array]]
|
Array or (Array, Array): The least-squares solution, and optionally the |
Union[Array, Tuple[Array, Array]]
|
native per-rank diagnostic status. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If dtypes or sharding specifications are unsupported. |
ValueError
|
If shapes, tile sizes, or mesh layouts are incompatible. |
Notes
- If the native solver fails, the solution is filled with NaN and the status is non-zero.
jaxmg.least_squares_jit_ctx(a, b, T_A, mesh=None, matrix_specs=None, *, in_specs=None, pad=True, _prepared=None)
¤
Solve a least-squares system inside a caller-owned jax.jit.
This lower-level interface performs the same work as
:func:jaxmg.least_squares but leaves the outer JIT boundary to the caller.
It returns a_work and b_work so an outer compiled function can
donate both inputs into shape-compatible native work outputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array
|
Rank-2 input matrix sharded over a one- or two-axis device mesh. |
required |
b
|
Array
|
Rank-1 or rank-2 solve input with |
required |
T_A
|
int
|
Square cuSOLVERMp tile width. |
required |
mesh
|
Mesh or AbstractMesh
|
JAX mesh used by
|
None
|
matrix_specs
|
PartitionSpec or tuple / list[PartitionSpec]
|
Rank-2 matrix sharding. If omitted, read from the sharding of
|
None
|
in_specs
|
P | Tuple[P] | List[P] | None
|
Backwards-compatible alias for |
None
|
pad
|
bool
|
If True (default), add tile-aligned local capacity where required. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
Array
|
|
Array
|
solve-input work buffers, least-squares solution, and native per-rank |
|
Array
|
status. The first |
Raises:
| Type | Description |
|---|---|
TypeError
|
If dtypes or sharding specifications are unsupported. |
ValueError
|
If shapes, tile sizes, or mesh layouts are incompatible. |
Notes
- If the native solver fails, the solution is filled with NaN and the status is non-zero.
Use least_squares_shardmap_ctx for local blocks inside your own jax.shard_map.
The previous global-array context interface is now named least_squares_jit_ctx.
jaxmg.least_squares_shardmap_ctx(a, b, T_A, matrix_specs=None, *, in_specs=None, pad=True, _prepared=None)
¤
Solve a least-squares system inside a caller's jax.shard_map.
A follows matrix_specs; B shares its row sharding and may have
distributed or replicated columns. Replicated copies become duplicate
right-hand sides in one distributed QR solve, not separate factorizations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array
|
Local block of a global M x N matrix, with M >= N. |
required |
b
|
Array
|
Local RHS matrix or vector, with A's dtype and row layout. |
required |
T_A
|
int
|
Square tile width used by cuSOLVERMp. |
required |
matrix_specs
|
P | Tuple[P] | List[P] | None
|
A's partition specification in the enclosing shard map; defaults to the context mesh axes in order. |
None
|
in_specs
|
P | Tuple[P] | List[P] | None
|
Backwards-compatible alias for |
None
|
pad
|
bool
|
Allow local tile-capacity padding; otherwise require aligned blocks. |
True
|
Returns:
| Type | Description |
|---|---|
Array
|
|
Array
|
retains the logical M-row layout. The native backend restores the |
Array
|
N-row solution across process rows, preserving B's column replication. |
Array
|
Return both work buffers from an outer jitted function when donating inputs. |
Tuple[Array, Array, Array, Array]
|
Status contains this rank's diagnostics. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If dtypes or partition specifications are unsupported. |
ValueError
|
If called outside shard_map or shapes are incompatible. |