Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,44 @@ public String toString() {
}
}

/**
* A generic holder for 16b (fp16/bfloat16) tensor. Not intended for general use. User can only
* use #Tensor.getDataAsUnsignedByteArray() to get raw bytes. Users need to parse it.
*/
static class Tensor_raw_data_16b extends Tensor {
private final ByteBuffer data;
private final DType myDtype;

private Tensor_raw_data_16b(ByteBuffer data, long[] shape, DType dtype) {
super(shape);
this.data = data;
this.myDtype = dtype;
}

@Override
public DType dtype() {
return myDtype;
}

@Override
Buffer getRawDataBuffer() {
return data;
}

@Override
public byte[] getDataAsUnsignedByteArray() {
data.rewind();
byte[] arr = new byte[data.remaining()];
data.get(arr);
return arr;
}

@Override
public String toString() {
return String.format("Tensor(%s, dtype=%d)", Arrays.toString(shape), this.myDtype);
}
}

// region checks
private static void checkArgument(boolean expression, String errorMessage, Object... args) {
if (!expression) {
Expand Down Expand Up @@ -674,6 +712,10 @@ private static Tensor nativeNewTensor(
tensor = new Tensor_uint8(data, shape);
} else if (DType.INT8.jniCode == dtype) {
tensor = new Tensor_int8(data, shape);
} else if (DType.HALF.jniCode == dtype) {
tensor = new Tensor_raw_data_16b(data, shape, DType.HALF);
} else if (DType.HALF.jniCode == dtype) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is same check as L715, should be DType.BFLOAT16.jniCode == dtype?

tensor = new Tensor_raw_data_16b(data, shape, DType.BFLOAT16);
} else {
throw new IllegalArgumentException("Unknown Tensor dtype");
}
Expand Down
Loading