shardy

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 14, 2025 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package shardy provides the types needed to define a distributed computation topology. This is used for leveraging XLA Shardy [1]

[1] https://github.com/openxla/shardy

Index

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 MeshAxisSpec struct {
	AxisName string

	// PreSize, Size are only set if defining a sub-axis of the mesh.
	PreSize, Size int
}

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):

  1. The tensor can also be sharded across mesh "sub-axes" -- seed detailed documentation in [1]
  2. 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL