Documentation
¶
Overview ¶
Package shardy provides the types needed to define a distributed computation topology. This is used for leveraging XLA Shardy [1]
Index ¶
- type DeviceMesh
- func (m *DeviceMesh) AxesNames() []string
- func (m *DeviceMesh) AxesSizes() []int
- func (m *DeviceMesh) AxisSize(axisName string) (int, error)
- func (m *DeviceMesh) ComputeReplicaGroups(axes []string) ([][]int, error)
- func (m *DeviceMesh) LogicalDeviceAssignment() []int
- func (m *DeviceMesh) Name() string
- func (m *DeviceMesh) NumDevices() int
- func (m *DeviceMesh) Rank() int
- func (m *DeviceMesh) SetLogicalDeviceAssignment(devices ...int) error
- func (m *DeviceMesh) String() string
- func (m *DeviceMesh) ToStableHLO() string
- type MeshAxisSpec
- type ShardingSpec
- func (s *ShardingSpec) AddReplicated() *ShardingSpec
- func (s *ShardingSpec) AddShardedAxis(meshAxesNames ...string) *ShardingSpec
- func (s *ShardingSpec) IsReplicated() bool
- func (s *ShardingSpec) Rank() int
- func (s *ShardingSpec) ToStableHLO() string
- func (s *ShardingSpec) ToValueAttribute(shape shapes.Shape) string
- func (s *ShardingSpec) Validate() error
- func (s *ShardingSpec) ValidateShape(shape shapes.Shape) error
- type TensorAxisSpec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DeviceMesh ¶
type DeviceMesh struct {
// contains filtered or unexported fields
}
DeviceMesh defines the logical topology of a set of devices on a backend.
func NewDeviceMesh ¶
func NewDeviceMesh(name string, axesSizes []int, axesNames []string) (*DeviceMesh, error)
NewDeviceMesh creates a new logical topology of a set of devices.
- name: the name of the mesh, it must be a valid StableHLO identifier (see stablehlo.NormalizeIdentifier).
- axesSizes: defines the number of devices along each mesh axis, one value per axis.
- axesNames: the names of the mesh axes. One value per axis. They must also be valid StableHLO identifiers (see stablehlo.NormalizeName).
The default mapping of logical devices numbers to the mesh is sequential, starting from 0, but it can be changed with the DeviceMesh.SetLogicalDeviceAssignment() method.
func (*DeviceMesh) AxesNames ¶
func (m *DeviceMesh) AxesNames() []string
AxesNames returns a copy of the mesh's axis names.
func (*DeviceMesh) AxesSizes ¶
func (m *DeviceMesh) AxesSizes() []int
AxesSizes returns a copy of the mesh's axesSizes.
func (*DeviceMesh) AxisSize ¶
func (m *DeviceMesh) AxisSize(axisName string) (int, error)
AxisSize returns the number of devices along the given mesh axis.
func (*DeviceMesh) ComputeReplicaGroups ¶
func (m *DeviceMesh) ComputeReplicaGroups(axes []string) ([][]int, error)
ComputeReplicaGroups returns the replica groups participating in some collective (distributed) operation given the axes along which the operation is performed.
Each replica group (a []int) includes the device indices (from the LogicalDeviceAssignment) for the axes specified. The other axes will be split into different replica groups.
Example:
m := NewDeviceMesh([]int{2, 2}, []string{"batch", "data"})
batchGroups, _ := m.ComputeReplicaGroups([]string{"batch"}) // -> [][]int{{0, 2}, {1, 3}}
dataGroups, _ := m.ComputeReplicaGroups([]string{"data"}) // -> [][]int{{0, 1}, {2, 3}}
globalGroups, _ := m.ComputeReplicaGroups([]string{"batch", "data"}) // -> [][]int{{0, 1, 2, 3}}
func (*DeviceMesh) LogicalDeviceAssignment ¶
func (m *DeviceMesh) LogicalDeviceAssignment() []int
LogicalDeviceAssignment returns the list of devices in the mesh, in the order they appear in the mesh.
It can return nil if no assignment was set with SetLogicalDeviceAssignment() -- in which case it will default to a sequential assignment starting from 0.
func (*DeviceMesh) Name ¶
func (m *DeviceMesh) Name() string
func (*DeviceMesh) NumDevices ¶
func (m *DeviceMesh) NumDevices() int
NumDevices returns the total number of devices in the mesh.
func (*DeviceMesh) Rank ¶
func (m *DeviceMesh) Rank() int
Rank returns the number of axes in the mesh.
func (*DeviceMesh) SetLogicalDeviceAssignment ¶
func (m *DeviceMesh) SetLogicalDeviceAssignment(devices ...int) error
SetLogicalDeviceAssignment sets the assignment of logical devices to the mesh.
The length of devices must be equal to NumDevices(). And it should include all numbers from 0 to NumDevices()-1.
It returns an error if logicalDeviceAssignment has invalid device numbers or len(devices) != NumDevices().
func (*DeviceMesh) String ¶
func (m *DeviceMesh) String() string
String implements the fmt.Stringer interface.
func (*DeviceMesh) ToStableHLO ¶
func (m *DeviceMesh) ToStableHLO() string
ToStableHLO returns the StableHLO representation of the mesh, as it should be used in the module body. E.g.: sdy.mesh @mesh = <["data"=4, "model"=2]>
type MeshAxisSpec ¶
type ShardingSpec ¶
type ShardingSpec struct {
Mesh *DeviceMesh
Axes []TensorAxisSpec
}
ShardingSpec (also known as PartitionSpec in JAX) defines how a logical tensor is to be sharded (partitioned) across a DeviceMesh. This is used by Shardy, and is based on its documentation in [1].
The definition is per axis of the logical tensor -- and not per axis of the Mesh, a common confusion. If not all axes of the Tensor are defined, the tail axes are considered simply to be replicated across the whole mesh.
Each tensor axis can be replicated or sharded across one or more mesh axes.
Example:
mesh := NewDeviceMesh("my_mesh", []int{2, 2}, []string{"data", "model"})
// Input's "batch" axis is sharded across the "data" axis of the mesh.
inputSharding := NewShardingSpec(mesh).AddShardedAxis("data")
// First axis is replicated, second is shared across "model" devices
variableSharding := NewShardingSpec(mesh).AddReplicated().AddShardedAxis("model")
// Second axis is sharded across both "data" and "model" devices.
largeWeights := NewShardingSpec(mesh).AddReplicated().AddShardedAxis("data", "model")
There are two advanced features supported but not tested (pls if you need let us know how it goes, or if you find any issues):
- The tensor can also be sharded across mesh "sub-axes" -- seed detailed documentation in [1]
- If using ShardingSpec for hints, instead of mesh axes one can give an "open" (in StableHLO marked as "?") axis, with the semantics that XLA Shardy can choose any mesh axis (or axes) to shard the tensor. See [1].
[1] https://github.com/openxla/shardy/blob/main/docs/sharding_representation.md
func NewShardingSpec ¶
func NewShardingSpec(mesh *DeviceMesh) *ShardingSpec
NewShardingSpec creates a new ShardingSpec.
func (*ShardingSpec) AddReplicated ¶
func (s *ShardingSpec) AddReplicated() *ShardingSpec
AddReplicated adds a new replicated axis to the ShardingSpec.
It returns itself, so calls can be chained.
func (*ShardingSpec) AddShardedAxis ¶
func (s *ShardingSpec) AddShardedAxis(meshAxesNames ...string) *ShardingSpec
AddShardedAxis adds a new sharded axis to the ShardingSpec using one or more mesh axes.
It returns itself, so calls can be chained.
func (*ShardingSpec) IsReplicated ¶
func (s *ShardingSpec) IsReplicated() bool
IsReplicated returns true if the tensor is fully replicated (i.e., not sharded along any axis and not marked as "open").
func (*ShardingSpec) Rank ¶
func (s *ShardingSpec) Rank() int
Rank returns the number of axes this ShardingSpec describes.
Notice this may be smaller than the rank of the tensor using it: if a tensor axis is not defined in ShardingSpec, it is assumed to be replicated.
func (*ShardingSpec) ToStableHLO ¶
func (s *ShardingSpec) ToStableHLO() string
ToStableHLO converts the ShardingSpec to its StableHLO string representation. See details in: https://github.com/openxla/shardy/blob/main/docs/sharding_representation.md
func (*ShardingSpec) ToValueAttribute ¶
func (s *ShardingSpec) ToValueAttribute(shape shapes.Shape) string
ToValueAttribute converts the ShardingSpec to a StableHLO attribute of a value with the given shape.
Notice the rank of the ShardingSpec may be smaller than the rank of the shape, in which case the extra axes are assumed to be replicated (empty).
E.g.: "#sdy.sharding<@mesh, [{\"data\"}, {}]>"
func (*ShardingSpec) Validate ¶
func (s *ShardingSpec) Validate() error
Validate checks that the ShardingSpec is valid for the given mesh.
func (*ShardingSpec) ValidateShape ¶
func (s *ShardingSpec) ValidateShape(shape shapes.Shape) error
type TensorAxisSpec ¶
type TensorAxisSpec struct {
MeshAxes []MeshAxisSpec
Opened bool // If opened to further sharding.
}
TensorAxisSpec specifies how a tensor axis is to be sharded (or replicated). See details in ShardingSpec.
Usually, one would create this using ShardingSpec.AddShardedAxis or ShardingSpec.AddReplicated