Skip to content

Commit

Permalink
Spotless fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craigacp authored and karllessard committed Feb 24, 2024
1 parent 911fb28 commit de1d6f0
Show file tree
Hide file tree
Showing 50 changed files with 299 additions and 228 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<maven.javadoc.skip>true</maven.javadoc.skip>
<maven.source.skip>true</maven.source.skip>
<gpg.skip>true</gpg.skip>
<spotless.version>2.38.0</spotless.version>
<spotless.version>2.43.0</spotless.version>
</properties>

<repositories>
Expand Down Expand Up @@ -564,7 +564,7 @@
<lineEndings/>
<java>
<googleJavaFormat>
<version>1.17.0</version>
<version>1.20.0</version>
</googleJavaFormat>
<removeUnusedImports/>
</java>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@

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

import org.bytedeco.javacpp.Pointer;
import org.bytedeco.javacpp.PointerPointer;
import org.bytedeco.javacpp.PointerScope;
import org.tensorflow.internal.c_api.TF_Operation;
import org.tensorflow.internal.c_api.TF_Output;
import org.tensorflow.internal.c_api.TFJ_Scope;
import org.tensorflow.internal.c_api.TFJ_GradFuncAdapter;
import org.tensorflow.internal.c_api.TFJ_GraphId;
import org.tensorflow.internal.c_api.TFJ_Scope;
import org.tensorflow.internal.c_api.TF_Operation;
import org.tensorflow.internal.c_api.TF_Output;

