Element conversion and mixed-type tensor arithmetic #
TorchLean tensors remain homogeneous: every individual tensor has one element type and one corresponding physical storage representation. This module provides explicit element conversion and an extensible promotion relation for operations whose inputs have different element types.
The built-in promotion order is
UInt8 < Nat < Int < Rat < Float32 < Float
Moving from exact values to an IEEE type rounds to nearest with ties to even.
Rational conversion rounds the exact quotient, avoiding intermediate numerator or
denominator overflow. User-defined element types can participate by providing
ElementCast and ElementPromotion instances.
The common element type and two conversions selected for a mixed operation.
The result is an outParam, so typeclass search determines it from the
ordered input pair before looking for arithmetic or storage instances.
Library instances are symmetric; custom instances should normally provide
both operand orders.
- left : α → γ
Convert the left element to the common type.
- right : β → γ
Convert the right element to the common type.
Instances
Apply the registered element conversion.
Instances For
Build a promotion from two explicit conversions to one result type.
Instances For
Every element type converts to itself.
Round an exact rational through Lean's quotient and residual-bit model.
Instances For
Convert an exact rational to binary32, rounding once to nearest with ties to even.
Small exactly representable integers use native division. Larger values use Lean's float model
before packing, so a finite ratio near one does not become ∞ / ∞.
Instances For
Convert an exact rational to binary64, rounding once to nearest with ties to even.
Instances For
Explicit binary64-to-binary32 conversion using Lean's IEEE rounding.
Execution strategy for pointwise tensor addition.
The low-priority instance preserves the fully generic mixed-type operation. Packed element types can provide a higher-priority implementation while proving the same coordinate semantics.
- scalar : α → β → γ
Scalar operation implemented by this tensor kernel.
Build the pointwise sum.
- apply_at {shape : Shape} (left : Rep α shape) (right : Rep β shape) (coordinate : Coord shape) : (apply left right).get coordinate = scalar (left.get coordinate) (right.get coordinate)
The tensor implementation agrees with its scalar operation.
Instances
Execution strategy for pointwise tensor subtraction.
- scalar : α → β → γ
Scalar operation implemented by this tensor kernel.
Build the pointwise difference.
- apply_at {shape : Shape} (left : Rep α shape) (right : Rep β shape) (coordinate : Coord shape) : (apply left right).get coordinate = scalar (left.get coordinate) (right.get coordinate)
The tensor implementation agrees with its scalar operation.
Instances
Execution strategy for pointwise tensor multiplication.
- scalar : α → β → γ
Scalar operation implemented by this tensor kernel.
Build the pointwise product.
- apply_at {shape : Shape} (left : Rep α shape) (right : Rep β shape) (coordinate : Coord shape) : (apply left right).get coordinate = scalar (left.get coordinate) (right.get coordinate)
The tensor implementation agrees with its scalar operation.
Instances
Execution strategy for pointwise tensor division.
- scalar : α → β → γ
Scalar operation implemented by this tensor kernel.
Build the pointwise quotient.
- apply_at {shape : Shape} (left : Rep α shape) (right : Rep β shape) (coordinate : Coord shape) : (apply left right).get coordinate = scalar (left.get coordinate) (right.get coordinate)
The tensor implementation agrees with its scalar operation.
Instances
Generic one-pass fallback for every supported scalar promotion.
Packed Float addition uses one exact-size native output loop.
Packed byte-to-float promotion is fused with packed Float addition.
Packed Float addition is fused with packed byte-to-float promotion.
Generic one-pass fallback for every supported subtraction promotion.
Packed Float subtraction uses one exact-size native output loop.
Generic one-pass fallback for every supported multiplication promotion.
Packed Float multiplication uses one exact-size native output loop.
Generic one-pass fallback for every supported division promotion.
Packed Float division uses one exact-size native output loop.
Convert every element while preserving the tensor's static shape.
The target Storage instance selects the target physical buffer, so a
cast to Float writes a FloatArray and a cast to UInt8 writes a
ByteArray.
Instances For
Reading a cast tensor converts the element at the same coordinate.
Add two tensors pointwise, promoting their element types when necessary.
Both conversions and the addition occur in one output-building pass.
Instances For
Coordinate semantics of pointwise tensor addition.
Subtract two tensors pointwise, promoting their element types when necessary.
Both conversions and the subtraction occur in one output-building pass.
Instances For
Coordinate semantics of pointwise tensor subtraction.
Multiply two tensors pointwise, promoting their element types when necessary.
Both conversions and the multiplication occur in one output-building pass.
Instances For
Coordinate semantics of pointwise tensor multiplication.
Divide two tensors pointwise, promoting their element types when necessary.
Both conversions and the division occur in one output-building pass.
Instances For
Coordinate semantics of pointwise tensor division.