Hi! I’m new here so please let me know if this question is better suited for elsewhere.
I am trying to translate some of the hugging-face tensorflow models to tosa mlir, but I am running into some trouble as it seems like the translations of some tensorflow operations are dropped.
First I use the following python code to translate the model to a pbtxt:
import tensorflow as tf
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
from transformers import AutoConfig, TFAutoModel
config = AutoConfig.from_pretrained('bert-base-cased')
model = TFAutoModel.from_config(config)
# Convert model to ConcreteFunction
full_model = tf.function(lambda x: model(x))
full_model = full_model.get_concrete_function(
tf.TensorSpec(model.dummy_inputs["input_ids"].shape, model.dummy_inputs["input_ids"].dtype))
# frozen ConcreteFunction
frozen_func = convert_variables_to_constants_v2(full_model)
frozen_func.graph.as_graph_def()
print("Frozen model inputs: ")
print(frozen_func.inputs)
print("Frozen model outputs: ")
print(frozen_func.outputs)
tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
logdir='',
name=f"frozen_model_bert.pbtxt",
as_text=True)
Then I use tf-mlir-translate to translate the resulting pbtxt into tf dialect using this command:
tf-mlir-translate --tf-input-arrays=x:0 --tf-input-shapes=3,5 --tf-output-arrays=Identity:0,Identity_1:0 --tf-enable-shape-inference-on-import --graphdef-to-mlir frozen_model_bert.pbtxt -o bert_tf.mlir
Finally, I use tf-opt to translate tf dialect to tosa using this command:
tf-opt --tf-to-tosa-pipeline --tf-executor-break-up-islands --tf-executor-graph-pruning bert_tf.mlir -o bert_tosa.mlir
I am able to generate the tosa mlir file without errors, but the resulting mlir has a mix of the tf dialect and the tosa dialect. For example here is a couple of snippets of the generated MLIR:
%outputs_62, %control_63 = tf_executor.island wraps "tosa.const"() {value = dense<0> : tensor<i32>} : () -> tensor<i32>
%outputs_64, %control_65 = tf_executor.island wraps "tf.Range"(%outputs_62, %outputs_60, %outputs_58) {device = ""} : (tensor<i32>, tensor<i32>, tensor<i32>) -> tensor<5xi32>
%outputs_66, %control_67 = tf_executor.island wraps "tf.ExpandDims"(%outputs_64, %outputs_26) {device = ""} : (tensor<5xi32>, tensor<i32>) -> tensor<1x5xi32>
%outputs_68, %control_69 = tf_executor.island wraps "tf.GatherV2"(%outputs_34, %outputs_66, %outputs_32) {batch_dims = 0 : i64, device = ""} : (tensor<512x768xf32>, tensor<1x5xi32>, tensor<i32>) -> tensor<1x5x768xf32>
%outputs_70, %control_71 = tf_executor.island wraps "tf.Tile"(%outputs_68, %outputs_56) {device = ""} : (tensor<1x5x768xf32>, tensor<3xi32>) -> tensor<3x5x768xf32>
%outputs_72, %control_73 = tf_executor.island wraps "tosa.const"() {value = dense<0.000000e+00> : tensor<768xf32>} : () -> tensor<768xf32>
%outputs_1195, %control_1196 = tf_executor.island wraps "tosa.add"(%outputs_1193, %outputs_40) : (tensor<3x5x768xf32>, tensor<3x5x768xf32>) -> tensor<3x5x768xf32>
%outputs_1197, %control_1198 = tf_executor.island wraps "tf.Mean"(%outputs_1195, %outputs_52) {device = "", keep_dims = true} : (tensor<3x5x768xf32>, tensor<1xi32>) -> tensor<3x5x1xf32>
%outputs_1199, %control_1200 = tf_executor.island wraps "tosa.identity"(%outputs_1197) : (tensor<3x5x1xf32>) -> tensor<3x5x1xf32>
%outputs_1201, %control_1202 = tf_executor.island wraps "tosa.sub"(%outputs_1195, %outputs_1199) : (tensor<3x5x768xf32>, tensor<3x5x1xf32>) -> tensor<3x5x768xf32>
%outputs_1203, %control_1204 = tf_executor.island wraps "tosa.mul"(%outputs_1201, %outputs_1201) {shift = 0 : i32} : (tensor<3x5x768xf32>, tensor<3x5x768xf32>) -> tensor<3x5x768xf32>
Is there a way to get a full translation to tosa? Looking at legalize_tf.cc it looks like most of these ops should be supported? Am I going about attempting to translate the model correctly, or is there a better way to do it?