Skip to content

Add support for fastmath 32-bit floating point divide - #6788

Merged
sklam merged 3 commits into
numba:masterfrom
testhound:testhound/fastmath_fdiv
Mar 8, 2021
Merged

sklam merged 3 commits into
numba:masterfrom
testhound:testhound/fastmath_fdiv

Conversation

@testhound

Copy link
Copy Markdown
Contributor

This pull request adds support for generating more efficient code for 32-bit floating point divides in the presence of fastmath. This should be the final piece to: #6183.

@gmarkall gmarkall added 3 - Ready for Review CUDA CUDA related issue/PR labels Mar 3, 2021

@gmarkall gmarkall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the general approach here looks good - I have a few comments about the implementation and testing on the diff. I think with my suggested changes to the maybe_fast_truediv function, the following should be true:

  • A division by zero involving float32 operands in a kernel jitted with debug=True will raise a ZeroDivisionError following the launch.
  • No exception should be raised following the launch of a kernel jitted with fastmath=True, debug=True.

If this is the case, could you add a test that tests both these conditions hold please?

Comment on lines +19 to +20
self.assertIn('div.approx.ftz.f32', fastver.ptx)
self.assertNotIn('div.approx.ftz.f32', precver.ptx)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In addition to replacing the check here, we could also check that the non-fastmath version uses the non-ftz version of the divide too:

Suggested change
self.assertIn('div.approx.ftz.f32', fastver.ptx)
self.assertNotIn('div.approx.ftz.f32', precver.ptx)
self.assertIn('div.approx.ftz.f32', fastver.ptx)
self.assertNotIn('div.approx.ftz.f32', precver.ptx)
self.assertIn('div.rn.f32', precver.ptx)
self.assertNotIn('div.rn.f32', fastver.ptx)

Comment thread numba/cuda/mathimpl.py Outdated
Comment on lines +76 to +77
with cgutils.if_zero(builder, args[1]):
context.error_model.fp_zero_division(builder, ("division by zero",))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this should be moved inside the else branch - if fastmath is on, we don't want to be emitting code to raise on a division by zero.

Comment thread numba/cuda/mathimpl.py Outdated
Comment on lines +80 to +84
fast_replacement = 'fast_fdividef'
actual_libfunc = getattr(libdevice, fast_replacement)
sig = typing.signature(float32, float32, float32)
libfunc_impl = context.get_function(actual_libfunc, sig)
return libfunc_impl(builder, args)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps this can be simplified a bit, i.e.:

Suggested change
fast_replacement = 'fast_fdividef'
actual_libfunc = getattr(libdevice, fast_replacement)
sig = typing.signature(float32, float32, float32)
libfunc_impl = context.get_function(actual_libfunc, sig)
return libfunc_impl(builder, args)
sig = typing.signature(float32, float32, float32)
impl = context.get_function(libdevice.fast_fdividef, sig)
return impl(builder, args)

(I think the pattern using getattr fitted for the trig functions etc. below, but isn't necessary for getting the implementation for a single function)


class TestFastMathOption(CUDATestCase):
@skip_on_cudasim('fast divide not available in CUDASIM')
def test_kernel(self):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For a while I was wondering what this is checking that differs from what test_divf checks for below... Eventually I concluded that it tests that casting an integer to a float32 then using it in a division results in a div.approx instruction being emitted - given how finicky casts and the type system can be, I think it's good to keep this check alongside test_divf - Could you add a comment here explaining that this is testing the cast of an int being used in the divide, to make it a bit easier for future readers to see the difference?

Alternatively - do you have a different view on the difference between this and test_divf? I reached my conclusion by trying to derive meaning from the code (thinking backwards a bit) so maybe there's something else going on here that I haven't followed.

@gmarkall gmarkall added 4 - Waiting on author Waiting for author to respond to review and removed 3 - Ready for Review labels Mar 3, 2021
@gmarkall gmarkall added 4 - Waiting on CI Review etc done, waiting for CI to finish Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm and removed 4 - Waiting on author Waiting for author to respond to review labels Mar 4, 2021

@gmarkall gmarkall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Many thanks for the changes - they look good to me. Tests pass locally with hardware and the simulator.

I think the doc failure is unrelated to the contents of this PR - it is either a transient HTTP failure or an issue that needs resolving with the Numba docs.

@esc Could this have a buildfarm run please?

@esc

esc commented Mar 4, 2021

Copy link
Copy Markdown
Member

Buildfarm ID: numba_smoketest_cuda_yaml_20

@gmarkall

gmarkall commented Mar 5, 2021

Copy link
Copy Markdown
Member

@esc How did it go? :-)

@stuartarchibald

Copy link
Copy Markdown
Contributor

@esc How did it go? :-)

Passed.

@stuartarchibald stuartarchibald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the patch.

@stuartarchibald stuartarchibald added 5 - Ready to merge Review and testing done, is ready to merge BuildFarm Passed For PRs that have been through the buildfarm and passed and removed 4 - Waiting on CI Review etc done, waiting for CI to finish Pending BuildFarm For PRs that have been reviewed but pending a push through our buildfarm labels Mar 8, 2021
@stuartarchibald stuartarchibald added this to the Numba 0.54 RC milestone Mar 8, 2021
@stuartarchibald

Copy link
Copy Markdown
Contributor

Not to resolve in this PR, but this raises an interesting question about flag compatibility, should fastmath and error model be tested to ensure compatibility?

@sklam
sklam merged commit 11b70f5 into numba:master Mar 8, 2021
@testhound
testhound deleted the testhound/fastmath_fdiv branch June 1, 2021 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

5 - Ready to merge Review and testing done, is ready to merge BuildFarm Passed For PRs that have been through the buildfarm and passed CUDA CUDA related issue/PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants