Skip to content

Integer floor for negative input yields wrong result #2174

@Gerstenberger

Description

@Gerstenberger

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:
int_model
Graph for floats:
float_model

Additional context

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAn unexpected problem or unintended behaviorpending on user responseWaiting for more information or validation from user

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions