[Tensorflow] The following operations cannot be legalized: tf.VariableV2

Hello, this is my first time posting here. Let me know whether this question is suited to be posted to this forum or not.

I have been trying to translate tensorflow models into MLIR. As such I successfully used tf-mlir-translate , tf-opt with very simple TF models like Conv2D, MatMul,MaxPool…, achieving the expected results.

However, if I try to do the same with the official example tf.Variable with the three steps below:

tf-mlir-translate -graphdef-to-mlir -tf-enable-shape-inference-on-import=false target.pbtxt -tf-prune-unused-nodes -tf-control-output-arrays=Variable/Assign,AssignAdd -o target.mlir
tf-opt -tf-executor-to-functional-conversion target.mlir -o target-func.mlir
tf-opt --tf-to-hlo-pipeline target-func.mlir -o target-mhlo.mlir

I get the following error message with the 3’th step:

target-func.mlir:2:3: error: The following operations cannot be legalized: tf.Assign (count: 1); tf.AssignAdd (count: 1); tf.VariableV2 (count: 1). These legalization failure(s) may be due to missing TF to HLO lowerings and/or unsupported attributes, etc.
  func @main() attributes {tf.entry_function = {control_outputs = "Variable/Assign,AssignAdd", inputs = "", outputs = ""}} {
  ^
target-func.mlir:2:3: error: Emitting more detail about one op that failed to legalize...
  func @main() attributes {tf.entry_function = {control_outputs = "Variable/Assign,AssignAdd", inputs = "", outputs = ""}} {
  ^
target-func.mlir:7:10: error: 'tf.Assign' op is not legalizable
    %2 = "tf.Assign"(%0, %cst_0) {_class = ["loc:@Variable"], device = "", use_locking = true, validate_shape = true} : (tensor<!tf.f32ref>, tensor<f32>) -> tensor<*x!tf.f32ref>
         ^
target-func.mlir:7:10: note: see current operation: %4 = "tf.Assign"(%2, %0) {_class = ["loc:@Variable"], device = "", use_locking = true, validate_shape = true} : (tensor<!tf.f32ref>, tensor<f32>) -> tensor<!tf.f32ref>

As of right now, is there a way to lower these tf operations into mhlo?

Reference variable are deprecated in TensorFlow for > 4 years I believe, and they have never been supported in TF2XLA as far as I know. You need to use resource variables instead.

3ks a lot.
i’ve not found the op definition of ResourceVariable for tf dialect in tf_generated_ops.td. Or maybe it is not supported yet in tf dialect?
and not found any example with ResourceVariable in the tf Official website mlir directory graphdef2mlir . is there any example for ResourceVariable translation from tf to mhlo anywhere?

ResourceVariable isn’t an operation. But if you search for resource you should find plenty of example of tensor wrapping this dtype.

many 3ks, i’m trying the resource examples now.

So far, there are 116 Ops defined in HLO . and no resource variables ops defined yet.

Could i add resource variables ops definitions to HLO? and is this the right way to make it?

Hey,

HLO ops are intended for the numeric computation side and are mostly pure with state management is handled outside them. For TF this is done via TF ops and the state hoisted/sunk outside stateless parts (see tensorflow/resource_op_lifting.mlir at master · tensorflow/tensorflow · GitHub for an example test showing some uses there). While there have been some considerations for managing some state there, there is no plans and unlikely to be added vs having a purer numeric set for compilation.

Related to your question though, there is not yet a process to propose HLOs (closest is probably creating an issue on TF repo)

– Jacques

1 Like

We have issues on the mlir-hlo repo as well, that may be more scoped (pull-requests go to TensorFlow though).