Ordinary functions on tensors #
f.run input (device := cpu) applies an ordinary Lean scalar function to a tensor.
f.run input (device := gpu) uses generated CUDA for supported scalar functions, and the existing
LibTorch operations for supported whole-tensor functions. grad := true records either kind
through the existing operation interface and returns its forward value and input pullback.
f.zip left right combines two equally shaped tensors with a binary scalar function.
Captured parameters are constants. Scalar mapping preserves the input shape; whole-tensor functions
can reduce or change shape. The call site infers the element type, storage and result shape.
Named numerical recurrences can use the same API when their recursive argument is last and their
parameters stay fixed. GPU lowering derives a loop and checks agreement with their Lean equations.
CPU execution does not require GPU-compatible arithmetic. A scalar type without a GPU encoding
runs on CPU with a diagnostic, retaining its type and precision. Unsupported source for an
otherwise supported GPU type, compilation failures and execution failures remain errors.
Prepare a certified GPU representation of a unary or binary scalar function.
Instances For
Determine the result shape from the ordinary function's signature, before call-site recording. This lets indexing and other consumers infer their types without waiting for source elaboration.
Instances
Call-site execution selected from the function's input and output types.
- run : TorchLean.Tensor α input → NN.Backend.Device → IO (match grad with | false => TorchLean.Tensor α output | true => TorchLean.autograd.Result α input output)
Instances For
Apply a scalar function to every tensor entry on the selected device, preserving its shape.
CPU evaluates the original Lean function. GPU supports native FP32/FP64 and configured binary arithmetic. Types without a GPU encoding run on CPU with a diagnostic, without narrowing values. Unsupported functions, unavailable CUDA and other devices still produce IO errors. GPU source recognition happens during Lean elaboration; native compilation and execution remain external trust boundaries. The compilation argument is supplied automatically.
Instances For
Record with the existing tape without narrowing a custom function's element carrier.
Instances For
Whole-tensor GPU execution shares the recorded runtime's precision policy.
Instances For
Infer scalar mapping or whole-tensor execution and prepare the existing operation recording.
Instances For
Execute an ordinary scalar or tensor function with inferred shapes and element type.
Scalar functions map over entries; tensor functions receive the whole input. CPU forward-only
calls evaluate the original function. Set grad := true to record supported operations on the
existing tape and return a value with backward and close. Unsupported recordings fail rather
than receive invented derivatives. Native Float32 and Float keep binary32 and binary64 on the
same GPU tape. Configured binary arithmetic retains complete words on that tape; its operator
subset is narrower than native LibTorch's. CPU-only carriers keep their precision through a
reported fallback. Scalar branch decisions synchronize to Lean and record only the chosen path.
The execution argument is prepared automatically at the call site.
Instances For
Combine corresponding entries of two tensors on the selected device.
The shared shape prevents truncation or accidental broadcasting. CPU uses the packed binary map;
GPU compiles the binary scalar function into one custom operation. Captured scalar parameters
are allowed, with the same source-equivalence and foreign-code boundaries as Function.run.