petamoriken / float16 Goto Github PK
View Code? Open in Web Editor NEWStage 3 IEEE 754 half-precision floating-point ponyfill
Home Page: https://github.com/tc39/proposal-float16array
License: MIT License
Stage 3 IEEE 754 half-precision floating-point ponyfill
Home Page: https://github.com/tc39/proposal-float16array
License: MIT License
Hi,
I am using your library in hopes to cut down on memory consumption in my application. I have an array of floats with ~15 million entries, and I have found array lookups to be much slower since converting my Float32Array data type into a Float16Array.
I am trying to figure out if I am doing something incorrectly or if a performance hit is expected on lookups of an array of such a large size. Float16.js get()
method is taking lots of time on my performance scans.
float_16_arr[i]; // much slower
float_32_arr[i]; // much faster
Thank you!
Hi. Thanks for making this library. I have a WebGL2 project that requires potentially thousands of floating point texture reads per draw call. Cache and bandwidth performance could be improved by using a fp16 texture instead of fp32, but trying to naively use float16.Float16Array as a replacement for Float32Array causes the error WebGL: INVALID_OPERATION: texImage2D: no bound PIXEL_UNPACK_BUFFER
in chrome.
My original code is:
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB32F, res[0], res[1], 0, gl.RGB, gl.FLOAT, new Float32Array(bvhBuffer));
My new/broken code is:
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB16F, res[0], res[1], 0, gl.RGB, gl.HALF_FLOAT, new float16.Float16Array(bvhBuffer));
Any guidance would be appreciated.
Scenario:
My library contains your package as a dependency, this library is imported from create-react-app (CRA) project.
Library code:
const { getFloat16 } = require("@petamoriken/float16");
function conversion(value) {
var buffer = new ArrayBuffer(4);
var view = new DataView(buffer);
view.setUint16(0, value);
return getFloat16(view, 0, false);
}
The issue:
Uncaught TypeError: getFloat16 is not a function
at myLibrary(conversion.js:7:1)
Why would the import inside the CRA project be undefined when the library has it as dependency?
I've checked for circular dependencies using:
npx madge --circular --extensions ts,js ./
Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: ~project/node_modules/@petamoriken/float16/browser/float16.js: unknown Statement of type "ForOfStatement"
Since this hasn't been reported before, I suspect the issue is elsewhere.
@petamoriken/float16 which is a dependency of geotiff which is a dependency of OpenLayers
Float16Array constructor doesn't work with SharedArrayBuffers. Normally it should also work with those buffers just like the other typed arrays.
console.log(
new Float16Array(new ArrayBuffer(8)), // Float16Array(4) [ 0, 0, 0, 0 ]
new Float16Array(new SharedArrayBuffer(8)) // Float16Array(0) []
);
console.log(
new Uint16Array(new ArrayBuffer(8)), // Uint16Array(4) [ 0, 0, 0, 0 ]
new Uint16Array(new SharedArrayBuffer(8)) // Uint16Array(4) [ 0, 0, 0, 0 ]
);
Thanks for this fantastic library!
from the example, the expected output of the following map should be:
const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
const hey = array.map((value) => value);
console.log(">>>", hey)
Expected Output
>>> [ 2, 2.19921875, 2.3984375, 2.599609375 ]
Actual Output (wrong)
>>> Proxy(Float16Array) {0: 32256, 1: 32256, 2: 32256, 3: 32256, buffer: ArrayBuffer(8), byteLength: 8, byteOffset: 0, length: 4}
This, however, works:
const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
const hey = array.map((value) => console.log(">>>", value ));
Outputs: (correct)
=>>> 2
>>> 2.19921875
>>> 2.400390625
>>> 2.599609375
Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js): Error: ~project/node_modules/@petamoriken/float16/browser/float16.js: unknown Statement of type "ForOfStatement"
Since this hasn't been reported before, I suspect the issue is elsewhere.
@petamoriken/float16 which is a dependency of geotiff which is a dependency of OpenLayers
Complete error from Chrome
olPersonMap.js:487 Uncaught Error: Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: /myApp/node_modules/@petamoriken/float16/browser/float16.js: unknown Statement of type "ForOfStatement"
at Ep.explodeStatement (:3000/myApp/node_modules/regenerator-transform/lib/emit.js:541:13)
at :3000/myApp/node_modules/regenerator-transform/lib/emit.js:305:12
at Array.forEach ()
at Ep.explodeStatement (:3000/myApp/node_modules/regenerator-transform/lib/emit.js:304:22)
at Ep.explode (:3000/myApp/node_modules/regenerator-transform/lib/emit.js:268:40)
at PluginPass. (:3000/myApp/node_modules/regenerator-transform/lib/visit.js:98:17)
at PluginPass. (:3000/myApp/node_modules/regenerator-transform/lib/util.js:25:17)
at newFn (:3000/myApp/node_modules/@babel/traverse/lib/visitors.js:159:21)
at NodePath._call (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:46:20)
at NodePath.call (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:36:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:92:8)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitSingle (:3000/myApp/node_modules/@babel/traverse/lib/context.js:72:19)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:121:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitMultiple (:3000/myApp/node_modules/@babel/traverse/lib/context.js:67:17)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:119:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitSingle (:3000/myApp/node_modules/@babel/traverse/lib/context.js:72:19)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:121:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitMultiple (:3000/myApp/node_modules/@babel/traverse/lib/context.js:67:17)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:119:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitSingle (:3000/myApp/node_modules/@babel/traverse/lib/context.js:72:19)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:121:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitMultiple (:3000/myApp/node_modules/@babel/traverse/lib/context.js:67:17)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:119:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitSingle (:3000/myApp/node_modules/@babel/traverse/lib/context.js:72:19)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:121:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at NodePath.visit (:3000/myApp/node_modules/@babel/traverse/lib/path/context.js:90:52)
at TraversalContext.visitQueue (:3000/myApp/node_modules/@babel/traverse/lib/context.js:96:16)
at TraversalContext.visitMultiple (:3000/myApp/node_modules/@babel/traverse/lib/context.js:67:17)
at TraversalContext.visit (:3000/myApp/node_modules/@babel/traverse/lib/context.js:119:19)
at traverseNode (:3000/myApp/node_modules/@babel/traverse/lib/traverse-node.js:18:17)
at ./node_modules/@petamoriken/float16/browser/float16.js (olPersonMap.js:487:1)
at webpack_require (bootstrap:63:1)
at ./node_modules/geotiff/dist-module/geotiffimage.js (geotiffimage.js:1:1)
at webpack_require (bootstrap:63:1)
at ./node_modules/geotiff/dist-module/geotiff.js (geotiff.js:1:1)
at webpack_require (bootstrap:63:1)
at ./node_modules/ol/source/GeoTIFF.js (GeoTIFF.js:1:1)
at webpack_require (bootstrap:63:1)
at ./node_modules/ol/source.js (source.js:1:1)
at webpack_require (bootstrap:63:1)
I am trying to convert float16 to float32 using inbuilt functions however getting same output:
const array = new Float16Array([1.0, 1.1, 1.2, 1.3]);
for (const value of array) {
// 1, 1.099609375, 1.2001953125, 1.2998046875
console.log(value);
}
const float32Array = new Float32Array(array.length);
// Iterate over the Float16Array and convert each element to Float32
for (let i = 0; i < array.length; i++) {
float32Array[i] = array[i];
}
// Now float32Array contains the converted values
console.log("f32:", float32Array);
}
I've been looking into tests now that the proposal is stage 3, and in the process I noticed there's at least one bug in how this library handles rounding of very small values.
Specifically:
The smallest float16 value (call it F16_MIN) is 5.9604644775390625e-8
.
Half of that is 2.98023223876953125e-8
, which is precisely representable as a double, i.e. a JS number.
The next precisely representable double above F16_MIN/2 is 2.980232238769531911744490042422139897126953655970282852649688720703125e-8
. Call that TEST.
TEST should round to F16_MIN: TEST is closer to F16_MIN than to 0. (F16_MIN - TEST < TEST
)
But console.log(f16round(2.980232238769531911744490042422139897126953655970282852649688720703125e-8))
gives 0. Per above, that's wrong.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.