torch-mlir icon indicating copy to clipboard operation
torch-mlir copied to clipboard

can we support float16(half) datatype?

Open FullZing opened this issue 2 years ago • 6 comments

my model is float16 model which failed to transform in torch-mlir https://github.com/llvm/torch-mlir/blob/32159c4e545359d4ad9e3b492c07e07528f5b47e/python/torch_mlir/dialects/torch/importer/jit_ir/csrc/torch_to_mlir_utils.cpp#L327

case ScalarType::Int: case ScalarType::Long: case ScalarType::Float: case ScalarType::Double: case ScalarType::Bool: case ScalarType::QInt8: case ScalarType::BFloat16: default: throwUnsupportedTensorError(); }

can we support fp16?

FullZing avatar May 14 '22 09:05 FullZing

This is probably easy given the Half is a native APFloat type in LLVM. You probably want to look at @ashay 's commit adding BF16 here: https://github.com/llvm/torch-mlir/pull/761

powderluv avatar May 14 '22 23:05 powderluv

maybe not.

below are interface exposed from MLIR, as you can see, it doesnot support Half :( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrBoolGet( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrUInt8Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrInt8Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrUInt16Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrInt16Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrUInt32Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrInt32Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrUInt64Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrInt64Get( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrFloatGet( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrDoubleGet( MLIR_CAPI_EXPORTED MlirAttribute mlirDenseElementsAttrBFloat16Get(

FullZing avatar May 16 '22 06:05 FullZing

@FullZing Yeah, we were in a similar situation for BFloat16, so I added the mlirDenseElementsAttrBFloat16Get() to the MLIR C API in https://reviews.llvm.org/D123981. Perhaps you could add a similar function upstream for Half.

ashay avatar May 16 '22 06:05 ashay

+1, this should be quite easy to add by repeating https://reviews.llvm.org/D123981 + https://github.com/llvm/torch-mlir/pull/761 but for float16

@FullZing -- can you do that?

silvasean avatar May 16 '22 08:05 silvasean

@ashay @silvasean I have added a patch: https://reviews.llvm.org/D130069; please review for me.

tanyokwok avatar Jul 19 '22 09:07 tanyokwok

@fortianyou That patch looks good to me, but could you add "ashay-github" as a reviewer? I botched the signup process on Phabricator, which is why it says that the email address for the account "ashay" was not verified. Sorry about the hassle!

ashay avatar Jul 19 '22 15:07 ashay

We added support for this in #1169

silvasean avatar Oct 07 '22 14:10 silvasean