Skip to content

Instantly share code, notes, and snippets.

@chelini
chelini / mha_readme.md
Last active June 8, 2023 06:59
MHA Readme

MHA in keras:

tf.keras.layers.MultiHeadAttention(
    num_heads,
    key_dim,
    value_dim=None,
    dropout=0.0,
    use_bias=True,
    output_shape=None,
@chelini
chelini / mha.py
Last active June 7, 2023 12:52
MHA
import tensorflow as tf
from tensorflow.python.pywrap_mlir import import_graphdef
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.python.compiler.mlir import mlir
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
model = layers.MultiHeadAttention(num_heads=2, key_dim=2)
target = tf.keras.Input(shape=[8, 16], batch_size=64, dtype=tf.float32)
source = tf.keras.Input(shape=[4, 16], batch_size=64, dtype=tf.float32)
@chelini
chelini / bert-tiny.mlir
Created June 6, 2023 07:23
BertForMaskedLM taken from torch-mlir example: `examples/torchscript_stablehlo_backend_tinybert.py`
This file has been truncated, but you can view the full file.
#map = affine_map<(d0, d1) -> (0, d1)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
#map2 = affine_map<(d0, d1, d2, d3) -> (d0, 0, 0, d3)>
#map3 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
#map4 = affine_map<(d0, d1, d2, d3) -> ()>
#map5 = affine_map<(d0, d1, d2) -> (d0, d1)>
#map6 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
#map7 = affine_map<(d0, d1, d2) -> (0, d1, d2)>
#map8 = affine_map<(d0, d1, d2) -> (d0, d1, 0)>
#map9 = affine_map<(d0, d1, d2) -> (d2)>
import tensorflow as tf
from tensorflow.python.pywrap_mlir import import_graphdef
from tensorflow import keras
from tensorflow.keras import layers
class DotProductAttention(keras.layers.Layer):
def __init__(self, use_scale=True, **kwargs):
super(DotProductAttention, self).__init__(**kwargs)
self.use_scale = use_scale
@chelini
chelini / concat_with_pad.mlir
Last active May 31, 2023 14:47
Concat with Pad
#map2 = affine_map<(d0, d1, d2) -> (d0, d1)>
#map3 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
func.func @main(%arg0 : tensor<1x384xi32>) -> tensor<1x384x384xf32> {
%c1 = arith.constant 1 : index
%c0 = arith.constant 0 : index
%cst_607 = arith.constant 0.000000e+00 : f32
%cst_608 = arith.constant dense<0.00165016507> : tensor<1x30522x128xf32>
%10 = tensor.empty() : tensor<1x384x128xf32>
@chelini
chelini / mobilebert-add-in-place.mlir
Created May 22, 2023 14:26
Make the add in place
#map = affine_map<(d0, d1) -> (d1)>
#map1 = affine_map<(d0, d1) -> (d0, d1)>
module {
func.func @main(%arg0: tensor<32x384xf32>, %arg1: tensor<384x32xf32>, %arg2: tensor<32x384xf32>, %arg3: tensor<32x384xf32>, %arg4: tensor<32x384xf32>, %arg5: tensor<1x384x32xf32>, %arg6: tensor<1x384x32xf32>, %arg7: tensor<1x384x32xf32>) -> tensor<1536x384xf32> {
%cst = arith.constant 0.000000e+00 : f32
%cst_0 = arith.constant dense<0.00164203614> : tensor<384xf32>
%0 = tensor.empty() : tensor<384x384xf32>
%1 = linalg.fill ins(%cst : f32) outs(%0 : tensor<384x384xf32>) -> tensor<384x384xf32>
%2 = linalg.matmul ins(%arg1, %arg2 : tensor<384x32xf32>, tensor<32x384xf32>) outs(%1 : tensor<384x384xf32>) -> tensor<384x384xf32>
%3 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "parallel"]} ins(%cst_0 : tensor<384xf32>) outs(%2 : tensor<384x384xf32>) {
@chelini
chelini / MobileBert
Created May 22, 2023 09:56
MobileBert stripped linalg.
This file has been truncated, but you can view the full file.
#map = affine_map<(d0, d1) -> (d0, d1)>
#map1 = affine_map<(d0, d1, d2) -> (d0, d2)>
#map2 = affine_map<(d0, d1, d2) -> (d0, d1)>
#map3 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
#map4 = affine_map<(d0, d1, d2, d3) -> (d0, d1)>
#map5 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>
#map6 = affine_map<(d0, d1) -> (d1)>
#map7 = affine_map<(d0, d1, d2, d3) -> (d0, d2, d1, d3)>
#map8 = affine_map<(d0, d1, d2, d3) -> (d0, d3, d1, d2)>
#map9 = affine_map<(d0, d1, d2, d3) -> (d0, d1, d3, d2)>