TorchLean API

NN.Tensor.Internal.Elab.Native.Pointwise

Certified native pointwise kernels #

Packed scalar types can replace the generic output-building loop with one operation-specific native loop while retaining the ordinary pointwise tensor definition as their proof-visible semantics. Mixed packed byte/float addition also fuses scalar promotion into the output loop.

Reference bodies and their native twins #

Each proof-visible kernel has an @[extern] twin that calls the reference body. A @[csimp] lemma selects the twin during compilation; its borrow annotations match the C entry point. Keeping one Lean body prevents the two models from drifting apart. As with Array.zipWith, buffer operations truncate to the shorter input; tensor callers already have matching lengths by their shapes. The C implementation's agreement with these models remains an FFI trust boundary.

Proof-visible packed floating-point addition.

Instances For
    @[extern torchlean_float_array_add]

    Native packed floating-point addition, compiled to one C loop.

    Instances For
      @[csimp]

      Compile packed floating-point addition to one native loop.

      Proof-visible packed floating-point subtraction.

      Instances For
        @[extern torchlean_float_array_sub]

        Native packed floating-point subtraction, compiled to one C loop.

        Instances For
          @[csimp]

          Compile packed floating-point subtraction to one native loop.

          Proof-visible packed floating-point multiplication.

          Instances For
            @[extern torchlean_float_array_mul]

            Native packed floating-point multiplication, compiled to one C loop.

            Instances For
              @[csimp]

              Compile packed floating-point multiplication to one native loop.

              Proof-visible packed floating-point division.

              Instances For
                @[extern torchlean_float_array_div]

                Native packed floating-point division, compiled to one C loop.

                Instances For
                  @[csimp]

                  Compile packed floating-point division to one native loop.

                  Proof-visible fused byte promotion and floating-point addition.

                  Instances For
                    @[extern torchlean_byte_float_array_add]

                    Native fused byte promotion and floating-point addition.

                    Instances For

                      Proof-visible fused floating-point and promoted-byte addition.

                      Instances For
                        @[extern torchlean_float_byte_array_add]

                        Native fused floating-point and promoted-byte addition.

                        Instances For

                          Add two packed Float tensors in one preallocated native loop.

                          Lean evaluation uses the ordinary packed-array model. Generated code calls torchlean_float_array_add.

                          Instances For

                            Subtract two packed Float tensors in one preallocated native loop.

                            Instances For

                              Multiply two packed Float tensors in one preallocated native loop.

                              Instances For

                                Divide two packed Float tensors in one preallocated native loop.

                                Instances For
                                  def TorchLean.Tensor.Internal.Elab.Impl.nativeUInt8FloatAdd {shape : Shape} (left : Rep UInt8 shape) (right : Rep Float shape) :
                                  Rep Float shape

                                  Promote packed bytes and add them to packed floats in one native output loop.

                                  The proof-visible definition is the canonical promoted pointwise operation.

                                  Instances For
                                    def TorchLean.Tensor.Internal.Elab.Impl.nativeFloatUInt8Add {shape : Shape} (left : Rep Float shape) (right : Rep UInt8 shape) :
                                    Rep Float shape

                                    Add packed floats to promoted packed bytes in one native output loop.

                                    Operand order remains explicit so IEEE exceptional behavior matches scalar Float addition exactly.

                                    Instances For
                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatAdd_data {shape : Shape} (left right : Rep Float shape) :
                                      (nativeFloatAdd left right).data = Array.zipWith (fun (x1 x2 : Float) => x1 + x2) left.data right.data

                                      The ordinary array observation of packed addition is pointwise addition.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatSub_data {shape : Shape} (left right : Rep Float shape) :
                                      (nativeFloatSub left right).data = Array.zipWith (fun (x1 x2 : Float) => x1 - x2) left.data right.data

                                      The ordinary array observation of packed subtraction is pointwise subtraction.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatMul_data {shape : Shape} (left right : Rep Float shape) :
                                      (nativeFloatMul left right).data = Array.zipWith (fun (x1 x2 : Float) => x1 * x2) left.data right.data

                                      The ordinary array observation of packed multiplication is pointwise multiplication.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatDiv_data {shape : Shape} (left right : Rep Float shape) :
                                      (nativeFloatDiv left right).data = Array.zipWith (fun (x1 x2 : Float) => x1 / x2) left.data right.data

                                      The ordinary array observation of packed division is pointwise division.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeUInt8FloatAdd_data {shape : Shape} (left : Rep UInt8 shape) (right : Rep Float shape) :
                                      (nativeUInt8FloatAdd left right).data = Array.zipWith (fun (x : UInt8) (y : Float) => x.toFloat + y) left.data right.data

                                      The ordinary array observation of byte/float addition includes promotion.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatUInt8Add_data {shape : Shape} (left : Rep Float shape) (right : Rep UInt8 shape) :
                                      (nativeFloatUInt8Add left right).data = Array.zipWith (fun (x : Float) (y : UInt8) => x + y.toFloat) left.data right.data

                                      The ordinary array observation of float/byte addition includes promotion.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatAdd_apply {shape : Shape} (left right : Rep Float shape) (coordinate : Coord shape) :
                                      (nativeFloatAdd left right).get coordinate = left.get coordinate + right.get coordinate

                                      Native packed addition has the canonical coordinate semantics.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatSub_apply {shape : Shape} (left right : Rep Float shape) (coordinate : Coord shape) :
                                      (nativeFloatSub left right).get coordinate = left.get coordinate - right.get coordinate

                                      Native packed subtraction has the canonical coordinate semantics.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatMul_apply {shape : Shape} (left right : Rep Float shape) (coordinate : Coord shape) :
                                      (nativeFloatMul left right).get coordinate = left.get coordinate * right.get coordinate

                                      Native packed multiplication has the canonical coordinate semantics.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatDiv_apply {shape : Shape} (left right : Rep Float shape) (coordinate : Coord shape) :
                                      (nativeFloatDiv left right).get coordinate = left.get coordinate / right.get coordinate

                                      Native packed division has the canonical coordinate semantics.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeUInt8FloatAdd_apply {shape : Shape} (left : Rep UInt8 shape) (right : Rep Float shape) (coordinate : Coord shape) :
                                      (nativeUInt8FloatAdd left right).get coordinate = (left.get coordinate).toFloat + right.get coordinate

                                      Fused byte/float addition has the canonical promoted semantics.

                                      @[simp]
                                      theorem TorchLean.Tensor.Internal.Elab.Impl.nativeFloatUInt8Add_apply {shape : Shape} (left : Rep Float shape) (right : Rep UInt8 shape) (coordinate : Coord shape) :
                                      (nativeFloatUInt8Add left right).get coordinate = left.get coordinate + (right.get coordinate).toFloat

                                      Fused float/byte addition has the canonical promoted semantics.