jax.export 模块#
jax.export 是一个用于导出和序列化 JAX 函数以便永久存档的库。
请参阅 导出和序列化 文档。
类#
- class jax.export.Exported(fun_name, in_tree, in_avals, out_tree, out_avals, in_shardings_hlo, out_shardings_hlo, nr_devices, platforms, ordered_effects, unordered_effects, disabled_safety_checks, mlir_module_serialized, calling_convention_version, module_kept_var_idx, uses_global_constants, _get_vjp)[source]#
已降级为 StableHLO 的 JAX 函数。
- 参数:
fun_name (str)
in_tree (tree_util.PyTreeDef)
in_avals (tuple[core.ShapedArray, ...])
out_tree (tree_util.PyTreeDef)
out_avals (tuple[core.ShapedArray, ...])
in_shardings_hlo (tuple[HloSharding | None, ...])
out_shardings_hlo (tuple[HloSharding | None, ...])
nr_devices (int)
ordered_effects (tuple[effects.Effect, ...])
unordered_effects (tuple[effects.Effect, ...])
disabled_safety_checks (Sequence[DisabledSafetyCheck])
mlir_module_serialized (bytes)
calling_convention_version (int)
uses_global_constants (bool)
- in_tree#
描述已降级 JAX 函数的(args, kwargs)元组的 PyTreeDef。实际降级不依赖于
in_tree,但可用于使用相同的参数结构调用导出的函数。- 类型:
tree_util.PyTreeDef
- out_tree#
描述已降级 JAX 函数结果的 PyTreeDef。
- 类型:
tree_util.PyTreeDef
- in_shardings_hlo#
扁平化的输入分片,一个长度与
in_avals相同的序列。None表示未指定分片。请注意,这些不包括分片或分片中使用的实际设备。有关将这些转换为可与 JAX API 结合使用的分片规范的方法,请参见in_shardings_jax。- 类型:
tuple[HloSharding | None, …]
- out_shardings_hlo#
扁平化的输出分片,一个长度与
out_avals相同的序列。None表示未指定分片。请注意,这些不包括分片或分片中使用的实际设备。有关将这些转换为可与 JAX API 结合使用的分片规范的方法,请参见out_shardings_jax。- 类型:
tuple[HloSharding | None, …]
- platforms#
包含函数应导出的平台的元组。JAX 的平台集合是开放式的;用户可以添加平台。JAX 内置平台包括:‘tpu’、‘cpu’、‘cuda’、‘rocm’。请参阅 https://jax.net.cn/en/latest/export/export.html#cross-platform-and-multi-platform-export。
- ordered_effects#
序列化模块中存在的有序效应。此项自序列化版本 9 起可用。有关存在有序效应时的调用约定,请参阅 https://jax.net.cn/en/latest/export/export.html#module-calling-convention。
- 类型:
tuple[effects.Effect, …]
- calling_convention_version#
已导出模块调用约定的版本号。有关更多版本信息,请参阅 https://jax.net.cn/en/latest/export/export.html#calling-convention-versions。
- 类型:
- uses_global_constants#
是否
mlir_module_serialized使用形状多态或多平台导出。这可能是因为in_avals包含维度变量,或者由于 Exported 模块的内部调用包含维度变量或平台索引参数。此类模块在 XLA 编译之前需要进行形状细化。- 类型:
- disabled_safety_checks#
已在导出时禁用的安全检查描述符列表。请参阅
DisabledSafetyCheck的文档字符串。- 类型:
Sequence[DisabledSafetyCheck]
- _get_vjp#
一个可选函数,它接收当前的导出的函数并返回导出的 VJP 函数。VJP 函数接收一个扁平的参数列表,从原始参数开始,然后是每个原始输出的余切参数。它返回一个包含对应于扁平化原始输入的余切的元组。
有关
mlir_module()方法的调用约定的描述,请参阅 https://jax.net.cn/en/latest/export/export.html#module-calling-convention。- call(*args, **kwargs)[source]#
从 JAX 程序中调用导出的函数。
- 参数:
args – 要传递给导出的函数的 positional 参数。这应该是一个与函数导出时所用参数具有相同 PyTree 结构的数组的 pytree。
kwargs – 要传递给导出的函数的 keyword 参数。
- Returns: 结果数组的 pytree,其结构与导出的函数的结果相同。
results of the exported function.
调用支持反向模式 AD 以及导出的所有功能:形状多态、多平台、设备多态。请参阅 [JAX export documentation](https://jax.net.cn/en/latest/export/export.html) 中的示例。
- in_shardings_jax(mesh)[source]#
创建与
self.in_shardings_hlo对应的 Shardings。Exported 对象将
in_shardings_hlo存储为 HloShardings,它们独立于分片或设备集。此方法构造的 Sharding 可用于 JAX API,例如jax.jit()或jax.device_put()。示例用法
>>> from jax import export, sharding >>> # Prepare the exported object: >>> exp_mesh = sharding.Mesh(jax.devices(), ("a",)) >>> exp = export.export(jax.jit(lambda x: jax.numpy.add(x, x), ... in_shardings=sharding.NamedSharding(exp_mesh, sharding.PartitionSpec("a"))) ... )(np.arange(jax.device_count())) >>> exp.in_shardings_hlo ({devices=[8]<=[8]},) >>> # Create a mesh for running the exported object >>> run_mesh = sharding.Mesh(jax.devices()[::-1], ("b",)) >>> # Put the args and kwargs on the appropriate devices >>> run_arg = jax.device_put(np.arange(jax.device_count()), ... exp.in_shardings_jax(run_mesh)[0]) >>> res = exp.call(run_arg) >>> res.addressable_shards [Shard(device=CpuDevice(id=7), index=(slice(0, 1, None),), replica_id=0, data=[0]), Shard(device=CpuDevice(id=6), index=(slice(1, 2, None),), replica_id=0, data=[2]), Shard(device=CpuDevice(id=5), index=(slice(2, 3, None),), replica_id=0, data=[4]), Shard(device=CpuDevice(id=4), index=(slice(3, 4, None),), replica_id=0, data=[6]), Shard(device=CpuDevice(id=3), index=(slice(4, 5, None),), replica_id=0, data=[8]), Shard(device=CpuDevice(id=2), index=(slice(5, 6, None),), replica_id=0, data=[10]), Shard(device=CpuDevice(id=1), index=(slice(6, 7, None),), replica_id=0, data=[12]), Shard(device=CpuDevice(id=0), index=(slice(7, 8, None),), replica_id=0, data=[14])]
- 参数:
mesh (mesh_lib.Mesh)
- 返回类型:
Sequence[sharding.Sharding | None]
- out_shardings_jax(mesh)[source]#
创建与
self.out_shardings_hlo对应的 Shardings。请参阅 in_shardings_jax 的文档。
- 参数:
mesh (mesh_lib.Mesh)
- 返回类型:
Sequence[sharding.Sharding | None]
- class jax.export.DisabledSafetyCheck(_impl)[source]#
在(反)序列化时应跳过的安全检查。
其中大多数检查在序列化时执行,但有些则推迟到反序列化。禁用的检查列表会附加到序列化中,例如作为
jax.export.Exported的字符串属性序列,或者tf.XlaCallModuleOp的字符串属性序列。使用 jax2tf 时,可以通过传递
TF_XLA_FLAGS=--tf_xla_call_module_disabled_checks=platform来禁用更多反序列化安全检查。- 参数:
_impl (str)
- classmethod custom_call(target_name)[source]#
允许序列化一个尚不确定稳定的调用目标。
仅在序列化时生效。 :param target_name: 要允许的自定义调用目标的名称。
- 参数:
target_name (str)
- 返回类型:
函数#
|
导出 JAX 函数以进行持久化序列化。 |
|
反序列化一个 Exported 对象。 |
int([x]) -> integer int(x, base=10) -> integer |
|
int([x]) -> integer int(x, base=10) -> integer |
|
检索默认导出平台。 |
|
|
注册自定义 PyTree 节点以进行序列化和反序列化。 |
|
注册一个 namedtuple 以进行序列化和反序列化。 |