Skip to content

gh-153740: Optimize PyFloat_Pack/Unpack2 using native _Float16 - #154796

Open
skirpichev wants to merge 21 commits into
python:mainfrom
skirpichev:use-_Float16/153740
Open

gh-153740: Optimize PyFloat_Pack/Unpack2 using native _Float16#154796
skirpichev wants to merge 21 commits into
python:mainfrom
skirpichev:use-_Float16/153740

Conversation

@skirpichev

@skirpichev skirpichev commented Jul 28, 2026

Copy link
Copy Markdown
Member

@skirpichev

Copy link
Copy Markdown
Member Author

Not sure how to fix this error:

/usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x25
Objects/floatobject.o: in function `PyFloat_Pack2':
floatobject.c:(.text.PyFloat_Pack2[PyFloat_Pack2]+0x32): undefined reference to `__truncdfhf2'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

CC @StanFromIreland (fuzzers)

@StanFromIreland

Copy link
Copy Markdown
Member

The configure probe only tests a compile-time _Float16 constant, so it reports "yes" in the OSS-Fuzz Docker container where clang accepts the type but the container's pre-GCC-12 libgcc lacks __truncdfhf2 which is the runtime helper the PyFloat_Pack2 double to _Float16 conversion links against. I suggest you make configure do a runtime double to _Float16 conversion.

@skirpichev
skirpichev force-pushed the use-_Float16/153740 branch from 46fe384 to 8bda53a Compare July 30, 2026 00:18
@skirpichev

Copy link
Copy Markdown
Member Author

I suggest you make configure do a runtime double to _Float16 conversion.

Good idea, that works.

@skirpichev
skirpichev marked this pull request as ready for review July 30, 2026 01:03
Comment thread configure.ac Outdated
@skirpichev skirpichev added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 30, 2026
@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @skirpichev for commit ac38cb8 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F154796%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Jul 30, 2026
@skirpichev skirpichev removed their assignment Jul 30, 2026
Comment thread configure.ac Outdated
@skirpichev

This comment was marked as outdated.

@bedevere-bot

This comment was marked as outdated.

@skirpichev

This comment was marked as outdated.

@bedevere-bot

This comment was marked as outdated.

@skirpichev
skirpichev force-pushed the use-_Float16/153740 branch from a69877f to 6bfff56 Compare August 4, 2026 01:15
@skirpichev

Copy link
Copy Markdown
Member Author

!buildbot risc

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @skirpichev for commit 6bfff56 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F154796%2Fmerge

The command will test the builders whose names match following regular expression: risc

The builders matched are:

  • RISC-V 64-bit Ubuntu PR
  • riscv64 Ubuntu PR
  • RISC-V 64-bit Ubuntu NoGIL PR
  • RISC-V 64-bit Ubuntu Clang PR

@skirpichev
skirpichev marked this pull request as ready for review August 4, 2026 03:43

@hpkfft hpkfft 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.

This looks good.
I tested it using Intel's icx compiler and CFLAGS -mavx512f -mavx512fp16 and test_float passes.

Comment thread Objects/floatobject.c Outdated
@skirpichev

Copy link
Copy Markdown
Member Author

CC @vstinner, does it make sense for you?

@vstinner

Copy link
Copy Markdown
Member

CC @vstinner, does it make sense for you?

On the main branch, PyFloat_Pack2() is 107 lines long and PyFloat_Unpack2() is 55 lines long. It's the same C code on all platforms and the test suite pass on all platforms.

With change, PyFloat_Pack2() is 171 lines long and PyFloat_Unpack2() is 112 lines long. So this change makes the C implementation twice bigger and makes it depend on the C compiler (_Float16 support) and the architecture (add #ifndef __riscv).

What are the advantages of using _Float16?

@hpkfft

hpkfft commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

A simpler implementation might be to fall-through to the "main" code path if the input is NaN. I think this would avoid the conditional compilation for __riscv. Something like the following:

int PyFloat_Pack2(double x, char *data, int le) {
#if HAVE_FLOAT16
    if (!isnan(x)) {
        _Float16 y = (_Float16)x;
        // etc.
        return 0;
    }
#endif

@hpkfft

hpkfft commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Building Python with this PR (as it's currently written) and -mavx512f -mavx512fp16, I see performance improve from 5.20 ns to 2.55 ns as measured by the following Google benchmark:

#include <Python.h>
#include <benchmark/benchmark.h>

static void BM_Pack2(benchmark::State& state) {
    char p[2];
    for (auto _ : state)
        PyFloat_Pack2(1.0, p, 1);
}
BENCHMARK(BM_Pack2);

BENCHMARK_MAIN();

on Intel Sapphire Rapids (which supports native float16 arithmetic and conversions).

@skirpichev

Copy link
Copy Markdown
Member Author

What are the advantages of using _Float16?

Speed.

A simpler implementation might be to fall-through to the "main" code path if the input is NaN.

Good idea. The current patch looks close to other cases, where special handling of nans is required. But here an alternative implementation will be kept for a while, lets use this!

Comment thread configure.ac
# Check for native half-float type.
AC_CACHE_CHECK([for _Float16 support], [ac_cv_float16_supported],
WITH_SAVE_ENV([
CFLAGS="$CFLAGS -O0"

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.

Why do you change the compiler flags?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

To disable optimizations.

@vstinner

Copy link
Copy Markdown
Member

What are the advantages of using _Float16?

Speed.

In that case, you should rewrite your PR title to "Optimize PyFloat_Pack/Unpack2 using native _Float16", and provide a benchmark.

@skirpichev skirpichev changed the title gh-153740: use native _Float16 in PyFloat_Pack/Unpack2 gh-153740: Optimize PyFloat_Pack/Unpack2 using native _Float16 Aug 11, 2026
Comment thread Objects/floatobject.c Outdated
uriesmooth

This comment was marked as spam.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants