|
NEP 4.3.0
NetCDF Expansion Pack
|
bfloat16 ("brain floating point") is a 16-bit floating-point format: 1 sign bit, 8 exponent bits and 7 mantissa bits. It is the top half of an IEEE float32, so it keeps float32's full range (up to about 3.39e38) and gives up precision: about 2-3 significant decimal digits, with a relative rounding error of at most 2^-8 (about 0.4%). For data that doesn't need more precision than that, such as many model output fields, bfloat16 halves the storage of float32 and often compresses much better.
NEP supports bfloat16 in two ways:
This page gives an overview with code from examples/nxcdf/bfloat16.c and the benchmark results. The full reference, including NC_FLOAT16 and the HDF5 conversion details, is NXCDF_float16.md.
nxCDF uses the private nxcdf_convert_type() engine — made permanent in Sprint 1H — for standard float/double calls on bfloat16 and float16 variables and supported attributes, with NEP round-to-nearest-even. The temporary NEP_INTERNAL_CONVERT option and NEP_HAS_INTERNAL_CONVERT marker were removed after the engine decision; passing the option now fails at configure time. IEEE float16 joined the engine in Sprint 1J (v4.3.0); its H5Tconvert path was removed. The independent nc_*_bfloat16 and nc_*_float16 APIs are unchanged. See nxcdf.md.
The committed charts and benchmark numbers in examples/nxcdf/conversion_*.csv record the Sprint 1H OFF/ON comparison; the internal engine is now the only conversion path.
NC_BFLOAT16 (value 18, defined in include/nep.h) is a nxCDF atomic type. To use it:
A 16-bit IEEE half-precision type, NC_FLOAT16, is also available. It has 3 more mantissa bits than bfloat16 but a maximum of 65504. See NXCDF_float16.md for a comparison.
libnep declares eight functions in nep.h, following the netCDF naming pattern:
For example:
The buffer type nc_bfloat16_t is the compiler's __bf16 where the compiler supports it (for example GCC 13 on x86-64; clang 18 supports __bf16 but NEP doesn't detect it, so clang gets uint16_t; NEP_HAVE_NATIVE_BFLOAT16 is 1), and uint16_t otherwise. Either way it is 2 bytes holding the bfloat16 bit pattern. With __bf16, C casts between values convert numerically, so code that keeps raw bits in a uint16_t array passes it by casting the pointer, (nc_bfloat16_t *)(void *)bits, as the example does.
What the functions do depends on the type of the variable in the file:
| Variable type | Read (nc_get_*_bfloat16) | Write (nc_put_*_bfloat16) |
|---|---|---|
| NC_BFLOAT16 | raw bits, no conversion | raw bits, no conversion; stored as 2 bytes per value |
| NC_FLOAT | NEP rounds each float to bfloat16 (to nearest, ties to even) | NEP widens each value to float (exact); stored as 4 bytes |
| NC_DOUBLE | NEP rounds each double to bfloat16 in a single rounding | NEP widens each value to double (exact); stored as 8 bytes |
| NC_CHAR | NC_ECHAR | NC_ECHAR |
| any other type | NC_EBADTYPE | NC_EBADTYPE |
On a read, a finite value too large for bfloat16 is stored as ±infinity, the rest of the values are still converted, and the call returns NC_ERANGE, as netCDF's own typed calls do. Writes never overflow.
The functions use only the public netCDF API, so they work on any file netCDF can open. You can read an NC_FLOAT variable from an ordinary netCDF-4 file as bfloat16, as the example does with its input. Note that writing into an NC_FLOAT or NC_DOUBLE variable saves no space; only an NC_BFLOAT16 variable in a nxCDF file stores 2 bytes per value.
The example uses test/data/netCDF-4/gdas_sfc_sample.nc, a sample of NOAA GDAS surface output with 1,179,648 values per field (768 x 1536 grid). It reads the 2 m temperature field tmp2m, writes it to a float32 file and a bfloat16 file, reopens the bfloat16 file and checks the result.
The input is an ordinary netCDF-4 file with tmp2m stored as NC_FLOAT. The program reads it twice: once as float for reference, and once as bfloat16, so NEP does the conversion:
It then checks NEP's conversion against the reference conversion in examples/nxcdf/bfloat16_conv.h, which rounds to nearest, ties to even:
The bfloat16 file is created with NC_NXCDF, tmp2m is defined as NC_BFLOAT16 with no fill value, and the bits are written with nc_put_vara_bfloat16. The float32 file is written the same way with NC_FLOAT and nc_put_vara_float:
The program reopens the file with NC_NXCDF | NC_NOWRITE and reads the raw bits with nc_get_vara_bfloat16. As a cross-check, it also reads the same values as float through HDF5's (exact) widening conversion:
Its output on the reference machine:
The bfloat16 file is half the size, and the largest error is 1 K out of about 220-317 K, a relative error just under the 2^-8 (0.00390625) bound. Build and run instructions are in NXCDF_float16.md.
examples/nxcdf/bfloat16_perf.c writes and reads four GDAS fields (tmp2m temperature, prate_ave precipitation rate, soilw1 soil moisture, and land land mask) in six configurations:
Each is run with contiguous storage and with deflate1 (shuffle plus deflate level 1, one 768 x 1536 chunk per time step). Each file holds 10 copies of the field as time steps (NCOPIES in the source), written and read one step at a time, so the fixed cost of creating, opening and closing a file is spread over 10 fields' worth of data. Every configuration runs 10 times and the minimum time is kept. The times are disk-bound: each write ends with an fsync() of the file, and before each read the file is dropped from the operating system's page cache, so every byte is written to and read from the storage device (see examples/perf_disk.h). This is the case that matters when processing more data than fits in memory. Speeds are given in MB/s of float32 data (10 x 4.72 MB per file), so every configuration is measured against the same user data. The results below come from a RelWithDebInfo (-O2) build on the machine (Intel i7-6800K, a Kingston SATA SSD with ext4, HDF5 2.1.1, NetCDF-C 4.10.2-development) recorded in examples/nxcdf/reference_machine.txt, and the full results are in examples/nxcdf/reference_results.csv.
Figure 1. Size in MB of each 10-copy file, in the four main configurations (float32/bfloat16 x contiguous/deflate1). Contiguous bfloat16 files are 23.6 MB against 47.2 MB for float32 (ratio 2.00), whichever way the data was converted. With deflate1, bfloat16 compresses much better than float32, because rounding to 7 mantissa bits leaves far fewer distinct values: tmp2m drops from 858,874 distinct values to 68, and its file from 25.0 MB to 2.0 MB (12.7x smaller); prate_ave is 2.5x smaller and soilw1 2.8x. land has only 3 distinct values, all exact in bfloat16, so both versions compress to under 1 MB (1.17x).
Figure 2. Write (top) and read (bottom) speed in MB/s of float32 data, in the four main configurations. Write time runs from nc_create through nc_close and the fsync(), including the conversion of each step to bfloat16; read time runs from nc_open (with the file out of the page cache) through nc_close, including the conversion of each step back to float. Each bar is the fastest of 10 runs. Taller is better.
The same NC_BFLOAT16 variable can also be written with nc_put_vara_float and read with nc_get_vara_float, letting HDF5 do the conversion inside H5Dwrite/H5Dread. Figure 3 adds those bars to the same chart.
Figure 3. The same write (top) and read (bottom) speeds with the bfloat16 (HDF5) configurations added.
A second benchmark, examples/nxcdf/bfloat16_io_perf.c, measures pure I/O with no conversion at all. It uses test/data/NXCDF/gdas_sfc_sample_bfloat16.nc, a bfloat16 copy of the GDAS sample made by make_bfloat16_sample (the values come from nc_get_var_bfloat16 on the source NC_FLOAT variables; time is a fixed dimension of length 1, and the data variables carry no _FillValue/missing_value, so fill points hold the bfloat16 rounding of 9.99e+20). The benchmark checks that the sample's bits are exactly float_to_bfloat16() of the float sample, then times writing and reading 10 copies of the float32 data and of the bfloat16 bits in the four configurations, disk-bound as above.
Figure 4. Pure I/O write (top) and read (bottom) speed in MB/s of float32 data, with each field held as float32 or as bfloat16 bits, in each configuration. Speedups are the float32 time of the same storage configuration divided by the bfloat16 time.
The maximum relative error is 0.00389 for tmp2m, prate_ave and soilw1 in every bfloat16 configuration, just under the 2^-8 bound, and 0 for land. In absolute terms that is 1 K for tmp2m, 3.0e-05 kg/m²/s for prate_ave and 9.8e-04 for soilw1.
NC_FLOAT16 (value 17, IEEE 754 binary16) is the other 16-bit atomic type: 1 sign bit, 5 exponent bits and 10 mantissa bits. It trades bfloat16's range for precision — about 3 significant decimal digits (relative error at most 2^-11), but a maximum of 65504 and a smallest normal value of about 6.1e-5, below which values become subnormals and lose precision. It suits data bounded well under 65504 where the extra mantissa bits matter more than the range.
Since Sprint 1J (v4.3.0) the typed float/double calls on NC_FLOAT16 variables convert through the internal engine, with the same round-to-nearest-even and NC_ERANGE semantics as bfloat16; the old H5Tconvert path is gone.
libnep also provides the nc_*_float16 family, mirroring nc_*_bfloat16:
The buffer type is nc_float16_t — _Float16 where the compiler has it (NEP_HAVE_NATIVE_FLOAT16 = 1), uint16_t otherwise. On NC_FLOAT16 variables the functions exchange raw bits; on NC_FLOAT/NC_DOUBLE variables NEP converts with round-to-nearest-even and reports finite overflow (magnitude at or above 65520) as NC_ERANGE. The float16.c example, the float16_perf/float16_io_perf benchmarks, the committed gdas_sfc_sample_float16.nc and the performance results are documented in NXCDF_float16.md.