Skip to content

float16 constant readout doesn't match with it's original numpy array #53260

Closed

Description

@youchangkim

I tried to create float16 constant from numpy array and read it back, but the returned array doesn't match with it's original numpy array. This seems to be happening only at M1 Macs.

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 11.3.1 on M1 Mac mini
  • TensorFlow installed from (source or binary): Binary (tensorflow-macos)
  • TensorFlow version (use command below): unknown 2.5.0
  • Python version: 3.8.12

Standalone code to reproduce the issue

import numpy as np
import tensorflow.compat.v1 as tf

np.random.seed(0)
with tf.Graph().as_default(), tf.Session() as sess:
    x = np.random.sample([1, 1, 1, 1, 768]).astype(np.float16)
    y = tf.constant(x)

    x = np.ones([1, 1, 384, 1, 1], dtype=np.float16)
    y = tf.constant(x, name='97f6762f328ca2b7cceaf2851e0c01a6')
    z = y * 3

    t = sess.graph.get_tensor_by_name('97f6762f328ca2b7cceaf2851e0c01a6:0')
    v = sess.run(t, feed_dict={})

    #print(x.squeeze())
    #print(v.squeeze())
    assert np.array_equiv(x, v)

Describe the current behavior

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    assert np.array_equiv(x, v)
AssertionError

Describe the expected behavior

Two arrays should be equal.

