deeplearning4j/deeplearning4j · error · UnsupportedOperationException

Not yet implemnted: FP16 reading from buffer

Error message

Not yet implemnted: FP16 reading from buffer

What it means

The TensorFlow FP16 tensor mapper's getBuffer() hook has not been implemented, so reading FP16 tensor data through the NIO buffer path is unsupported. The library can read FP16 values element-wise via getValue() (using HalfIndexer.toFloat on halfVal bytes) but throws this UnsupportedOperationException when the buffer-based read API is used. It is a deliberate stub, not a data corruption issue.

Solutions

  1. Convert the FP16 tensor to FP32 (float) in the original TensorFlow model before export so the float mapper path is used.
  2. Read values element-wise via getValue(float[], int), which is implemented for FP16 using HalfIndexer.toFloat.
  3. Patch/override the mapper's getBuffer() to decode the ByteBuffer shorts with HalfIndexer and file an issue upstream.
  4. Avoid the code path that requests buffer-based reads for HALF dtypes (check the import API used).

Example fix

// before
ByteBuffer buf = mapper.getBuffer(byteBuffer);
float[] vals = ...;
// after
float[] vals = new float[tensorInfoCount];
for (int i = 0; i < vals.length; i++) {
    mapper.getValue(vals, i); // uses HalfIndexer.toFloat(getHalfVal(i))
}
Defensive patterns

Strategy: fallback

Validate before calling

if (tensor.getDtype() == org.tensorflow.proto.DataType.DT_HALF) {
    // avoid getBuffer(); use per-element reads or convert to DT_FLOAT first
}

Try / catch

try {
    ByteBuffer buf = mapper.getBuffer(bb);
} catch (UnsupportedOperationException e) {
    // fall back to per-element getValue(int) reads
}

Prevention

When it happens

Trigger: Importing a TensorFlow graph/protobuf containing a HALF (float16) tensor through TFTensorMappers when the code path calls getBuffer(ByteBuffer) instead of the element-wise getValue() path — e.g. a mapper implementation or caller that prefers the bulk buffer read.

Common situations: Importing TF frozen graphs or checkpoints that contain float16 weights (e.g. FP16-optimized models); using APIs that materialize tensor contents via ByteBuffer rather than per-element access during TF model import into ND4J/DL4J.

Related errors


AI-assisted analysis of deeplearning4j/deeplearning4j@4c22ac5fe4 (2026-09-07). Data as JSON: /api/errors/174ff12e955bbaea. Report an issue: GitHub.

Appendix: source

Thrown at nd4j/nd4j-backends/nd4j-api-parent/nd4j-api/src/main/java/org/nd4j/imports/graphmapper/tf/tensors/TFTensorMappers.java:212

    public static class Float16TensorMapper extends BaseTensorMapper<float[], Buffer>  {
        public Float16TensorMapper(TensorProto tensorProto) {
            super(tensorProto);
        }

        @Override
        public int valueCount() {
            return tfTensor.getHalfValCount();
        }

        @Override
        public float[] newArray(int length) {
            return new float[length];
        }

        @Override
        public Buffer getBuffer(ByteBuffer bb) {
            throw new UnsupportedOperationException("Not yet implemnted: FP16 reading from buffer");
        }

        @Override
        public void getValue(float[] jArr, int i) {
            int asIntBytes = tfTensor.getHalfVal(i);
            jArr[i] = HalfIndexer.toFloat(asIntBytes);
        }

        @Override
        public void getValue(float[] jArr, Buffer buffer, int i){
            throw new UnsupportedOperationException("Not yet implemented: FP16 reading from buffer");
        }

        @Override
        public INDArray arrayFor(long[] shape, float[] jArr) {
            //Edge case: sometimes tf has single float value for entire array (getFloatValCount() == 1)
            if(jArr.length == 1 && ArrayUtil.prod(shape) > 1)
                return Nd4j.createUninitialized(DataType.HALF, shape).assign(jArr[0]);

View on GitHub (pinned to 4c22ac5fe4)