jax.lax 模块#

jax.lax 是一个原始操作库,它是 jax.numpy 等库的基础。转换规则(如 JVP 和批处理规则)通常定义为对 jax.lax 原始操作的转换。

许多原始操作是对等效 XLA 操作的薄包装,这些操作在 XLA 操作语义文档中进行了描述。在少数情况下,JAX 会偏离 XLA,通常是为了确保操作集在 JVP 和转置规则的操作下是闭合的。

如果可能,请优先使用 jax.numpy 等库,而不是直接使用 jax.laxjax.numpy API 遵循 NumPy,因此比 jax.lax API 更稳定,更不容易更改。

运算符#

abs(x)

逐元素绝对值:\(|x|\)

acos(x)

逐元素反余弦:\(\mathrm{acos}(x)\)

acosh(x)

逐元素反双曲余弦:\(\mathrm{acosh}(x)\)

add(x, y)

逐元素加法:\(x + y\)

after_all(*operands)

合并一个或多个 XLA 令牌值。

approx_max_k(operand, k[, ...])

以近似方式返回 operand 中最大的 k 个值及其索引。

approx_min_k(operand, k[, ...])

以近似方式返回 operand 中最小的 k 个值及其索引。

argmax(operand, axis, index_dtype)

计算沿 axis 的最大元素的索引。

argmin(operand, axis, index_dtype)

计算沿 axis 的最小元素的索引。

asin(x)

逐元素反正弦:\(\mathrm{asin}(x)\)

asinh(x)

逐元素反双曲正弦:\(\mathrm{asinh}(x)\)

atan(x)

逐元素反正切:\(\mathrm{atan}(x)\)

atan2(x, y)

两个变量的逐元素反正切:\(\mathrm{atan}({x \over y})\)

atanh(x)

逐元素反双曲正切:\(\mathrm{atanh}(x)\)

batch_matmul(lhs, rhs[, precision])

批量矩阵乘法。

bessel_i0e(x)

指数缩放的 0 阶修正贝塞尔函数:\(\mathrm{i0e}(x) = e^{-|x|} \mathrm{i0}(x)\)

bessel_i1e(x)

指数缩放的 1 阶修正贝塞尔函数:\(\mathrm{i1e}(x) = e^{-|x|} \mathrm{i1}(x)\)

betainc(a, b, x)

逐元素正则化不完全贝塔积分。

bitcast_convert_type(operand, new_dtype)

逐元素位转换。

bitwise_and(x, y)

逐元素 AND:\(x \wedge y\)

bitwise_not(x)

逐元素 NOT:\(\neg x\)

bitwise_or(x, y)

逐元素 OR:\(x \vee y\)

bitwise_xor(x, y)

逐元素异或:\(x \oplus y\)

population_count(x)

逐元素 popcount,计算每个元素中设置的位数。

broadcast(operand, sizes[, sharding])

广播一个数组,添加新的前导维度

broadcast_in_dim(operand, shape, ...[, sharding])

包装 XLA 的 BroadcastInDim 运算符。

broadcast_shapes()

返回 shapes 的 NumPy 广播产生的形状。

broadcast_to_rank(x, rank)

添加前导维度 1,使 x 的秩为 rank

broadcasted_iota(dtype, shape, dimension[, ...])

围绕 iota 的便捷包装器。

cbrt(x)

逐元素立方根:\(\sqrt[3]{x}\)

ceil(x)

逐元素向上取整:\(\left\lceil x \right\rceil\)

clamp(min, x, max)

逐元素钳位操作。

clz(x)

逐元素计算前导零的数量。

collapse(operand, start_dimension[, ...])

将数组的多个维度折叠成单个维度。

complex(x, y)

逐元素构造复数:\(x + jy\)

concatenate(operands, dimension)

沿维度连接一系列数组。

conj(x)

逐元素复共轭函数:\(\overline{x}\)

conv(lhs, rhs, window_strides, padding[, ...])

conv_general_dilated的便捷包装器。

convert_element_type(operand, new_dtype)

逐元素类型转换。

conv_dimension_numbers(lhs_shape, rhs_shape, ...)

将卷积dimension_numbers转换为ConvDimensionNumbers

conv_general_dilated(lhs, rhs, ...[, ...])

通用的n维卷积运算符,具有可选的空洞卷积。

conv_general_dilated_local(lhs, rhs, ...[, ...])

