Repository: deepjavalibrary/djl
Pull request #3148 (2024-05-02 18:44:24)

[api] Fixes logging invoke convention
## Description ##

Brief description of what this PR is about

- If this change is a backward incompatible change, why must this change be made?
- Interesting edge cases to note here



Patch:
diff --git a/api/src/main/java/ai/djl/repository/SimpleUrlRepository.java b/api/src/main/java/ai/djl/repository/SimpleUrlRepository.java
index b01ed33c3..171d8b6b3 100644
--- a/api/src/main/java/ai/djl/repository/SimpleUrlRepository.java
+++ b/api/src/main/java/ai/djl/repository/SimpleUrlRepository.java
@@ -90,7 +90,7 @@ public class SimpleUrlRepository extends AbstractRepository {
                 return Collections.singletonList(mrl);
             }
         } catch (IOException e) {
-            logger.warn("Failed to connect URL: " + uri, e);
+            logger.warn("Failed to connect URL: {}", uri, e);
         }
         return Collections.emptyList();
     }
@@ -141,7 +141,7 @@ public class SimpleUrlRepository extends AbstractRepository {
                 conn.setRequestMethod("HEAD");
                 int code = conn.getResponseCode();
                 if (code != 200) {
-                    logger.info("request error: " + code);
+                    logger.info("request error: {}", code);
                     return -1;
                 }
                 return conn.getContentLength();
diff --git a/api/src/main/java/ai/djl/repository/zoo/ModelZoo.java b/api/src/main/java/ai/djl/repository/zoo/ModelZoo.java
index 4d8b77108..fa8e48934 100644
--- a/api/src/main/java/ai/djl/repository/zoo/ModelZoo.java
+++ b/api/src/main/java/ai/djl/repository/zoo/ModelZoo.java
@@ -302,7 +302,7 @@ public abstract class ModelZoo {
                 Utils.deleteQuietly(tmp);
             }
         } catch (IOException e) {
-            logger.warn("Failed load index of models: " + app, e);
+            logger.warn("Failed load index of models: {}", app, e);
         }
 
         return Collections.emptyMap();
diff --git a/api/src/main/java/ai/djl/training/listener/MemoryTrainingListener.java b/api/src/main/java/ai/djl/training/listener/MemoryTrainingListener.java
index b906c3980..86d7b721f 100644
--- a/api/src/main/java/ai/djl/training/listener/MemoryTrainingListener.java
+++ b/api/src/main/java/ai/djl/training/listener/MemoryTrainingListener.java
@@ -177,7 +177,7 @@ public class MemoryTrainingListener extends TrainingListenerAdapter {
                     metrics.addMetric("rss", rss, Unit.BYTES);
                 }
             } catch (IOException e) {
-                logger.error("Failed execute cmd: " + cmd, e);
+                logger.error("Failed execute cmd: {}", cmd, e);
             }
         }
     }
diff --git a/api/src/main/java/ai/djl/util/NativeResource.java b/api/src/main/java/ai/djl/util/NativeResource.java
index 9d83d49a4..3cc91919f 100644
--- a/api/src/main/java/ai/djl/util/NativeResource.java
+++ b/api/src/main/java/ai/djl/util/NativeResource.java
@@ -58,7 +58,7 @@ public abstract class NativeResource<T> implements AutoCloseable {
         T reference = handle.get();
         if (reference == null) {
             if (TRACK_RESOURCE) {
-                logger.error("Native resource " + uid + " is released. Closed at:", exception);
+                logger.error("Native resource {} is released. Closed at:", uid, exception);
             }
             String message = "Native resource has been released already.";
             throw new IllegalStateException(message);
diff --git a/api/src/main/java/ai/djl/util/Platform.java b/api/src/main/java/ai/djl/util/Platform.java
index b2ce08c38..2ab25c2f3 100644
--- a/api/src/main/java/ai/djl/util/Platform.java
+++ b/api/src/main/java/ai/djl/util/Platform.java
@@ -305,7 +305,7 @@ public final class Platform {
                         <= Integer.parseInt(system.flavor.substring(2, 5))) {
             return true;
         }
-        logger.warn("The bundled library: " + this + " doesn't match system: " + system);
+        logger.warn("The bundled library: {}} doesn't match system: {}", this, system);
         return false;
     }
 
diff --git a/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/CachedOp.java b/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/CachedOp.java
index b1ca8e49a..7f96ee0d6 100644
--- a/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/CachedOp.java
+++ b/engines/mxnet/mxnet-engine/src/main/java/ai/djl/mxnet/engine/CachedOp.java
@@ -124,11 +124,9 @@ public class CachedOp extends NativeResource<Pointer> {
                 String key = pair.getKey();
                 if (!"prob_label".equals(key) && !"softmax_label".equals(key)) {
                     logger.warn(
-                            "Input "
-                                    + key
-                                    + " not found, set NDArray to Shape("
-                                    + batchSize
-                                    + ") by default");
+                            "Input {} not found, set NDArray to Shape({}) by default",
+                            key,
+                            batchSize);
                 }
                 allInputsNDArray[pair.getValue()] =
                         (MxNDArray) inputManager.create(new Shape(batchSize));
diff --git a/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngine.java b/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngine.java
index 7f48547b4..f909a5788 100644
--- a/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngine.java
+++ b/engines/pytorch/pytorch-engine/src/main/java/ai/djl/pytorch/engine/PtEngine.java
@@ -69,8 +69,8 @@ public final class PtEngine extends Engine {
                             + " inference latency and throughput. See:"
                             + " https://docs.djl.ai/docs/development/inference_performance_optimization.html#graph-executor-optimization");
             }
