~/bend-docscommunity

tensor_layer_norm.bend checks

raw source on the hub · import stelliferous@0.0.4.0/tensor_layer_norm.bend as Tensor_layer_norm

Layer normalization over the last axis, composed of public operations in a fixed FP32 order. Gamma and beta have the extent of that axis. Parameters return, and backward reads xhat and the deviation that forward saved. A failed input, or gamma of another extent, fails every result computed.

7 imports
import Base
import ./tensor.bend as Tensors
import ./tensor_f32.bend as F
import ./tensor_layout.bend as Layout
import ./tensor_shape.bend as Shapes
import ./tensor_tiled.bend as Tiled
import ./tensor_tiled_map.bend as TiledMap

Definitions

def Tensor source · line 13 · raw

Type

def Saved source · line 16 · raw

Type

def Parameters source · line 19 · raw

Type

def Forward source · line 22 · raw

Type

def Backward source · line 25 · raw

Type

def Evaluated source · line 28 · raw

Type

def inverse_deviation source · line 32 · raw

@epsilon:F32 -> @variance:F32 -> F32

1 / sqrt(variance + epsilon).

def square source · line 35 · raw

@value:F32 -> F32

def last_of source · line 40 · raw

@shape:List<&2, U32> -> List<&2, U32>

The extents of a shape with only the last one left, and the axes before it.

def before_last source · line 46 · raw

@remaining:Nat -> @+axis:U32 -> List<&2, U32>

def before source · line 52 · raw

@count:Nat -> Nat

def last_axis source · line 57 · raw

@+shape:List<&2, U32> -> U32

def unit_extents source · line 60 · raw

@+shape:List<&2, U32> -> List<&2, U32>

def line_mean source · line 64 · raw

@tensor:Tensor -> @+shape:List<&2, U32> -> Tensor

The mean along the last axis, with that axis of extent 1.

def deviation_of source · line 67 · raw

@+epsilon:F32 -> @+shape:List<&2, U32> -> @difference:Tensor -> Tensor

def scaled_by source · line 70 · raw

@difference:Tensor -> @deviations:Pair(Tensor, Tensor) -> Saved

def scaled source · line 74 · raw

@+epsilon:F32 -> @+shape:List<&2, U32> -> @copies:Pair(Tensor, Tensor) -> Saved

def centered source · line 78 · raw

@+epsilon:F32 -> @+shape:List<&2, U32> -> @copies:Pair(Tensor, Tensor) -> Saved

def normalized source · line 85 · raw

@x:Tensor -> @+epsilon:F32 -> @+shape:List<&2, U32> -> Saved

(xhat, deviation) over each line of the last axis: xhat = (x - mean) * deviation, with deviation = 1 / sqrt(mean((x - mean)^2) + epsilon) in the shape of x with the last extent 1.

def affine source · line 88 · raw

@xhat:Tensor -> @gamma:Tensor -> @beta:Tensor -> Tensor

def forward_kept source · line 93 · raw

@xhats:Pair(Tensor, Tensor) -> @deviation:Tensor -> @gammas:Pair(Tensor, Tensor) -> @betas:Pair(Tensor, Tensor) -> Forward

def forward_saved source · line 99 · raw

@saved:Saved -> @gamma:Tensor -> @beta:Tensor -> Forward

def forward_failed source · line 104 · raw

@+problem:0xa2af805348d03b11eb853850859654f6/tensor.Problem -> @+shape:List<&2, U32> -> @+workers:U32 -> @parameters:Parameters -> Forward

A failed forward fails every result it computes and returns gamma and beta.

def mismatched source · line 108 · raw

@+shape:List<&2, U32> -> 0xa2af805348d03b11eb853850859654f6/tensor.Problem

The problem of a gamma that is not one per element of the last axis.

def forward_when source · line 111 · raw

@same:Bool -> @x:Tensor -> @+shape:List<&2, U32> -> @+workers:U32 -> @gamma:Tensor -> @beta:Tensor -> @+epsilon:F32 -> Forward

def matches source · line 117 · raw

@+shape:List<&2, U32> -> @+gamma_shape:List<&2, U32> -> Bool