通用的n维非共享卷积运算符,具有可选的空洞卷积。

conv_general_dilated_patches(lhs, ...[, ...])

提取conv_general_dilated感受野内的patch。

conv_transpose(lhs, rhs, strides, padding[, ...])

用于计算 N 维卷积“转置”的便捷包装器。

conv_with_general_padding(lhs, rhs, ...[, ...])

conv_general_dilated的便捷包装器。

cos(x)

逐元素余弦:\(\mathrm{cos}(x)\)

cosh(x)

逐元素双曲余弦:\(\mathrm{cosh}(x)\)

cumlogsumexp(operand[, axis, reverse])

计算沿的累计对数和指数。

cummax(operand[, axis, reverse])

计算沿的累计最大值。

cummin(operand[, axis, reverse])

计算沿的累计最小值。

cumprod(operand[, axis, reverse])

计算沿的累计乘积。

cumsum(operand[, axis, reverse])

计算沿的累计和。

digamma(x)

逐元素双伽马函数:\(\psi(x)\)

div(x, y)

逐元素除法:\(x \over y\)

dot(lhs, rhs[, precision, ...])

向量/向量、矩阵/向量和矩阵/矩阵乘法。

dot_general(lhs, rhs, dimension_numbers[, ...])

通用点积/缩并运算符。

dynamic_index_in_dim(operand, index[, axis, ...])

用于执行整数索引的动态切片的便捷包装器。

dynamic_slice(operand, start_indices, ...)

包装 XLA 的 DynamicSlice 运算符。

dynamic_slice_in_dim(operand, start_index, ...)

应用于一个维度的lax.dynamic_slice()的便捷包装器。

dynamic_update_index_in_dim(operand, update, ...)

dynamic_update_slice() 的便捷包装器,用于更新单个axis中大小为 1 的切片。

dynamic_update_slice(operand, update, ...)

包装 XLA 的 DynamicUpdateSlice 运算符。

dynamic_update_slice_in_dim(operand, update, ...)

dynamic_update_slice() 的便捷包装器,用于更新单个 axis 中的切片。

eq(x, y)

逐元素等于:\(x = y\)

erf(x)

逐元素误差函数:\(\mathrm{erf}(x)\)

erfc(x)

逐元素互补误差函数:\(\mathrm{erfc}(x) = 1 - \mathrm{erf}(x)\)

erf_inv(x)

逐元素逆误差函数:\(\mathrm{erf}^{-1}(x)\)

exp(x)

逐元素指数:\(e^x\)

expand_dims(array, dimensions)

在数组中插入任意数量的大小为 1 的维度。

expm1(x)

逐元素 \(e^{x} - 1\)

fft(x, fft_type, fft_lengths)

floor(x)

逐元素向下取整:\(\left\lfloor x \right\rfloor\)

full(shape, fill_value[, dtype, sharding])

返回一个用fill_value填充的shape数组。

full_like(x, fill_value[, dtype, shape, ...])

基于示例数组x创建类似 np.full 的完整数组。

gather(operand, start_indices, ...[, ...])

收集运算符。

ge(x, y)

逐元素大于或等于:\(x \geq y\)

gt(x, y)

逐元素大于:\(x > y\)

igamma(a, x)

逐元素正则化不完全伽马函数。

igammac(a, x)

逐元素互补正则化不完全伽马函数。

imag(x)

逐元素提取虚部:\(\mathrm{Im}(x)\)

index_in_dim(operand, index[, axis, keepdims])

围绕 lax.slice() 的便捷封装,用于执行整数索引。

index_take(src, idxs, axes)

integer_pow(x, y)

逐元素幂运算:\(x^y\),其中 \(y\) 是一个固定的整数。

iota(dtype, size)

封装 XLA 的 Iota 运算符。

is_finite(x)

逐元素 \(\mathrm{isfinite}\)

le(x, y)

逐元素小于或等于:\(x \leq y\)

lgamma(x)

逐元素对数伽马函数:\(\mathrm{log}(\Gamma(x))\)

log(x)

逐元素自然对数:\(\mathrm{log}(x)\)

log1p(x)

逐元素 \(\mathrm{log}(1 + x)\)

logistic(x)

逐元素逻辑(sigmoid)函数:\(\frac{1}{1 + e^{-x}}\)

lt(x, y)

