Arrays¶
tinax.array owns NumPy, JAX, DLPack, host materialization, logical array inspection, and a small set of validated array operations. Its conversion functions make copying and ownership explicit; its operations close silent-failure gaps in the equivalent jax.numpy/jax.nn calls.
See the Array Ownership guide for task-oriented usage.
from_numpy
¶
Create a JAX array from a NumPy array with explicit copy and placement policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
ndarray
|
Source NumPy array to convert. |
required |
copy
|
bool
|
If |
required |
device
|
Device | Sharding | None
|
Device or sharding to place the result on. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
A JAX array holding the converted data, placed according to |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
from_dlpack
¶
from_dlpack(
array: object,
*,
copy: bool | None = None,
device: Device | Sharding | None = None,
) -> Array
Import a DLPack provider, completing copy=True before returning independent storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
object
|
Object exposing the DLPack protocol to import. |
required |
copy
|
bool | None
|
Copy policy. |
None
|
device
|
Device | Sharding | None
|
Device or sharding for the imported array. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
A JAX array backed by the imported buffer. When |
Array
|
storage is guaranteed independent, including for same-device JAX providers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
to_numpy
¶
Synchronize a fully addressable JAX array and return a writable copy or read-only host value.
Blocks until the array is ready, then materializes its data on the host.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Array
|
Fully addressable JAX array to materialize on the host. |
required |
writable
|
bool
|
If |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
The array's data as a |
ndarray
|
copies; read-only results are a synchronized host view. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
inspect_array
¶
inspect_array(array: Array) -> ArrayInfo
Inspect logical metadata without synchronizing or materializing array data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Array
|
JAX array to inspect. Its data is never copied to the host. |
required |
Returns:
| Type | Description |
|---|---|
ArrayInfo
|
An |
ArrayInfo
|
commitment, and addressability. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ArrayInfo
dataclass
¶
ArrayInfo(
shape: tuple[int, ...],
dtype: object,
logical_nbytes: int | None,
committed: bool,
fully_addressable: bool,
)
Immutable logical metadata whose byte count is nominal rather than allocator memory.
Attributes:
| Name | Type | Description |
|---|---|---|
shape |
tuple[int, ...]
|
Logical shape of the array. |
dtype |
object
|
Array dtype. Extended dtypes report |
logical_nbytes |
int | None
|
Logical size in bytes, or |
committed |
bool
|
Whether the array is committed to a specific device. |
fully_addressable |
bool
|
Whether every shard is addressable by the current process. |
stack_batch
¶
Stack arrays after requiring one shared dtype, where jnp.stack silently promotes mismatches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arrays
|
Sequence[Array]
|
Non-empty sequence of JAX arrays to stack. All elements must share one dtype. |
required |
axis
|
int
|
Position of the new stacked axis. Defaults to 0. |
0
|
Returns:
| Type | Description |
|---|---|
Array
|
The arrays stacked along |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
safe_astype
¶
Cast to dtype, requiring allow_lossy=True for a narrowing cast that jnp.astype performs silently.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
array
|
Array
|
JAX array to cast. |
required |
dtype
|
DTypeLike
|
Target dtype. |
required |
allow_lossy
|
bool
|
Whether to permit a cast that can lose precision or overflow. Defaults
to |
False
|
Returns:
| Type | Description |
|---|---|
Array
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the cast from |
one_hot
¶
Encode integer indices as one-hot rows, rejecting inputs jax.nn.one_hot would silently zero out.
Does not validate that concrete index values lie in 0..num_classes - 1: that requires
concrete values and would break tracing under jax.jit. Out-of-range indices still
produce an all-zero row, matching jax.nn.one_hot's own documented behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
Array
|
JAX array of integer indices to encode. |
required |
num_classes
|
int
|
Number of one-hot classes. Must be a positive integer. |
required |
dtype
|
DTypeLike | None
|
Output dtype. |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
A one-hot encoding of |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |