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

[ci] Upgrade spotbugs to 5.0.14 #2594

Merged
merged 1 commit into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -16,31 +16,15 @@
import ai.djl.translate.Transform;
import ai.djl.util.RandomUtils;

import java.util.Random;

/**
* A {@link Transform} that randomly flip the input image left to right with a probability of 0.5.
*/
public class RandomFlipLeftRight implements Transform {
Integer seed;

/** Creates a new instance of {@code RandomFlipLeftRight}. */
public RandomFlipLeftRight() {}

/**
* Creates a new instance of {@code RandomFlipLeftRight} with the given seed.
*
* @param seed the value of the seed
*/
public RandomFlipLeftRight(int seed) {
this.seed = seed;
}

/** {@inheritDoc} */
@Override
public NDArray transform(NDArray array) {
Random rnd = (seed != null) ? new Random(seed) : RandomUtils.RANDOM;
if (rnd.nextFloat() > 0.5) {
if (RandomUtils.nextFloat() > 0.5) {
array.flip(1);
}
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,15 @@
import ai.djl.translate.Transform;
import ai.djl.util.RandomUtils;

import java.util.Random;

/**
* A {@link Transform} that randomly flip the input image top to bottom with a probability of 0.5.
*/
public class RandomFlipTopBottom implements Transform {

Integer seed;

/** Creates a new instance of {@code RandomFlipTopBottom}. */
public RandomFlipTopBottom() {}

/**
* Creates a new instance of {@code RandomFlipTopBottom} with the given seed.
*
* @param seed the value of the seed
*/
public RandomFlipTopBottom(int seed) {
this.seed = seed;
}

/** {@inheritDoc} */
@Override
public NDArray transform(NDArray array) {
Random rnd = (seed != null) ? new Random(seed) : RandomUtils.RANDOM;
if (rnd.nextFloat() > 0.5) {
if (RandomUtils.nextFloat() > 0.5) {
array.flip(0);
}
return array;
Expand Down
21 changes: 3 additions & 18 deletions api/src/main/java/ai/djl/training/dataset/RandomSampler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.stream.LongStream;

/**
Expand All @@ -26,19 +25,6 @@
* <p>{@code RandomSampler} samples the data from [0, dataset.size) randomly.
*/
public class RandomSampler implements Sampler.SubSampler {
private Integer seed;

/** Creates a new instance of {@code RandomSampler}. */
public RandomSampler() {}

/**
* Creates a new instance of {@code RandomSampler} with the given seed.
*
* @param seed the value of the seed
*/
public RandomSampler(int seed) {
this.seed = seed;
}

private static void swap(long[] arr, int i, int j) {
long tmp = arr[i];
Expand All @@ -49,23 +35,22 @@ private static void swap(long[] arr, int i, int j) {
/** {@inheritDoc} */
@Override
public Iterator<Long> sample(RandomAccessDataset dataset) {
return new Iterate(dataset, seed);
return new Iterate(dataset);
}

static class Iterate implements Iterator<Long> {

private long[] indices;
private long current;

Iterate(RandomAccessDataset dataset, Integer seed) {
Iterate(RandomAccessDataset dataset) {
long size = dataset.size();
current = 0;
indices = LongStream.range(0, size).toArray();
Random rnd = (seed != null) ? new Random(seed) : RandomUtils.RANDOM;
// java array didn't support index greater than max integer
// so cast to int for now
for (int i = Math.toIntExact(size) - 1; i > 0; --i) {
swap(indices, i, rnd.nextInt(i));
swap(indices, i, RandomUtils.nextInt(i));
}
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id "com.github.spotbugs" version "4.7.1" apply false
id "com.github.spotbugs" version "5.0.14" apply false
}

defaultTasks 'build'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/** {@code LgbmEngineProvider} is the LightGBM implementation of {@link EngineProvider}. */
public class LgbmEngineProvider implements EngineProvider {

private static Engine engine;
private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
Expand All @@ -34,9 +34,11 @@ public int getEngineRank() {

/** {@inheritDoc} */
@Override
public synchronized Engine getEngine() {
public Engine getEngine() {
if (engine == null) {
engine = LgbmEngine.newInstance();
synchronized (LgbmEngineProvider.class) {
engine = LgbmEngine.newInstance();
}
}
return engine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/** {@code XgbEngineProvider} is the XGBoost implementation of {@link EngineProvider}. */
public class XgbEngineProvider implements EngineProvider {

private static Engine engine;
private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
Expand All @@ -34,9 +34,11 @@ public int getEngineRank() {

/** {@inheritDoc} */
@Override
public synchronized Engine getEngine() {
public Engine getEngine() {
if (engine == null) {
engine = XgbEngine.newInstance();
synchronized (XgbEngineProvider.class) {
engine = XgbEngine.newInstance();
}
}
return engine;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (MxEngineProvider.class) {
engine = MxEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected NDList forwardInternal(
boolean training,
PairList<String, Object> params) {
if (first) {
synchronized (MxSymbolBlock.class) {
synchronized (this) {
if (first) {
// create CachedOp is not thread-safe
// add synchronized block to avoid creating multiple CachedOps
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (OrtEngineProvider.class) {
engine = OrtEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (PpEngineProvider.class) {
engine = PpEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (PtEngineProvider.class) {
engine = PtEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected NDList forwardInternal(
JniUtils.setGraphExecutorOptimize(setOptimizer);
}
if (first) {
synchronized (PtSymbolBlock.class) {
synchronized (this) {
if (first) {
inputDescriptions = new PairList<>();
outputDescriptions = new PairList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (TfEngineProvider.class) {
engine = TfEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (TrtEngineProvider.class) {
engine = TrtEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public int getEngineRank() {
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (this) {
synchronized (TfLiteEngineProvider.class) {
engine = TfLiteEngine.newInstance();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ DataType dataTypeOf(Object o) {
return DataType.STRING;
}
}
} else {
throw new NullPointerException();
}
throw new IllegalArgumentException(
"DataType error: cannot resolve DataType of " + o.getClass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package ai.djl.timeseries.transform;

import ai.djl.ndarray.NDArray;
import ai.djl.util.RandomUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
* Keeps track of the average time series length and adjusts the probability per time point such
Expand Down Expand Up @@ -62,10 +62,9 @@ public List<Integer> call(NDArray ts) {

double prob = numInstances / avgLength;
List<Integer> indices = new ArrayList<>();
Random random = new Random();
while (indices.isEmpty()) {
for (int i = 0; i < windowSize; i++) {
if (random.nextDouble() < prob) {
if (RandomUtils.RANDOM.nextDouble() < prob) {
indices.add(i + bound[0]);
}
}
Expand Down
26 changes: 3 additions & 23 deletions tools/conf/findbugs-exclude.xml
Original file line number Diff line number Diff line change
@@ -1,36 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FindBugsFilter>
<Match>
<Bug pattern="DM_EXIT,DMI_EMPTY_DB_PASSWORD,DMI_HARDCODED_ABSOLUTE_FILENAME,EI_EXPOSE_REP,EI_EXPOSE_REP2,SF_SWITCH_FALLTHROUGH,NM_CONFUSING"/>
<Bug pattern="DM_EXIT,DMI_EMPTY_DB_PASSWORD,DMI_HARDCODED_ABSOLUTE_FILENAME,EI_EXPOSE_REP,EI_EXPOSE_REP2,EI_EXPOSE_STATIC_REP2,MS_EXPOSE_REP,SF_SWITCH_FALLTHROUGH,NM_CONFUSING"/>
</Match>
<!-- low priority issues-->
<Match>
<Bug pattern="DM_CONVERT_CASE,SE_TRANSIENT_FIELD_NOT_RESTORED,UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR,BC_UNCONFIRMED_CAST_OF_RETURN_VALUE"/>
</Match>
<Match>
<Bug pattern="PZLA_PREFER_ZERO_LENGTH_ARRAYS,DB_DUPLICATE_SWITCH_CLAUSES,BC_UNCONFIRMED_CAST,URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD"/>
<Bug pattern="PZLA_PREFER_ZERO_LENGTH_ARRAYS,DB_DUPLICATE_SWITCH_CLAUSES,BC_UNCONFIRMED_CAST,URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD,URF_UNREAD_FIELD"/>
</Match>

<!-- wildcard suppression -->
<Match>
<Class name="~ai\.djl\.mxnet\.jnarator\.parser\..*"/>
</Match>
<Match>
<Class name="~org\.tensorflow\.lite\..*"/>
</Match>
<!-- function suppression -->
<Match>
<Bug pattern="DC_DOUBLECHECK"/>
<Class name="~ai\.djl\.mxnet\.engine\.MxSymbolBlock"/>
<Method name="forwardInternal"/>
</Match>
<Match>
<Bug pattern="DC_DOUBLECHECK"/>
<Class name="~ai\.djl\.pytorch\.engine\.PtSymbolBlock"/>
<Method name="forwardInternal"/>
</Match>
<Match>
<Bug pattern="URF_UNREAD_FIELD"/>
<Class name="~ai\.djl\.pytorch\.engine\.PtNDArray"/>
<Bug pattern="MS_FINAL_PKGPROTECT,MS_PKGPROTECT,NM_METHOD_NAMING_CONVENTION,SF_SWITCH_NO_DEFAULT,DC_DOUBLECHECK,IS2_INCONSISTENT_SYNC"/>
</Match>
</FindBugsFilter>