逐元素小于:\(x < y\)

max(x, y)

逐元素最大值:\(\mathrm{max}(x, y)\)

min(x, y)

逐元素最小值:\(\mathrm{min}(x, y)\)

mul(x, y)

逐元素乘法:\(x \times y\)

ne(x, y)

逐元素不等于:\(x \neq y\)

neg(x)

逐元素取反:\(-x\)

nextafter(x1, x2)

返回 x1 之后,朝向 x2 方向的下一个可表示的值。

optimization_barrier(operand, /)

阻止编译器跨越屏障移动操作。

pad(operand, padding_value, padding_config)

对数组应用低、高和/或内部填充。

platform_dependent(*args[, default])

分阶段输出特定于平台代码。

polygamma(m, x)

逐元素多伽马函数:\(\psi^{(m)}(x)\)

population_count(x)

逐元素 popcount,计算每个元素中设置的位数。

pow(x, y)

逐元素幂运算:\(x^y\)

random_gamma_grad(a, x)

来自 Gamma(a, 1) 的样本的逐元素导数。

real(x)

逐元素提取实部:\(\mathrm{Re}(x)\)

reciprocal(x)

逐元素倒数:\(1 \over x\)

reduce(operands, init_values, computation, ...)

封装 XLA 的 Reduce 运算符。

reduce_precision(operand, exponent_bits, ...)

封装 XLA 的 ReducePrecision 运算符。

reduce_window(operand, init_value, ...[, ...])

封装 XLA 的 ReduceWindowWithGeneralPadding 运算符。

rem(x, y)

逐元素取余:\(x \bmod y\)

reshape(operand, new_sizes[, dimensions, ...])

封装 XLA 的 Reshape 运算符。

rev(operand, dimensions)

封装 XLA 的 Rev 运算符。

rng_bit_generator(key, shape[, dtype, algorithm])

无状态 PRNG 位生成器。

rng_uniform(a, b, shape)

有状态 PRNG 生成器。

round(x[, rounding_method])

逐元素四舍五入。

rsqrt(x)

逐元素倒数平方根:\(1 \over \sqrt{x}\)

scatter(operand, scatter_indices, updates, ...)

散布更新运算符。

scatter_add(operand, scatter_indices, ...[, ...])

散布加法运算符。

scatter_apply(operand, scatter_indices, ...)

散布应用运算符。

scatter_max(operand, scatter_indices, ...[, ...])

散布最大值运算符。

scatter_min(operand, scatter_indices, ...[, ...])

散布最小值运算符。

scatter_mul(operand, scatter_indices, ...[, ...])

散布乘法运算符。

shift_left(x, y)

逐元素左移:\(x \ll y\)

shift_right_arithmetic(x, y)

逐元素算术右移:\(x \gg y\)

shift_right_logical(x, y)

逐元素逻辑右移:\(x \gg y\)

sign(x)

逐元素符号函数。

sin(x)

逐元素正弦函数:\(\mathrm{sin}(x)\)

sinh(x)

逐元素双曲正弦函数:\(\mathrm{sinh}(x)\)

slice(operand, start_indices, limit_indices)

封装了 XLA 的 Slice 操作符。

slice_in_dim(operand, start_index, limit_index)

围绕 lax.slice() 的便捷封装,仅应用于一个维度。

排序()

封装了 XLA 的 Sort 操作符。

sort_key_val(keys, values[, dimension, ...])

沿着 dimensionkeys 进行排序,并将相同的排列应用于 values

split(operand, sizes[, axis])

沿着 axis 分割数组。

sqrt(x)

逐元素平方根:\(\sqrt{x}\)

square(x)

逐元素平方:\(x^2\)

squeeze(array, dimensions)

从数组中挤压任意数量的大小为 1 的维度。

sub(x, y)

逐元素减法:\(x - y\)

tan(x)

逐元素正切:\(\mathrm{tan}(x)\)

tanh(x)

逐元素双曲正切:\(\mathrm{tanh}(x)\)

top_k(operand, k)

返回 operand 的最后一个轴上的前 k 个值及其索引。

transpose(operand, permutation)

封装了 XLA 的 Transpose 操作符。

zeros_like_array(x)

zeta(x, q)

逐元素 Hurwitz zeta 函数:\(\zeta(x, q)\)

控制流运算符#

associative_scan(fn, elems[, reverse, axis])

