src/tensor/relu_LAWS.bend open laws/TODOs
raw source on the hub · import bendtorch-core@0.1.0.0/src/tensor/relu_LAWS.bend as Relu_LAWS
2 imports
import Base import ./elementwise.bend as Elementwise
Laws
law below_zero_words provedin PROOF.bendsource · line 46 · raw
@+x:F32 -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.below_zero(x) == Bool.and(U32.is_lt(2147483648, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.bits(x)), U32.is_le(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.bits(x), 4286578688)) : Bool}below_zero is IEEE 754 x < 0: true exactly on the words above -0.0
(0x80000000) up to -inf (0xFF800000). +0.0, -0.0 and every NaN, of either
sign, are not below zero.
law at_most_zero_words provedin PROOF.bendsource · line 54 · raw
@+x:F32 -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.at_most_zero(x) == Bool.or(U32.is_eq(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.bits(x), 0), Bool.and(U32.is_le(2147483648, 0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.bits(x)), U32.is_le(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.bits(x), 4286578688))) : Bool}at_most_zero is IEEE 754 x <= 0: true exactly on +0.0 (0x00000000) and
the words from -0.0 (0x80000000) up to -inf (0xFF800000). No NaN is at most
zero.
law below_zero_edges provedin PROOF.bendsource · line 62 · raw
{below_zero_at(edges) == [False{}, False{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, False{}, False{}, False{}, False{}, False{}] : List<&2, Bool>}The two comparisons at the edge words, as PyTorch's x < 0 and x <= 0
answer them.
law at_most_zero_edges provedin PROOF.bendsource · line 67 · raw
{at_most_zero_at(edges) == [True{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, True{}, False{}, False{}, False{}, False{}, False{}, False{}] : List<&2, Bool>}
law scalar_relu_left_below provedin PROOF.bendsource · line 76 · raw
@+left:F32 -> @+right:F32 -> @below:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.below_zero(left) == True{} : Bool} -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.scalar(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluLeft{}, left, right) == 0.0 : F32}ReluLeft is PyTorch's relu, clamp_min(x, 0), on the left word: +0.0 where the word is below zero, and the word itself otherwise, so -0.0 stays -0.0 and a NaN keeps its sign and payload. The right word is ignored: the forward is the pointwise product with a rank-0 +0.0.
law scalar_relu_left_kept provedin PROOF.bendsource · line 82 · raw
@+left:F32 -> @+right:F32 -> @kept:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.below_zero(left) == False{} : Bool} -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.scalar(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluLeft{}, left, right) == left : F32}
law scalar_relu_backward_zeroed provedin PROOF.bendsource · line 92 · raw
@+left:F32 -> @+right:F32 -> @zeroed:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.at_most_zero(left) == True{} : Bool} -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.scalar(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluBackward{}, left, right) == 0.0 : F32}ReluBackward is PyTorch's relu gradient, threshold_backward(g, relu(x), 0), with x on the left and g on the right: +0.0 where x is at most zero, so the gradient at +0.0 and -0.0 is +0.0, and g's own word otherwise, NaN x included.
law scalar_relu_backward_passed provedin PROOF.bendsource · line 98 · raw
@+left:F32 -> @+right:F32 -> @passed:{0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.at_most_zero(left) == False{} : Bool} -> {0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.scalar(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluBackward{}, left, right) == right : F32}
law relu_edges provedin PROOF.bendsource · line 105 · raw
{scalar_at(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluLeft{}, 0, edges) == [0, 2147483648, 1, 0, 8388607, 0, 8388608, 0, 1065353216, 0, 2139095039, 0, 2139095040, 0, 2139095041, 4286578689, 2143289344, 4290772992, 2147483647, 4294967295] : List<&2, U32>}The words PyTorch 2.14.1's torch.relu returns at the edge words.
law relu_backward_edges provedin PROOF.bendsource · line 113 · raw
{scalar_at(0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.ReluBackward{}, 1075838976, edges) == [0, 0, 1075838976, 0, 1075838976, 0, 1075838976, 0, 1075838976, 0, 1075838976, 0, 1075838976, 0, 1075838976, 1075838976, 1075838976, 1075838976, 1075838976, 1075838976] : List<&2, U32>}The gradient words PyTorch 2.14.1 gives torch.relu at the edge words, for g = 2.5 (0x40200000).
Definitions
def from_bits source · line 7 · raw
@word:U32 -> F32
def edges source · line 15 · raw
List<&2, U32>
The edge words: +0.0, -0.0, the smallest and largest denormals, the smallest normal, 1.0, the largest finite, the infinities, the signaling NaN 0x7F800001, the quiet NaN 0x7FC00000 and the all-ones NaN 0x7FFFFFFF, each followed by its negative.
def below_zero_at source · line 20 · raw
@words:List<&2, U32> -> List<&2, Bool>
def at_most_zero_at source · line 27 · raw
@words:List<&2, U32> -> List<&2, Bool>
def scalar_at source · line 36 · raw
@+operation:0x1689e5deb278c16abd3bd45eac101ef3/src/tensor/elementwise.Operation -> @+right:U32 -> @words:List<&2, U32> -> List<&2, U32>
The output words of operation with each word as the left operand and
right as the right one.