Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Made tf.abs underflow-safe on CPU & GPU and added test for it. #1391

Merged
merged 4 commits into from
Nov 15, 2018

Conversation

DirkToewe
Copy link
Contributor

@DirkToewe DirkToewe commented Nov 14, 2018

Description

Fixes part of #895. The CPU backend now uses Math.hypot and the WebGL backend uses length albeit with some workaround to ensure underflow-safety (which is otherwise not given, at least on Intel GPUs).


For repository owners only:

Please remember to apply all applicable tags to your pull request.
Tags: FEATURE, BREAKING, BUG, PERF, DEV, DOC, SECURITY

For more info see: /~https://github.com/tensorflow/tfjs/blob/master/DEVELOPMENT.md


This change is Reviewable

Copy link
Contributor

@nsthorat nsthorat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix, really nice work! I sent some minor styling comments just so you get familiar with our style for the future :)

Reviewable status: 0 of 1 approvals obtained (waiting on @DirkToewe)


src/kernels/webgl/complex_abs_gpu.ts, line 29 at r1 (raw file):

    this.userCode = `
      void main() {
        float re = abs( getRealAtOutCoords() );

could you remove the extra white space? after and before the parens (here and below)


src/kernels/webgl/complex_abs_gpu.ts, line 31 at r1 (raw file):

        float re = abs( getRealAtOutCoords() );
        float im = abs( getImagAtOutCoords() );
        float mx = max(re,im);

there should be an extra space between re, im


src/kernels/webgl/complex_abs_gpu.ts, line 34 at r1 (raw file):

        // sadly the length function in glsl is not underflow-safe
        // (at least not on Integl GPUs). So the safe solution is

Intel


src/kernels/webgl/complex_abs_gpu.ts, line 37 at r1 (raw file):

        // to ensure underflow-safety in all cases.
        setOutput(
          mx * length(  vec2(1, min(re,im)/mx)  )

remove the extra spaces inside of length()


src/ops/unary_ops_test.ts, line 173 at r1 (raw file):

  it('is underflow-safe for complex64', () => {
    const a = tf.complex(
       /*re=*/[ 1e-30, 0     ],

no need for these comments :)

@DirkToewe
Copy link
Contributor Author

Done, hope I didn't forget anything.

Copy link
Contributor

@nsthorat nsthorat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM -- I will merge after one small change which will fix the travis build

Reviewable status: 0 of 1 approvals obtained (waiting on @DirkToewe)


src/ops/unary_ops_test.ts, line 173 at r2 (raw file):

  it('is underflow-safe for complex64', () => {
    const a = tf.complex(
       [1e-30, 0    ],

One last comment, instead of 1e-30 can you use tf.ENV.get('EPSILON') which is the smallest representable value larger than zero (works on all devices, this is why travis is failing for iOS which is float16 which has a larger epsilon).

@DirkToewe
Copy link
Contributor Author

DirkToewe commented Nov 14, 2018

eps² will not underflow, but I tried to improvise...

There was a 0/0 bug that caused the NaN and which should now be fixed.

@nsthorat
Copy link
Contributor

Hm, I know what the issue is (we're uploading this as a uniform) so I'll merge this change and then fix it :)

@nsthorat
Copy link
Contributor

Really nice work!

@DirkToewe
Copy link
Contributor Author

DirkToewe commented Nov 15, 2018

Thank You!
What issue are You referring to? The 0/0 was in complex_abs_gpu and I hope I fixed it. My other statement about eps² generally not rounding down to zero is not a bug. What would be necessary is another constant like ENV.get('MIN_VALUE') to determine a value that is close to underflow.

Take float64 for example: Number.EPSILON=2.220446049250313e-16 is actually a ginormously huge number compared to Number.MIN_VALUE=5e-324. It is only small compared to one in that 1+eps = 1.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants