TorchLean API

NN.Tensor.Internal.Lowering.Rearrange

Primitive lowering for rearrange #

A checked rearrange lowers directly to three tensor operations:

  1. reshape physical input groups into elementary input axes;
  2. reindex those axes into output order;
  3. reshape elementary output axes into physical output groups.

The definitions operate on the existing checked transformation rather than introducing an operator-specific program wrapper. The main theorem proves that this explicit tensor program equals the independent coordinate denotation for every scalar type and every valid checked rearrange.

A checked input shape has the same size as its elementary-axis shape.

A checked output shape has the same size as its elementary-axis shape.

theorem TorchLean.Tensor.Internal.AxisTuple.groupedCoordEquivOfEq_eq_reshapeCoordEquiv {ι : Type u} {shape : Shape} (length : ι) (groups : List (List ι)) (hShape : groupedShape length groups = shape) :
groupedCoordEquivOfEq length groups hShape = (Rep.reshapeCoordEquiv ).symm.trans (coordEquiv length groups.flatten)

Ungrouping certified tensor axes is row-major reshape followed by conversion to a named elementary-axis tuple.

Converting an input tensor coordinate to named elementary axes factors through the row-major reshape used by lowering.

Converting an output tensor coordinate to named elementary axes factors through the row-major reshape used by lowering.

Execute the explicit reshape, axis permutation, and reshape stages.

Instances For
    theorem TorchLean.Tensor.Internal.Lowering.rearrangeTensor_correct {α : Type u} [Storage α] (checked : Check.CheckedTransform) (hKind : checked.value.normalized.kind = Check.TransformKind.rearrange) (inputTensor : checked.InputTensor α) :
    rearrangeTensor checked hKind inputTensor = Semantics.denoteRearrange checked hKind inputTensor

    Primitive rearrange lowering is correct for every scalar type and input tensor.