Skip to content

Commit 2f22b57

Browse files
committed
Small-ish review changes
Mostly comments and renaming Signed-off-by: Ryan Nett <[email protected]>
1 parent dfaac8d commit 2f22b57

File tree

17 files changed

+99
-97
lines changed

17 files changed

+99
-97
lines changed

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/EagerSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import org.tensorflow.internal.c_api.TFE_Context;
3030
import org.tensorflow.internal.c_api.TFE_ContextOptions;
3131
import org.tensorflow.internal.c_api.TF_Status;
32-
import org.tensorflow.op.JavaScope;
32+
import org.tensorflow.op.OpScope;
3333
import org.tensorflow.op.Scope;
3434
import org.tensorflow.op.core.Assign;
3535
import org.tensorflow.op.core.Placeholder;
@@ -398,7 +398,7 @@ void detach(Pointer... resources) {
398398
private final WeakPointerScope nativeResources;
399399
private TFE_Context nativeHandle;
400400

401-
private final Scope baseScope = new JavaScope(this);
401+
private final Scope baseScope = new OpScope(this);
402402

403403
private EagerSession(Options options) {
404404
this.nativeResources = new WeakPointerScope();

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/GradientAdapterHelpers.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@
2323
import org.tensorflow.internal.c_api.NativeOutputVector;
2424
import org.tensorflow.internal.c_api.Node;
2525
import org.tensorflow.internal.c_api.TF_Operation;
26+
import org.tensorflow.op.RawGradientAdapter;
27+
import org.tensorflow.op.TypedGradientAdapter;
2628

27-
/**
28-
* Helpers for {@link org.tensorflow.op.TypedGradientAdapter} and {@link
29-
* org.tensorflow.op.RawGradientAdapter}.
30-
*/
29+
/** Helpers for {@link TypedGradientAdapter} and {@link RawGradientAdapter}. */
3130
public class GradientAdapterHelpers {
3231

3332
/**
34-
* Convert a array of native outputs to a list of {@link Output}s.
33+
* Convert an array of native outputs to a list of {@link Output}s.
3534
*
3635
* @param g the graph the outputs are in
3736
* @param nativeOutputs the native outputs to convert
37+
* @return a list of Outputs
3838
*/
3939
public static List<Output<?>> fromNativeOutputs(Graph g, NativeOutputVector nativeOutputs) {
4040
List<Output<?>> gradInputs = new ArrayList<>((int) nativeOutputs.size());

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/Graph.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
import org.tensorflow.internal.c_api.TF_Status;
5858
import org.tensorflow.internal.c_api.TF_WhileParams;
5959
import org.tensorflow.ndarray.StdArrays;
60-
import org.tensorflow.op.JavaScope;
6160
import org.tensorflow.op.Op;
61+
import org.tensorflow.op.OpScope;
6262
import org.tensorflow.op.Ops;
6363
import org.tensorflow.op.Scope;
6464
import org.tensorflow.op.core.Constant;
@@ -90,7 +90,7 @@ public Graph() {
9090
/** Create a Graph from an existing handle (takes ownership). */
9191
Graph(TF_Graph nativeHandle) {
9292
this.nativeHandle = nativeHandle;
93-
this.baseScope = new JavaScope(this);
93+
this.baseScope = new OpScope(this);
9494
allGraphs.add(this);
9595
}
9696

@@ -375,6 +375,7 @@ public synchronized Set<GraphOperation> subgraphFrom(Set<Operand<?>> inputs) {
375375
*
376376
* @param type of the Operation (i.e., identifies the computation to be performed)
377377
* @param name to refer to the created Operation in the graph.
378+
* @param scope the scope to use for the operation
378379
* @return an {@link OperationBuilder}, which will add the Operation to the graph when {@link
379380
* OperationBuilder#build()} is invoked. If {@link OperationBuilder#build()} is not invoked,
380381
* then some resources may leak.
@@ -1309,7 +1310,8 @@ private static SaverDef addVariableSaver(Graph graph) {
13091310
.build();
13101311
}
13111312

1312-
private static Set<Graph> allGraphs = Collections.newSetFromMap(new WeakHashMap<>());
1313+
private static Set<Graph> allGraphs =
1314+
Collections.synchronizedSet(Collections.newSetFromMap(new WeakHashMap<>()));
13131315

13141316
/**
13151317
* Find the graph with the matching underlying native pointer.

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/NativeScope.java renamed to tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/GradientScope.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,31 @@
2929
import org.tensorflow.internal.c_api.TF_Scope;
3030

3131
/** A {@link Scope} implementation backed by a native scope. Only used for gradient declarations. */
32-
public final class NativeScope implements Scope {
32+
public final class GradientScope implements Scope {
3333

3434
@Override
3535
public ExecutionEnvironment env() {
3636
return graph;
3737
}
3838

3939
@Override
40-
public NativeScope withSubScope(String childScopeName) {
41-
return new NativeScope(nativeScope.NewSubScope(childScopeName), graph, childScopeName);
40+
public GradientScope withSubScope(String childScopeName) {
41+
return new GradientScope(nativeScope.NewSubScope(childScopeName), graph, childScopeName);
4242
}
4343

4444
@Override
45-
public NativeScope withName(String opName) {
46-
return new NativeScope(nativeScope, graph, opName);
45+
public GradientScope withName(String opName) {
46+
return new GradientScope(nativeScope, graph, opName);
4747
}
4848

4949
@Override
50-
public NativeScope withNameAsSubScope(String defaultName) {
50+
public GradientScope withNameAsSubScope(String defaultName) {
5151
return withSubScope(opName);
5252
}
5353

5454
@Override
55-
public NativeScope withDevice(DeviceSpec deviceSpec) {
56-
return new NativeScope(nativeScope.WithDevice(deviceSpec.toString()), graph);
55+
public GradientScope withDevice(DeviceSpec deviceSpec) {
56+
return new GradientScope(nativeScope.WithDevice(deviceSpec.toString()), graph);
5757
}
5858

5959
@Override
@@ -76,7 +76,7 @@ public String makeUnique(String id) {
7676
public void refreshNames() {}
7777

7878
@Override
79-
public NativeScope withControlDependencies(Iterable<Op> controls) {
79+
public GradientScope withControlDependencies(Iterable<Op> controls) {
8080
List<Op> controlDeps =
8181
StreamSupport.stream(controls.spliterator(), false).collect(Collectors.toList());
8282
NativeOperation ops = new NativeOperation(controlDeps.size());
@@ -90,7 +90,7 @@ public NativeScope withControlDependencies(Iterable<Op> controls) {
9090
.put(new NativeOperation(((GraphOperation) op).getUnsafeNativeHandle().node()));
9191
}
9292

93-
return new NativeScope(nativeScope.WithControlDependencies(new NativeOperation(ops)), graph);
93+
return new GradientScope(nativeScope.WithControlDependencies(new NativeOperation(ops)), graph);
9494
}
9595

9696
@Override
@@ -108,7 +108,7 @@ public Scope withControlDependencyOps(Iterable<Operation> controls) {
108108
.put(new NativeOperation(((GraphOperation) op).getUnsafeNativeHandle().node()));
109109
}
110110

111-
return new NativeScope(nativeScope.WithControlDependencies(new NativeOperation(ops)), graph);
111+
return new GradientScope(nativeScope.WithControlDependencies(new NativeOperation(ops)), graph);
112112
}
113113

114114
@Override
@@ -129,11 +129,11 @@ public boolean isInit() {
129129
return false;
130130
}
131131

132-
NativeScope(TF_Scope nativeScope, Graph graph) {
132+
GradientScope(TF_Scope nativeScope, Graph graph) {
133133
this(nativeScope, graph, null);
134134
}
135135

136-
private NativeScope(TF_Scope nativeScope, Graph graph, String opName) {
136+
private GradientScope(TF_Scope nativeScope, Graph graph, String opName) {
137137
this.graph = graph;
138138
this.nativeScope = nativeScope;
139139
this.opName = opName;

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/JavaScope.java renamed to tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/OpScope.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@
2626
* A Java implementation of {@link Scope}. This is used in all cases except custom gradient
2727
* definitions.
2828
*/
29-
public final class JavaScope implements Scope {
29+
public final class OpScope implements Scope {
3030

3131
/**
3232
* Create a new top-level scope.
3333
*
3434
* @param env The execution environment used by the scope.
3535
*/
36-
public JavaScope(ExecutionEnvironment env) {
36+
public OpScope(ExecutionEnvironment env) {
3737
this(env, new NameScope(env), new ArrayList<>(), DeviceSpec.newBuilder().build(), false);
3838
}
3939

@@ -43,19 +43,19 @@ public ExecutionEnvironment env() {
4343
}
4444

4545
@Override
46-
public JavaScope withSubScope(String childScopeName) {
47-
return new JavaScope(
46+
public OpScope withSubScope(String childScopeName) {
47+
return new OpScope(
4848
env, nameScope.withSubScope(childScopeName, env), controlDependencies, deviceSpec, isInit);
4949
}
5050

5151
@Override
52-
public JavaScope withName(String opName) {
53-
return new JavaScope(env, nameScope.withName(opName), controlDependencies, deviceSpec, isInit);
52+
public OpScope withName(String opName) {
53+
return new OpScope(env, nameScope.withName(opName), controlDependencies, deviceSpec, isInit);
5454
}
5555

5656
@Override
57-
public JavaScope withNameAsSubScope(String defaultName) {
58-
return new JavaScope(
57+
public OpScope withNameAsSubScope(String defaultName) {
58+
return new OpScope(
5959
env,
6060
nameScope.withSubScope(nameScope.makeOpName(defaultName), env),
6161
controlDependencies,
@@ -64,13 +64,13 @@ public JavaScope withNameAsSubScope(String defaultName) {
6464
}
6565

6666
@Override
67-
public JavaScope withDevice(DeviceSpec deviceSpec) {
68-
return new JavaScope(env, nameScope, controlDependencies, deviceSpec, isInit);
67+
public OpScope withDevice(DeviceSpec deviceSpec) {
68+
return new OpScope(env, nameScope, controlDependencies, deviceSpec, isInit);
6969
}
7070

7171
@Override
72-
public JavaScope withInitScope() {
73-
return new JavaScope(env.initEnv(), nameScope, new ArrayList<>(), deviceSpec, true);
72+
public OpScope withInitScope() {
73+
return new OpScope(env.initEnv(), nameScope, new ArrayList<>(), deviceSpec, true);
7474
}
7575

7676
@Override
@@ -88,7 +88,7 @@ public void refreshNames() {
8888
nameScope.importIdsFrom(env);
8989
}
9090

91-
private JavaScope(
91+
private OpScope(
9292
ExecutionEnvironment env,
9393
NameScope nameScope,
9494
List<Operation> controlDependencies,
@@ -114,7 +114,7 @@ public Scope withControlDependencyOps(Iterable<Operation> controls) {
114114
}
115115
}
116116

117-
return new JavaScope(env, nameScope, toAdd, deviceSpec, isInit);
117+
return new OpScope(env, nameScope, toAdd, deviceSpec, isInit);
118118
}
119119

120120
@Override

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/RawGradientAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public NativeStatus call(
5454
throw new IllegalStateException("No graph found for native gradient scope.");
5555
}
5656

57-
Scope nativeScope = new NativeScope(scope, g);
57+
Scope nativeScope = new GradientScope(scope, g);
5858
Ops tf = new Ops(nativeScope);
5959

6060
List<Output<?>> gradInputs = GradientAdapterHelpers.fromNativeOutputs(g, grad_inputs);

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/op/TypedGradientAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public NativeStatus call(
6161
throw new IllegalStateException("No graph found for native gradient scope.");
6262
}
6363

64-
Scope nativeScope = new NativeScope(scope, g);
64+
Scope nativeScope = new GradientScope(scope, g);
6565

6666
Ops tf = new Ops(nativeScope);
6767

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/op/ScopeTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void testSeparateOps() {
5050
@Test
5151
public void basicNames() {
5252
try (Graph g = new Graph()) {
53-
Scope root = new JavaScope(g);
53+
Scope root = new OpScope(g);
5454
assertEquals("add", root.makeOpName("add"));
5555
assertEquals("add_1", root.makeOpName("add"));
5656
assertEquals("add_2", root.makeOpName("add"));
@@ -61,7 +61,7 @@ public void basicNames() {
6161
@Test
6262
public void hierarchicalNames() {
6363
try (Graph g = new Graph()) {
64-
Scope root = new JavaScope(g);
64+
Scope root = new OpScope(g);
6565
Scope child = root.withSubScope("child");
6666
assertEquals("child/add", child.makeOpName("add"));
6767
assertEquals("child/add_1", child.makeOpName("add"));
@@ -87,7 +87,7 @@ public void hierarchicalNames() {
8787
@Test
8888
public void scopeAndOpNames() {
8989
try (Graph g = new Graph()) {
90-
Scope root = new JavaScope(g);
90+
Scope root = new OpScope(g);
9191

9292
Scope child = root.withSubScope("child");
9393

@@ -100,7 +100,7 @@ public void scopeAndOpNames() {
100100
@Test
101101
public void validateNames() {
102102
try (Graph g = new Graph()) {
103-
Scope root = new JavaScope(g);
103+
Scope root = new OpScope(g);
104104

105105
final String[] invalid_names = {
106106
"_", "-", "-x", // Names are constrained to start with [A-Za-z0-9.]
@@ -137,7 +137,7 @@ public void validateNames() {
137137
@Test
138138
public void basic() {
139139
try (Graph g = new Graph()) {
140-
Scope s = new JavaScope(g);
140+
Scope s = new OpScope(g);
141141
Const<TInt32> c1 = Const.create(s, 42);
142142
assertEquals("Const", c1.output().op().name());
143143
Const<TInt32> c2 = Const.create(s, 7);
@@ -152,7 +152,7 @@ public void basic() {
152152
@Test
153153
public void hierarchy() {
154154
try (Graph g = new Graph()) {
155-
Scope root = new JavaScope(g);
155+
Scope root = new OpScope(g);
156156
Scope child = root.withSubScope("child");
157157
assertEquals("child/Const", Const.create(child, 42).output().op().name());
158158
assertEquals("child/four", Const.create(child.withName("four"), 4).output().op().name());
@@ -163,7 +163,7 @@ public void hierarchy() {
163163
public void composite() {
164164
try (Graph g = new Graph();
165165
Session sess = new Session(g)) {
166-
Scope s = new JavaScope(g);
166+
Scope s = new OpScope(g);
167167
Output<TInt32> data =
168168
Const.create(s.withName("data"), new int[] {600, 470, 170, 430, 300}).output();
169169

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/op/core/BooleanMaskTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.tensorflow.Operand;
2424
import org.tensorflow.Session;
2525
import org.tensorflow.ndarray.Shape;
26-
import org.tensorflow.op.JavaScope;
26+
import org.tensorflow.op.OpScope;
2727
import org.tensorflow.op.Scope;
2828
import org.tensorflow.types.TBool;
2929
import org.tensorflow.types.TInt32;
@@ -34,7 +34,7 @@ public class BooleanMaskTest {
3434
public void testBooleanMask() {
3535
try (Graph g = new Graph();
3636
Session sess = new Session(g)) {
37-
Scope scope = new JavaScope(g);
37+
Scope scope = new OpScope(g);
3838

3939
Operand<TInt32> input = Constant.arrayOf(scope, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
4040
Operand<TInt32> input2 = ExpandDims.create(scope, input, Constant.scalarOf(scope, 0));

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/op/core/BooleanMaskUpdateTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.tensorflow.Session;
2626
import org.tensorflow.Tensor;
2727
import org.tensorflow.ndarray.Shape;
28-
import org.tensorflow.op.JavaScope;
28+
import org.tensorflow.op.OpScope;
2929
import org.tensorflow.op.Scope;
3030
import org.tensorflow.types.TBool;
3131
import org.tensorflow.types.TInt32;
@@ -36,7 +36,7 @@ public class BooleanMaskUpdateTest {
3636
public void testBooleanMaskUpdateSlice() {
3737
try (Graph g = new Graph();
3838
Session sess = new Session(g)) {
39-
Scope scope = new JavaScope(g);
39+
Scope scope = new OpScope(g);
4040

4141
Operand<TInt32> input =
4242
Constant.tensorOf(scope, new int[][] {{0, 0, 0}, {1, 1, 1}, {2, 2, 2}});
@@ -75,7 +75,7 @@ public void testBooleanMaskUpdateSlice() {
7575
public void testBooleanMaskUpdateSliceWithBroadcast() {
7676
try (Graph g = new Graph();
7777
Session sess = new Session(g)) {
78-
Scope scope = new JavaScope(g);
78+
Scope scope = new OpScope(g);
7979

8080
Operand<TInt32> input =
8181
Constant.tensorOf(scope, new int[][] {{0, 0, 0}, {1, 1, 1}, {2, 2, 2}});
@@ -114,7 +114,7 @@ public void testBooleanMaskUpdateSliceWithBroadcast() {
114114
public void testBooleanMaskUpdateAxis() {
115115
try (Graph g = new Graph();
116116
Session sess = new Session(g)) {
117-
Scope scope = new JavaScope(g);
117+
Scope scope = new OpScope(g);
118118

119119
Operand<TInt32> input =
120120
Constant.tensorOf(scope, new int[][][] {{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}});

0 commit comments

Comments
 (0)