使用关联的二元运算并行执行扫描。

cond(pred, true_fun, false_fun, *operands[, ...])

有条件地应用 true_funfalse_fun

fori_loop(lower, upper, body_fun, init_val, *)

通过归约为 jax.lax.while_loop(),从 lower 循环到 upper

map(f, xs, *[, batch_size])

将函数映射到前导数组轴上。

scan(f, init[, xs, length, reverse, unroll, ...])

在执行状态的同时,扫描前导数组轴上的函数。

select(pred, on_true, on_false)

基于布尔谓词在两个分支之间选择。

select_n(which, *cases)

从多个案例中选择数组值。

switch(index, branches, *operands[, operand])

应用由 index 给出的 branches 中的恰好一个。

while_loop(cond_fun, body_fun, init_val)

cond_fun 为 True 时,重复调用循环中的 body_fun

自定义梯度运算符#

stop_gradient(x)

停止梯度计算。

custom_linear_solve(matvec, b, solve[, ...])

使用隐式定义的梯度执行无矩阵线性求解。

custom_root(f, initial_guess, solve, ...[, ...])

可微地求解函数的根。

并行运算符#

all_gather(x, axis_name, *[, ...])

跨所有副本收集 x 的值。

all_to_all(x, axis_name, split_axis, ...[, ...])

实现映射轴并映射不同的轴。

psum(x, axis_name, *[, axis_index_groups])

在 pmapped 轴 axis_name 上计算 x 的 all-reduce 总和。

psum_scatter(x, axis_name, *[, ...])

类似于 psum(x, axis_name),但每个设备仅保留部分结果。

pmax(x, axis_name, *[, axis_index_groups])

在 pmapped 轴 axis_name 上计算 x 的 all-reduce 最大值。

pmin(x, axis_name, *[, axis_index_groups])

在 pmapped 轴 axis_name 上计算 x 的 all-reduce 最小值。

pmean(x, axis_name, *[, axis_index_groups])

在 pmapped 轴 axis_name 上计算 x 的 all-reduce 平均值。

ppermute(x, axis_name, perm)

根据排列 perm 执行集合排列。

pshuffle(x, axis_name, perm)

jax.lax.ppermute 的便捷封装,带有备用排列编码

pswapaxes(x, axis_name, axis, *[, ...])

将 pmapped 轴 axis_name 与未映射轴 axis 交换。

axis_index(axis_name)

返回沿映射轴 axis_name 的索引。

线性代数运算符 (jax.lax.linalg)#

cholesky(x, *[, symmetrize_input])

Cholesky 分解。

eig(x, *[, compute_left_eigenvectors, ...])

一般矩阵的特征分解。

eigh(x, *[, lower, symmetrize_input, ...])

厄米特矩阵的特征分解。

hessenberg(a)

将一个方阵缩减为上Hessenberg形式。

lu(x)

带有部分选主的LU分解。

householder_product(a, taus)

基本Householder反射器的乘积。

qdwh(x, *[, is_hermitian, max_iterations, ...])

用于极分解的基于QR的动态加权Halley迭代。

qr()

QR分解。

schur(x, *[, compute_schur_vectors, ...])

svd()

奇异值分解。

triangular_solve(a, b, *[, left_side, ...])

三角解法。

tridiagonal(a, *[, lower])

将对称/厄米特矩阵缩减为三对角形式。

tridiagonal_solve(dl, d, du, b)

计算三对角线性系统的解。

参数类#

class jax.lax.ConvDimensionNumbers(lhs_spec, rhs_spec, out_spec)[source]#

描述卷积的批次、空间和特征维度。

参数:
  • lhs_spec (Sequence[int]) – 一个非负整数维度数字的元组,包含(批次维度,特征维度,空间维度...)

  • rhs_spec (Sequence[int]) – 一个非负整数维度数字的元组,包含(输出特征维度,输入特征维度,空间维度...)

  • out_spec (Sequence[int]) – 一个非负整数维度数字的元组,包含(批次维度,特征维度,空间维度...)

jax.lax.ConvGeneralDilatedDimensionNumbers#

tuple[str, str, str] | ConvDimensionNumbers | None 的别名

class jax.lax.DotAlgorithm(lhs_precision_type, rhs_precision_type, accumulation_type, lhs_component_count=1, rhs_component_count=1, num_primitive_operations=1, allow_imprecise_accumulation=False)[source]#

