From 72f676656c71958ee93fa5b8f865eff1b9be815f Mon Sep 17 00:00:00 2001 From: Max Ren Date: Mon, 2 Oct 2023 16:56:09 -0700 Subject: [PATCH] Cast long inputs to float32 before feeding to xnnpack graph runtime (#553) Summary: In DeepLab v3, we saw that some inputs are int64, (this is largely from interpolation decomposition). XNNPACK can not handle int64, which means that the int64 inputs would wrongly be interpreted as float32. In order to correctly feed the right inputs to XNNPACK, we must cast the int64 data to float32. We do something similar when casting int32 data to int64 when we are returning indicies from argmax pooling. Reviewed By: digantdesai Differential Revision: D49797819 --- backends/xnnpack/runtime/XNNExecutor.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/backends/xnnpack/runtime/XNNExecutor.cpp b/backends/xnnpack/runtime/XNNExecutor.cpp index 384b44d1f3b..5060b96e58e 100644 --- a/backends/xnnpack/runtime/XNNExecutor.cpp +++ b/backends/xnnpack/runtime/XNNExecutor.cpp @@ -68,6 +68,17 @@ Error XNNExecutor::set_external_input(uint32_t id, Tensor* input) { return Error::NotSupported; #endif } else { + // TODO(T165403530): Test insure accuracy for int64 --> float32 conversion + if (input->scalar_type() == ScalarType::Long) { + // Input data type is int64. However, XNNPACK doesn't support + // int64. This means that the data needs to be casted to float + // In order for XNNPACK to properly use it. + const int64_t* data_64 = input->const_data_ptr(); + float* data_f32 = input->mutable_data_ptr(); + for (int j = 0; j < input->numel(); j++) { + data_f32[j] = data_64[j]; + } + } externals_.emplace_back(xnn_external_value{id, input->mutable_data_ptr()}); } return Error::Ok;