-
Notifications
You must be signed in to change notification settings - Fork 463
Description
Describe the bug
Currently, we need a ceildiv operation to compute the sequence lengths after convolution for sequence models.
The ceildiv operation is implemented as
ceildiv(a, b) := -floordiv(-a, b)
For integer inputs, after conversion from TensorFlow to ONNX, this yields the wrong results. Looks like it is doing a floordiv(a, b) instead.
For floating point inputs (changing the signature types), the programs below yield the correct expected result.
Please see example with outputs below in to reproduce
Urgency
Currently we have this case in one of our production models, which yields wrong recognition due to the bug.
System information
- OS Platform and Distribution (e.g., Linux Ubuntu 18.04*): Linux Ubuntu 18.04.6 LTS
- TensorFlow Version: 2.12.0
- Python version: 3.9.13
- ONNX version: 1.14.0
- ONNXRuntime version: 1.14.1
To Reproduce
Running the following program
import tensorflow as tf
x = [3, 7, 10]
class CustomModule(tf.Module):
@tf.function(input_signature=[tf.TensorSpec([None], tf.int32), tf.TensorSpec([], tf.int32)])
def __call__(self, a, b):
return -tf.math.floordiv(-a, b)
ceildiv = CustomModule()
# correctly produces [1, 3, 4]
print(ceildiv(x, 3).numpy())
ceildiv(x, 3)
tf.saved_model.save(ceildiv, "saved-model-example")then exporting it to onnx with
python3 -m tf2onnx.convert --saved-model saved-model-example/ --output example.onnx
and running it with
import numpy as np
import onnxruntime as ort
ort_sess = ort.InferenceSession('example.onnx')
x = np.array([3, 7, 10]).astype(np.int32)
y = np.array([3]).astype(np.int32)
print(ort_sess.run(None, {'a': x, 'b': y}))yields the wrong behaviour.
For me the output is
First Script: [1, 3, 4]
Second Script: [1, 2, 3]
Second Script floats: [1.0, 3.0, 4.0]
Screenshots
Graph for Integer:

Graph for floats:

Additional context
None