指定用于计算点积的算法。

当用于指定 dot()dot_general() 和其他点积函数的 precision 输入时,此数据结构用于控制用于计算点积的算法的属性。此 API 控制计算所用的精度,并允许用户访问特定于硬件的加速。

对这些算法的支持取决于平台,并且当编译计算时,使用不支持的算法将引发 Python 异常。在至少某些平台上已知支持的算法在 DotAlgorithmPreset 枚举中列出,并且这些是尝试此 API 的良好起点。

“点算法”由以下参数指定

  • lhs_precision_typerhs_precision_type,操作的 LHS 和 RHS 四舍五入到的数据类型。

  • accumulation_type 用于累加的数据类型。

  • lhs_component_countrhs_component_countnum_primitive_operations 适用于将 LHS 和/或 RHS 分解为多个组件并在这些值上执行多个操作的算法,通常是为了模拟更高的精度。对于没有分解的算法,这些值应设置为 1

  • allow_imprecise_accumulation 用于指定是否允许在某些步骤中以较低的精度进行累加(例如,CUBLASLT_MATMUL_DESC_FAST_ACCUM)。

StableHLO 规范 对于点操作不要求精度类型与输入或输出的存储类型相同,但是某些平台可能要求这些类型匹配。此外,如果指定,dot_general() 的返回类型始终由输入算法的 accumulation_type 参数定义。

示例

使用 32 位浮点累加器累加两个 16 位浮点数

>>> algorithm = DotAlgorithm(
...     lhs_precision_type=np.float16,
...     rhs_precision_type=np.float16,
...     accumulation_type=np.float32,
... )
>>> lhs = jnp.array([1.0, 2.0, 3.0, 4.0], dtype=np.float16)
>>> rhs = jnp.array([1.0, 2.0, 3.0, 4.0], dtype=np.float16)
>>> dot(lhs, rhs, precision=algorithm)  
array([ 1.,  4.,  9., 16.], dtype=float16)

或者,等效地,使用预设

>>> algorithm = DotAlgorithmPreset.F16_F16_F32
>>> dot(lhs, rhs, precision=algorithm)  
array([ 1.,  4.,  9., 16.], dtype=float16)

也可以按名称指定预设

>>> dot(lhs, rhs, precision="F16_F16_F32")  
array([ 1.,  4.,  9., 16.], dtype=float16)

preferred_element_type 参数可用于在不向下转换累积类型的情况下返回输出

>>> dot(lhs, rhs, precision="F16_F16_F32", preferred_element_type=np.float32)  
array([ 1.,  4.,  9., 16.], dtype=float32)
参数:
  • lhs_precision_type (DTypeLike)

  • rhs_precision_type (DTypeLike)

  • accumulation_type (DTypeLike)

  • lhs_component_count (int)

  • rhs_component_count (int)

  • num_primitive_operations (int)

  • allow_imprecise_accumulation (bool)

class jax.lax.DotAlgorithmPreset(value)[source]#

用于计算点积的已知算法的枚举。

Enum 提供了一组命名的 DotAlgorithm 对象,已知至少在平台上受支持。有关这些算法行为的更多详细信息,请参阅 DotAlgorithm 文档。

在调用 dot()dot_general() 或大多数其他 JAX 点积函数时,可以通过传递此 Enum 的成员或其名称作为字符串,并使用 precision 参数,从此列表中选择一个算法。

例如,用户可以使用此 Enum 直接指定预设

>>> lhs = jnp.array([1.0, 2.0, 3.0, 4.0], dtype=np.float16)
>>> rhs = jnp.array([1.0, 2.0, 3.0, 4.0], dtype=np.float16)
>>> algorithm = DotAlgorithmPreset.F16_F16_F32
>>> dot(lhs, rhs, precision=algorithm)  
array([ 1.,  4.,  9., 16.], dtype=float16)

或者,等效地,可以按名称指定它们

>>> dot(lhs, rhs, precision="F16_F16_F32")  
array([ 1.,  4.,  9., 16.], dtype=float16)

预设的名称通常为 LHS_RHS_ACCUM,其中 LHSRHS 分别是 lhsrhs 输入的元素类型,ACCUM 是累加器的元素类型。某些预设有额外的后缀,并且每个后缀的含义在下面记录。支持的预设是

