Documentation
¶
Overview ¶
Package distributed defines the following objects related to cross-device execution:
- DeviceMesh: expresses the topology of a set of devices, in terms of axis and their sizes. - ShardingSpec: defines how a logical tensor is sharded across a DeviceMesh.
These are based on XLA Shardy, but could potentially be used by other backends implementing a similar approach to distribution.
Index ¶
- Constants
- Variables
- func IsNameValid(name string) bool
- func StrategyStrings() []string
- type AxisSpec
- 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) SetName(name string)
- func (m *DeviceMesh) String() string
- func (m *DeviceMesh) ToBackendsMesh() compute.Mesh
- type ShardingSpec
- func (s *ShardingSpec) IsReplicated() bool
- func (s *ShardingSpec) LogicalShapeForShard(shardShape shapes.Shape) shapes.Shape
- func (s *ShardingSpec) NumDevicesShardingAxis(axis int) int
- func (s *ShardingSpec) Rank() int
- func (s *ShardingSpec) ShardShape(logicalShape shapes.Shape) shapes.Shape
- func (s *ShardingSpec) String() string
- func (s *ShardingSpec) ToBackendsSpec() *compute.ShardingSpec
- func (s *ShardingSpec) Validate() error
- type SpecBuilder
- type Strategy
Constants ¶
const DefaultMeshName = "mesh"
Variables ¶
var ReplicatedAxis = AxisSpec(nil)
ReplicatedAxis is a special AxisSpec that means the tensor axis is replicated.
Functions ¶
func IsNameValid ¶
IsNameValid checks whether a name is a valid identifier for a mesh name or axis name.
func StrategyStrings ¶
func StrategyStrings() []string
StrategyStrings returns a slice of all String values of the enum
Types ¶
type AxisSpec ¶
type AxisSpec []string
AxisSpec specifies how a tensor axis is to be sharded (or replicated). See details in ShardingSpec.
It's a list of mesh axes names, in order. An empty list means the axis is replicated.
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(axesSizes []int, axesNames []string) (*DeviceMesh, error)
NewDeviceMesh creates a new logical topology of a set of devices.
- 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).
Used by ShardingSpec to describe how inputs and outputs are sharded, when using AutoSharding distribution strategy. There can be more than one mesh organization in the same computation.
For the experimental "SPMD" Strategy the axesSizes should be 1D, e.g., NewDeviceMesh([]int{8}, []string{"replica"}).
A DeviceMesh can also be assigned a name, but because there is usually only one mesh, it's set to the default name "mesh" (DefaultMeshName).
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) 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) ToBackendsMesh ¶
func (m *DeviceMesh) ToBackendsMesh() compute.Mesh
ToBackendsMesh converts mesh to a compute.Mesh.
type ShardingSpec ¶
type ShardingSpec struct {
Mesh *DeviceMesh
Axes []AxisSpec
}
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(backend, []int{2, 2}, []string{"data", "model"})
// Input's "batch" axis is sharded across the "data" axis of the mesh.
inputSharding := distributed.NewShardingSpec(mesh, 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 NewReplicatedShardingSpec ¶
func NewReplicatedShardingSpec(mesh *DeviceMesh) *ShardingSpec
NewReplicatedShardingSpec creates a new ShardingSpec that is replicated across all mesh axes. It's the simplest sharding spec.
func NewShardingSpec ¶
func NewShardingSpec(mesh *DeviceMesh, axisSpec ...AxisSpec) (*ShardingSpec, error)
NewShardingSpec creates a new ShardingSpec for a tensor, defined over the given mesh axes.
It takes an axisSpec for each axis of the tensor (omitted axes are assumed to be replicated).
There is also the BuildSpec function for a more ergonomic spec creation.
func (*ShardingSpec) IsReplicated ¶
func (s *ShardingSpec) IsReplicated() bool
IsReplicated returns true if the tensor is fully replicated (i.e., not sharded along any axis).
func (*ShardingSpec) LogicalShapeForShard ¶
func (s *ShardingSpec) LogicalShapeForShard(shardShape shapes.Shape) shapes.Shape
LogicalShapeForShard calculates the logical shape of a tensor given its shard shape and the sharding specification.
The shard shape is assumed to be the shape of the tensor on a single device. The logical shape is the shape of the full tensor across all devices.
If the sharding spec is nil, or if the rank of the shard shape does not match the spec, it returns the shard shape as is (assuming it's replicated or fully local).
func (*ShardingSpec) NumDevicesShardingAxis ¶
func (s *ShardingSpec) NumDevicesShardingAxis(axis int) int
NumDevicesShardingAxis returns the number of devices that will be used to shard the tensor along the given tensor axis. If the axis is replicated, it returns 1.
Notice this is about the tensor axis, not the mesh axis. A tensor axis can be sharded across multiple mesh axes.
func (*ShardingSpec) Rank ¶
func (s *ShardingSpec) Rank() int
Rank returns the rank of the tensor this ShardingSpec describes.
func (*ShardingSpec) ShardShape ¶
func (s *ShardingSpec) ShardShape(logicalShape shapes.Shape) shapes.Shape
ShardShape calculates the shard shape of a tensor given its logical shape and the sharding specification.
The logical shape is the shape of the full tensor across all devices. The shard shape is the shape of the tensor on a single device.
If the sharding spec is nil, or if the rank of the logical shape does not match the spec, it returns the logical shape as is (assuming it's replicated or fully local).
If the logical shape is not divisible by the sharding spec, it returns an invalid shape.
func (*ShardingSpec) String ¶
func (s *ShardingSpec) String() string
String returns a human-readable string representation of the ShardingSpec. Returns "<nil>" if s is nil.
func (*ShardingSpec) ToBackendsSpec ¶
func (s *ShardingSpec) ToBackendsSpec() *compute.ShardingSpec
ToBackendsSpec converts the ShardingSpec to a compute.ShardingSpec. It works if the ShardingSpec is nil as well.
func (*ShardingSpec) Validate ¶
func (s *ShardingSpec) Validate() error
Validate the spec returning an error if something is invalid.
type SpecBuilder ¶
type SpecBuilder struct {
// contains filtered or unexported fields
}
SpecBuilder is a more ergonomic way of building SharingSpec.
func BuildSpec ¶
func BuildSpec(mesh *DeviceMesh) *SpecBuilder
BuildSpec is a more ergonomic way of building SharingSpec.
Example:
spec, err := distributed.BuildSpec(mesh).R().S("model").Done()
func (*SpecBuilder) Done ¶
func (b *SpecBuilder) Done() (*ShardingSpec, error)
Done builds the ShardingSpec according to the builder specification.
func (*SpecBuilder) R ¶
func (b *SpecBuilder) R() *SpecBuilder
R adds a replicated axis to the ShardingSpec being built.
func (*SpecBuilder) S ¶
func (b *SpecBuilder) S(meshAxes ...string) *SpecBuilder
S adds a sharded axis along the meshAxes to the ShardingSpec being built.
type Strategy ¶
type Strategy int
Strategy is an enumeration of specific strategies for distributed execution.
A graph.Graph is always associated with a single strategy.
const ( // None is the default strategy: there is no strategy, usually associated with single-device execution. // If the user is doing distributed execution with this strategy, they are handling all the details themselves. None Strategy = iota // SPMD is a simple strategy for single-program, multiple-data (SPMD) execution. // There will be a DeviceMesh with one axis with the participating devices, and the inputs to the // execution are expected to either be dtensor.Tensor or slices of values (of the Go any type), one per device. // // The abstraction "leaks", meaning the user needs to be aware of what is going on, and axes that are sharded. // For instance, a BatchNormalization implementation should check if this is the strategy it is running in, // and if so, do a CrossReplicaReduceSum on the batch axis when computing the mean and variance. SPMD // AutoSharding is a strategy where one provides sharding specs for the inputs and outputs (and optionally // hints in the middle of the computation), and the backend will automatically shard the computation as it // sees fit. // // The underlying auto-sharding algorithm is described in the paper "AutoSharding: General and Scalable // Parallelization for ML Computation Graphs" [1]. And this strategy relies on XLA Shardy [2], so it's only // available with the `xla` (`stablehlo`) backend. It's the same technology used by Jax and Pytorch XLA. // // This strategy works as follows: // // - Computation is written as if it was a single program, with no "collective" operations. It will be // automatically partitioned into multiple programs by the underlying backend (XLA Shardy). // - The inputs are dtensor.Tensor, and their sharding helps guide the sharding of the computation. // // [1] https://arxiv.org/abs/2105.04663 // [2] https://openxla.org/shardy/ AutoSharding )
func StrategyString ¶
StrategyString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.
func StrategyValues ¶
func StrategyValues() []Strategy
StrategyValues returns all values of the enum
func (Strategy) IsAStrategy ¶
IsAStrategy returns "true" if the value is listed in the enum definition. "false" otherwise