Skip to content

Fix Scope name collisions #248

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

Merged
merged 11 commits into from
May 28, 2021
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
14 changes: 0 additions & 14 deletions license-header

This file was deleted.

18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,23 @@
<removeUnusedImports/>

<licenseHeader>
<file>./license-header</file>
<content>
/* Copyright $YEAR The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=======================================================================
*/
</content>
</licenseHeader>
</java>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ public final class Ops {

public final SparseOps sparse;

public final TpuOps tpu;

public final BitwiseOps bitwise;

public final TpuOps tpu;

public final MathOps math;

public final AudioOps audio;
Expand Down Expand Up @@ -385,8 +385,8 @@ private Ops(Scope scope) {
random = new RandomOps(this);
strings = new StringsOps(this);
sparse = new SparseOps(this);
tpu = new TpuOps(this);
bitwise = new BitwiseOps(this);
tpu = new TpuOps(this);
math = new MathOps(this);
audio = new AudioOps(this);
signal = new SignalOps(this);
Expand Down Expand Up @@ -7884,7 +7884,7 @@ public final Scope scope() {
* Creates an API for building operations in the provided execution environment
*/
public static Ops create(ExecutionEnvironment env) {
return new Ops(new Scope(env));
return new Ops(env.baseScope());
}

/**
Expand All @@ -7893,6 +7893,6 @@ public static Ops create(ExecutionEnvironment env) {
* <p>Invoking this method is equivalent to {@code Ops.create(EagerSession.getDefault())}.
*/
public static Ops create() {
return new Ops(new Scope(EagerSession.getDefault()));
return create(EagerSession.getDefault());
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2019-2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=======================================================================
*/
package org.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TFE_ContextOptionsSetAsync;
Expand All @@ -29,6 +29,7 @@
import org.tensorflow.internal.c_api.TFE_ContextOptions;
import org.tensorflow.internal.c_api.TF_Status;
import org.tensorflow.op.Op;
import org.tensorflow.op.Scope;
import org.tensorflow.op.core.Assign;
import org.tensorflow.op.core.Placeholder;
import org.tensorflow.op.core.Variable;
Expand Down Expand Up @@ -112,7 +113,8 @@ public Options devicePlacementPolicy(DevicePlacementPolicy value) {
* Configures the session based on the data found in the provided configuration.
*
* @param config a config protocol buffer
* @see <a href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto">config.proto</a></a>
* @see <a
* href="https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto">config.proto</a></a>
*/
public Options config(ConfigProto config) {
this.config = config;
Expand Down Expand Up @@ -306,6 +308,11 @@ public void checkInput(Op input) {
}
}

@Override
public Scope baseScope() {
return baseScope;
}

TFE_Context nativeHandle() {
checkSession();
return nativeHandle;
Expand All @@ -314,17 +321,16 @@ TFE_Context nativeHandle() {
/**
* Attach the list of native resources to this eager session scope.
*
* <p>When the eager session is closed (i.e. by calling {@link #close()} explicitly or
* implicitly via try-with-resources), all native resources attached to the session will be
* released as well, unless so other references are {@link Pointer#retainReference() retaining}
* them.</p>
* <p>When the eager session is closed (i.e. by calling {@link #close()} explicitly or implicitly
* via try-with-resources), all native resources attached to the session will be released as well,
* unless so other references are {@link Pointer#retainReference() retaining} them.
*
* <p>Attached resources can still be garbage collected though if their associated {@link Pointer}
* is no longer reachable in Java, independently of their reference count. Therefore, it is
* assumed that these resources are not required by the native library once the Java client no
* longer needs them.</p>
* longer needs them.
*
* <p>Attaching a resource already attached to this session will have no effect.</p>
* <p>Attaching a resource already attached to this session will have no effect.
*
* @param resources resources to attach to the session
*/
Expand All @@ -339,14 +345,14 @@ void attach(Pointer... resources) {
* Detach a list of resources from this eager session scope.
*
* <p>Detached native resources will prevent them to be automatically released when the session is
* closed.</p>
* closed.
*
* <p>Note though that this method will decrement the reference count of each resources being
* detached, which may automatically released them if that count reaches 0. Therefore,
* invoking {@link Pointer#retainReference()} prior to this call on any resource that must remain
* valid after being detached might be required.</p>
* detached, which may automatically released them if that count reaches 0. Therefore, invoking
* {@link Pointer#retainReference()} prior to this call on any resource that must remain valid
* after being detached might be required.
*
* <p>Detaching a resource that is not attached to this session will have no effect.</p>
* <p>Detaching a resource that is not attached to this session will have no effect.
*
* @param resources resources to detach from the session
*/
Expand All @@ -362,6 +368,8 @@ void detach(Pointer... resources) {
private final WeakPointerScope nativeResources;
private TFE_Context nativeHandle;

private final Scope baseScope = new Scope(this);

private EagerSession(Options options) {
this.nativeResources = new WeakPointerScope();
this.nativeHandle = allocate(options.async, options.devicePlacementPolicy.code, options.config);
Expand All @@ -381,7 +389,8 @@ private synchronized void doClose() {
}
}

private static TFE_Context allocate(boolean async, int devicePlacementPolicy, ConfigProto config) {
private static TFE_Context allocate(
boolean async, int devicePlacementPolicy, ConfigProto config) {
try (PointerScope scope = new PointerScope()) {
TFE_ContextOptions opts = TFE_ContextOptions.newContextOptions();
TF_Status status = TF_Status.newStatus();
Expand All @@ -390,7 +399,7 @@ private static TFE_Context allocate(boolean async, int devicePlacementPolicy, Co
TFE_ContextOptionsSetConfig(opts, configBytes, configBytes.capacity(), status);
status.throwExceptionIfNotOK();
}
TFE_ContextOptionsSetAsync(opts, (byte)(async ? 1 : 0));
TFE_ContextOptionsSetAsync(opts, (byte) (async ? 1 : 0));
TFE_ContextOptionsSetDevicePlacementPolicy(opts, devicePlacementPolicy);
TFE_Context context = TFE_NewContext(opts, status);
status.throwExceptionIfNotOK();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2019-2021 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=======================================================================
*/
package org.tensorflow;

import org.tensorflow.op.Op;
import org.tensorflow.op.Scope;

/**
* Defines an environment for creating and executing TensorFlow {@link Operation}s.
*/
/** Defines an environment for creating and executing TensorFlow {@link Operation}s. */
public interface ExecutionEnvironment {

enum Types {
Expand Down Expand Up @@ -49,11 +48,12 @@ default boolean isOpEnabled(String opType) {
}

/**
* Checks that {@code input} is valid to use as an input in this execution environment. Throws {@link
* IllegalArgumentException} if not.
* Checks that {@code input} is valid to use as an input in this execution environment. Throws
* {@link IllegalArgumentException} if not.
*
* @param input The op to check
* @throws IllegalArgumentException if input can't be used as an input in this execution environment.
* @throws IllegalArgumentException if input can't be used as an input in this execution
* environment.
*/
void checkInput(Op input);

Expand All @@ -71,4 +71,10 @@ default boolean isEager() {
default boolean isGraph() {
return environmentType() == Types.GRAPH;
}

/**
* Get the top level scope for this execution environment. Is cached, which is necessary to
* prevent name collisions.
*/
Scope baseScope();
}
Loading