Blyx Nest Blyx language
Registry / Framework
framework · v0.1.1

Flightdeck softmax-backward

Normalize attention scores along each row and compute the softmax vector-Jacobian product.

Source repository · Package JSON

Use Flightdeck softmax-backward

use vendor::fd_softmax_backward::softmax_backward;

Run the example

nest demo fd_softmax_backward@0.1.1
blyxc native-run fd_softmax_backward-demo/main.blyx
Example source and expected output
use vendor::fd_softmax_backward::softmax_backward;
fn main() { fd_softmax_backward(2, 7); }

Expected output

module {
  func.func @main(%x: tensor<2x7xf32>, %upstream: tensor<2x7xf32>) -> (tensor<2x7xf32>, tensor<2x7xf32>) {
    %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor<2x7xf32>
    %v1 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor<f32>
    %v2 = stablehlo.reduce(%x init: %v1) applies stablehlo.maximum across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v3 = stablehlo.broadcast_in_dim %v2, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v4 = stablehlo.subtract %x, %v3 : tensor<2x7xf32>
    %v5 = stablehlo.exponential %v4 : tensor<2x7xf32>
    %v6 = stablehlo.constant dense<0.00000000000000000e+00> : tensor<f32>
    %v7 = stablehlo.reduce(%v5 init: %v6) applies stablehlo.add across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v8 = stablehlo.broadcast_in_dim %v7, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v9 = stablehlo.log %v8 : tensor<2x7xf32>
    %v10 = stablehlo.subtract %v4, %v9 : tensor<2x7xf32>
    %v11 = stablehlo.exponential %v10 : tensor<2x7xf32>
    %v12 = stablehlo.multiply %upstream, %v11 : tensor<2x7xf32>
    %v13 = stablehlo.reduce(%v12 init: %v6) applies stablehlo.add across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v14 = stablehlo.broadcast_in_dim %v13, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v15 = stablehlo.subtract %upstream, %v14 : tensor<2x7xf32>
    %v16 = stablehlo.multiply %v11, %v15 : tensor<2x7xf32>
    return %v11, %v16 : tensor<2x7xf32>, tensor<2x7xf32>
  }
}

Published versions

VersionPackageSource commitSHA-256
0.1.1Download ZIP · Browse filesbf9c3d500386a659415bab7b8dc3cb8c5d4a3e90ee06fc5834760e102b5c4c371426f58cb1
0.1.0Download ZIP · Browse filesd5835c51092d69e5ec2c2fef1556ec8c6f5480d4b4f11d356584fd85dd8a99ce2603416a40