tensorflow / tensorflow

An Open Source Machine Learning Framework for Everyone
https://tensorflow.org
Apache License 2.0
186.33k stars 74.31k forks source link

tf.cast to int8 produce wrong number #79689

Open JonOberry4662 opened 4 days ago

JonOberry4662 commented 4 days ago

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

tf2.15 - 2.17

Custom code

No

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

input is a long list include random numbers. tf.cast to int8 output is different from numpy cast.

The strange thing is if you truncate the long input list to a short list then things works fine.

The code is straight forward, you can reproduce it in the colab

Standalone code to reproduce the issue

https://colab.research.google.com/drive/18dYjvY6JQk79hVq8JsjcWEwIG5KBuF1v?usp=sharing

Relevant log output

NumPy Casted values (int8): 
 [   0    0    0    0    1   -1    0    3   -3    2   -2    0   50   21
 -125   62   25   31  127   -9  117   61  123   47  -20   28   52   36
  -43  -45  -84  118   37  -17   -6  -79    1   75  -45  -60 -103  -63
   85 -112   76   96  -56   86  -32 -108 -105 -121   -2  121   86  -54
   91  -55   36 -119   -3  -36   95  127 -105  -60   37   -9 -106    7
  -31  105   13 -103 -123   79   17  -48 -108  -56  -87 -128   35  -94
  -45  118  -91   86  -63  -43   77    1 -127  -16  -71  -73  -76  -15
  -11]
TensorFlow Casted values (int8): 
 [   0 -128    0    0    1   -1    0    3   -3    2   -2    0  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  127  127
  127  127  127  127  127  127  127  127  127  127  127  127  -76  -15
  -11]
VadisettyRahul commented 4 days ago

Hi @tilakrayal @JonOberry4662

If it's feasible, I'd like to investigate the issue, it seems to me that there are bugs in the casting operations in tensorflow/core/kernels/.

I can try to review the logic that defines the casting behavior for int8. You can add additional checks or adjust the way the values ​​are handled.

What do you think?

JonOberry4662 commented 4 days ago

@VadisettyRahul

Btw it works fine on tf2.12

https://colab.research.google.com/drive/1M0EcrsktHufkVZQomN8Z9eHZ20I35nCl?usp=sharing