-            logger.info("Number of inter-op threads is " + JniUtils.getNumInteropThreads());
-            logger.info("Number of intra-op threads is " + JniUtils.getNumThreads());
+            logger.info("Number of inter-op threads is {}", JniUtils.getNumInteropThreads());
+            logger.info("Number of intra-op threads is {}", JniUtils.getNumThreads());
 
             String paths = Utils.getEnvOrSystemProperty("PYTORCH_EXTRA_LIBRARY_PATH");
             if (paths != null) {
diff --git a/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfSymbolBlock.java b/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfSymbolBlock.java
index 2453e58c2..25b803df6 100644
--- a/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfSymbolBlock.java
+++ b/engines/tensorflow/tensorflow-engine/src/main/java/ai/djl/tensorflow/engine/TfSymbolBlock.java
@@ -75,17 +75,14 @@ public class TfSymbolBlock extends AbstractSymbolBlock implements AutoCloseable
         } else {
             Set<String> keys = signatureDefMap.keySet();
             logger.warn(
-                    "SignatureDefKey: "
-                            + signatureDefKey
-                            + "not found in Saved Model Bundle."
-                            + "Available keys: "
-                            + String.join(" ", keys)
-                            + "Please use .optOption(\"SignatureDefKey\", \"value\") with"
-                            + " Criteria.builder to load the model.Normally the value is"
-                            + " \"default\" for TF1.x models and \"serving_default\" for TF2.x"
-                            + " models. Refer to:"
-                            + " https://www.tensorflow.org/guide/saved_modelLoading the model using"
-                            + " next available key.");
+                    "SignatureDefKey: {} not found in Saved Model Bundle.Available keys: {} Please"
+                        + " use .optOption(\"SignatureDefKey\", \"value\") with Criteria.builder to"
+                        + " load the model.Normally the value is \"default\" for TF1.x models and"
+                        + " \"serving_default\" for TF2.x models. Refer to:"
+                        + " https://www.tensorflow.org/guide/saved_modelLoading the model using"
+                        + " next available key.",
+                    signatureDefKey,
+                    String.join(" ", keys));
             servingDefault = signatureDefMap.get(keys.iterator().next());
         }
         describeInput();
diff --git a/examples/src/main/java/ai/djl/examples/inference/BertClassification.java b/examples/src/main/java/ai/djl/examples/inference/BertClassification.java
index 954704517..96404ab27 100644
--- a/examples/src/main/java/ai/djl/examples/inference/BertClassification.java
+++ b/examples/src/main/java/ai/djl/examples/inference/BertClassification.java
@@ -60,7 +60,7 @@ public final class BertClassification {
 
         Classifications[] results = predict(inputs);
         for (int i = 0; i < inputs.size(); i++) {
-            logger.info("Prediction for: " + inputs.get(i) + "\n" + results[i].toString());
+            logger.info("Prediction for: {}\n{}", inputs.get(i), results[i]);
         }
     }
 
