asc2.mask
- asc2.mask(*, count: RuntimeInt, other: RuntimeNumeric | None = None) Generator[None, Any, None]
- asc2.mask(*, bits: Iterable[RuntimeInt], other: RuntimeNumeric | None = None) Generator[None, Any, None]
[Experimental] A context manager for masked operations on tensors.
Within the mask context, operations are applied only to the specified elements, with other elements optionally set to a different value.
Two masking modes are supported:
Count-based masking: Apply operations to the first
countelements along the innermost dimension. Elements beyondcountare set toother(default 0).Bit-based masking: Apply operations to elements where the bit index (computed from position) falls within the range specified by
bits.bitsmust contain exactly two integers defining the range.
- Parameters:
count – The number of elements to apply operations to (from the start). Mutually exclusive with
bits.bits – A tuple of two integers defining a bit-based range for masking. Mutually exclusive with
count.other – The value to use for elements outside the mask. Default is 0.
- Raises:
TypeError – If
otheris not a scalar,countis not an integer, orbitsdoes not contain integersRuntimeError – If
bitsdoes not contain exactly two integersValueError – If neither or both of
countandbitsare provided
Warning
This is an experimental API which is not guaranteed to work for every relevant vector operation. Its interface, availability, and functional coverage may change in the future.
Note
Exactly one of
countorbitsmust be provided. This context manager can only be applied to vector operations (e.g., add, exp), not to load/store operations.Examples
Apply addition only to first 8 elements, others set to 0:
with asc2.mask(count=8, other=0): result = tensor_a + tensor_b
Apply exp only to elements within bit range, others set to -1:
with asc2.mask(bits=[0, 64], other=-1): result = asc2.exp(tensor)