8277304: Java support for FP16 #164
8277304: Java support for FP16 #164smita-kamath wants to merge 12 commits intoopenjdk:vectorIntrinsics+fp16from
Conversation
|
👋 Welcome back svkamath! A progress list of the required criteria for merging this PR into |
Webrevs
|
|
I could be useful to support Brainfloats in addition to standard FP16? https://en.wikipedia.org/wiki/Bfloat16_floating-point_format |
|
Hi Smita, |
…moves redundant code from classes
|
convert0 routine should be extended for half float types. |
| static final short MAX_VALUE = valueOf(0x1.fffffeP+127f); | ||
| /* Definitions for FP16 */ | ||
| static final short MIN_VALUE = valueOf(0x0.000002P-126f); | ||
| /* Definitions for FP16 */ | ||
| static final short POSITIVE_INFINITY = valueOf(1.0f/0.0f); | ||
| /* Definitions for FP16 */ | ||
| static final short NEGATIVE_INFINITY = valueOf(-1.0f/0.0f); | ||
| /* Definitions for FP16 */ | ||
| static final int SIZE = 16; | ||
| /* Definitions for FP16 */ | ||
| static final int BYTES = SIZE / Byte.SIZE; |
There was a problem hiding this comment.
All static constants should be made public on the lines of Float.MAX_VALUE.
| static final short MAX_VALUE = valueOf(0x1.fffffeP+127f); | ||
| /* Definitions for FP16 */ | ||
| static final short MIN_VALUE = valueOf(0x0.000002P-126f); |
There was a problem hiding this comment.
Min and max value needs to be correctly updated.
| * @since 10/01/2021 | ||
| */ | ||
| @SuppressWarnings("cast") | ||
| public final class Halffloat { |
There was a problem hiding this comment.
Should Halffloat be a sub-class of Number and implement Comparable interfaces., like BigInteger which is another non-primitive box type.
| * @return short value of float provided | ||
| */ | ||
| public static short valueOf(float f) { | ||
| int val = Float.floatToIntBits(f); |
There was a problem hiding this comment.
As per JVM specification of d2f semantics, "A finite value too small to be represented as a float is converted
to a zero of the same sign; a finite value too large to be represented
as a float is converted to an infinity of the same sign. A double
NaN is converted to a float NaN."
I think we can apply same semantics for halffloat values if floting point argument is beyond the halffloat range.
| #if[short] | ||
| wb_.get{#if[byte]?(:Short(}o + i * $sizeInBytes$)); | ||
| #else[short] | ||
| wb_.get{#if[byte]?(:$Type$(}o + i * $sizeInBytes$)); | ||
| #end[short] |
There was a problem hiding this comment.
Please introduce a new abstract variable in generation script which is Short for halfloat or $Type otherwise. IT will help in folding this special handling.
| #if[short] | ||
| public static final VectorSpecies<Halffloat> SPECIES_PREFERRED | ||
| = VectorSpecies.ofPreferred(Halffloat.class); | ||
| #end[short] | ||
| #else[FP] | ||
| public static final VectorSpecies<$Boxtype$> SPECIES_PREFERRED | ||
| = ($Type$Species) VectorSpecies.ofPreferred($type$.class); |
There was a problem hiding this comment.
Please introduce a new abstract variable in generation script which is Halffloat for halfloat case or $type otherwise. IT will help in folding this special handling.
| @@ -0,0 +1,112 @@ | |||
| /* | |||
| * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved. | |||
There was a problem hiding this comment.
Copyright year should be 2022,
| return s.ldOp(wb, off, vm, | ||
| (wb_, o, i) -> wb_.getInt(o + i * 4)); | ||
| (wb_, o, i) -> | ||
| wb_.getInt(o + i * 4)); | ||
| }); |
There was a problem hiding this comment.
Kindly remove these formatting changes from all integral type vector classes, they are anyways not impacting logic.
| public float floatValue() { | ||
| int val = (int)value; | ||
| float f = Float.intBitsToFloat(((val&0x8000)<<16) | (((val&0x7c00)+0x1C000)<<13) | ((val&0x03FF)<<13)); | ||
| return f; | ||
| } |
There was a problem hiding this comment.
Please add a switch to handle special case values (NaN, Inf, -Inf), i.e. HF.NaN -> Float.NaN :
| * @return boolean value | ||
| */ | ||
| public static boolean isFinite(float f) { | ||
| return Math.abs(f) <= Halffloat.MAX_VALUE; |
There was a problem hiding this comment.
We are comparing abs value with short. wont work well.
| if (!isFinite(f)) return Halffloat.POSITIVE_INFINITY; | ||
|
|
||
| int val = Float.floatToIntBits(f); |
There was a problem hiding this comment.
Handling for NaN and NEG_INF missing.
| * Halffloat constructor | ||
| * @param value short value assigned to halffloat | ||
| */ | ||
| public Halffloat(short value) { |
There was a problem hiding this comment.
It will be useful if we can have a constructor accepting float also.
| * @return short value of float provided | ||
| */ | ||
| public static short valueOf(float f) { | ||
| if (!Float.isFinite(f)) return Halffloat.POSITIVE_INFINITY; |
There was a problem hiding this comment.
Will be appropriate if range bound checks are done against Halffloat bounds.
| if (f < Halffloat.MIN_FLOAT_VALUE) return Halffloat.NEGATIVE_INFINITY; | ||
|
|
||
| int val = Float.floatToIntBits(f); | ||
| val = ((((val>>16)&0x8000)|((((val&0x7f800000)-0x38000000)>>13)&0x7c00)|((val>>13)&0x03ff))); |
There was a problem hiding this comment.
Kindly also add the link to paper.
|
Initial version looks ok to me, we can do refinement in subsequent patches. |
|
@smita-kamath This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been no new commits pushed to the As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@jatin-bhateja, @sviswa7) but any other Committer may sponsor as well. ➡️ To flag this PR as ready for integration with the above commit message, type |
|
Reference to Fast float to halffloat conversion: http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf |
jatin-bhateja
left a comment
There was a problem hiding this comment.
Failures seen during jtreg, needs fix.
|
/integrate |
|
@smita-kamath |
|
/sponsor |
|
Going to push as commit 6b7b107. |
|
@sviswa7 @smita-kamath Pushed as commit 6b7b107. 💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored. |
Initial FP16 vectorAPI Java support.
Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/panama-vector pull/164/head:pull/164$ git checkout pull/164Update a local copy of the PR:
$ git checkout pull/164$ git pull https://git.openjdk.org/panama-vector pull/164/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 164View PR using the GUI difftool:
$ git pr show -t 164Using diff file
Download this PR as a diff file:
https://git.openjdk.org/panama-vector/pull/164.diff