/** Helper base class for custom gradient adapters <b>INTERNAL USE ONLY</b> */
public abstract class AbstractGradientAdapter extends TFJ_GradFuncAdapter {
Expand All @@ -35,10 +34,17 @@ protected AbstractGradientAdapter() {
super();
}

protected abstract List<Operand<?>> apply(Graph graph, TFJ_Scope scope, GraphOperation operation, List<Output<?>> gradInputs);
protected abstract List<Operand<?>> apply(
Graph graph, TFJ_Scope scope, GraphOperation operation, List<Output<?>> gradInputs);

@Override
public int call(TFJ_GraphId nativeGraphId, TFJ_Scope nativeScope, TF_Operation nativeOperation, TF_Output nativeGradInputs, int nativeGradInputsLength, PointerPointer nativeGradOutputsPtr) {
public int call(
TFJ_GraphId nativeGraphId,
TFJ_Scope nativeScope,
TF_Operation nativeOperation,
TF_Output nativeGradInputs,
int nativeGradInputsLength,
PointerPointer nativeGradOutputsPtr) {
try (PointerScope callScope = new PointerScope()) {
var graph = Graph.findGraph(nativeGraphId);
var operation = new GraphOperation(graph, nativeOperation);
Expand Down Expand Up @@ -67,7 +73,8 @@ private static List<Output<?>> fromNativeOutputs(Graph g, TF_Output nativeOutput
List<Output<?>> outputs = new ArrayList<>(length);
for (int i = 0; i < length; ++i) {
var nativeOutput = nativeOutputs.position(i);
outputs.add(i, new Output<>(new GraphOperation(g, nativeOutput.oper()), nativeOutput.index()));
outputs.add(
i, new Output<>(new GraphOperation(g, nativeOutput.oper()), nativeOutput.index()));
}
return outputs;
}
Expand All @@ -79,13 +86,15 @@ private static List<Output<?>> fromNativeOutputs(Graph g, TF_Output nativeOutput
* @return pointer to the native array of outputs
*/
private static TF_Output toNativeOutputs(List<Operand<?>> outputs) {
// Use malloc to allocate native outputs, as they will be freed by the native layer and we do not want JavaCPP to deallocate them
var nativeOutputs = new TF_Output(Pointer.malloc((long)outputs.size() * Pointer.sizeof(TF_Output.class)));
// Use malloc to allocate native outputs, as they will be freed by the native layer and we do
// not want JavaCPP to deallocate them
var nativeOutputs =
new TF_Output(Pointer.malloc((long) outputs.size() * Pointer.sizeof(TF_Output.class)));

for (int i = 0; i < outputs.size(); ++i) {
var output = outputs.get(i).asOutput();
var nativeOutput = nativeOutputs.getPointer(i);
nativeOutput.oper(((GraphOperation)output.op()).getUnsafeNativeHandle());
nativeOutput.oper(((GraphOperation) output.op()).getUnsafeNativeHandle());
nativeOutput.index(output.index());
}
return nativeOutputs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_GetGraphId;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_AddGradientsWithPrefix;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteGraph;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_FinishWhile;
Expand All @@ -28,7 +29,6 @@
import static org.tensorflow.internal.c_api.global.tensorflow.TF_ImportGraphDefOptionsSetPrefix;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_NewGraph;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_NewWhile;
import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_GetGraphId;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.ArrayDeque;
Expand All @@ -50,6 +50,7 @@
import org.bytedeco.javacpp.PointerScope;
import org.bytedeco.javacpp.SizeTPointer;
import org.tensorflow.exceptions.TensorFlowException;
import org.tensorflow.internal.c_api.TFJ_GraphId;
import org.tensorflow.internal.c_api.TF_Buffer;
import org.tensorflow.internal.c_api.TF_Function;
import org.tensorflow.internal.c_api.TF_Graph;
Expand All @@ -58,7 +59,6 @@
import org.tensorflow.internal.c_api.TF_Output;
import org.tensorflow.internal.c_api.TF_Status;
import org.tensorflow.internal.c_api.TF_WhileParams;
import org.tensorflow.internal.c_api.TFJ_GraphId;
import org.tensorflow.ndarray.StdArrays;
import org.tensorflow.op.Op;
import org.tensorflow.op.OpScope;
Expand Down Expand Up @@ -1319,7 +1319,8 @@ private static SaverDef addVariableSaver(Graph graph) {
.build();
}

private static final Map<TFJ_GraphId, Graph> ALL_GRAPHS = Collections.synchronizedMap(new WeakHashMap<>());
private static final Map<TFJ_GraphId, Graph> ALL_GRAPHS =
Collections.synchronizedMap(new WeakHashMap<>());

/**
* Find the graph with the matching ID.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_UnmapOperationName;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_AddControlInput;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_AddInput;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_AddInputList;
Expand All @@ -39,7 +40,6 @@
import static org.tensorflow.internal.c_api.global.tensorflow.TF_SetAttrTypeList;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_SetAttrValueProto;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_SetDevice;
import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_UnmapOperationName;

import java.nio.charset.Charset;
import java.util.Arrays;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static class TensorDescription {

/** The name of the tensor's operand in the graph */
public final String name;

/** The data type of the tensor */
public final DataType dataType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,36 @@
*/
package org.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_HasGradient;
import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_RegisterCustomGradient;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteBuffer;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteLibraryHandle;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetAllOpList;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetOpList;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_LoadLibrary;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_RegisterFilesystemPlugin;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_Version;
import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_HasGradient;
import static org.tensorflow.internal.c_api.global.tensorflow.TFJ_RegisterCustomGradient;

import com.google.protobuf.InvalidProtocolBufferException;
import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import org.bytedeco.javacpp.PointerScope;
import org.tensorflow.exceptions.TensorFlowException;
import org.tensorflow.internal.c_api.TFJ_GradFuncAdapter;
import org.tensorflow.internal.c_api.TFJ_RuntimeLibrary;
import org.tensorflow.internal.c_api.TF_Buffer;
import org.tensorflow.internal.c_api.TF_Library;
import org.tensorflow.internal.c_api.TF_Status;
import org.tensorflow.internal.c_api.TFJ_GradFuncAdapter;
import org.tensorflow.internal.c_api.TFJ_RuntimeLibrary;
import org.tensorflow.op.CustomGradient;
import org.tensorflow.op.RawCustomGradient;
import org.tensorflow.op.RawOpInputs;
import org.tensorflow.op.annotation.OpInputsMetadata;
import org.tensorflow.op.annotation.OpMetadata;
import org.tensorflow.proto.OpList;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;

/** Static utility methods describing the TensorFlow runtime. */
public final class TensorFlow {

Expand Down Expand Up @@ -199,7 +198,8 @@ static synchronized boolean hasGradient(String opType) {
* @return {@code true} if the gradient was registered, {@code false} if there was already a
* gradient registered for this op
*/
public static synchronized boolean registerCustomGradient(String opType, RawCustomGradient gradient) {
public static synchronized boolean registerCustomGradient(
String opType, RawCustomGradient gradient) {
if (isWindowsOs()) {
throw new UnsupportedOperationException(
"Custom gradient registration is not supported on Windows systems.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_BFLOAT16} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_BFLOAT16} tensors to a n-dimensional data
* space.
*/
public final class TBfloat16Mapper extends TensorMapper<TBfloat16> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_BOOL} tensors to a n-dimensional
* data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_BOOL} tensors to a n-dimensional data
* space.
*/
public final class TBoolMapper extends TensorMapper<TBool> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_HALF} tensors to a n-dimensional
* data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_HALF} tensors to a n-dimensional data
* space.
*/
public final class TFloat16Mapper extends TensorMapper<TFloat16> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_FLOAT} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_FLOAT} tensors to a n-dimensional data
* space.
*/
public final class TFloat32Mapper extends TensorMapper<TFloat32> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_DOUBLE} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_DOUBLE} tensors to a n-dimensional data
* space.
*/
public final class TFloat64Mapper extends TensorMapper<TFloat64> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_INT32} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_INT32} tensors to a n-dimensional data
* space.
*/
public final class TInt32Mapper extends TensorMapper<TInt32> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.tensorflow.types.TInt64;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_INT64} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_INT64} tensors to a n-dimensional data
* space.
*/
public final class TInt64Mapper extends TensorMapper<TInt64> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.tensorflow.types.TString;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_STRING} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_STRING} tensors to a n-dimensional data
* space.
*/
public final class TStringMapper extends TensorMapper<TString> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TUint16;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_Uint16} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_Uint16} tensors to a n-dimensional data
* space.
*/
public final class TUint16Mapper extends TensorMapper<TUint16> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.tensorflow.types.TUint8;