DEFAULT = 1#

将根据输入和输出类型选择算法。

ANY_F8_ANY_F8_F32 = 2#

接受任何 float8 输入类型,并累积到 float32 中。

ANY_F8_ANY_F8_F32_FAST_ACCUM = 3#

类似于 ANY_F8_ANY_F8_F32,但使用更快的累积,代价是精度较低。

ANY_F8_ANY_F8_ANY = 4#

类似于 ANY_F8_ANY_F8_F32,但累积类型由 preferred_element_type 控制。

ANY_F8_ANY_F8_ANY_FAST_ACCUM = 5#

类似于 ANY_F8_ANY_F8_F32_FAST_ACCUM,但累积类型由 preferred_element_type 控制。

F16_F16_F16 = 6#
F16_F16_F32 = 7#
BF16_BF16_BF16 = 8#
BF16_BF16_F32 = 9#
BF16_BF16_F32_X3 = 10#

_X3 后缀表示该算法使用 3 次运算来模拟更高的精度。

BF16_BF16_F32_X6 = 11#

类似于 BF16_BF16_F32_X3,但使用 6 次运算而不是 3 次。

TF32_TF32_F32 = 12#
TF32_TF32_F32_X3 = 13#

_X3 后缀表示该算法使用 3 次运算来模拟更高的精度。

F32_F32_F32 = 14#
F64_F64_F64 = 15#
property supported_lhs_types: tuple[DTypeLike, ...] | None[源代码]#
property supported_rhs_types: tuple[DTypeLike, ...] | None[源代码]#
property accumulation_type: DTypeLike | None[源代码]#
supported_output_types(lhs_dtype, rhs_dtype)[源代码]#
参数:
  • lhs_dtype (DTypeLike)

  • rhs_dtype (DTypeLike)

返回类型:

tuple[DTypeLike, …] | None

class jax.lax.FftType(value)[源代码]#

描述要执行的 FFT 操作。

FFT = 0#

正向复数到复数的 FFT。

IFFT = 1#

反向复数到复数的 FFT。

IRFFT = 3#

反向实数到复数的 FFT。

RFFT = 2#

正向实数到复数的 FFT。

class jax.lax.GatherDimensionNumbers(offset_dims, collapsed_slice_dims, start_index_map, operand_batching_dims=(), start_indices_batching_dims=())[源代码]#

描述 XLA 的 Gather 运算符的维度编号参数。有关维度编号含义的更多详细信息,请参阅 XLA 文档。

参数:
  • offset_dims (tuple[int, ...]) – gather 输出中偏移到从 operand 切片的数组中的维度集合。必须是一个按升序排列的整数元组,每个整数表示输出的维度编号。

  • collapsed_slice_dims (tuple[int, ...]) – operandslice_sizes[i] == 1 且在 gather 输出中不应具有对应维度的维度 i 的集合。必须是一个按升序排列的整数元组。

  • start_index_map (tuple[int, ...]) – 对于 start_indices 中的每个维度,给出 operand 中要切片的对应维度。必须是大小等于 start_indices.shape[-1] 的整数元组。

  • operand_batching_dims (tuple[int, ...]) – operandslice_sizes[i] == 1 且在 start_indices(在 start_indices_batching_dims 中的相同索引处)和 gather 输出中都应具有对应维度的批处理维度 i 的集合。必须是一个按升序排列的整数元组。

  • start_indices_batching_dims (tuple[int, ...]) – start_indices 中批量处理维度 i 的集合,这些维度应在 operand (在 operand_batching_dims 中对应的索引处)和 gather 的输出中都有相应的维度。必须是整数的元组(顺序根据与 operand_batching_dims 的对应关系确定)。

与 XLA 的 GatherDimensionNumbers 结构不同,index_vector_dim 是隐式的;始终存在索引向量维度,并且它必须始终是最后一个维度。要收集标量索引,请添加大小为 1 的尾随维度。

class jax.lax.GatherScatterMode(value)[source]#

描述如何在 gather 或 scatter 中处理越界索引。

可能的值为

CLIP

索引将被钳制到最近的有效值,即,使得要 gather 的整个窗口都在范围内。

FILL_OR_DROP

如果要 gather 的窗口的任何部分超出范围,则返回的整个窗口(即使是原本在范围内的元素)都将填充一个常量。如果 scatter 的窗口的任何部分超出范围,则将丢弃整个窗口。

