From 73b08c7254d2b155801cc5758b32ed12e9453c17 Mon Sep 17 00:00:00 2001 From: dariuszzbyrad Date: Tue, 1 Sep 2020 13:16:08 +0200 Subject: [PATCH] (Dl4j) performance improvments (#9078) * Removed invoking the boolean constructor Signed-off-by: Dariusz Zbyrad * Use bulk operations instead of iteration Signed-off-by: Dariusz Zbyrad * Removed manuall array copy Signed-off-by: Dariusz Zbyrad --- .../keras/preprocessing/text/KerasTokenizer.java | 4 +--- .../keras/preprocessors/ReshapePreprocessor.java | 4 +--- .../java/org/ansj/recognition/impl/BookRecognition.java | 4 +--- .../java/org/ansj/recognition/impl/StopRecognition.java | 8 ++------ .../models/word2vec/wordstore/VocabularyHolder.java | 5 ++--- .../deeplearning4j/nn/graph/vertex/impl/StackVertex.java | 4 +--- .../nn/layers/variational/VariationalAutoencoder.java | 3 +-- .../parameterserver/training/SharedTrainingMaster.java | 2 +- 8 files changed, 10 insertions(+), 24 deletions(-) diff --git a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessing/text/KerasTokenizer.java b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessing/text/KerasTokenizer.java index 6461d1644..18d4bcc87 100644 --- a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessing/text/KerasTokenizer.java +++ b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessing/text/KerasTokenizer.java @@ -205,9 +205,7 @@ public class KerasTokenizer { ArrayList sortedVocabulary = new ArrayList<>(); if (outOfVocabularyToken != null) sortedVocabulary.add(outOfVocabularyToken); - for (String word: sortedWordCounts.keySet()) { - sortedVocabulary.add(word); - } + sortedVocabulary.addAll(sortedWordCounts.keySet()); for (int i = 0; i < sortedVocabulary.size(); i++) wordIndex.put(sortedVocabulary.get(i), i+1); diff --git a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessors/ReshapePreprocessor.java b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessors/ReshapePreprocessor.java index 16fa1eed2..5beacbb08 100644 --- a/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessors/ReshapePreprocessor.java +++ b/deeplearning4j/deeplearning4j-modelimport/src/main/java/org/deeplearning4j/nn/modelimport/keras/preprocessors/ReshapePreprocessor.java @@ -96,9 +96,7 @@ public class ReshapePreprocessor extends BaseInputPreProcessor { int shapeLength = shape.length; val miniBatchShape = new long[shapeLength + 1]; miniBatchShape[0] = miniBatchSize; - for (int i = 1; i < miniBatchShape.length; i++) { - miniBatchShape[i] = shape[i - 1]; - } + System.arraycopy(shape, 0, miniBatchShape, 1, miniBatchShape.length - 1); return miniBatchShape; } diff --git a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/BookRecognition.java b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/BookRecognition.java index b6ddaefec..3ec51b493 100644 --- a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/BookRecognition.java +++ b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/BookRecognition.java @@ -67,9 +67,7 @@ public class BookRecognition implements Recognition { } if (mergeList != null) { - for (Term term : list) { - list.add(term); - } + list.addAll(list); } result.setTerms(list); diff --git a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/StopRecognition.java b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/StopRecognition.java index 4449155af..f39982ec8 100644 --- a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/StopRecognition.java +++ b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp-chinese/src/main/java/org/ansj/recognition/impl/StopRecognition.java @@ -48,9 +48,7 @@ public class StopRecognition implements Recognition { * @return */ public StopRecognition insertStopWords(String... stopWords) { - for (String words : stopWords) { - stop.add(words); - } + stop.addAll(Arrays.asList(stopWords)); return this; } @@ -60,9 +58,7 @@ public class StopRecognition implements Recognition { * @param stopWords */ public void insertStopNatures(String... stopNatures) { - for (String natureStr : stopNatures) { - natureStop.add(natureStr); - } + natureStop.addAll(Arrays.asList(stopNatures)); } /** diff --git a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/word2vec/wordstore/VocabularyHolder.java b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/word2vec/wordstore/VocabularyHolder.java index 1f636d5e6..abd4b7b54 100644 --- a/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/word2vec/wordstore/VocabularyHolder.java +++ b/deeplearning4j/deeplearning4j-nlp-parent/deeplearning4j-nlp/src/main/java/org/deeplearning4j/models/word2vec/wordstore/VocabularyHolder.java @@ -345,9 +345,8 @@ public class VocabularyHolder implements Serializable { if (word.getRetentionStep() < retentionDelay - 1) { word.incrementRetentionStep(); } else { - for (int x = 1; x < retentionDelay; x++) { - word.getFrequencyShift()[x - 1] = word.getFrequencyShift()[x]; - } + if (retentionDelay - 1 >= 0) + System.arraycopy(word.getFrequencyShift(), 1, word.getFrequencyShift(), 0, retentionDelay - 1); } } logger.info("Scavenger was activated. Vocab size before: [" + initialSize + "], after: [" + vocabulary.size() diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl/StackVertex.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl/StackVertex.java index 3be9d6895..0d79167c8 100644 --- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl/StackVertex.java +++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/graph/vertex/impl/StackVertex.java @@ -77,9 +77,7 @@ public class StackVertex extends BaseGraphVertex { // create the new shape outShape[0] = nStack * inShape[0]; - for (int i = 1; i < inShape.length; i++) { - outShape[i] = inShape[i]; - } + System.arraycopy(inShape, 1, outShape, 1, inShape.length - 1); boolean variableLengthTS = false; if (inShape.length == 3) { diff --git a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/variational/VariationalAutoencoder.java b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/variational/VariationalAutoencoder.java index 8e7cee485..4396f747f 100644 --- a/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/variational/VariationalAutoencoder.java +++ b/deeplearning4j/deeplearning4j-nn/src/main/java/org/deeplearning4j/nn/layers/variational/VariationalAutoencoder.java @@ -838,8 +838,7 @@ public class VariationalAutoencoder implements Layer { return; } - for (TrainingListener listener : listeners) - trainingListeners.add(listener); + trainingListeners.addAll(Arrays.asList(listeners)); } @Override diff --git a/deeplearning4j/deeplearning4j-scaleout/spark/dl4j-spark-parameterserver/src/main/java/org/deeplearning4j/spark/parameterserver/training/SharedTrainingMaster.java b/deeplearning4j/deeplearning4j-scaleout/spark/dl4j-spark-parameterserver/src/main/java/org/deeplearning4j/spark/parameterserver/training/SharedTrainingMaster.java index f90bbdcf6..840466284 100644 --- a/deeplearning4j/deeplearning4j-scaleout/spark/dl4j-spark-parameterserver/src/main/java/org/deeplearning4j/spark/parameterserver/training/SharedTrainingMaster.java +++ b/deeplearning4j/deeplearning4j-scaleout/spark/dl4j-spark-parameterserver/src/main/java/org/deeplearning4j/spark/parameterserver/training/SharedTrainingMaster.java @@ -915,7 +915,7 @@ public class SharedTrainingMaster extends BaseTrainingMaster