jax.sharding
模块#
类#
- class jax.sharding.Sharding#
描述
jax.Array
如何在设备上布局。- addressable_devices_indices_map(global_shape)[源代码]#
从可寻址设备到每个设备包含的数组数据切片的映射。
addressable_devices_indices_map
包含适用于可寻址设备的device_indices_map
的部分。- 参数:
global_shape (Shape)
- 返回类型:
Mapping[Device, Index | None]
- devices_indices_map(global_shape)[源代码]#
返回从设备到每个设备包含的数组切片的映射。
该映射包括所有全局设备,即包括来自其他进程的不可寻址设备。
- 参数:
global_shape (Shape)
- 返回类型:
Mapping[Device, Index]
- is_equivalent_to(other, ndim)[源代码]#
如果两个分片等效,则返回
True
。如果两个分片将相同的逻辑数组分片放置在相同的设备上,则它们是等效的。
例如,如果
NamedSharding
和PositionalSharding
都将数组的相同分片放置在相同的设备上,则它们可能是等效的。
- property is_fully_addressable: bool[源代码]#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- class jax.sharding.SingleDeviceSharding#
基类:
Sharding
一种将其数据放置在单个设备上的
Sharding
。- 参数:
device – 单个
Device
。
示例
>>> single_device_sharding = jax.sharding.SingleDeviceSharding( ... jax.devices()[0])
- devices_indices_map(global_shape)[源代码]#
返回从设备到每个设备包含的数组切片的映射。
该映射包括所有全局设备,即包括来自其他进程的不可寻址设备。
- 参数:
global_shape (Shape)
- 返回类型:
Mapping[Device, Index]
- 属性 is_fully_addressable: bool[源代码]#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- 类 jax.sharding.NamedSharding#
基类:
Sharding
NamedSharding
使用命名轴表示分片。NamedSharding
是一对设备Mesh
和PartitionSpec
,后者描述如何在该网格上对数组进行分片。Mesh
是 JAX 设备的多维 NumPy 数组,其中网格的每个轴都有一个名称,例如'x'
或'y'
。PartitionSpec
是一个元组,其元素可以是None
、一个网格轴或一个网格轴元组。每个元素描述如何跨零个或多个网格维度对输入维度进行分区。例如,PartitionSpec('x', 'y')
表示数据的第一个维度跨网格的x
轴进行分片,第二个维度跨网格的y
轴进行分片。分布式数组和自动并行化(https://jax.ac.cn/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html#namedsharding-gives-a-way-to-express-shardings-with-names)教程有更多详细信息和图表,解释了如何使用
Mesh
和PartitionSpec
。- 参数:
mesh – 一个
jax.sharding.Mesh
对象。spec – 一个
jax.sharding.PartitionSpec
对象。
示例
>>> from jax.sharding import Mesh >>> from jax.sharding import PartitionSpec as P >>> mesh = Mesh(np.array(jax.devices()).reshape(2, 4), ('x', 'y')) >>> spec = P('x', 'y') >>> named_sharding = jax.sharding.NamedSharding(mesh, spec)
- 属性 is_fully_addressable: bool[源代码]#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- 属性 mesh#
(self) -> object
- 属性 spec#
(self) -> object
- 类 jax.sharding.PositionalSharding(devices, *, memory_kind=None)[源代码]#
基类:
Sharding
- 参数:
devices (Sequence[xc.Device] | np.ndarray)
memory_kind (str | None)
- 属性 is_fully_addressable: bool#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- 类 jax.sharding.PmapSharding#
基类:
Sharding
描述了
jax.pmap()
使用的分片。- 类方法 default(shape, sharded_dim=0, devices=None)[源代码]#
创建一个
PmapSharding
,它与jax.pmap()
使用的默认位置相匹配。- 参数:
shape (Shape) – 输入数组的形状。
sharded_dim (int | None) – 输入数组在其上进行分片的维度。默认为 0。
devices (Sequence[xc.Device] | None | None) – 要使用的可选设备序列。如果省略,则使用隐式
used (pmap 使用的设备顺序是) –
jax.local_devices()
。of (即顺序) –
jax.local_devices()
。
- 返回类型:
- 属性 devices#
(self) -> ndarray
- devices_indices_map(global_shape)[源代码]#
返回从设备到每个设备包含的数组切片的映射。
该映射包括所有全局设备,即包括来自其他进程的不可寻址设备。
- 参数:
global_shape (Shape)
- 返回类型:
Mapping[Device, Index]
- is_equivalent_to(other, ndim)[源代码]#
如果两个分片等效,则返回
True
。如果两个分片将相同的逻辑数组分片放置在相同的设备上,则它们是等效的。
例如,如果
NamedSharding
和PositionalSharding
都将数组的相同分片放置在相同的设备上,则它们可能是等效的。- 参数:
self (PmapSharding)
other (PmapSharding)
ndim (int)
- 返回类型:
- 属性 is_fully_addressable: bool#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- shard_shape(global_shape)[源代码]#
返回每个设备上数据的形状。
此函数返回的分片形状是根据
global_shape
和分片的属性计算得出的。- 参数:
global_shape (Shape)
- 返回类型:
Shape
- 属性 sharding_spec#
(self) -> jax::ShardingSpec
- 类 jax.sharding.GSPMDSharding#
基类:
Sharding
- 属性 is_fully_addressable: bool#
此分片是否完全可寻址?
如果当前进程可以寻址在
Sharding
中命名的所有设备,则该分片是完全可寻址的。 在多进程 JAX 中,is_fully_addressable
等效于“is_local”。
- 类 jax.sharding.PartitionSpec(*partitions)[源代码]#
描述如何将数组在设备网格上分区的元组。
每个元素可以是
None
、字符串或字符串元组。有关更多详细信息,请参阅jax.sharding.NamedSharding
的文档。这个类的存在是为了让 JAX 的 pytree 实用程序能够区分分区规范和应视为 pytree 的元组。
- 类 jax.sharding.Mesh(devices, axis_names, *, axis_types=None)[源代码]#
声明此管理器范围内可用的硬件资源。
特别地,所有
axis_names
在托管块内都将成为有效的资源名称,例如可以在jax.experimental.pjit.pjit()
的in_axis_resources
参数中使用。另请参阅 JAX 的多进程编程模型(https://jax.ac.cn/en/latest/multi_process.html)和分布式数组与自动并行化教程(https://jax.ac.cn/en/latest/notebooks/Distributed_arrays_and_automatic_parallelization.html)如果在多个线程中进行编译,请确保
with Mesh
上下文管理器位于线程将执行的函数内部。- 参数:
devices (np.ndarray) – 一个包含 JAX 设备对象的 NumPy ndarray 对象(例如,从
jax.devices()
获取)。axis_names (tuple[MeshAxisName, ...]) – 要分配给
devices
参数维度的资源轴名称序列。它的长度应与devices
的秩匹配。axis_types (MeshAxisType)
示例
>>> from jax.experimental.pjit import pjit >>> from jax.sharding import Mesh >>> from jax.sharding import PartitionSpec as P >>> import numpy as np ... >>> inp = np.arange(16).reshape((8, 2)) >>> devices = np.array(jax.devices()).reshape(4, 2) ... >>> # Declare a 2D mesh with axes `x` and `y`. >>> global_mesh = Mesh(devices, ('x', 'y')) >>> # Use the mesh object directly as a context manager. >>> with global_mesh: ... out = pjit(lambda x: x, in_shardings=None, out_shardings=None)(inp)
>>> # Initialize the Mesh and use the mesh as the context manager. >>> with Mesh(devices, ('x', 'y')) as global_mesh: ... out = pjit(lambda x: x, in_shardings=None, out_shardings=None)(inp)
>>> # Also you can use it as `with ... as ...`. >>> global_mesh = Mesh(devices, ('x', 'y')) >>> with global_mesh as m: ... out = pjit(lambda x: x, in_shardings=None, out_shardings=None)(inp)
>>> # You can also use it as `with Mesh(...)`. >>> with Mesh(devices, ('x', 'y')): ... out = pjit(lambda x: x, in_shardings=None, out_shardings=None)(inp)