Kina.ai

Example

storylm0_rollout.kina

Source
let greedy_row = fn(
  logits: tensor<F32, [1, 1, 8], @host>
) -> tensor<I64, [1, 1], @host> {
  let tokens = logits.reduce(op=argmax, axis=2) in
  tokens
} in
let sample_row = fn(
  logits: tensor<F32, [1, 1, 8], @host>,
  u: tensor<F32, [1, 1, 8], @host>,
  temperature: tensor<F32, [1, 1], @host>
) -> tensor<I64, [1, 1], @host> {
  let log_u = log(u) in
  let neg_log_u = -log_u in
  let log_neg_log_u = log(neg_log_u) in
  let gumbel = -log_neg_log_u in
  let t_b = temperature.broadcast([1, 1, 8]) in
  let scaled = logits / t_b in
  let scores = scaled + gumbel in
  let tokens = scores.reduce(op=argmax, axis=2) in
  tokens
} in
let storylm0_rollout = fn(
  token_ids: tensor<I64, [1, 36], @host>,
  token_embedding: tensor<F32, [8, 4], @host>,
  causal_mask: tensor<F32, [1, 1, 36, 36], @host>,
  attention_scale: tensor<F32, [1, 1, 1, 1], @host>,
  wq: tensor<F32, [4, 4], @host>,
  wk: tensor<F32, [4, 4], @host>,
  wv: tensor<F32, [4, 4], @host>,
  wo: tensor<F32, [4, 4], @host>,
  lm_head: tensor<F32, [4, 8], @host>
) -> tensor<F32, [1, 36, 8], @host> {
  let x = token_embedding.gather(token_ids) in
  let wq_broadcast = wq.broadcast([1, 4, 4]) in
  let wk_broadcast = wk.broadcast([1, 4, 4]) in
  let wv_broadcast = wv.broadcast([1, 4, 4]) in
  let wo_broadcast = wo.broadcast([1, 4, 4]) in
  let lm_head_broadcast = lm_head.broadcast([1, 4, 8]) in
  let q_flat = x @ wq_broadcast in
  let k_flat = x @ wk_broadcast in
  let v_flat = x @ wv_broadcast in
  let q = q_flat.reshape([1, 36, 2, 2]).transpose([0, 2, 1, 3]) in
  let k_t = k_flat.reshape([1, 36, 2, 2]).transpose([0, 2, 3, 1]) in
  let v = v_flat.reshape([1, 36, 2, 2]).transpose([0, 2, 1, 3]) in
  let scores = q @ k_t in
  let scale_broadcast = attention_scale.broadcast([1, 2, 36, 36]) in
  let mask_broadcast = causal_mask.broadcast([1, 2, 36, 36]) in
  let masked_scores = (scores * scale_broadcast) + mask_broadcast in
  let probs = masked_scores.softmax(axis=3) in
  let context = probs @ v in
  let merged = context.transpose([0, 2, 1, 3]).reshape([1, 36, 4]) in
  let projected = merged @ wo_broadcast in
  let residual = x + projected in
  let logits = residual @ lm_head_broadcast in
  logits
} in
let token_ids = tensor<I64, [1, 36], @host> in
let token_embedding = tensor<F32, [8, 4], @host> in
let causal_mask = tensor<F32, [1, 1, 36, 36], @host> in
let attention_scale = tensor<F32, [1, 1, 1, 1], @host> in
let wq = tensor<F32, [4, 4], @host> in
let wk = tensor<F32, [4, 4], @host> in
let wv = tensor<F32, [4, 4], @host> in
let wo = tensor<F32, [4, 4], @host> in
let lm_head = tensor<F32, [4, 8], @host> in
storylm0_rollout(token_ids, token_embedding, causal_mask, attention_scale, wq, wk, wv, wo, lm_head)
Emitted MLIR (golden)
module {
  func.func @fn_val_0(%1: tensor<1x32x64xf32>, %2: tensor<1x1x64xf32>) -> tensor<1x32x64xf32> attributes {llvm.emit_c_interface} {
      %3 = tensor.empty() : tensor<1x32x64xf32>
      %4 = linalg.mul ins(%1, %1 : tensor<1x32x64xf32>, tensor<1x32x64xf32>) outs(%3 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %7 = arith.constant 0.000000e+00 : f32
      %5 = tensor.empty() : tensor<1x32x1xf32>
      %6 = linalg.fill ins(%7 : f32) outs(%5 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %8 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, 0)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%4 : tensor<1x32x64xf32>) outs(%6 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %9 = arith.addf %a0, %a1 : f32
        linalg.yield %9 : f32
      } -> tensor<1x32x1xf32>
      %10 = arith.constant dense<0.000000e+00> : tensor<1x1x1xf32>
      %11 = tensor.empty() : tensor<1x32x1xf32>
      %12 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, 0, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%10 : tensor<1x1x1xf32>) outs(%11 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x1xf32>
      %13 = tensor.empty() : tensor<1x32x1xf32>
      %14 = linalg.mul ins(%8, %12 : tensor<1x32x1xf32>, tensor<1x32x1xf32>) outs(%13 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %15 = arith.constant dense<0.000000e+00> : tensor<1x1x1xf32>
      %16 = tensor.empty() : tensor<1x32x1xf32>
      %17 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, 0, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%15 : tensor<1x1x1xf32>) outs(%16 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x1xf32>
      %18 = tensor.empty() : tensor<1x32x1xf32>
      %19 = linalg.add ins(%14, %17 : tensor<1x32x1xf32>, tensor<1x32x1xf32>) outs(%18 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %23 = tensor.empty() : tensor<1x32x1xf32>
      %24 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%19 : tensor<1x32x1xf32>) outs(%23 : tensor<1x32x1xf32>) {
      ^bb0(%x: f32, %_: f32):
        %20 = math.sqrt %x : f32
        %21 = arith.constant 1.000000e+00 : f32
        %22 = arith.divf %21, %20 : f32
        linalg.yield %22 : f32
      } -> tensor<1x32x1xf32>
      %25 = tensor.empty() : tensor<1x32x64xf32>
      %26 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, d1, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%24 : tensor<1x32x1xf32>) outs(%25 : tensor<1x32x64xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x64xf32>
      %27 = tensor.empty() : tensor<1x32x64xf32>
      %28 = linalg.mul ins(%1, %26 : tensor<1x32x64xf32>, tensor<1x32x64xf32>) outs(%27 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %29 = tensor.empty() : tensor<1x32x64xf32>
      %30 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, 0, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%2 : tensor<1x1x64xf32>) outs(%29 : tensor<1x32x64xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x64xf32>
      %31 = tensor.empty() : tensor<1x32x64xf32>
      %32 = linalg.mul ins(%28, %30 : tensor<1x32x64xf32>, tensor<1x32x64xf32>) outs(%31 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
    return %32 : tensor<1x32x64xf32>
  }
  func.func @fn_val_33(%34: tensor<1x32x64xf32>, %35: tensor<1x64x128xf32>, %36: tensor<1x128x64xf32>) -> tensor<1x32x64xf32> attributes {llvm.emit_c_interface} {
      %37 = arith.constant dense<0.000000e+00> : tensor<1x32x128xf32>
      %38 = linalg.batch_matmul ins(%34, %35 : tensor<1x32x64xf32>, tensor<1x64x128xf32>) outs(%37 : tensor<1x32x128xf32>) -> tensor<1x32x128xf32>
      %44 = tensor.empty() : tensor<1x32x128xf32>
      %45 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%38 : tensor<1x32x128xf32>) outs(%44 : tensor<1x32x128xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %39 = arith.constant 1.000000e+00 : f32
        %40 = arith.negf %a0 : f32
        %41 = math.exp %40 : f32
        %42 = arith.addf %39, %41 : f32
        %43 = arith.divf %39, %42 : f32
        linalg.yield %43 : f32
      } -> tensor<1x32x128xf32>
      %46 = tensor.empty() : tensor<1x32x128xf32>
      %47 = linalg.mul ins(%38, %45 : tensor<1x32x128xf32>, tensor<1x32x128xf32>) outs(%46 : tensor<1x32x128xf32>) -> tensor<1x32x128xf32>
      %48 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %49 = linalg.batch_matmul ins(%47, %36 : tensor<1x32x128xf32>, tensor<1x128x64xf32>) outs(%48 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
    return %49 : tensor<1x32x64xf32>
  }
  func.func @fn_val_50(%51: tensor<1x32x64xf32>, %52: tensor<1x64x64xf32>, %53: tensor<1x64x64xf32>, %54: tensor<1x64x64xf32>, %55: tensor<1x64x64xf32>) -> tensor<1x32x64xf32> attributes {llvm.emit_c_interface} {
      %56 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %57 = linalg.batch_matmul ins(%51, %52 : tensor<1x32x64xf32>, tensor<1x64x64xf32>) outs(%56 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %58 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %59 = linalg.batch_matmul ins(%51, %53 : tensor<1x32x64xf32>, tensor<1x64x64xf32>) outs(%58 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %60 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %61 = linalg.batch_matmul ins(%51, %54 : tensor<1x32x64xf32>, tensor<1x64x64xf32>) outs(%60 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %62 = tensor.empty() : tensor<1x64x32xf32>
      %63 = linalg.transpose ins(%59 : tensor<1x32x64xf32>) outs(%62 : tensor<1x64x32xf32>) permutation = [0, 2, 1]
      %64 = arith.constant dense<0.000000e+00> : tensor<1x32x32xf32>
      %65 = linalg.batch_matmul ins(%57, %63 : tensor<1x32x64xf32>, tensor<1x64x32xf32>) outs(%64 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %66 = arith.constant dense<0.000000e+00> : tensor<1x1x1xf32>
      %67 = tensor.empty() : tensor<1x32x32xf32>
      %68 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, 0, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%66 : tensor<1x1x1xf32>) outs(%67 : tensor<1x32x32xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x32xf32>
      %69 = tensor.empty() : tensor<1x32x32xf32>
      %70 = linalg.mul ins(%65, %68 : tensor<1x32x32xf32>, tensor<1x32x32xf32>) outs(%69 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %73 = arith.constant 0xFF800000 : f32
      %71 = tensor.empty() : tensor<1x32x1xf32>
      %72 = linalg.fill ins(%73 : f32) outs(%71 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %74 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, 0)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%70 : tensor<1x32x32xf32>) outs(%72 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %75 = arith.maximumf %a0, %a1 : f32
        linalg.yield %75 : f32
      } -> tensor<1x32x1xf32>
      %76 = tensor.empty() : tensor<1x32x32xf32>
      %77 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, d1, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%74 : tensor<1x32x1xf32>) outs(%76 : tensor<1x32x32xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x32xf32>
      %78 = tensor.empty() : tensor<1x32x32xf32>
      %79 = linalg.sub ins(%70, %77 : tensor<1x32x32xf32>, tensor<1x32x32xf32>) outs(%78 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %80 = tensor.empty() : tensor<1x32x32xf32>
      %81 = linalg.exp ins(%79 : tensor<1x32x32xf32>) outs(%80 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %84 = arith.constant 0xFF800000 : f32
      %82 = tensor.empty() : tensor<1x32x1xf32>
      %83 = linalg.fill ins(%84 : f32) outs(%82 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %85 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, 0)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%70 : tensor<1x32x32xf32>) outs(%83 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %86 = arith.maximumf %a0, %a1 : f32
        linalg.yield %86 : f32
      } -> tensor<1x32x1xf32>
      %87 = tensor.empty() : tensor<1x32x32xf32>
      %88 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, d1, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%85 : tensor<1x32x1xf32>) outs(%87 : tensor<1x32x32xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x32xf32>
      %89 = tensor.empty() : tensor<1x32x32xf32>
      %90 = linalg.sub ins(%70, %88 : tensor<1x32x32xf32>, tensor<1x32x32xf32>) outs(%89 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %91 = tensor.empty() : tensor<1x32x32xf32>
      %92 = linalg.exp ins(%90 : tensor<1x32x32xf32>) outs(%91 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %95 = arith.constant 0.000000e+00 : f32
      %93 = tensor.empty() : tensor<1x32x1xf32>
      %94 = linalg.fill ins(%95 : f32) outs(%93 : tensor<1x32x1xf32>) -> tensor<1x32x1xf32>
      %96 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, 0)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%92 : tensor<1x32x32xf32>) outs(%94 : tensor<1x32x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %97 = arith.addf %a0, %a1 : f32
        linalg.yield %97 : f32
      } -> tensor<1x32x1xf32>
      %98 = tensor.empty() : tensor<1x32x32xf32>
      %99 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, d1, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%96 : tensor<1x32x1xf32>) outs(%98 : tensor<1x32x32xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x32x32xf32>
      %100 = tensor.empty() : tensor<1x32x32xf32>
      %101 = linalg.div ins(%81, %99 : tensor<1x32x32xf32>, tensor<1x32x32xf32>) outs(%100 : tensor<1x32x32xf32>) -> tensor<1x32x32xf32>
      %102 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %103 = linalg.batch_matmul ins(%101, %61 : tensor<1x32x32xf32>, tensor<1x32x64xf32>) outs(%102 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %104 = arith.constant dense<0.000000e+00> : tensor<1x32x64xf32>
      %105 = linalg.batch_matmul ins(%103, %55 : tensor<1x32x64xf32>, tensor<1x64x64xf32>) outs(%104 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
    return %105 : tensor<1x32x64xf32>
  }
  func.func @fn_val_106(%107: tensor<1x32x64xf32>, %108: tensor<1x1x64xf32>, %109: tensor<1x64x64xf32>, %110: tensor<1x64x64xf32>, %111: tensor<1x64x64xf32>, %112: tensor<1x64x64xf32>, %113: tensor<1x1x64xf32>, %114: tensor<1x64x128xf32>, %115: tensor<1x128x64xf32>) -> tensor<1x32x64xf32> attributes {llvm.emit_c_interface} {
      %116 = func.call @fn_val_0(%107, %108) : (tensor<1x32x64xf32>, tensor<1x1x64xf32>) -> tensor<1x32x64xf32>
      %117 = func.call @fn_val_50(%116, %109, %110, %111, %112) : (tensor<1x32x64xf32>, tensor<1x64x64xf32>, tensor<1x64x64xf32>, tensor<1x64x64xf32>, tensor<1x64x64xf32>) -> tensor<1x32x64xf32>
      %118 = tensor.empty() : tensor<1x32x64xf32>
      %119 = linalg.add ins(%107, %117 : tensor<1x32x64xf32>, tensor<1x32x64xf32>) outs(%118 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
      %120 = func.call @fn_val_0(%119, %113) : (tensor<1x32x64xf32>, tensor<1x1x64xf32>) -> tensor<1x32x64xf32>
      %121 = func.call @fn_val_33(%120, %114, %115) : (tensor<1x32x64xf32>, tensor<1x64x128xf32>, tensor<1x128x64xf32>) -> tensor<1x32x64xf32>
      %122 = tensor.empty() : tensor<1x32x64xf32>
      %123 = linalg.add ins(%119, %121 : tensor<1x32x64xf32>, tensor<1x32x64xf32>) outs(%122 : tensor<1x32x64xf32>) -> tensor<1x32x64xf32>
    return %123 : tensor<1x32x64xf32>
  }
  func.func @fn_val_124(%125: tensor<1x1x8xf32>) -> tensor<1x1xi64> attributes {llvm.emit_c_interface} {
      %126 = arith.constant 0xFF800000 : f32
      %127 = arith.constant 0 : i64
      %128 = tensor.empty() : tensor<1x1xf32>
      %129 = linalg.fill ins(%126 : f32) outs(%128 : tensor<1x1xf32>) -> tensor<1x1xf32>
      %130 = tensor.empty() : tensor<1x1xi64>
      %131 = linalg.fill ins(%127 : i64) outs(%130 : tensor<1x1xi64>) -> tensor<1x1xi64>
      %132, %133 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%125 : tensor<1x1x8xf32>) outs(%129, %131 : tensor<1x1xf32>, tensor<1x1xi64>) {
      ^bb0(%a0: f32, %a1: f32, %a2: i64):
        %134 = arith.cmpf ogt, %a0, %a1 : f32
        %135 = arith.select %134, %a0, %a1 : f32
        %136 = linalg.index 2 : index
        %137 = arith.index_cast %136 : index to i64
        %138 = arith.select %134, %137, %a2 : i64
        linalg.yield %135, %138 : f32, i64
      } -> (tensor<1x1xf32>, tensor<1x1xi64>)
    return %133 : tensor<1x1xi64>
  }
  func.func @fn_val_139(%140: tensor<1x1x8xf32>, %141: tensor<1x1x8xf32>, %142: tensor<1x1xf32>) -> tensor<1x1xi64> attributes {llvm.emit_c_interface} {
      %143 = tensor.empty() : tensor<1x1x8xf32>
      %144 = linalg.log ins(%141 : tensor<1x1x8xf32>) outs(%143 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %145 = tensor.empty() : tensor<1x1x8xf32>
      %146 = linalg.negf ins(%144 : tensor<1x1x8xf32>) outs(%145 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %147 = tensor.empty() : tensor<1x1x8xf32>
      %148 = linalg.log ins(%146 : tensor<1x1x8xf32>) outs(%147 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %149 = tensor.empty() : tensor<1x1x8xf32>
      %150 = linalg.negf ins(%148 : tensor<1x1x8xf32>) outs(%149 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %151 = tensor.empty() : tensor<1x1x8xf32>
      %152 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (0, 0)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%142 : tensor<1x1xf32>) outs(%151 : tensor<1x1x8xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x1x8xf32>
      %153 = tensor.empty() : tensor<1x1x8xf32>
      %154 = linalg.div ins(%140, %152 : tensor<1x1x8xf32>, tensor<1x1x8xf32>) outs(%153 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %155 = tensor.empty() : tensor<1x1x8xf32>
      %156 = linalg.add ins(%154, %150 : tensor<1x1x8xf32>, tensor<1x1x8xf32>) outs(%155 : tensor<1x1x8xf32>) -> tensor<1x1x8xf32>
      %157 = arith.constant 0xFF800000 : f32
      %158 = arith.constant 0 : i64
      %159 = tensor.empty() : tensor<1x1xf32>
      %160 = linalg.fill ins(%157 : f32) outs(%159 : tensor<1x1xf32>) -> tensor<1x1xf32>
      %161 = tensor.empty() : tensor<1x1xi64>
      %162 = linalg.fill ins(%158 : i64) outs(%161 : tensor<1x1xi64>) -> tensor<1x1xi64>
      %163, %164 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel", "reduction"]
      } ins(%156 : tensor<1x1x8xf32>) outs(%160, %162 : tensor<1x1xf32>, tensor<1x1xi64>) {
      ^bb0(%a0: f32, %a1: f32, %a2: i64):
        %165 = arith.cmpf ogt, %a0, %a1 : f32
        %166 = arith.select %165, %a0, %a1 : f32
        %167 = linalg.index 2 : index
        %168 = arith.index_cast %167 : index to i64
        %169 = arith.select %165, %168, %a2 : i64
        linalg.yield %166, %169 : f32, i64
      } -> (tensor<1x1xf32>, tensor<1x1xi64>)
    return %164 : tensor<1x1xi64>
  }
  func.func @fn_val_170(%171: tensor<1x36xi64>, %172: tensor<8x4xf32>, %173: tensor<1x1x36x36xf32>, %174: tensor<1x1x1x1xf32>, %175: tensor<4x4xf32>, %176: tensor<4x4xf32>, %177: tensor<4x4xf32>, %178: tensor<4x4xf32>, %179: tensor<4x8xf32>) -> tensor<1x36x8xf32> attributes {llvm.emit_c_interface} {
      %180 = arith.constant 0 : i64
      %181 = arith.constant 8 : i64
      %182 = tensor.empty() : tensor<1x36xi64>
      %183 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>],
        iterator_types = ["parallel", "parallel"]
      } ins(%171 : tensor<1x36xi64>) outs(%182 : tensor<1x36xi64>) {
      ^bb0(%candidate: i64, %_: i64):
        %184 = arith.cmpi sge, %candidate, %180 : i64
        %185 = arith.cmpi slt, %candidate, %181 : i64
        %186 = arith.andi %184, %185 : i1
        cf.assert %186, "gather index out of bounds"
        linalg.yield %candidate : i64
      } -> tensor<1x36xi64>
      %187 = tensor.empty() : tensor<1x36x4xf32>
      %188 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%183 : tensor<1x36xi64>) outs(%187 : tensor<1x36x4xf32>) {
      ^bb0(%idx: i64, %_: f32):
        %189 = arith.index_cast %idx : i64 to index
        %190 = linalg.index 2 : index
        %191 = tensor.extract %172[%189, %190] : tensor<8x4xf32>
        linalg.yield %191 : f32
      } -> tensor<1x36x4xf32>
      %192 = tensor.empty() : tensor<1x4x4xf32>
      %193 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%175 : tensor<4x4xf32>) outs(%192 : tensor<1x4x4xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x4x4xf32>
      %194 = tensor.empty() : tensor<1x4x4xf32>
      %195 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%176 : tensor<4x4xf32>) outs(%194 : tensor<1x4x4xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x4x4xf32>
      %196 = tensor.empty() : tensor<1x4x4xf32>
      %197 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%177 : tensor<4x4xf32>) outs(%196 : tensor<1x4x4xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x4x4xf32>
      %198 = tensor.empty() : tensor<1x4x4xf32>
      %199 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%178 : tensor<4x4xf32>) outs(%198 : tensor<1x4x4xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x4x4xf32>
      %200 = tensor.empty() : tensor<1x4x8xf32>
      %201 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2) -> (d1, d2)>, affine_map<(d0, d1, d2) -> (d0, d1, d2)>],
        iterator_types = ["parallel", "parallel", "parallel"]
      } ins(%179 : tensor<4x8xf32>) outs(%200 : tensor<1x4x8xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x4x8xf32>
      %202 = arith.constant dense<0.000000e+00> : tensor<1x36x4xf32>
      %203 = linalg.batch_matmul ins(%188, %193 : tensor<1x36x4xf32>, tensor<1x4x4xf32>) outs(%202 : tensor<1x36x4xf32>) -> tensor<1x36x4xf32>
      %204 = arith.constant dense<0.000000e+00> : tensor<1x36x4xf32>
      %205 = linalg.batch_matmul ins(%188, %195 : tensor<1x36x4xf32>, tensor<1x4x4xf32>) outs(%204 : tensor<1x36x4xf32>) -> tensor<1x36x4xf32>
      %206 = arith.constant dense<0.000000e+00> : tensor<1x36x4xf32>
      %207 = linalg.batch_matmul ins(%188, %197 : tensor<1x36x4xf32>, tensor<1x4x4xf32>) outs(%206 : tensor<1x36x4xf32>) -> tensor<1x36x4xf32>
      %208 = arith.constant dense<[1, 36, 2, 2]> : tensor<4xi32>
      %209 = tensor.reshape %203(%208) : (tensor<1x36x4xf32>, tensor<4xi32>) -> tensor<1x36x2x2xf32>
      %210 = tensor.empty() : tensor<1x2x36x2xf32>
      %211 = linalg.transpose ins(%209 : tensor<1x36x2x2xf32>) outs(%210 : tensor<1x2x36x2xf32>) permutation = [0, 2, 1, 3]
      %212 = arith.constant dense<[1, 36, 2, 2]> : tensor<4xi32>
      %213 = tensor.reshape %205(%212) : (tensor<1x36x4xf32>, tensor<4xi32>) -> tensor<1x36x2x2xf32>
      %214 = tensor.empty() : tensor<1x2x2x36xf32>
      %215 = linalg.transpose ins(%213 : tensor<1x36x2x2xf32>) outs(%214 : tensor<1x2x2x36xf32>) permutation = [0, 2, 3, 1]
      %216 = arith.constant dense<[1, 36, 2, 2]> : tensor<4xi32>
      %217 = tensor.reshape %207(%216) : (tensor<1x36x4xf32>, tensor<4xi32>) -> tensor<1x36x2x2xf32>
      %218 = tensor.empty() : tensor<1x2x36x2xf32>
      %219 = linalg.transpose ins(%217 : tensor<1x36x2x2xf32>) outs(%218 : tensor<1x2x36x2xf32>) permutation = [0, 2, 1, 3]
      %220 = tensor.collapse_shape %211 [[0, 1], [2], [3]] : tensor<1x2x36x2xf32> into tensor<2x36x2xf32>
      %221 = tensor.collapse_shape %215 [[0, 1], [2], [3]] : tensor<1x2x2x36xf32> into tensor<2x2x36xf32>
      %222 = arith.constant dense<0.000000e+00> : tensor<2x36x36xf32>
      %223 = linalg.batch_matmul ins(%220, %221 : tensor<2x36x2xf32>, tensor<2x2x36xf32>) outs(%222 : tensor<2x36x36xf32>) -> tensor<2x36x36xf32>
      %224 = tensor.expand_shape %223 [[0, 1], [2], [3]] : tensor<2x36x36xf32> into tensor<1x2x36x36xf32>
      %225 = tensor.empty() : tensor<1x2x36x36xf32>
      %226 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (0, 0, 0, 0)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]
      } ins(%174 : tensor<1x1x1x1xf32>) outs(%225 : tensor<1x2x36x36xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x2x36x36xf32>
      %227 = tensor.empty() : tensor<1x2x36x36xf32>
      %228 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (0, 0, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]
      } ins(%173 : tensor<1x1x36x36xf32>) outs(%227 : tensor<1x2x36x36xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x2x36x36xf32>
      %229 = tensor.empty() : tensor<1x2x36x36xf32>
      %230 = linalg.mul ins(%224, %226 : tensor<1x2x36x36xf32>, tensor<1x2x36x36xf32>) outs(%229 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %231 = tensor.empty() : tensor<1x2x36x36xf32>
      %232 = linalg.add ins(%230, %228 : tensor<1x2x36x36xf32>, tensor<1x2x36x36xf32>) outs(%231 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %235 = arith.constant 0xFF800000 : f32
      %233 = tensor.empty() : tensor<1x2x36x1xf32>
      %234 = linalg.fill ins(%235 : f32) outs(%233 : tensor<1x2x36x1xf32>) -> tensor<1x2x36x1xf32>
      %236 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, 0)>],
        iterator_types = ["parallel", "parallel", "parallel", "reduction"]
      } ins(%232 : tensor<1x2x36x36xf32>) outs(%234 : tensor<1x2x36x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %237 = arith.maximumf %a0, %a1 : f32
        linalg.yield %237 : f32
      } -> tensor<1x2x36x1xf32>
      %238 = tensor.empty() : tensor<1x2x36x36xf32>
      %239 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (0, d1, d2, 0)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]
      } ins(%236 : tensor<1x2x36x1xf32>) outs(%238 : tensor<1x2x36x36xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x2x36x36xf32>
      %240 = tensor.empty() : tensor<1x2x36x36xf32>
      %241 = linalg.sub ins(%232, %239 : tensor<1x2x36x36xf32>, tensor<1x2x36x36xf32>) outs(%240 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %242 = tensor.empty() : tensor<1x2x36x36xf32>
      %243 = linalg.exp ins(%241 : tensor<1x2x36x36xf32>) outs(%242 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %246 = arith.constant 0xFF800000 : f32
      %244 = tensor.empty() : tensor<1x2x36x1xf32>
      %245 = linalg.fill ins(%246 : f32) outs(%244 : tensor<1x2x36x1xf32>) -> tensor<1x2x36x1xf32>
      %247 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, 0)>],
        iterator_types = ["parallel", "parallel", "parallel", "reduction"]
      } ins(%232 : tensor<1x2x36x36xf32>) outs(%245 : tensor<1x2x36x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %248 = arith.maximumf %a0, %a1 : f32
        linalg.yield %248 : f32
      } -> tensor<1x2x36x1xf32>
      %249 = tensor.empty() : tensor<1x2x36x36xf32>
      %250 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (0, d1, d2, 0)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]
      } ins(%247 : tensor<1x2x36x1xf32>) outs(%249 : tensor<1x2x36x36xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x2x36x36xf32>
      %251 = tensor.empty() : tensor<1x2x36x36xf32>
      %252 = linalg.sub ins(%232, %250 : tensor<1x2x36x36xf32>, tensor<1x2x36x36xf32>) outs(%251 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %253 = tensor.empty() : tensor<1x2x36x36xf32>
      %254 = linalg.exp ins(%252 : tensor<1x2x36x36xf32>) outs(%253 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %257 = arith.constant 0.000000e+00 : f32
      %255 = tensor.empty() : tensor<1x2x36x1xf32>
      %256 = linalg.fill ins(%257 : f32) outs(%255 : tensor<1x2x36x1xf32>) -> tensor<1x2x36x1xf32>
      %258 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, 0)>],
        iterator_types = ["parallel", "parallel", "parallel", "reduction"]
      } ins(%254 : tensor<1x2x36x36xf32>) outs(%256 : tensor<1x2x36x1xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        %259 = arith.addf %a0, %a1 : f32
        linalg.yield %259 : f32
      } -> tensor<1x2x36x1xf32>
      %260 = tensor.empty() : tensor<1x2x36x36xf32>
      %261 = linalg.generic {
        indexing_maps = [affine_map<(d0, d1, d2, d3) -> (0, d1, d2, 0)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>],
        iterator_types = ["parallel", "parallel", "parallel", "parallel"]
      } ins(%258 : tensor<1x2x36x1xf32>) outs(%260 : tensor<1x2x36x36xf32>) {
      ^bb0(%a0: f32, %a1: f32):
        linalg.yield %a0 : f32
      } -> tensor<1x2x36x36xf32>
      %262 = tensor.empty() : tensor<1x2x36x36xf32>
      %263 = linalg.div ins(%243, %261 : tensor<1x2x36x36xf32>, tensor<1x2x36x36xf32>) outs(%262 : tensor<1x2x36x36xf32>) -> tensor<1x2x36x36xf32>
      %264 = tensor.collapse_shape %263 [[0, 1], [2], [3]] : tensor<1x2x36x36xf32> into tensor<2x36x36xf32>
      %265 = tensor.collapse_shape %219 [[0, 1], [2], [3]] : tensor<1x2x36x2xf32> into tensor<2x36x2xf32>
      %266 = arith.constant dense<0.000000e+00> : tensor<2x36x2xf32>
      %267 = linalg.batch_matmul ins(%264, %265 : tensor<2x36x36xf32>, tensor<2x36x2xf32>) outs(%266 : tensor<2x36x2xf32>) -> tensor<2x36x2xf32>
      %268 = tensor.expand_shape %267 [[0, 1], [2], [3]] : tensor<2x36x2xf32> into tensor<1x2x36x2xf32>
      %269 = tensor.empty() : tensor<1x36x2x2xf32>
      %270 = linalg.transpose ins(%268 : tensor<1x2x36x2xf32>) outs(%269 : tensor<1x36x2x2xf32>) permutation = [0, 2, 1, 3]
      %271 = arith.constant dense<[1, 36, 4]> : tensor<3xi32>
      %272 = tensor.reshape %270(%271) : (tensor<1x36x2x2xf32>, tensor<3xi32>) -> tensor<1x36x4xf32>
      %273 = arith.constant dense<0.000000e+00> : tensor<1x36x4xf32>
      %274 = linalg.batch_matmul ins(%272, %199 : tensor<1x36x4xf32>, tensor<1x4x4xf32>) outs(%273 : tensor<1x36x4xf32>) -> tensor<1x36x4xf32>
      %275 = tensor.empty() : tensor<1x36x4xf32>
      %276 = linalg.add ins(%188, %274 : tensor<1x36x4xf32>, tensor<1x36x4xf32>) outs(%275 : tensor<1x36x4xf32>) -> tensor<1x36x4xf32>
      %277 = arith.constant dense<0.000000e+00> : tensor<1x36x8xf32>
      %278 = linalg.batch_matmul ins(%276, %201 : tensor<1x36x4xf32>, tensor<1x4x8xf32>) outs(%277 : tensor<1x36x8xf32>) -> tensor<1x36x8xf32>
    return %278 : tensor<1x36x8xf32>
  }
  func.func @main() -> tensor<1x36x8xf32> {
    %279 = arith.constant dense<0> : tensor<1x36xi64>
    %280 = arith.constant dense<0.000000e+00> : tensor<8x4xf32>
    %281 = arith.constant dense<0.000000e+00> : tensor<1x1x36x36xf32>
    %282 = arith.constant dense<0.000000e+00> : tensor<1x1x1x1xf32>
    %283 = arith.constant dense<0.000000e+00> : tensor<4x4xf32>
    %284 = arith.constant dense<0.000000e+00> : tensor<4x4xf32>
    %285 = arith.constant dense<0.000000e+00> : tensor<4x4xf32>
    %286 = arith.constant dense<0.000000e+00> : tensor<4x4xf32>
    %287 = arith.constant dense<0.000000e+00> : tensor<4x8xf32>
    %288 = func.call @fn_val_170(%279, %280, %281, %282, %283, %284, %285, %286, %287) : (tensor<1x36xi64>, tensor<8x4xf32>, tensor<1x1x36x36xf32>, tensor<1x1x1x1xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x4xf32>, tensor<4x8xf32>) -> tensor<1x36x8xf32>
    return %288 : tensor<1x36x8xf32>
  }
}