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

Flightdeck masked-attention

Normalize only unmasked attention scores and propagate through allowed positions. Fully masked rows return zeros. Use a binary mask.

Source repository · Package JSON

Use Flightdeck masked-attention

use vendor::fd_masked_attention::masked_attention;

Run the example

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

Expected output

module {
  func.func @main(%x: tensor<2x7xf32>, %mask: tensor<2x7xf32>, %upstream: tensor<2x7xf32>) -> (tensor<2x7xf32>, tensor<2x7xf32>) {
    %v0 = stablehlo.constant dense<0.00000000000000000e+00> : tensor<2x7xf32>
    %v1 = stablehlo.compare GT, %mask, %v0, FLOAT : (tensor<2x7xf32>, tensor<2x7xf32>) -> tensor<2x7xi1>
    %v2 = stablehlo.constant dense<3.40282339999999992e+38> : tensor<2x7xf32>
    %v3 = stablehlo.negate %v2 : tensor<2x7xf32>
    %v4 = stablehlo.select %v1, %x, %v3 : tensor<2x7xi1>, tensor<2x7xf32>
    %v5 = stablehlo.constant dense<-3.40282346000000007e+38> : tensor<f32>
    %v6 = stablehlo.reduce(%v4 init: %v5) applies stablehlo.maximum across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v7 = stablehlo.broadcast_in_dim %v6, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v8 = stablehlo.subtract %x, %v7 : tensor<2x7xf32>
    %v9 = stablehlo.constant dense<9.00000000000000000e+01> : tensor<2x7xf32>
    %v10 = stablehlo.negate %v9 : tensor<2x7xf32>
    %v11 = stablehlo.select %v1, %v8, %v10 : tensor<2x7xi1>, tensor<2x7xf32>
    %v12 = stablehlo.exponential %v11 : tensor<2x7xf32>
    %v13 = stablehlo.multiply %v12, %mask : tensor<2x7xf32>
    %v14 = stablehlo.constant dense<0.00000000000000000e+00> : tensor<f32>
    %v15 = stablehlo.reduce(%v13 init: %v14) applies stablehlo.add across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v16 = stablehlo.broadcast_in_dim %v15, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v17 = stablehlo.constant dense<1.00000000000000008e-30> : tensor<2x7xf32>
    %v18 = stablehlo.maximum %v16, %v17 : tensor<2x7xf32>
    %v19 = stablehlo.divide %v13, %v18 : tensor<2x7xf32>
    %v20 = stablehlo.multiply %upstream, %v19 : tensor<2x7xf32>
    %v21 = stablehlo.reduce(%v20 init: %v14) applies stablehlo.add across dimensions = [1] : (tensor<2x7xf32>, tensor<f32>) -> tensor<2xf32>
    %v22 = stablehlo.broadcast_in_dim %v21, dims = [0] : (tensor<2xf32>) -> tensor<2x7xf32>
    %v23 = stablehlo.subtract %upstream, %v22 : tensor<2x7xf32>
    %v24 = stablehlo.multiply %v19, %v23 : tensor<2x7xf32>
    return %v19, %v24 : tensor<2x7xf32>, tensor<2x7xf32>
  }
}

Published versions

VersionPackageSource commitSHA-256
0.1.1Download ZIP · Browse filesbf9c3d5003b5c134d7f6cedc6d867f0abc40d292a3cfbd597ed9dd603ae53e8775bb48b93d
0.1.0Download ZIP · Browse filesd5835c51095ed6bb842caa9bc68b1ac639e9dbc6d1ba08d5bb1f2a6cb5340e1fa4f853c036