-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support half precision floating point types #313
Merged
+2,854
−277
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cd86683
Create new half-float type and array
mairooni 7ccb7bf
Include unittests to check the current functionality of half floats
mairooni bf4fb52
Merge branch 'develop' into feat/float16
mairooni 81c043b
Prototype for half float addition
mairooni b90cb07
Merge branch 'develop' into feat/float16
mairooni 1c9889d
Support the creation of a new HalfFloat instance in the kernel
mairooni eec336e
A node that represents the creation of a new halfnode instance
mairooni 68bdd10
Add support for subtraction, multiplication and division between half…
mairooni be02782
[WIP] Support float-16 types for SPIRV
mairooni d0d6f18
Remove unused method
mairooni f784ce1
Support half-float type for ptx backend
mairooni 31e38a7
Merge branch 'develop' into feat/float16
mairooni 8d7eec4
Merge branch 'develop' into feat/float16
mairooni fccdbec
Merge branch 'develop' into feat/float16
mairooni 45ae694
[half][spirv] Initial support for FP16 (Half) for SPIR-V
jjfumero 53481f4
Merge remote-tracking branch 'refs/remotes/origin/feat/float16' into …
jjfumero 64fdd76
Update license headers with the latest date
mairooni 95a7557
[spirv][half] Initialization supported
jjfumero 7a2f41d
[spirv] Toolkit dependency updated
jjfumero 7b80f92
Merge remote-tracking branch 'refs/remotes/origin/feat/float16' into …
jjfumero 805669e
Query the opencl extensions to check if fp16 is supported
mairooni f2579d1
Merge branch 'develop' into feat/float16
mairooni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
tornado-api/src/main/java/uk/ac/manchester/tornado/api/types/HalfFloat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright (c) 2024, APT Group, Department of Computer Science, | ||
* The University of Manchester. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package uk.ac.manchester.tornado.api.types; | ||
|
||
/** | ||
* This class represents a float-16 instance (half float). The data is stored in a short field, to be | ||
* compliant with the representation for float-16 used in the {@link Float} class. The class encapsulates | ||
* methods for getting the data in float-16 and float-32 format, and for basic arithmetic operations (i.e. | ||
* addition, subtraction, multiplication and division). | ||
*/ | ||
public class HalfFloat { | ||
|
||
private short halfFloatValue; | ||
|
||
/** | ||
* Constructs a new instance of the {@code HalfFloat} out of a float value. | ||
* To convert the float to a float-16, the floatToFloat16 function of the {@link Float} | ||
* class is used. | ||
* | ||
* @param halfFloat | ||
* The float value that will be stored in a half-float format. | ||
*/ | ||
public HalfFloat(float halfFloat) { | ||
this.halfFloatValue = Float.floatToFloat16(halfFloat); | ||
} | ||
|
||
/** | ||
* Constructs a new instance of the {@code HalfFloat} with a given short value. | ||
* | ||
* @param halfFloat | ||
* The short value that represents the half float. | ||
*/ | ||
public HalfFloat(short halfFloat) { | ||
this.halfFloatValue = halfFloat; | ||
} | ||
|
||
/** | ||
* Gets the half-float stored in the class. | ||
* | ||
* @return The half float value stored in the {@code HalfFloat} object. | ||
*/ | ||
public short getHalfFloatValue() { | ||
return this.halfFloatValue; | ||
} | ||
|
||
/** | ||
* Gets the half-float stored in the class in a 32-bit representation. | ||
* | ||
* @return The float-32 equivalent value the half float stored in the {@code HalfFloat} object. | ||
*/ | ||
public float getFloat32() { | ||
return Float.float16ToFloat(halfFloatValue); | ||
} | ||
|
||
/** | ||
* Takes two half float values, converts them to a 32-bit representation and performs an addition. | ||
* | ||
* @param a | ||
* The first float-16 input for the addition. | ||
* @param b | ||
* The second float-16 input for the addition. | ||
* @return The result of the addition. | ||
*/ | ||
private static float addHalfFloat(short a, short b) { | ||
float floatA = Float.float16ToFloat(a); | ||
float floatB = Float.float16ToFloat(b); | ||
return floatA + floatB; | ||
} | ||
|
||
/** | ||
* Takes two {@code HalfFloat} objects and returns a new {@HalfFloat} instance | ||
* that contains the results of the addition. | ||
* | ||
* @param a | ||
* The first {@code HalfFloat} input for the addition. | ||
* @param b | ||
* The second {@code HalfFloat} input for the addition. | ||
* @return A new {@HalfFloat} containing the results of the addition. | ||
*/ | ||
public static HalfFloat add(HalfFloat a, HalfFloat b) { | ||
float result = addHalfFloat(a.getHalfFloatValue(), b.getHalfFloatValue()); | ||
return new HalfFloat(result); | ||
} | ||
|
||
/** | ||
* Takes two half float values, converts them to a 32-bit representation and performs a subtraction. | ||
* | ||
* @param a | ||
* The first float-16 input for the subtraction. | ||
* @param b | ||
* The second float-16 input for the subtraction. | ||
* @return The result of the subtraction. | ||
*/ | ||
private static float subHalfFloat(short a, short b) { | ||
float floatA = Float.float16ToFloat(a); | ||
float floatB = Float.float16ToFloat(b); | ||
return floatA - floatB; | ||
} | ||
|
||
/** | ||
* Takes two {@code HalfFloat} objects and returns a new {@HalfFloat} instance | ||
* that contains the results of the subtraction. | ||
* | ||
* @param a | ||
* The first {@code HalfFloat} input for the subtraction. | ||
* @param b | ||
* The second {@code HalfFloat} input for the subtraction. | ||
* @return A new {@HalfFloat} containing the results of the subtraction. | ||
*/ | ||
public static HalfFloat sub(HalfFloat a, HalfFloat b) { | ||
float result = subHalfFloat(a.getHalfFloatValue(), b.getHalfFloatValue()); | ||
return new HalfFloat(result); | ||
} | ||
|
||
/** | ||
* Takes two half float values, converts them to a 32-bit representation and performs a multiplication. | ||
* | ||
* @param a | ||
* The first float-16 input for the multiplication. | ||
* @param b | ||
* The second float-16 input for the multiplication. | ||
* @return The result of the multiplication. | ||
*/ | ||
private static float multHalfFloat(short a, short b) { | ||
float floatA = Float.float16ToFloat(a); | ||
float floatB = Float.float16ToFloat(b); | ||
return floatA * floatB; | ||
} | ||
|
||
/** | ||
* Takes two {@code HalfFloat} objects and returns a new {@HalfFloat} instance | ||
* that contains the results of the multiplication. | ||
* | ||
* @param a | ||
* The first {@code HalfFloat} input for the multiplication. | ||
* @param b | ||
* The second {@code HalfFloat} input for the multiplication. | ||
* @return A new {@HalfFloat} containing the results of the multiplication. | ||
*/ | ||
public static HalfFloat mult(HalfFloat a, HalfFloat b) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: any reason we called it mult and mul |
||
float result = multHalfFloat(a.getHalfFloatValue(), b.getHalfFloatValue()); | ||
return new HalfFloat(result); | ||
} | ||
|
||
/** | ||
* Takes two half float values, converts them to a 32-bit representation and performs a division. | ||
* | ||
* @param a | ||
* The first float-16 input for the division. | ||
* @param b | ||
* The second float-16 input for the division. | ||
* @return The result of the division. | ||
*/ | ||
private static float divHalfFloat(short a, short b) { | ||
float floatA = Float.float16ToFloat(a); | ||
float floatB = Float.float16ToFloat(b); | ||
return floatA / floatB; | ||
} | ||
|
||
/** | ||
* Takes two {@code HalfFloat} objects and returns a new {@HalfFloat} instance | ||
* that contains the results of the division. | ||
* | ||
* @param a | ||
* The first {@code HalfFloat} input for the division. | ||
* @param b | ||
* The second {@code HalfFloat} input for the division. | ||
* @return A new {@HalfFloat} containing the results of the division. | ||
*/ | ||
public static HalfFloat div(HalfFloat a, HalfFloat b) { | ||
float result = divHalfFloat(a.getHalfFloatValue(), b.getHalfFloatValue()); | ||
return new HalfFloat(result); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be reverted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We keep this until we merge the code from the Beehive SPIR-V Toolkit