One gamma per element of the last axis of x.

def forward_shapes source · line 122 · raw

@x:Tensor -> @+shape:List<&2, U32> -> @+workers:U32 -> @gamma_measured:Pair(Tensor, List<&2, U32>) -> @beta:Tensor -> @+epsilon:F32 -> Forward

def forward_status source · line 126 · raw

@+status:0xa2af805348d03b11eb853850859654f6/tensor.Status -> @+shape:List<&2, U32> -> @+workers:U32 -> @+epsilon:F32 -> @x:Tensor -> @parameters:Parameters -> Forward

def forward source · line 135 · raw

@x:Tensor -> @parameters:Parameters -> @+epsilon:F32 -> Forward

x in [..., D], gamma and beta in [D]: (xhat * gamma + beta, ((xhat, deviation), (gamma, beta))).

def three source · line 140 · raw

@pair:Pair(Tensor, Tensor) -> Pair(Tensor, Pair(Tensor, Tensor))

def input_gradient source · line 144 · raw

@+shape:List<&2, U32> -> @dzs:Pair(Tensor, Pair(Tensor, Tensor)) -> @xhat:Tensor -> @xhat_product:Tensor -> @deviation:Tensor -> Tensor

def back_parts source · line 148 · raw

@+shape:List<&2, U32> -> @dys:Pair(Tensor, Pair(Tensor, Tensor)) -> @xhats:Pair(Tensor, Pair(Tensor, Tensor)) -> @deviation:Tensor -> @gammas:Pair(Tensor, Tensor) -> Backward

def back_shaped source · line 156 · raw

@dy:Tensor -> @+shape:List<&2, U32> -> @saved:Saved -> @gamma:Tensor -> Backward

def backward_failed source · line 161 · raw

@+problem:0xa2af805348d03b11eb853850859654f6/tensor.Problem -> @+shape:List<&2, U32> -> @+workers:U32 -> @gamma:Tensor -> Backward

A failed dy fails every gradient and returns gamma.

def back_status source · line 164 · raw

@+status:0xa2af805348d03b11eb853850859654f6/tensor.Status -> @+shape:List<&2, U32> -> @+workers:U32 -> @dy:Tensor -> @saved:Saved -> @gamma:Tensor -> Backward

def backward source · line 172 · raw

@dy:Tensor -> @saved:Saved -> @gamma:Tensor -> Backward

dy and what forward saved: (dx, (dgamma, (dbeta, gamma))). With dz = dy * gamma: dx = (dz - mean(dz) - xhat * mean(dz * xhat)) * deviation along the last axis, dgamma the sum of dy * xhat and dbeta the sum of dy over the other axes.

def evaluated_kept source · line 177 · raw

@saved:Saved -> @gammas:Pair(Tensor, Tensor) -> @betas:Pair(Tensor, Tensor) -> Evaluated

def evaluate_failed source · line 183 · raw

@+problem:0xa2af805348d03b11eb853850859654f6/tensor.Problem -> @+shape:List<&2, U32> -> @+workers:U32 -> @parameters:Parameters -> Evaluated

def evaluate_when source · line 186 · raw

@same:Bool -> @x:Tensor -> @+shape:List<&2, U32> -> @+workers:U32 -> @gamma:Tensor -> @beta:Tensor -> @+epsilon:F32 -> Evaluated

def evaluate_shapes source · line 191 · raw

@x:Tensor -> @+shape:List<&2, U32> -> @+workers:U32 -> @gamma_measured:Pair(Tensor, List<&2, U32>) -> @beta:Tensor -> @+epsilon:F32 -> Evaluated

def evaluate_status source · line 195 · raw

@+status:0xa2af805348d03b11eb853850859654f6/tensor.Status -> @+shape:List<&2, U32> -> @+workers:U32 -> @+epsilon:F32 -> @x:Tensor -> @parameters:Parameters -> Evaluated

def evaluate source · line 203 · raw

@x:Tensor -> @parameters:Parameters -> @+epsilon:F32 -> Evaluated

The same output as forward, without what backward reads.