Operations¶
Layout and quantization aware high-level math operations.
- taktiny.utils.ops.linear(x, w, b=None, *, quant=None, module_path='', precision=None, preferred_element_type=None, out_sharding=None)[source]¶
Compute
x @ w + bwith optional quantized multiplication.xhas shape(..., input_features)andwhas shape(input_features, output_features), matching Taktiny Linear’s kernel layout. Leading input dimensions are preserved. Bias must broadcast to the result; a quantized bias is dequantized before addition.quant=Noneuses JAX for dense inputs and Qwix for QArray inputs.quantaccepts a dtype string, a Qwix rule, a sequence of rules, or a PtqProvider/QtProvider. Strings use Taktiny’s weight-only shorthand. Rules matchmodule_path(empty by default) anddot_generalin first-match order.weight_qtypeapplies towandact_qtypetox. Existing QArrays are dequantized before applying a selected rule. A rule with unset qtypes disables quantization for its matched operation.QtRuleuses Qwix’s custom-gradient training kernels;bwd_qtypecontrols backward quantization. Ordinary rules and string shortcuts use PTQ, not trainable weight quantization. For training, pass floating-point parameters and a QtRule. Static activation scales and stochastic rounding are not supported by these stateless functions. QT currently does not support an explicitout_shardingargument. Training uses Qwix QT kernels; supported formats and backward operations depend on Qwix and the execution backend.Returns a dense JAX array.
preferred_element_typeselects its dtype, including after bias addition; otherwise the dtype is promoted from the inputs and bias, using QArray scale dtypes. Raw FP8 inputs promote to BF16; integer-only inputs withquantset promote to FP32. Accumulation may use a higher precision than the returned dtype.out_shardingis passed to the dot operation where supported.- Return type:
- Parameters:
x (ArrayLike | QArray)
w (ArrayLike | QArray)
b (ArrayLike | QArray | None)
quant (QuantConfig)
module_path (str)
precision (None | str | Precision | tuple[str, str] | tuple[Precision, Precision] | DotAlgorithm | DotAlgorithmPreset)
preferred_element_type (str | type[Any] | dtype | SupportsDType | None)
out_sharding (NamedSharding | None)
Example
>>> linear(jnp.ones((2, 3)), jnp.ones((3, 4))).shape (2, 4) >>> linear(jnp.ones((2, 3)), jnp.ones((3, 4)), quant='int8', ... preferred_element_type=jnp.bfloat16).dtype dtype(bfloat16)
- taktiny.utils.ops.einsum(subscripts, *operands, quant=None, module_path='', optimize='auto', precision=None, preferred_element_type=None, out_sharding=None)[source]¶
Evaluate an Einstein summation with optional quantized contractions.
Uses the string-equation form of
jax.numpy.einsum. Dense array-like inputs are converted to JAX arrays, while QArrays retain their quantized representation whenquantandout_shardingare unset. In that case, QArray inputs selectqwix.einsumand its dequantization fallbacks.quantaccepts the same rule configurations aslinear(). Strings are weight-only shortcuts. In two-operand expressions the first operand is the activation and the second is the weight, independent of contraction ordering. Rules matchmodule_pathand eithereinsumordot_general. QtRule enables training and itsbwd_qtypecontrols backward quantization. Rule-based expressions support at most two operands; transformations that obscure operand roles raise an error. Unary operations remain ordinary JAX operations. Returns a dense JAX array.Static scales, stochastic rounding, and explicit output sharding for QT have the same limitations as
linear().precisionis forwarded to the selected backend.optimizeis supported for dense inputs and whenquantis set. Withquant=None, unsharded QArray inputs require'auto'because Qwix manages its own contraction path.preferred_element_typeselects the returned dtype; otherwise input dtypes (QArray scale dtypes) are promoted. Raw FP8 promotes to BF16, and integer-only inputs promote to FP32 whenquantis set. Accumulation may use higher precision.out_shardingis passed to JAX’s einsum. Ifquantis unset, this sharded path dequantizes QArrays and uses dense computation.- Return type:
- Parameters:
subscripts (str)
operands (ArrayLike | QArray)
quant (QuantConfig)
module_path (str)
optimize (Any)
precision (None | str | Precision | tuple[str, str] | tuple[Precision, Precision] | DotAlgorithm | DotAlgorithmPreset)
preferred_element_type (str | type[Any] | dtype | SupportsDType | None)
out_sharding (NamedSharding | None)
Example
>>> einsum('...i,io->...o', jnp.ones((2, 3)), jnp.ones((3, 4))).shape (2, 4)
- taktiny.utils.ops.conv_general_dilated(lhs, rhs, window_strides, padding, lhs_dilation=None, rhs_dilation=None, dimension_numbers=None, feature_group_count=1, batch_group_count=1, precision=None, preferred_element_type=None, *, out_sharding=None, quant=None, module_path='')[source]¶
Convolve activations
lhswith kernelrhsusing JAX or Qwix.Layout, strides, dilation, and grouping follow
jax.lax.conv_general_dilated.dimension_numbersdescribes input, kernel, and output layouts, e.g.('NWC', 'WIO', 'NWC')for channels-last 1-D convolution. Padding is'VALID','SAME','SAME_LOWER', or one(low, high)pair per spatial dimension. Dilation inserts gaps in the input or kernel; strides control output sampling.Dense array-like inputs use JAX; QArrays use Qwix.
quantaccepts a weight-only dtype string, a Qwix rule, a rule sequence, or PtqProvider / QtProvider. Rules matchmodule_pathandconv_general_dilatedin first-match order. A selected rule dequantizes existing QArrays before applying itsact_qtypeto lhs andweight_qtypeto rhs. Unmatched or disabled rules use dense computation.Ordinary rules quantize operands at each call. For training, use floating- point parameters and QtRule;
bwd_qtypecontrols backward quantization through Qwix QT. Supported formats and gradients depend on Qwix and the backend. Tiled convolution quantization, static activation calibration, QT stochastic rounding, and convolutionadditional_qt_configare unsupported.Returns a dense array.
preferred_element_typeselects the result dtype; otherwise operand dtypes (scale dtypes for QArrays) are promoted. FP8 inputs promote to BF16 and quantized integer inputs to FP32. Precision is forwarded to JAX/PTQ; Qwix QT controls its own contraction precision.out_shardingis forwarded to the convolution operation.- Return type:
- Parameters:
lhs (ArrayLike | QArray)
rhs (ArrayLike | QArray)
dimension_numbers (tuple[str, str, str] | ConvDimensionNumbers | None)
feature_group_count (int)
batch_group_count (int)
precision (None | str | Precision | tuple[str, str] | tuple[Precision, Precision] | DotAlgorithm | DotAlgorithmPreset)
preferred_element_type (str | type[Any] | dtype | SupportsDType | None)
out_sharding (NamedSharding | None)
quant (QuantConfig)
module_path (str)
Example
>>> conv_general_dilated( ... jnp.ones((1, 8, 2)), jnp.ones((3, 2, 4)), (1,), 'SAME', ... dimension_numbers=('NWC', 'WIO', 'NWC'), ... ).shape (1, 8, 4)
- taktiny.utils.ops.dot_general(lhs, rhs, dimension_numbers, precision=None, preferred_element_type=None, *, out_sharding=None, quant=None, module_path='', lhs_weight=False, rhs_weight=True)[source]¶
Contract two operands using JAX or Qwix
dot_general.dimension_numbershas the same contracting and batch-axis layout asjax.lax.dot_general(). Dense inputs use JAX; without an explicit rule, QArray inputs use Qwix. A selectedquantrule dequantizes any existing QArrays before applying its configured operand quantization. Rules matchmodule_pathanddot_generalin first-match order.By default,
lhsis an activation andrhsis a weight. Setlhs_weightorrhs_weightto choose which operand receives the rule’sweight_qtype; other operands receiveact_qtype. Aqwix.QtRuleuses Qwix’s training kernel, including its optional backward quantization. Ordinary rules and strings use PTQ. QT does not currently support explicitout_sharding.The result is a dense JAX array. Its implicit dtype follows the operand dtypes (QArray scale dtypes), with FP8 promoted to BF16 and quantized integer inputs to FP32.
preferred_element_typeoverrides the result dtype.out_shardingis forwarded to the selected operation where supported.- Return type:
- Parameters:
lhs (ArrayLike | QArray)
rhs (ArrayLike | QArray)
dimension_numbers (tuple[tuple[Sequence[int], Sequence[int]], tuple[Sequence[int], Sequence[int]]])
precision (None | str | Precision | tuple[str, str] | tuple[Precision, Precision] | DotAlgorithm | DotAlgorithmPreset)
preferred_element_type (str | type[Any] | dtype | SupportsDType | None)
out_sharding (NamedSharding | None)
quant (QuantConfig)
module_path (str)
lhs_weight (bool)
rhs_weight (bool)
Example
>>> dot_general( ... jnp.ones((2, 3)), jnp.ones((3, 4)), ... (((1,), (0,)), ((), ())), ... ).shape (2, 4)