Activity

  1. added
    comp:ops
    TF 2.5
    on Dec 1, 2021
  2. sanatmpa1 commented on Dec 1, 2021

    @sanatmpa1

    @youchangkim,

    It is working fine when checked with TF 2.7.0 on colab. Here's the gist. Since its not a bug from TF end and you also mentioned that the issue is with M1, Can you try posting your question in this tf-metal developer forum of Apple? Thanks!

  3. added
    stat:awaiting response
    on Dec 6, 2021
  4. google-ml-butler commented on Dec 13, 2021

    @google-ml-butler

    This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

  5. added
    stale
    on Dec 13, 2021
  6. google-ml-butler commented on Dec 20, 2021

    @google-ml-butler

    Closing as stale. Please reopen if you'd like to work on this further.

  7. google-ml-butler commented on Dec 20, 2021

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  8. penpornk commented on Mar 8, 2022

    @penpornk
    Member

    @cfRod @everton1984 Have you seen similar issues before? Could you please help try this code on other Arm machines as well? I wonder if this is specific to M1.

    @youchangkim Are these lines necessary in producing the error? (We don't have M1s so it's hard for us to test.)

        x = np.random.sample([1, 1, 1, 1, 768]).astype(np.float16)
        y = tf.constant(x)
    

    Also, do you see the same error in eager mode?

  9. 6 remaining items

  10. youchangkim commented on Mar 10, 2022

    @youchangkim
    Author

    @penpornk I was only able to try 2.7.0 since 2.8.0 requires macOS 12.3 but I'm currently using 11.3. I can try 2.8.0 after I upgrade my OS. Interestingly TF 2.5.0 and 2.7.0 produces different outputs but both are wrong.

    TF 2.5.0

    tracing
    2022-03-09 15:56:44.089426: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
    [6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08 6.e-08
     6.e-08 6.e-08 6.e-08 6.e-08]
    Traceback (most recent call last):
      File "test3.py", line 19, in <module>
        assert np.array_equiv(y, np.ones(y.shape))
    AssertionError
    

    TF 2.7.0

    tracing
    [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
     0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
    Traceback (most recent call last):
      File "test3.py", line 19, in <module>
        assert np.array_equiv(y, np.ones(y.shape))
    AssertionError
    

    Things you asked:

    1. Remove name='97f6762f328ca2b7cceaf2851e0c01a6'

    Fail both 2.5.0 and 2.7.0

    1. Removing both print statements.

    Fail both 2.5.0 and 2.7.0

    1. Removing np.random.seed(0) or setting it to something else.

    Fail both 2.5.0 and 2.7.0, and setting seed to 1 doesn't help

    Applying 1, 2, and 3 at the same time also fails.

    import numpy as np
    import tensorflow as tf
    
    @tf.function
    def my_func():
        x = np.ones([1, 1, 384, 1, 1], dtype=np.float16)
        y = tf.constant(x)
        z = y * 3
    
        return z, y
    
    z, y = my_func()
    
    assert np.array_equiv(y, np.ones(y.shape))
  11. added
    stat:awaiting tensorflower
    on Mar 10, 2022
  12. penpornk commented on May 18, 2022

    @penpornk
    Member

    Quick update:
    Worked with @youchangkim offline and narrowed down to grappler optimization passes. It seems the GraphDef representation of the const all-one fp16 tensor was changed from

    I0331 20:22:16.951792 1635096 convert_attributes.cc:287] GraphDef attr value: tensor {
      dtype: DT_HALF
      tensor_shape {
        dim {
          size: 1
        }
        dim {
          size: 1
        }
        dim {
          size: 384
        }
        dim {
          size: 1
        }
        dim {
          size: 1
        }
      }
      half_val: 15360
    }
    

    to

    I0331 20:22:16.951792 1635096 convert_attributes.cc:287] GraphDef attr value: tensor {
      dtype: DT_HALF
      tensor_shape {
        dim {
          size: 1
        }
        dim {
          size: 1
        }
        dim {
          size: 384
        }
        dim {
          size: 1
        }
        dim {
          size: 1
        }
      }
      half_val: 1
    }
    

    after the tfg_optimizer pass.

  13. self-assigned this
    on Sep 12, 2022
  14. added
    TF 2.9
    and removed
    TF 2.5
    on Sep 12, 2022
  15. mohantym commented on Sep 12, 2022

    @mohantym
    Contributor

    @youchangkim ! @penpornk !
    I could replicate this in my mac M1 machine . It is not replicating in Colab though.

  16. removed their assignment
    on Sep 12, 2022
  17. kulinseth commented on Oct 20, 2022

    @kulinseth
    Contributor

    @penpornk any update on this ?
    the FP16 correctness issue seems to be affecting lot of TF core tests when run on Apple Silicon. They all pass on x86 machine like:

        //tensorflow/python/math_grad_test_cpu:
    
        XdivyTest.testNonZeroValuesGrad 
        XdivyTest.testZeroYGrad 
        testNegOneYGrad 
        Xlog1pyTest.testNonZeroValuesGrad 
        XlogyTest.testNonZeroValuesGrad
        XlogyTest.testZeroYGrad
    

    Similarly for mlir to Graph def tests its failing:

    //tensorflow/core/ir/importexport/tests/roundtrip:test21.pbtxt.test
    //tensorflow/core/ir/importexport/tests/roundtrip:negative_zero_constant.pbtxt.test 
    //tensorflow/core/ir/importexport/tests/mlir_to_graphdef:negative_zero_constant.mlir.test
    

    This makes sense as the GraphDef representation changed for Half changed after tfg pass.
    What I didn't follow is why its specific to arm64?

    cc @yarri-oss , @nitins17 and @learning-to-play

  18. penpornk commented on Dec 12, 2022

    @penpornk
    Member

    @youchangkim @kulinseth Sorry for the delay! We have synced offline but I'll post the update here too.

    This is where it went wrong:

    auto value = attr.getSplatValue<Eigen::half>().x;

    MLIR's getSplatValue is supposed to return 15360, but it returned 1.
    Here is a quick patch to work around this issue for now:

    diff --git a/tensorflow/core/ir/importexport/convert_tensor.cc b/tensorflow/core/ir/importexport/convert_tensor.cc
    index 2c6def949a1..a6aacc7a827 100644
    --- a/tensorflow/core/ir/importexport/convert_tensor.cc
    +++ b/tensorflow/core/ir/importexport/convert_tensor.cc
    @@ -342,7 +342,14 @@ void ConvertFloatElementsAttr(const DenseElementsAttr attr,
     void ConvertHalfElementsAttr(const DenseElementsAttr attr,
                                  RepeatedField<int>* output) {
       if (attr.isSplat()) {
    +    // TODO(penporn): Find why Splat doesn't work with Half on MacOS.
    +    // (Getting value = 1 instead of 15360 for a Half value of 1.)
    +#if defined(__APPLE__) && defined(__MACH__)
    +    auto value = *reinterpret_cast<const tensorflow::uint16*>(attr.getRawData().data());
    +#else
         auto value = attr.getSplatValue<Eigen::half>().x;
    +#endif  // __APPLE__ && __MACH__
    +
         if (value != Eigen::half(0) || std::signbit(static_cast<float>(value)))
           output->Add(value);
       } else {
    
  19. penpornk commented on Dec 15, 2022

    @penpornk
    Member

    @cantonios just submitted a proper fix here: d4ea582.
    Closing the issue now. Please feel free to reopen if it still isn't resolved on your side. :)

  20. google-ml-butler commented on Dec 15, 2022

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

TF 2.9comp:opsstat:awaiting tensorflowertype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions