beehive-lab / TornadoVM

TornadoVM: A practical and efficient heterogeneous programming framework for managed languages
https://www.tornadovm.org
Apache License 2.0
1.18k stars 112 forks source link

Support half precision floating point types #313

Closed mairooni closed 7 months ago

mairooni commented 7 months ago

Description

This PR provides support for half-float types, i.e. float values represented by 16 bits instead of 32. Since Java does not currently offer a half-float type, in this implementation it is represented by a new class, HalfFloat. The HalfFloat class offers two constructors. The first receives a float value, which is internally converted to float-16, using the Float.floatToFloat16 function. The second directly receives a short value, which represents a half-float. The HalfFloat class exposes a set of operations that can be performed between half-float values, specifically addition, subtraction, multiplication, and division.
Finally, a new off-heap array type for half-float values (HalfFloatArray class) has been included.

Backend/s tested

Mark the backends affected by this PR.

OS tested

Mark the OS where this PR is tested.

Did you check on FPGAs?

If it is applicable, check your changes on FPGAs.

How to test the new patch?

Tests have been added for the following scenarios:

1) Initialization of a new HalfFloat instance in the kernel: tornado-test -V uk.ac.manchester.tornado.unittests.arrays.TestArrays#testHalfFloatInitialization 2) Addition of two HalfFloatArrays: tornado-test -V uk.ac.manchester.tornado.unittests.arrays.TestArrays#testVectorAdditionHalfFloat 3) Subtraction of two HalfFloatArrays: tornado-test -V uk.ac.manchester.tornado.unittests.arrays.TestArrays#testVectorSubtractionHalfFloat 4) Multiplication of two HalfFloatArrays: tornado-test -V uk.ac.manchester.tornado.unittests.arrays.TestArrays#testVectorMultiplicationHalfFloat 5) Division between two HalfFloatArrays: tornado-test -V uk.ac.manchester.tornado.unittests.arrays.TestArrays#testVectorDivisionHalfFloat 6) Test the fromElements andfromArray functions of the HalfFloatArray class: tornado-test -V uk.ac.manchester.tornado.unittests.api.TestAPI#testSegmentsHalfFloats 7) Test the fromSegment function of the HalfFloatArray class: tornado-test -V uk.ac.manchester.tornado.unittests.api.TestAPI#testBuildWithSegmentsHalfFloat

jjfumero commented 7 months ago

Unit-test are passing for SPIR-V and PTX. For OpenCL, depending on the device, this passes.

In my case, using RTX 2060 and the NVIDIA Drivers 510.54 do not pass.

mairooni commented 7 months ago

I included a function that checks if FP16 is supported. However, since on my machine I do not have this capability but I can pass the tests for OpenCL, we decided with @jjfumero to keep the printing of the OpenCL pragma for the time being, regardless of whether it is supported, until we figure out why this is happening.

jjfumero commented 7 months ago

Yes, we can query the capability. The pragma can always be generated. We need to investigate how to run OpenCL FP16 on modern NVIDIA GPUs.

mikepapadim commented 7 months ago

Ok, lets merge it then and we can iterate for a fix in codegen when they add the extension in the driver

jjfumero commented 7 months ago

It appears a new conflict. @mairooni, can you take a look?