Skip to content

8277304: Java support for FP16 #164

Closed
smita-kamath wants to merge 12 commits intoopenjdk:vectorIntrinsics+fp16from
smita-kamath:fp16
Closed

8277304: Java support for FP16 #164
smita-kamath wants to merge 12 commits intoopenjdk:vectorIntrinsics+fp16from
smita-kamath:fp16

Conversation

@smita-kamath
Copy link
Contributor

@smita-kamath smita-kamath commented Nov 16, 2021

Initial FP16 vectorAPI Java support.


Progress

  • Change must not contain extraneous whitespace
  • Change must be properly reviewed (1 review required, with at least 1 Committer)

Issue

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/panama-vector pull/164/head:pull/164
$ git checkout pull/164

Update a local copy of the PR:
$ git checkout pull/164
$ git pull https://git.openjdk.org/panama-vector pull/164/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 164

View PR using the GUI difftool:
$ git pr show -t 164

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/panama-vector/pull/164.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 16, 2021

👋 Welcome back svkamath! A progress list of the required criteria for merging this PR into vectorIntrinsics+fp16 will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@smita-kamath smita-kamath changed the title FP16 Java support 8273322: Java support for FP16 Nov 17, 2021
@smita-kamath smita-kamath changed the title 8273322: Java support for FP16 8277304: Java support for FP16 Nov 17, 2021
@smita-kamath smita-kamath marked this pull request as ready for review November 17, 2021 00:17
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 17, 2021
@mlbridge
Copy link

mlbridge bot commented Nov 17, 2021

@LifeIsStrange
Copy link

LifeIsStrange commented Nov 24, 2021

I could be useful to support Brainfloats in addition to standard FP16? https://en.wikipedia.org/wiki/Bfloat16_floating-point_format

@jatin-bhateja
Copy link
Member

Hi Smita,
Following patch fixes the failing base testcase.
Thanks,
Jatin

diff --git a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractSpecies.java b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractSpecies.java
index 18c0cb9a725..9def1acdd61 100644
--- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractSpecies.java
+++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractSpecies.java
@@ -298,7 +298,16 @@ abstract class AbstractSpecies<E> extends jdk.internal.vm.vector.VectorSupport.V
         return makeDummyVector();
     }
     private AbstractVector<E> makeDummyVector() {
-        Object za = Array.newInstance(elementType(), laneCount);
+        Object za;
+        // FIXME: Remove following special handling for Halffloat
+        // till Valhalla integration when Halffloat will become a
+        // primitive class.
+        if (elementType() == Halffloat.class)  {
+           za = Array.newInstance(short.class, laneCount);
+        } else {
+           za = Array.newInstance(elementType(), laneCount);
+        }
+
         return dummyVector = vectorFactory.apply(za);
         // This is the only use of vectorFactory.
         // All other factory requests are routed
diff --git a/test/jdk/jdk/incubator/vector/AddTest.java b/test/jdk/jdk/incubator/vector/AddTest.java
index 7c6329a7a3c..7ce077f75ec 100644
--- a/test/jdk/jdk/incubator/vector/AddTest.java
+++ b/test/jdk/jdk/incubator/vector/AddTest.java
@@ -44,20 +44,18 @@ public class AddTest {
     static short[] a = new short[SIZE];
     static short[] b = new short[SIZE];
     static short[] c = new short[SIZE];
-
     static {
         for (int i = 0; i < SIZE; i++) {
-            a[i] = 0x3C66;
-            b[i] = 0x4066;
+            a[i] = Halffloat.valueOf((float)i);
+            b[i] = Halffloat.valueOf((float)i);
         }
     }
 
     static void workload() {
         for (int i = 0; i < a.length; i += SPECIES.length()) {
             HalffloatVector av = HalffloatVector.fromArray(SPECIES, a, i);
-            //HalffloatVector bv = HalffloatVector.fromArray(SPECIES, b, i);
-            //av.add(bv).intoArray(c, i);
-            av.intoArray(c,i);
+            HalffloatVector bv = HalffloatVector.fromArray(SPECIES, b, i);
+            av.add(bv).intoArray(c, i);
         }
     }
 
@@ -76,8 +74,14 @@ public class AddTest {
             workload();
         }
         for (int i = 0; i < a.length; i++) {
-            if (c[i] != a[i] + b[i])
+            Halffloat hfa = new Halffloat(a[i]);
+            Halffloat hfb = new Halffloat(b[i]);
+            Halffloat hfc = new Halffloat(c[i]);
+
+            if (hfc.floatValue() != (hfa.floatValue() + hfb.floatValue())) {
+                System.out.println("RES: " + hfc.floatValue() + " EXPECTED: " + (hfa.floatValue() + hfb.floatValue()));
                 throw new AssertionError();
+            }
         }
 
         /*Arrays.fill(c, 0.0f);
@@ -89,5 +93,6 @@ public class AddTest {
             if (c[i] != a[i] + b[i])
                 throw new AssertionError();
         }*/
+        System.out.println("PASSED");
     }
 }