PROMISE_IN_BOUNDS

用户承诺索引在范围内。不会执行额外的检查。实际上,在当前的 XLA 实现中,这意味着越界的 gather 将被钳制,但越界的 scatter 将被丢弃。如果索引超出范围,则梯度将不正确。

class jax.lax.Precision(value)[source]#

用于 lax 矩阵乘法相关函数的精度枚举。

JAX 函数的设备相关 precision 参数通常控制加速器后端(即 TPU 和 GPU)上数组计算的速度和准确性之间的权衡。对 CPU 后端没有影响。这仅对 float32 计算有影响,并且不会影响输入/输出数据类型。成员是

DEFAULT

最快的模式,但最不准确。在 TPU 上:以 bfloat16 执行 float32 计算。在 GPU 上:如果可用,则使用 tensorfloat32(例如,在 A100 和 H100 GPU 上),否则使用标准 float32(例如,在 V100 GPU 上)。别名:'default''fastest'

HIGH

较慢但更准确。在 TPU 上:以 3 次 bfloat16 传递执行 float32 计算。在 GPU 上:在可用时使用 tensorfloat32,否则使用 float32。别名: 'high'

HIGHEST

最慢但最准确。在 TPU 上:以 6 次 bfloat16 执行 float32 计算。别名:'highest'。在 GPU 上:使用 float32。

jax.lax.PrecisionLike#

别名:None | str | Precision | tuple[str, str] | tuple[Precision, Precision] | DotAlgorithm | DotAlgorithmPreset

class jax.lax.RandomAlgorithm(value)[source]#

描述用于 rng_bit_generator 的 PRNG 算法。

RNG_DEFAULT = 0#

平台的默认算法。

RNG_THREE_FRY = 1#

Threefry-2x32 PRNG 算法。

RNG_PHILOX = 2#

Philox-4x32 PRNG 算法。

class jax.lax.RoundingMethod(value)[source]#

用于处理 jax.lax.round() 中间值(例如,0.5)的舍入策略。

AWAY_FROM_ZERO = 0#

将中间值四舍五入远离零(例如,0.5 -> 1,-0.5 -> -1)。

TO_NEAREST_EVEN = 1#

将中间值四舍五入到最近的偶数整数。这也被称为“银行家舍入”(例如,0.5 -> 0,1.5 -> 2)。

class jax.lax.ScatterDimensionNumbers(update_window_dims, inserted_window_dims, scatter_dims_to_operand_dims, operand_batching_dims=(), scatter_indices_batching_dims=())[source]#

描述 XLA 的 Scatter 运算符的维度编号参数。 有关维度编号含义的更多详细信息,请参阅 XLA 文档。

参数:
  • update_window_dims (Sequence[int]) – updates 中作为窗口维度的一组维度。必须是按升序排列的整数元组,每个整数代表一个维度编号。

  • inserted_window_dims (Sequence[int]) – 必须插入到 updates 形状中的大小为 1 的窗口维度集。必须是按升序排列的整数元组,每个整数代表输出的维度编号。在 gather 的情况下,这些是 collapsed_slice_dims 的镜像。

  • scatter_dims_to_operand_dims (Sequence[int]) – 对于 scatter_indices 中的每个维度,给出 operand 中对应的维度。必须是大小等于 scatter_indices.shape[-1] 的整数序列。

  • operand_batching_dims (Sequence[int]) – operand 中应该在 scatter_indices (对应 scatter_indices_batching_dims 中相同索引的位置) 和 updates 中都有对应维度的批处理维度 i 的集合。 必须是一个按升序排列的整数元组。在 gather 的情况下,这些是 operand_batching_dims 的镜像。

  • scatter_indices_batching_dims (Sequence[int]) – scatter_indices 中应该在 operand (对应 operand_batching_dims 中相同索引的位置) 和 gather 的输出中都有对应维度的批处理维度 i 的集合。 必须是一个整数元组 (顺序根据与 input_batching_dims 的对应关系固定)。在 gather 的情况下,这些是 start_indices_batching_dims 的镜像。

与 XLA 的 ScatterDimensionNumbers 结构不同,index_vector_dim 是隐式的;始终存在一个索引向量维度,并且它必须始终是最后一个维度。要分散标量索引,请添加大小为 1 的尾随维度。