From b92b302a21e045e5e1fe1273cb2a718e9f90939d Mon Sep 17 00:00:00 2001 From: James Yuzawa Date: Tue, 9 Jan 2024 20:03:15 -0500 Subject: [PATCH] optimize --- .../onnxruntime/OnnxRuntimeExecutionMode.java | 12 ++-- .../onnxruntime/OnnxRuntimeLoggingLevel.java | 62 +++++++------------ .../OnnxRuntimeOptimizationLevel.java | 18 +++--- .../OnnxTensorElementDataType.java | 57 ++++++----------- .../com/jyuzawa/onnxruntime/OnnxType.java | 27 +++----- 5 files changed, 63 insertions(+), 113 deletions(-) diff --git a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeExecutionMode.java b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeExecutionMode.java index 04586bed..c93a5a3b 100644 --- a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeExecutionMode.java +++ b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeExecutionMode.java @@ -24,12 +24,10 @@ public int getNumber() { } public static final OnnxRuntimeExecutionMode forNumber(int number) { - switch (number) { - case 1: - return PARALLEL; - case 0: - default: - return SEQUENTIAL; - } + return switch (number) { + case 0 -> SEQUENTIAL; + case 1 -> PARALLEL; + default -> SEQUENTIAL; + }; } } diff --git a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeLoggingLevel.java b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeLoggingLevel.java index f8437215..5c6cbeb4 100644 --- a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeLoggingLevel.java +++ b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeLoggingLevel.java @@ -16,7 +16,6 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import java.util.function.Supplier; /** * The level for the internal logger within the ONNX runtime. @@ -51,19 +50,14 @@ public int getNumber() { * @return the level, VERBOSE if not found */ public static final OnnxRuntimeLoggingLevel forNumber(int number) { - switch (number) { - case 1: - return INFO; - case 2: - return WARNING; - case 3: - return ERROR; - case 4: - return FATAL; - case 0: - default: - return VERBOSE; - } + return switch (number) { + case 0 -> VERBOSE; + case 1 -> INFO; + case 2 -> WARNING; + case 3 -> ERROR; + case 4 -> FATAL; + default -> VERBOSE; + }; } @SuppressWarnings("unused") @@ -74,33 +68,19 @@ private static final void logCallback( MemoryAddress idAddress, MemoryAddress locationAddress, MemoryAddress messageAddress) { - String category = categoryAddress.address().getUtf8String(0); - String id = idAddress.address().getUtf8String(0); - String location = locationAddress.address().getUtf8String(0); - String message = messageAddress.address().getUtf8String(0); - Supplier line = () -> new StringBuilder() - .append(category) - .append(' ') - .append(id) - .append(' ') - .append(location) - .append(' ') - .append(message) - .toString(); - switch (OnnxRuntimeLoggingLevel.forNumber(level)) { - case VERBOSE: - LOG.log(Level.DEBUG, line); - break; - case INFO: - LOG.log(Level.INFO, line); - break; - case WARNING: - LOG.log(Level.WARNING, line); - break; - case FATAL: - case ERROR: - LOG.log(Level.ERROR, line); - break; + Level theLevel = + switch (OnnxRuntimeLoggingLevel.forNumber(level)) { + case VERBOSE -> Level.DEBUG; + case INFO -> Level.INFO; + case WARNING -> Level.WARNING; + case FATAL, ERROR -> Level.ERROR; + }; + if (LOG.isLoggable(theLevel)) { + String category = categoryAddress.address().getUtf8String(0); + String id = idAddress.address().getUtf8String(0); + String location = locationAddress.address().getUtf8String(0); + String message = messageAddress.address().getUtf8String(0); + LOG.log(theLevel, category + ' ' + id + ' ' + location + ' ' + message); } } diff --git a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeOptimizationLevel.java b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeOptimizationLevel.java index 619206ad..214ed635 100644 --- a/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeOptimizationLevel.java +++ b/src/main/java/com/jyuzawa/onnxruntime/OnnxRuntimeOptimizationLevel.java @@ -26,16 +26,12 @@ public int getNumber() { } public static final OnnxRuntimeOptimizationLevel forNumber(int number) { - switch (number) { - case 1: - return ENABLE_BASIC; - case 2: - return ENABLE_EXTENDED; - case 99: - return ENABLE_ALL; - case 0: - default: - return DISABLE_ALL; - } + return switch (number) { + case 0 -> DISABLE_ALL; + case 1 -> ENABLE_BASIC; + case 2 -> ENABLE_EXTENDED; + case 99 -> ENABLE_ALL; + default -> DISABLE_ALL; + }; } } diff --git a/src/main/java/com/jyuzawa/onnxruntime/OnnxTensorElementDataType.java b/src/main/java/com/jyuzawa/onnxruntime/OnnxTensorElementDataType.java index f587bcb8..724f108f 100644 --- a/src/main/java/com/jyuzawa/onnxruntime/OnnxTensorElementDataType.java +++ b/src/main/java/com/jyuzawa/onnxruntime/OnnxTensorElementDataType.java @@ -57,43 +57,26 @@ public int getNumber() { * @return the level, UNDEFINED if not found */ public static final OnnxTensorElementDataType forNumber(int number) { - switch (number) { - case 1: - return FLOAT; - case 2: - return UINT8; - case 3: - return INT8; - case 4: - return UINT16; - case 5: - return INT16; - case 6: - return INT32; - case 7: - return INT64; - case 8: - return STRING; - case 9: - return BOOL; - case 10: - return FLOAT16; - case 11: - return DOUBLE; - case 12: - return UINT32; - case 13: - return UINT64; - case 14: - return COMPLEX64; - case 15: - return COMPLEX128; - case 16: - return BFLOAT16; - case 0: - default: - return UNDEFINED; - } + return switch (number) { + case 0 -> UNDEFINED; + case 1 -> FLOAT; + case 2 -> UINT8; + case 3 -> INT8; + case 4 -> UINT16; + case 5 -> INT16; + case 6 -> INT32; + case 7 -> INT64; + case 8 -> STRING; + case 9 -> BOOL; + case 10 -> FLOAT16; + case 11 -> DOUBLE; + case 12 -> UINT32; + case 13 -> UINT64; + case 14 -> COMPLEX64; + case 15 -> COMPLEX128; + case 16 -> BFLOAT16; + default -> UNDEFINED; + }; } ValueLayout getValueLayout() { diff --git a/src/main/java/com/jyuzawa/onnxruntime/OnnxType.java b/src/main/java/com/jyuzawa/onnxruntime/OnnxType.java index ad959596..fe1ecd6a 100644 --- a/src/main/java/com/jyuzawa/onnxruntime/OnnxType.java +++ b/src/main/java/com/jyuzawa/onnxruntime/OnnxType.java @@ -34,22 +34,15 @@ public int getNumber() { * @return the level, UNKNOWN if not found */ public static final OnnxType forNumber(int number) { - switch (number) { - case 1: - return TENSOR; - case 2: - return SEQUENCE; - case 3: - return MAP; - case 4: - return OPAQUE; - case 5: - return SPARSETENSOR; - case 6: - return OPTIONAL; - case 0: - default: - return UNKNOWN; - } + return switch (number) { + case 0 -> UNKNOWN; + case 1 -> TENSOR; + case 2 -> SEQUENCE; + case 3 -> MAP; + case 4 -> OPAQUE; + case 5 -> SPARSETENSOR; + case 6 -> OPTIONAL; + default -> UNKNOWN; + }; } }