How to mofify tensor type?

I’d like to modify the tensor type
for example, the original IR is:
module {
func.func @forward(%arg0: tensor<1x3x224x224xf32>) → tensor<1x3x224x224xf32> {

return %2 : tensor<1x3x224x224xf32>
expected IR is:
module {
func.func @forward(%arg0: tensor<1x3x224x224xi8>) → tensor<1x3x224x224xi8> {

return %2 : tensor<1x3x224x224xi8>

type converter Pattern Rewriting : Generic DAG-to-DAG Rewriting - MLIR seems to be my solution.
but it is for dialect conversion, my case is transform in one dialect rather than conversion between dialects

what’s more, func bufferize pass seems to be another reference
but it is used for coverting tensor type to memref type by using to_memref, to_tensor op

i wonder if there is reference for my case.


See presentation * 2020-11-19: Type Conversions the Not-So-Hard Way: MLIR’s new composable bufferize passes ; slides - recording

In particular:

  • The dialect conversion infra is a misnomer – it can be used to convert even within the same dialect
  • That describes how to set up the type converter – indeed, func-bufferize is probably your best reference.

thank you very much.
i solved my problem and it works :slight_smile:

I have another question, below IR will work by adding addConversion:
func.func @forward(%arg0: tensor<1x3x224x224xf32>) → tensor<1x3x224x224xf32> {
return %2 : tensor<1x3x224x224xf32>
but it does not work if there is different data type:
func.func @forward(%arg0: tensor<1x3x224x224xf32>) → tensor<1xi64> {
%3 = xxop(%arg0) : tensor<1x3x224x224xf32> → tensor<1xi64>
return %3 : tensor<1xi64>
we only care f32 to int8 conversion, but the exist of 1xi64 will throw error:
error: failed to materialize conversion for block argument #0 that remained live after conversion

i have no idea why it reamined live after conversion, after looking into offical document,
This materialization is used in the following situations:

  • When a block argument has been converted to a different type, but the original argument still has users that will remain live after the conversion process has finished. *

it means Source Materialization should be defined ?

I think what you want is a pass through conversion, like addConversion([](Type type) { return type; }); – that should prevent the conversion from happening for i64. You will also want to adjust your other conversion to only convert i32

1 Like