|
NEP 4.3.0
NetCDF Expansion Pack
|
nxCDF supports two 16-bit floating-point atomic types: NC_FLOAT16 (IEEE 754 binary16) and NC_BFLOAT16 (brain floating point). Both store 2 bytes per value on disk, but they split the bits differently and suit different data.
| Property | NC_FLOAT16 | NC_BFLOAT16 |
|---|---|---|
| nc_type value | 17 (include/nep.h) | 18 (include/nep.h) |
| Layout | 1 sign, 5 exponent, 10 mantissa bits | 1 sign, 8 exponent, 7 mantissa bits |
| Range | up to 65504 | float32 range, up to ~3.39e38 |
| Precision | ~3 significant decimal digits | ~2-3 significant decimal digits |
| File type | H5T_IEEE_F16LE | H5T_FLOAT_BFLOAT16LE |
| Memory type | raw uint16_t (untyped calls), or float/double (typed calls) | the same |
Even smaller floats exist — NC_FLOAT8_E4M3, NC_FLOAT8_E5M2, NC_FLOAT6_E2M3, NC_FLOAT6_E3M2 and NC_FLOAT4_E2M1 are stored as raw uint8_t patterns; see the type table in nxcdf.md.
nxCDF uses the private, NEP-owned nxcdf_convert_type() conversion engine, selected as the permanent path in Sprint 1H; the temporary NEP_INTERNAL_CONVERT option and NEP_HAS_INTERNAL_CONVERT marker were removed, and passing the option now fails at configure time. Standard float/double calls on NC_BFLOAT16 use NEP round-to-nearest-even (449 becomes 448), double narrows once, and finite overflow produces signed infinity. Supported bfloat16 attributes use the same rules. Since Sprint 1J (v4.3.0), IEEE float16 uses the same engine: float/double calls on NC_FLOAT16 convert with NEP round-to-nearest-even, single rounding from double, signed infinity on finite overflow, and the former H5Tconvert path is gone. Values at or above 65520 (the halfway point past 65504, the largest binary16) overflow to infinity, as do the boundary roundings that reach it; finite overflow returns NC_ERANGE after all values are processed. Raw-bit I/O and the independent nc_*_bfloat16/nc_*_float16 functions do not change. The engine reports NC_ERANGE after completing out-of-range variable/attribute conversions and quantized writes. The warning does not undo writes, attribute replacement or dimension growth. Precision loss, fractional truncation and floating underflow are not range errors; nonfinite-to-integer conversion is.
The committed OFF/ON benchmark artifacts in examples/nxcdf/conversion_*.csv record the Sprint 1H comparison that selected this engine. See nxcdf.md for the conversion engine's scope.
bfloat16 keeps the full float32 exponent range and pays for it with a 7-bit mantissa, so it suits data where a relative error of up to 2^-8 (about 0.4%) is acceptable. IEEE binary16 makes the opposite trade: 3 more mantissa bits (for example, values 0.25 K apart in 256-512 K instead of 2 K), but a maximum of 65504 and a smallest normal value of about 6.1e-5, below which precision drops. The non-fill values in this sample (tmp2m 218.976-316.82 K, prate_ave 0-0.0109397 kg/m**2/s, soilw1 0.02-1, land 0-2) all fit within binary16's maximum, but the input's declared fill value 9.99e+20 and any small prate_ave values below 6.1e-5 do not fit binary16 well, while bfloat16 represents both. (No point in this sample actually equals the fill value.) In absolute terms the maximum errors on the four GDAS fields are 1 K for tmp2m (bfloat16 values are 1 K apart in 128-256 K and 2 K apart in 256-512 K, so tmp2m is stored to the nearest 1-2 K), 3.0e-05 kg/m**2/s for prate_ave, 9.8e-04 (fraction) for soilw1, and 0 for land, whose three small integer values are exactly representable.
libnep provides eight bfloat16 memory-type functions that work on any file the netCDF API can open, not just nxCDF:
The buffer type is nc_bfloat16_t: the compiler's __bf16 where available (NEP_HAVE_NATIVE_BFLOAT16 = 1), otherwise uint16_t. Both are 2 bytes holding the bfloat16 bit pattern, so raw-bits code casts the pointer — (nc_bfloat16_t *)(void *)bits — rather than assigning values.
What the functions do depends on the variable's type:
| Variable type | Behavior |
|---|---|
| NC_BFLOAT16 | Raw bit patterns, no conversion |
| NC_FLOAT, NC_DOUBLE | NEP converts: reads round to nearest, ties to even (single rounding from double); writes widen exactly |
| NC_CHAR | NC_ECHAR |
| anything else (incl. NC_FLOAT16) | NC_EBADTYPE |
On a get, a finite float or double whose rounding overflows is stored as ±inf, every value is still converted, and the call returns NC_ERANGE. Infinity and NaN are not range errors, and puts never overflow because widening is exact.
libnep provides the same eight functions for IEEE binary16, added in Sprint 1J (v4.3.0):
The buffer type is nc_float16_t: the compiler's _Float16 where available (NEP_HAVE_NATIVE_FLOAT16 = 1), otherwise uint16_t. Both are 2 bytes holding the binary16 bit pattern, so raw-bits code casts the pointer — (nc_float16_t *)(void *)bits — rather than assigning values.
| Variable type | Behavior |
|---|---|
| NC_FLOAT16 | Raw bit patterns, no conversion |
| NC_FLOAT, NC_DOUBLE | NEP converts: reads round to nearest, ties to even (single rounding from double); writes widen exactly |
| NC_CHAR | NC_ECHAR |
| anything else (incl. NC_BFLOAT16) | NC_EBADTYPE |
On a get, a finite float or double at or above 65520 in magnitude is stored as ±inf, every value is still converted, and the call returns NC_ERANGE. Infinity and NaN are not range errors, and puts never overflow because widening is exact. Values below 6.1e-5 in magnitude become subnormals or zero without a range warning.
bfloat16.c reads the tmp2m field and its grid_yt/grid_xt coordinate variables from the GDAS sample — twice: as NC_FLOAT for reference and as bfloat16 through nc_get_var_bfloat16, so NEP does the conversion — writes two nxCDF files — float_tmp2m.nc (NC_FLOAT) and bfloat16_tmp2m.nc (NC_BFLOAT16) — then reopens the bfloat16 file, verifies the bits round-tripped exactly, and reports file sizes and the maximum conversion error.
float32 to bfloat16 rounds to nearest, ties to even; NaN stays a quiet NaN and values past the maximum overflow to infinity. bfloat16 to float32 is an exact 16-bit left shift. The nc_*_bfloat16 functions use the same rounding; the program cross-checks nc_get_var_bfloat16 against float_to_bfloat16 on every point:
The file is created with NC_NXCDF, the variable is defined with NC_BFLOAT16 and no fill value, and the raw uint16_t bits are written with nc_put_vara_bfloat16 (the NC_FLOAT file is written with nc_put_vara_float):
The program reopens bfloat16_tmp2m.nc with NC_NXCDF | NC_NOWRITE, checks nc_inq_vartype returns NC_BFLOAT16, reads the bits with nc_get_vara_bfloat16 into a uint16_t buffer, memcmps them against the written bits, and computes the maximum absolute and relative error over the non-fill points. As a cross-check it also reads the same hyperslab with nc_get_vara_float into a float buffer and verifies that every value's bits equal the stored uint16_t pattern shifted left 16 bits — HDF5's bfloat16-to-float conversion is exact:
-DNEP_ZSTD_PLUGIN_DIR points ctest at a directory holding exactly one HDF5 zstd plugin, needed to read the compressed sample; NCRCENV_RC/NETCDF_RC point netcdf-c at the build-tree .ncrc that autoloads the nxCDF backend. The RelWithDebInfo build type (-O2) is what the benchmark results below were measured with; the benchmark programs print a warning when built without optimization. Real output on the reference machine:
h5dump -H bfloat16_tmp2m.nc shows DATATYPE H5T_FLOAT_BFLOAT16LE for tmp2m.
bfloat16_perf.c runs all four GDAS fields (tmp2m, prate_ave, soilw1, land) through six configurations — float/bfloat16/bfloat16_hdf5 x contiguous/deflate1 (shuffle + deflate level 1, chunks of 1x768x1536) — with 10 repetitions each, keeping the minimum times. Each file holds 10 copies of the field as time steps (NCOPIES), written and read one step at a time, so the fixed cost of creating, opening and closing a file is spread over more data. The bfloat16 rows convert in user code and use the untyped calls; the bfloat16_hdf5 rows define the same NC_BFLOAT16 variable but write with nc_put_vara_float and read with nc_get_vara_float, so HDF5 does the conversion inside the timed I/O and wconv_s/rconv_s are 0. For each bfloat16_hdf5 row a #-prefixed line is printed to stdout (not to the CSV) counting, for one copy, the stored values that differ from round-to-nearest-even, because HDF5 rounds bfloat16 ties away from zero:
Write time runs from just before conversion (bfloat16 only) through nc_close and an fsync() of the file; before each read the file is dropped from the page cache with posix_fadvise(POSIX_FADV_DONTNEED) (untimed), and read time runs from nc_open through nc_close plus the conversion back to float. Both times cover all 10 copies. Both helpers are in examples/perf_disk.h, so the times are disk-bound. The bfloat16 rows are checked bit-exact and the bfloat16_hdf5 rows are checked against the stored raw bits; the program fails if any contiguous bfloat16 file is not smaller than its float file, or if any relative error exceeds 2^-8.
The CSV written has columns:
(copies is NCOPIES; uncompressed_bytes, file_bytes, write_s and read_s cover all copies. ratio_vs_float is the matching float row's file_bytes divided by this row's; throughput is measured in MB/s of float32 data (10 x 4718592 bytes) for every row. wconv_s/rconv_s are the minimum float32-to-bfloat16 and bfloat16-to-float32 conversion times — parts of write_s/read_s; 0 for float and bfloat16_hdf5 rows.)
Measured on the machine recorded in examples/nxcdf/reference_machine.txt; the full CSV is examples/nxcdf/reference_results.csv. File sizes are in bytes, for files holding 10 copies of the field.
| Field | float contiguous | bfloat16 contiguous | bfloat16 (HDF5) contiguous | float deflate1 | bfloat16 deflate1 | bfloat16 (HDF5) deflate1 | max rel err | max rel err (HDF5) |
|---|---|---|---|---|---|---|---|---|
| tmp2m | 47192259 | 23599299 | 23599299 | 25022289 | 1977709 | 1979129 | 0.00389105 | 0.00389105 |
| prate_ave | 47192259 | 23599299 | 23599299 | 33543449 | 13157179 | 13157179 | 0.00389081 | 0.00389081 |
| soilw1 | 47192259 | 23599299 | 23599299 | 8538049 | 3021869 | 3021879 | 0.00388914 | 0.00388914 |
| land | 47192259 | 23599299 | 23599299 | 758369 | 646119 | 646119 | 0 | 0 |
What the numbers show:
bfloat16_io_perf.c times pure I/O with no conversion, using the committed bfloat16 copy of the GDAS sample test/data/NXCDF/gdas_sfc_sample_bfloat16.nc (see test/data/NXCDF/README.md for how it is made). It checks that the sample's stored bits are exactly float_to_bfloat16() of the float sample, then writes and reads 10 copies of each field (as time steps) as float32 and as bfloat16 bits, contiguous and deflate1, 10 repetitions each, minimum kept. Timing is disk-bound in the same way as bfloat16_perf (fsync() in the write time, page cache dropped before each read):
The CSV written has columns:
(copies is NCOPIES; sizes and times cover all copies, and write_MBps/read_MBps are MB/s of float32 data for every row. ratio_vs_float is the matching float row's file_bytes divided by this row's; write_speedup/read_speedup are the matching float row's times divided by this row's — 1.000 on the float rows.) Results are discussed in float16.md, and the reference CSV is examples/nxcdf/reference_io_results.csv.
float16.c is the binary16 version of the bfloat16 tutorial. It reads tmp2m and its grid_yt/grid_xt coordinate variables from the GDAS sample — twice: as NC_FLOAT for reference and as float16 through nc_get_var_float16, so NEP does the conversion — writes float_tmp2m.nc (NC_FLOAT) and float16_tmp2m.nc (NC_FLOAT16), reopens the float16 file, verifies the bits round-tripped exactly, and reports file sizes and the maximum conversion error. The conversion helpers live in float16_conv.h, with the same algorithms as the internal engine and nep_float16.c.
Real output on the reference machine:
The maximum relative error is just under the 2^-11 = 0.000488281 round-to-nearest bound for binary16. h5dump -H float16_tmp2m.nc shows DATATYPE H5T_IEEE_F16LE for tmp2m.
float16_perf.c runs the same four GDAS fields through six configurations — float/float16/float16_typed x contiguous/deflate1 — with the same 10-repetition, disk-bound, min-time methodology as bfloat16_perf. The float16 rows convert in user code with float_to_float16()/float16_to_float() and use nc_put_vara_float16/nc_get_vara_float16; the float16_typed rows define the same NC_FLOAT16 variable but write with nc_put_vara_float and read with nc_get_vara_float, so the internal engine converts inside the timed I/O. Both paths use NEP round-to-nearest-even, so there is no differing-rounding report: the typed path's stored bits are checked against float_to_float16() and must match exactly.
The error gate is a combined tolerance: a non-fill point fails only if its relative error exceeds 2^-11 and its absolute error exceeds 2^-25 (half the smallest binary16 subnormal). The absolute-error leg keeps prate_ave honest — values below the 6.1e-5 minimum normal become subnormals or zero, which is precision loss the format cannot avoid, not a conversion bug — while the relative leg still bounds every normal-range value.
Measured on the machine recorded in examples/nxcdf/reference_machine.txt; the full CSV is examples/nxcdf/reference_float16_results.csv. File sizes are in bytes, for files holding 10 copies of the field.
| Field | float contiguous | float16 contiguous | float16 (typed) contiguous | float deflate1 | float16 deflate1 | float16 (typed) deflate1 | max rel err |
|---|---|---|---|---|---|---|---|
| tmp2m | 47192259 | 23599299 | 23599299 | 25022289 | 5493699 | 5493699 | 0.000487924 |
| prate_ave | 47192259 | 23599299 | 23599299 | 33543449 | 9277849 | 9277849 | 1* |
| soilw1 | 47192259 | 23599299 | 23599299 | 8538049 | 3892069 | 3892069 | 0.000486854 |
| land | 47192259 | 23599299 | 23599299 | 758369 | 402889 | 402889 | 0 |
*prate_ave's maximum relative error of 1 comes from values below binary16's smallest normal that round to a subnormal or zero; its maximum absolute error is only 3.4e-06 kg/m**2/s.
What the numbers show:
float16_io_perf.c times pure I/O with no conversion, using the committed float16 copy of the GDAS sample test/data/NXCDF/gdas_sfc_sample_float16.nc (see test/data/NXCDF/README.md). It checks that the sample's stored bits are exactly float_to_float16() of the float sample, then writes and reads 10 copies of each field as float32 and as float16 bits, contiguous and deflate1, 10 repetitions each, minimum kept. Results are in examples/nxcdf/reference_float16_io_results.csv: