Skip to content

Commit 3b4533c

Browse files
authored
Fix Scope name collisions (#248)
1 parent ace917b commit 3b4533c

File tree

10 files changed

+537
-355
lines changed

10 files changed

+537
-355
lines changed

license-header

-14
This file was deleted.

pom.xml

+17-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,23 @@
342342
<removeUnusedImports/>
343343

344344
<licenseHeader>
345-
<file>./license-header</file>
345+
<content>
346+
/* Copyright $YEAR The TensorFlow Authors. All Rights Reserved.
347+
348+
Licensed under the Apache License, Version 2.0 (the "License");
349+
you may not use this file except in compliance with the License.
350+
You may obtain a copy of the License at
351+
352+
http://www.apache.org/licenses/LICENSE-2.0
353+
354+
Unless required by applicable law or agreed to in writing, software
355+
distributed under the License is distributed on an "AS IS" BASIS,
356+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
357+
See the License for the specific language governing permissions and
358+
limitations under the License.
359+
=======================================================================
360+
*/
361+
</content>
346362
</licenseHeader>
347363
</java>
348364
</configuration>

tensorflow-core/tensorflow-core-api/src/gen/annotations/org/tensorflow/op/Ops.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -354,10 +354,10 @@ public final class Ops {
354354

355355
public final SparseOps sparse;
356356

357-
public final TpuOps tpu;
358-
359357
public final BitwiseOps bitwise;
360358

359+
public final TpuOps tpu;
360+
361361
public final MathOps math;
362362

363363
public final AudioOps audio;
@@ -385,8 +385,8 @@ private Ops(Scope scope) {
385385
random = new RandomOps(this);
386386
strings = new StringsOps(this);
387387
sparse = new SparseOps(this);
388-
tpu = new TpuOps(this);
389388
bitwise = new BitwiseOps(this);
389+
tpu = new TpuOps(this);
390390
math = new MathOps(this);
391391
audio = new AudioOps(this);
392392
signal = new SignalOps(this);
@@ -7884,7 +7884,7 @@ public final Scope scope() {
78847884
* Creates an API for building operations in the provided execution environment
78857885
*/
78867886
public static Ops create(ExecutionEnvironment env) {
7887-
return new Ops(new Scope(env));
7887+
return new Ops(env.baseScope());
78887888
}
78897889

78907890
/**
@@ -7893,6 +7893,6 @@ public static Ops create(ExecutionEnvironment env) {
78937893
* <p>Invoking this method is equivalent to {@code Ops.create(EagerSession.getDefault())}.
78947894
*/
78957895
public static Ops create() {
7896-
return new Ops(new Scope(EagerSession.getDefault()));
7896+
return create(EagerSession.getDefault());
78977897
}
78987898
}

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

+35-26
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2019-2021 The TensorFlow Authors. All Rights Reserved.
22
3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
You may obtain a copy of the License at
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
66
7-
http://www.apache.org/licenses/LICENSE-2.0
8-
9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License.
14-
==============================================================================*/
7+
http://www.apache.org/licenses/LICENSE-2.0
158
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
=======================================================================
15+
*/
1616
package org.tensorflow;
1717

1818
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_ContextOptionsSetAsync;
@@ -29,6 +29,7 @@
2929
import org.tensorflow.internal.c_api.TFE_ContextOptions;
3030
import org.tensorflow.internal.c_api.TF_Status;
3131
import org.tensorflow.op.Op;
32+
import org.tensorflow.op.Scope;
3233
import org.tensorflow.op.core.Assign;
3334
import org.tensorflow.op.core.Placeholder;
3435
import org.tensorflow.op.core.Variable;
@@ -112,7 +113,8 @@ public Options devicePlacementPolicy(DevicePlacementPolicy value) {
112113
* Configures the session based on the data found in the provided configuration.
113114
*
114115
* @param config a config protocol buffer
115-
* @see <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto">config.proto</a></a>
116+
* @see <a
117+
* href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto">config.proto</a></a>
116118
*/
117119
public Options config(ConfigProto config) {
118120
this.config = config;
@@ -306,6 +308,11 @@ public void checkInput(Op input) {
306308
}
307309
}
308310

311+
@Override
312+
public Scope baseScope() {
313+
return baseScope;
314+
}
315+
309316
TFE_Context nativeHandle() {
310317
checkSession();
311318
return nativeHandle;
@@ -314,17 +321,16 @@ TFE_Context nativeHandle() {
314321
/**
315322
* Attach the list of native resources to this eager session scope.
316323
*
317-
* <p>When the eager session is closed (i.e. by calling {@link #close()} explicitly or
318-
* implicitly via try-with-resources), all native resources attached to the session will be
319-
* released as well, unless so other references are {@link Pointer#retainReference() retaining}
320-
* them.</p>
324+
* <p>When the eager session is closed (i.e. by calling {@link #close()} explicitly or implicitly
325+
* via try-with-resources), all native resources attached to the session will be released as well,
326+
* unless so other references are {@link Pointer#retainReference() retaining} them.
321327
*
322328
* <p>Attached resources can still be garbage collected though if their associated {@link Pointer}
323329
* is no longer reachable in Java, independently of their reference count. Therefore, it is
324330
* assumed that these resources are not required by the native library once the Java client no
325-
* longer needs them.</p>
331+
* longer needs them.
326332
*
327-
* <p>Attaching a resource already attached to this session will have no effect.</p>
333+
* <p>Attaching a resource already attached to this session will have no effect.
328334
*
329335
* @param resources resources to attach to the session
330336
*/
@@ -339,14 +345,14 @@ void attach(Pointer... resources) {
339345
* Detach a list of resources from this eager session scope.
340346
*
341347
* <p>Detached native resources will prevent them to be automatically released when the session is
342-
* closed.</p>
348+
* closed.
343349
*
344350
* <p>Note though that this method will decrement the reference count of each resources being
345-
* detached, which may automatically released them if that count reaches 0. Therefore,
346-
* invoking {@link Pointer#retainReference()} prior to this call on any resource that must remain
347-
* valid after being detached might be required.</p>
351+
* detached, which may automatically released them if that count reaches 0. Therefore, invoking
352+
* {@link Pointer#retainReference()} prior to this call on any resource that must remain valid
353+
* after being detached might be required.
348354
*
349-
* <p>Detaching a resource that is not attached to this session will have no effect.</p>
355+
* <p>Detaching a resource that is not attached to this session will have no effect.
350356
*
351357
* @param resources resources to detach from the session
352358
*/
@@ -362,6 +368,8 @@ void detach(Pointer... resources) {
362368
private final WeakPointerScope nativeResources;
363369
private TFE_Context nativeHandle;
364370

371+
private final Scope baseScope = new Scope(this);
372+
365373
private EagerSession(Options options) {
366374
this.nativeResources = new WeakPointerScope();
367375
this.nativeHandle = allocate(options.async, options.devicePlacementPolicy.code, options.config);
@@ -381,7 +389,8 @@ private synchronized void doClose() {
381389
}
382390
}
383391

384-
private static TFE_Context allocate(boolean async, int devicePlacementPolicy, ConfigProto config) {
392+
private static TFE_Context allocate(
393+
boolean async, int devicePlacementPolicy, ConfigProto config) {
385394
try (PointerScope scope = new PointerScope()) {
386395
TFE_ContextOptions opts = TFE_ContextOptions.newContextOptions();
387396
TF_Status status = TF_Status.newStatus();
@@ -390,7 +399,7 @@ private static TFE_Context allocate(boolean async, int devicePlacementPolicy, Co
390399
TFE_ContextOptionsSetConfig(opts, configBytes, configBytes.capacity(), status);
391400
status.throwExceptionIfNotOK();
392401
}
393-
TFE_ContextOptionsSetAsync(opts, (byte)(async ? 1 : 0));
402+
TFE_ContextOptionsSetAsync(opts, (byte) (async ? 1 : 0));
394403
TFE_ContextOptionsSetDevicePlacementPolicy(opts, devicePlacementPolicy);
395404
TFE_Context context = TFE_NewContext(opts, status);
396405
status.throwExceptionIfNotOK();

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

+24-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
1+
/* Copyright 2019-2021 The TensorFlow Authors. All Rights Reserved.
22
3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
You may obtain a copy of the License at
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
66
7-
http://www.apache.org/licenses/LICENSE-2.0
8-
9-
Unless required by applicable law or agreed to in writing, software
10-
distributed under the License is distributed on an "AS IS" BASIS,
11-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
See the License for the specific language governing permissions and
13-
limitations under the License.
14-
==============================================================================*/
7+
http://www.apache.org/licenses/LICENSE-2.0
158
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
=======================================================================
15+
*/
1616
package org.tensorflow;
1717

1818
import org.tensorflow.op.Op;
19+
import org.tensorflow.op.Scope;
1920

20-
/**
21-
* Defines an environment for creating and executing TensorFlow {@link Operation}s.
22-
*/
21+
/** Defines an environment for creating and executing TensorFlow {@link Operation}s. */
2322
public interface ExecutionEnvironment {
2423

2524
enum Types {
@@ -49,11 +48,12 @@ default boolean isOpEnabled(String opType) {
4948
}
5049

5150
/**
52-
* Checks that {@code input} is valid to use as an input in this execution environment. Throws {@link
53-
* IllegalArgumentException} if not.
51+
* Checks that {@code input} is valid to use as an input in this execution environment. Throws
52+
* {@link IllegalArgumentException} if not.
5453
*
5554
* @param input The op to check
56-
* @throws IllegalArgumentException if input can't be used as an input in this execution environment.
55+
* @throws IllegalArgumentException if input can't be used as an input in this execution
56+
* environment.
5757
*/
5858
void checkInput(Op input);
5959

@@ -71,4 +71,10 @@ default boolean isEager() {
7171
default boolean isGraph() {
7272
return environmentType() == Types.GRAPH;
7373
}
74+
75+
/**
76+
* Get the top level scope for this execution environment. Is cached, which is necessary to
77+
* prevent name collisions.
78+
*/
79+
Scope baseScope();
7480
}

0 commit comments

Comments
 (0)