diff --git a/examples/src/main/java/ai/djl/examples/inference/UniversalSentenceEncoder.java b/examples/src/main/java/ai/djl/examples/inference/UniversalSentenceEncoder.java
index bb86e23d9..01d64e98b 100644
--- a/examples/src/main/java/ai/djl/examples/inference/UniversalSentenceEncoder.java
+++ b/examples/src/main/java/ai/djl/examples/inference/UniversalSentenceEncoder.java
@@ -56,7 +56,7 @@ public final class UniversalSentenceEncoder {
 
         float[][] embeddings = UniversalSentenceEncoder.predict(inputs);
         for (int i = 0; i < inputs.size(); i++) {
-            logger.info("Embedding for: " + inputs.get(i) + "\n" + Arrays.toString(embeddings[i]));
+            logger.info("Embedding for: {}\n{}", inputs.get(i), Arrays.toString(embeddings[i]));
         }
     }
 
diff --git a/extensions/aws-ai/src/main/java/ai/djl/aws/s3/S3Repository.java b/extensions/aws-ai/src/main/java/ai/djl/aws/s3/S3Repository.java
index 898c1a94d..1fc499d5e 100644
--- a/extensions/aws-ai/src/main/java/ai/djl/aws/s3/S3Repository.java
+++ b/extensions/aws-ai/src/main/java/ai/djl/aws/s3/S3Repository.java
@@ -147,7 +147,7 @@ public class S3Repository extends AbstractRepository {
                 return Collections.singletonList(mrl);
             }
         } catch (IOException e) {
-            logger.warn("Failed to scan S3: " + bucket, e);
+            logger.warn("Failed to scan S3: {}", bucket, e);
         }
         return Collections.emptyList();
     }
diff --git a/extensions/hadoop/src/main/java/ai/djl/hadoop/hdfs/HdfsRepository.java b/extensions/hadoop/src/main/java/ai/djl/hadoop/hdfs/HdfsRepository.java
index f010b0760..ddcf82e1f 100644
--- a/extensions/hadoop/src/main/java/ai/djl/hadoop/hdfs/HdfsRepository.java
+++ b/extensions/hadoop/src/main/java/ai/djl/hadoop/hdfs/HdfsRepository.java
@@ -144,7 +144,7 @@ public class HdfsRepository extends AbstractRepository {
                 return Collections.singletonList(mrl);
             }
         } catch (IOException e) {
-            logger.warn("Failed to scan: " + uri.toString(), e);
+            logger.warn("Failed to scan: {}", uri, e);
         }
         return Collections.emptyList();
     }
@@ -157,7 +157,7 @@ public class HdfsRepository extends AbstractRepository {
         resolved = true;
         Artifact artifact = listFiles();
         if (artifact == null) {
-            logger.debug("No object found in hdfs: " + uri);
+            logger.debug("No object found in hdfs: {}", uri);
             return null;
         }
 
diff --git a/extensions/tokenizers/rust/src/lib.rs b/extensions/tokenizers/rust/src/lib.rs
index 6e427f683..af3fec7cd 100644
--- a/extensions/tokenizers/rust/src/lib.rs
+++ b/extensions/tokenizers/rust/src/lib.rs
@@ -48,13 +48,18 @@ pub extern "system" fn Java_ai_djl_huggingface_tokenizers_jni_TokenizersLibrary_
     mut env: JNIEnv<'local>,
     _: JObject,
     input: JString,
+    hf_token: JString,
 ) -> jlong {
     let identifier: String = env
         .get_string(&input)
         .expect("Couldn't get java string!")
         .into();
 
-    let parameters = FromPretrainedParameters::default();
+    let mut parameters = FromPretrainedParameters::default();
+    if !hf_token.is_null() {
+        let hf_token: String = env.get_string(&hf_token).unwrap().into();
+        parameters.auth_token = Some(hf_token);
+    }
     let tokenizer = Tokenizer::from_pretrained(identifier, Some(parameters));
 
     match tokenizer {
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizer.java b/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizer.java
index 8d32ccb4b..177366faf 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizer.java
+++ b/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizer.java
@@ -112,8 +112,12 @@ public final class HuggingFaceTokenizer extends NativeResource<Long> implements
     public static HuggingFaceTokenizer newInstance(String identifier, Map<String, String> options) {
         Ec2Utils.callHome("Huggingface");
         LibUtils.checkStatus();
+        String autoToken = Utils.getEnvOrSystemProperty("HF_TOKEN");
+        if (options != null) {
+            autoToken = options.getOrDefault("hf_token", autoToken);
+        }
 
-        long handle = TokenizersLibrary.LIB.createTokenizer(identifier);
+        long handle = TokenizersLibrary.LIB.createTokenizer(identifier, autoToken);
         return new HuggingFaceTokenizer(handle, options);
     }
 
@@ -543,12 +547,12 @@ public final class HuggingFaceTokenizer extends NativeResource<Long> implements
         if (padding == PaddingStrategy.MAX_LENGTH || isTruncate) {
             if (maxLength == -1) {
                 logger.warn(
-                        "maxLength is not explicitly specified, use modelMaxLength: "
-                                + modelMaxLength);
+                        "maxLength is not explicitly specified, use modelMaxLength: {}",
+                        modelMaxLength);
                 maxLength = modelMaxLength;
             } else if (maxLength > modelMaxLength) {
                 logger.warn(
-                        "maxLength is greater then modelMaxLength, change to: " + modelMaxLength);
+                        "maxLength is greater then modelMaxLength, change to: {}", modelMaxLength);
                 maxLength = modelMaxLength;
             }
 
@@ -560,12 +564,11 @@ public final class HuggingFaceTokenizer extends NativeResource<Long> implements
                         newMaxLength -= padToMultipleOf;
                     }
                     logger.warn(
-                            "maxLength ("
-                                    + maxLength
-                                    + ") is not a multiple of padToMultipleOf ("
-                                    + padToMultipleOf
-                                    + "), change to: "
-                                    + newMaxLength);
+                            "maxLength ({}) is not a multiple of padToMultipleOf ({}), change to:"
+                                    + " {}",
+                            maxLength,
+                            padToMultipleOf,
+                            newMaxLength);
                     maxLength = newMaxLength;
                 }
             }
