Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnnxRuntime and OnnxProtoBuilder continuation #320

Closed
wants to merge 4 commits into from
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
@@ -345,7 +345,7 @@ String getName(Value v, int subIndex) {
.tensor_type(new Tensor().elem_type(((OnnxType.TensorType)model.body().yieldType()).eType().id())))))
.opset_import(new OperatorSetIdProto().version(OPSET_VERSION))
.buf.toByteArray();
OnnxProtoPrinter.printModel(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN));
// OnnxProtoPrinter.printModel(ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN));
return ByteBuffer.allocateDirect(bytes.length).put(bytes).asReadOnlyBuffer();
}

10 changes: 5 additions & 5 deletions cr-examples/onnx/src/test/java/oracle/code/onnx/CNNTest.java
Original file line number Diff line number Diff line change
@@ -338,7 +338,7 @@ private static Tensor<Float> floatTensor(String resource, long... shape) throws
}
}

static List<Tensor> loadWeights() throws IOException {
static List<Tensor<Float>> loadWeights() throws IOException {
return List.of(floatTensor("conv1-weight-float-le", 6, 1, 5, 5),
floatTensor("conv1-bias-float-le", 6),
floatTensor("conv2-weight-float-le", 16, 6, 5, 5),
@@ -378,29 +378,29 @@ public void testModels() {

@Test
public void testInterpreter() throws Exception {
List<Tensor> weights = loadWeights();
var weights = loadWeights();
test(inputImage -> cnn(weights.get(0), weights.get(1), weights.get(2), weights.get(3), weights.get(4),
weights.get(5), weights.get(6), weights.get(7), weights.get(8), weights.get(9),
inputImage));
}

@Test
public void testProtobufModel() throws Exception {
List<Tensor> weights = loadWeights();
var weights = loadWeights();
test(inputImage -> new Tensor(OnnxRuntime.getInstance().runFunc(
OnnxTransformer.transform(MethodHandles.lookup(), getFuncOp("cnn")),
Stream.concat(weights.stream(), Stream.of(inputImage))
.map(t -> Optional.of(t.tensorAddr)).toList()).getFirst()));
}

private void test(Function<Tensor, Tensor> executor) throws Exception {
private void test(Function<Tensor<Byte>, Tensor<Float>> executor) throws Exception {
try (RandomAccessFile imagesF = new RandomAccessFile(IMAGES_PATH, "r");
RandomAccessFile labelsF = new RandomAccessFile(LABELS_PATH, "r")) {

ByteBuffer imagesIn = imagesF.getChannel().map(FileChannel.MapMode.READ_ONLY, IMAGES_HEADER_SIZE, imagesF.length() - IMAGES_HEADER_SIZE);
ByteBuffer labelsIn = labelsF.getChannel().map(FileChannel.MapMode.READ_ONLY, LABELS_HEADER_SIZE, labelsF.length() - LABELS_HEADER_SIZE);

Tensor inputImage = new Tensor(MemorySegment.ofBuffer(imagesIn), Tensor.ElementType.UINT8, new long[]{imagesF.length() - IMAGES_HEADER_SIZE});
Tensor<Byte> inputImage = new Tensor(MemorySegment.ofBuffer(imagesIn), Tensor.ElementType.UINT8, new long[]{imagesF.length() - IMAGES_HEADER_SIZE});

FloatBuffer result = executor.apply(inputImage).asByteBuffer().asFloatBuffer();