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+ */
1616package org .tensorflow ;
1717
1818import static org .tensorflow .internal .c_api .global .tensorflow .TFE_ContextOptionsSetAsync ;
2929import org .tensorflow .internal .c_api .TFE_ContextOptions ;
3030import org .tensorflow .internal .c_api .TF_Status ;
3131import org .tensorflow .op .Op ;
32+ import org .tensorflow .op .Scope ;
3233import org .tensorflow .op .core .Assign ;
3334import org .tensorflow .op .core .Placeholder ;
3435import 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 ();
0 commit comments