Giter VIP home page Giter VIP logo

Comments (7)

henon avatar henon commented on September 15, 2024 1

This is the first time I learn of the new Half type. I can support it of course

from numpy.net.

henon avatar henon commented on September 15, 2024 1

Here is a workaround for you. You need to do some bit fiddling and you need to know exactly how float16 numbers are represented in binary. Then you can create a byte[] with two bytes per Half and create a numpy array from that like this:

        [TestMethod]
        public async Task F16Workaround()
        {
            // use byte array to create float16 array

            // numbers in float16:
            // 0 01111 0000000000 = 2^0 * (1 + 0/1024) = 1 
            // 1 01111 0000000000 = -1 * 2 ^ 0 * (1 + 0 / 1024) = -1
            // 0 11110 1111111111 = -1^(0) * 2^(15) * (1 + 1023/1024) β‰ˆ 65504
            // see: https://devblogs.microsoft.com/dotnet/introducing-the-half-type/

            // these bytes in binary notation correspond to f16 numbers 1, -1 and 65504 (float16 max value)
            // note, the bytes are in reversed order to the bits shown above
            var bytes = new byte[] { 
                0b00000000, 0b00111100, // 1
                0b00000000, 0b10111100, // -1
                0b11111111, 0b01111011, // 65504
            };
            var floats = np.zeros(new Shape(3), np.float16);
            Console.WriteLine(floats.repr);
            // note, the using prevents a mem-leak with ctypes
            using (var ctypes = floats.PyObject.ctypes) { 
                long ptr = ctypes.data;
                Marshal.Copy(bytes, 0, new IntPtr(ptr), bytes.Length);
            }
            Console.WriteLine(floats.repr);
            Assert.AreEqual("array([ 1.00e+00, -1.00e+00,  6.55e+04], dtype=float16)", floats.repr);
        }

I guess you can probably directly copy the bytes from a Half[] into a byte[] with Marshal.Copy or so.

from numpy.net.

henon avatar henon commented on September 15, 2024 1

I'll also see if I can get multi targeting to work so I can support Half natively but it might take some time

from numpy.net.

henon avatar henon commented on September 15, 2024 1

@martindevans Sorry, I didn't have time to look into multi-targeting yet. Did the workaround help you?

from numpy.net.

henon avatar henon commented on September 15, 2024

Hmm, that is actually not that easy. Half is only available in .Net 5 and Numpy is still NetStandard 2.0

from numpy.net.

martindevans avatar martindevans commented on September 15, 2024

Thanks, I'll give that a go tomorrow.

from numpy.net.

martindevans avatar martindevans commented on September 15, 2024

I did test it a couple of weeks ago but in the end I decided to stick with float32, just for simplicity.

from numpy.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.