@openjdk openjdk bot removed the rfr Pull request is ready for review label Jun 13, 2022
@jatin-bhateja
Copy link
Member

Comment on lines 41 to 51
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;
Copy link
Member

Choose a reason for hiding this comment

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

All static constants should be made public on the lines of Float.MAX_VALUE.

Comment on lines 41 to 43
static final short MAX_VALUE = valueOf(0x1.fffffeP+127f);
/* Definitions for FP16 */
static final short MIN_VALUE = valueOf(0x0.000002P-126f);
Copy link
Member

Choose a reason for hiding this comment

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

Min and max value needs to be correctly updated.

* @since 10/01/2021
*/
@SuppressWarnings("cast")
public final class Halffloat {
Copy link
Member

Choose a reason for hiding this comment

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

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);
Copy link
Member

Choose a reason for hiding this comment

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

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.

Comment on lines 4998 to 5002
#if[short]
wb_.get{#if[byte]?(:Short(}o + i * $sizeInBytes$));
#else[short]
wb_.get{#if[byte]?(:$Type$(}o + i * $sizeInBytes$));
#end[short]
Copy link
Member

Choose a reason for hiding this comment

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

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.

Comment on lines 5767 to 5773
#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);
Copy link
Member

Choose a reason for hiding this comment

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

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.
Copy link
Member

@jatin-bhateja jatin-bhateja Jun 13, 2022

Choose a reason for hiding this comment

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

Copyright year should be 2022,

Comment on lines 3496 to 3501
return s.ldOp(wb, off, vm,
(wb_, o, i) -> wb_.getInt(o + i * 4));
(wb_, o, i) ->
wb_.getInt(o + i * 4));
});
Copy link
Member

@jatin-bhateja jatin-bhateja Jun 13, 2022

Choose a reason for hiding this comment

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

Kindly remove these formatting changes from all integral type vector classes, they are anyways not impacting logic.

@openjdk openjdk bot added the rfr Pull request is ready for review label Jun 14, 2022
Comment on lines 71 to 75
public float floatValue() {
int val = (int)value;
float f = Float.intBitsToFloat(((val&0x8000)<<16) | (((val&0x7c00)+0x1C000)<<13) | ((val&0x03FF)<<13));
return f;
}
Copy link
Member

Choose a reason for hiding this comment

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

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;
Copy link
Member

Choose a reason for hiding this comment

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

We are comparing abs value with short. wont work well.

Comment on lines 92 to 94
if (!isFinite(f)) return Halffloat.POSITIVE_INFINITY;

int val = Float.floatToIntBits(f);
Copy link
Member

@jatin-bhateja jatin-bhateja Jun 15, 2022

Choose a reason for hiding this comment

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

Handling for NaN and NEG_INF missing.

* Halffloat constructor
* @param value short value assigned to halffloat
*/
public Halffloat(short value) {
Copy link
Member

Choose a reason for hiding this comment

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

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;
Copy link
Member

Choose a reason for hiding this comment

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

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)));
Copy link
Member

Choose a reason for hiding this comment

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

Kindly also add the link to paper.

@jatin-bhateja
Copy link
Member

Initial version looks ok to me, we can do refinement in subsequent patches.

@openjdk
Copy link

openjdk bot commented Jun 16, 2022

@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:

8277304: Java support for FP16

Reviewed-by: sviswanathan

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 vectorIntrinsics+fp16 branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential automatic rebasing, please check the documentation for the /integrate command for further details.

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 /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 16, 2022
@smita-kamath
Copy link
Contributor Author

Reference to Fast float to halffloat conversion: http://fox-toolkit.org/ftp/fasthalffloatconversion.pdf

Copy link
Member

@jatin-bhateja jatin-bhateja left a comment

Choose a reason for hiding this comment

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

Failures seen during jtreg, needs fix.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Jun 16, 2022
Copy link
Collaborator

@sviswa7 sviswa7 left a comment

Choose a reason for hiding this comment

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

Looks good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Jun 22, 2022
@smita-kamath
Copy link
Contributor Author

/integrate

@openjdk openjdk bot added the sponsor Pull request is ready to be sponsored label Jun 22, 2022
@openjdk
Copy link

openjdk bot commented Jun 22, 2022

@smita-kamath
Your change (at version 536b40a) is now ready to be sponsored by a Committer.

@sviswa7
Copy link
Collaborator

sviswa7 commented Jun 22, 2022

/sponsor

@openjdk
Copy link

openjdk bot commented Jun 22, 2022

Going to push as commit 6b7b107.

@openjdk openjdk bot added the integrated Pull request has been integrated label Jun 22, 2022
@openjdk openjdk bot closed this Jun 22, 2022
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review sponsor Pull request is ready to be sponsored labels Jun 22, 2022
@openjdk
Copy link

openjdk bot commented Jun 22, 2022

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

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

Labels

integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

4 participants