TorchLean API

NN.Proofs.Autograd.Runtime.ShapeErasure

Shape Erasure #

Bridge lemmas between the proved typed context (TensorPack) and the runtime tape representation (Spec.SomeTensor stored in Arrays).

The conversion preserves order and stores each erased shape in the resulting SomeTensor.

theorem TorchLean.TensorPack.mem_toIndexedShapeErasedArray_lt {α : Type} [Storage α] {ss : List Spec.Shape} (ts : TensorPack α ss) (start : ) {pid : } {pg : Spec.SomeTensor α} :
(pid, pg) ts.toIndexedShapeErasedArray startpid < start + ss.length

Every (pid, tensor) produced by toIndexedShapeErasedArray ts start satisfies pid < start + ss.length.

This is bookkeeping used to prove runtime backward only references earlier nodes.

@[simp]
theorem TorchLean.TensorPack.toShapeErasedArray_cast {α : Type} [Storage α] {ss₁ ss₂ : List Spec.Shape} (h : ss₁ = ss₂) (xs : TensorPack α ss₁) :

toShapeErasedArray ignores type-level casts of the shape list.

@[simp]

toShapeErasedArray has the same size as the underlying shape list.

Recovering a typed pack ignores array entries before and after its encoded segment.

@[simp]

Recovering a typed pack after an untouched array prefix and shape erasure is lossless.

@[simp]

Recovering the expected typed pack immediately after shape erasure is lossless.

Splitting a typed context splits its erased array at the same boundary.

Recover just the typed input prefix of a complete runtime context.

Shape-erasing conversions #

The lemmas below show that these conversions preserve length and order and interact well with TorchLean.TensorPack.snoc and TorchLean.TensorPack.get. They are used later to relate runtime node ids to positions in the typed proof context Γ ++ ss.

@[simp]

toShapeErasedArray commutes with appending a value.

toShapeErasedArray of a cons context is array cons/append of the head element.

theorem TorchLean.TensorPack.get_toShapeErasedArray {α : Type} [Storage α] {ss : List Spec.Shape} (xs : TensorPack α ss) (i : Fin ss.length) :
let arr := xs.toShapeErasedArray; arr[i] = Spec.SomeTensor.ofTensor (xs.get i)

Array lookup through toShapeErasedArray corresponds to TorchLean.TensorPack.get after erasing the result's shape from its type.

This is the key lemma that lets us connect runtime indexing (arr[i]) to proof indexing (get xs i).