/**
* Maps memory of {@link org.tensorflow.proto.DataType#DT_UINT8} tensors to a
* n-dimensional data space.
* Maps memory of {@link org.tensorflow.proto.DataType#DT_UINT8} tensors to a n-dimensional data
* space.
*/
public final class TUint8Mapper extends TensorMapper<TUint8> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
*/
package org.tensorflow.op;

import java.util.List;
import org.tensorflow.Operand;
import org.tensorflow.Output;
import org.tensorflow.TensorFlow;
import org.tensorflow.internal.c_api.TFJ_GradFuncAdapter;

import java.util.List;

/**
* A custom gradient for ops of type {@link T}. Should be registered using {@link
* TensorFlow#registerCustomGradient(Class, CustomGradient)}.
Expand Down Expand Up @@ -56,7 +55,8 @@ public interface CustomGradient<T extends RawOpInputs> {
* <p>You should not be calling this yourself, use {@link TensorFlow#registerCustomGradient(Class,
* CustomGradient)}.
*/
static <T extends RawOpInputs<?>> TFJ_GradFuncAdapter adapter(CustomGradient<T> gradient, Class<T> opClass) {
static <T extends RawOpInputs<?>> TFJ_GradFuncAdapter adapter(
CustomGradient<T> gradient, Class<T> opClass) {
return new TypedGradientAdapter<T>(gradient, opClass);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import org.tensorflow.GraphOperation;
import org.tensorflow.Operation;
import org.tensorflow.OperationBuilder;
import org.tensorflow.internal.c_api.TF_Operation;
import org.tensorflow.internal.c_api.TFJ_Scope;
import org.tensorflow.internal.c_api.TF_Operation;

/** A {@link Scope} implementation backed by a native scope. */
public final class NativeScope implements Scope {
Expand Down Expand Up @@ -87,7 +87,8 @@ public void refreshNames() {}

@Override
public Scope withControlDependencies(Iterable<Op> controls) {
return withControlDependencyOps(StreamSupport.stream(controls.spliterator(), false)
return withControlDependencyOps(
StreamSupport.stream(controls.spliterator(), false)
.map(Op::op)
.collect(Collectors.toList()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ public interface RawCustomGradient {
static TFJ_GradFuncAdapter adapter(RawCustomGradient gradient) {
return new RawGradientAdapter(gradient);
}
}
}
Loading

0 comments on commit de1d6f0

Please sign in to comment.