diff --git a/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/jni/TokenizersLibrary.java b/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/jni/TokenizersLibrary.java
index 63a2f96fd..8acfa7d04 100644
--- a/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/jni/TokenizersLibrary.java
+++ b/extensions/tokenizers/src/main/java/ai/djl/huggingface/tokenizers/jni/TokenizersLibrary.java
@@ -20,7 +20,7 @@ public final class TokenizersLibrary {
 
     private TokenizersLibrary() {}
 
-    public native long createTokenizer(String identifier);
+    public native long createTokenizer(String identifier, String authToken);
 
     public native long createTokenizerFromString(String json);
 


Test patch:
diff --git a/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizerTest.java b/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizerTest.java
index 1b0883dfb..ead688539 100644
--- a/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizerTest.java
+++ b/extensions/tokenizers/src/test/java/ai/djl/huggingface/tokenizers/HuggingFaceTokenizerTest.java
@@ -14,6 +14,7 @@
 package ai.djl.huggingface.tokenizers;
 
 import ai.djl.huggingface.tokenizers.jni.CharSpan;
+import ai.djl.testing.TestRequirements;
 import ai.djl.training.util.DownloadUtils;
 import ai.djl.util.PairList;
 import ai.djl.util.Utils;
@@ -541,4 +542,17 @@ public class HuggingFaceTokenizerTest {
             Assert.assertEquals(tokens, expected);
         }
     }
+
+    @Test
+    public void testAuthToken() {
+        TestRequirements.notOffline();
+        System.setProperty("HF_TOKEN", "test_token");
+        try {
+            HuggingFaceTokenizer.newInstance("mistralai/Mixtral-8x7B-v0.1");
+        } catch (RuntimeException ignore) {
+            // access denied
+        } finally {
+            System.clearProperty("HF_TOKEN");
+        }
+    }
 }
diff --git a/integration/src/main/java/ai/djl/integration/IntegrationTest.java b/integration/src/main/java/ai/djl/integration/IntegrationTest.java
index f3871ff27..9f9bcf6e1 100644
--- a/integration/src/main/java/ai/djl/integration/IntegrationTest.java
+++ b/integration/src/main/java/ai/djl/integration/IntegrationTest.java
@@ -373,8 +373,7 @@ public class IntegrationTest {
                     logger.trace("", e.getCause());
                     result = TestResult.UNSUPPORTED;
                 } else {
-                    logger.error("Test {}.{} FAILED", getName(), method.getName());
-                    logger.error("", e.getCause());
+                    logger.error("Test {}.{} FAILED", getName(), method.getName(), e.getCause());
                     result = TestResult.FAILED;
                 }
             } finally {