1
- /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
1
+ /* Copyright 2019-2021 The TensorFlow Authors. All Rights Reserved.
2
2
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
6
6
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
15
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
+ =======================================================================
15
+ */
16
16
package org .tensorflow ;
17
17
18
18
import static org .tensorflow .internal .c_api .global .tensorflow .TFE_ContextOptionsSetAsync ;
29
29
import org .tensorflow .internal .c_api .TFE_ContextOptions ;
30
30
import org .tensorflow .internal .c_api .TF_Status ;
31
31
import org .tensorflow .op .Op ;
32
+ import org .tensorflow .op .Scope ;
32
33
import org .tensorflow .op .core .Assign ;
33
34
import org .tensorflow .op .core .Placeholder ;
34
35
import org .tensorflow .op .core .Variable ;
@@ -112,7 +113,8 @@ public Options devicePlacementPolicy(DevicePlacementPolicy value) {
112
113
* Configures the session based on the data found in the provided configuration.
113
114
*
114
115
* @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>
116
118
*/
117
119
public Options config (ConfigProto config ) {
118
120
this .config = config ;
@@ -306,6 +308,11 @@ public void checkInput(Op input) {
306
308
}
307
309
}
308
310
311
+ @ Override
312
+ public Scope baseScope () {
313
+ return baseScope ;
314
+ }
315
+
309
316
TFE_Context nativeHandle () {
310
317
checkSession ();
311
318
return nativeHandle ;
@@ -314,17 +321,16 @@ TFE_Context nativeHandle() {
314
321
/**
315
322
* Attach the list of native resources to this eager session scope.
316
323
*
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.
321
327
*
322
328
* <p>Attached resources can still be garbage collected though if their associated {@link Pointer}
323
329
* is no longer reachable in Java, independently of their reference count. Therefore, it is
324
330
* 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.
326
332
*
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.
328
334
*
329
335
* @param resources resources to attach to the session
330
336
*/
@@ -339,14 +345,14 @@ void attach(Pointer... resources) {
339
345
* Detach a list of resources from this eager session scope.
340
346
*
341
347
* <p>Detached native resources will prevent them to be automatically released when the session is
342
- * closed.</p>
348
+ * closed.
343
349
*
344
350
* <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.
348
354
*
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.
350
356
*
351
357
* @param resources resources to detach from the session
352
358
*/
@@ -362,6 +368,8 @@ void detach(Pointer... resources) {
362
368
private final WeakPointerScope nativeResources ;
363
369
private TFE_Context nativeHandle ;
364
370
371
+ private final Scope baseScope = new Scope (this );
372
+
365
373
private EagerSession (Options options ) {
366
374
this .nativeResources = new WeakPointerScope ();
367
375
this .nativeHandle = allocate (options .async , options .devicePlacementPolicy .code , options .config );
@@ -381,7 +389,8 @@ private synchronized void doClose() {
381
389
}
382
390
}
383
391
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 ) {
385
394
try (PointerScope scope = new PointerScope ()) {
386
395
TFE_ContextOptions opts = TFE_ContextOptions .newContextOptions ();
387
396
TF_Status status = TF_Status .newStatus ();
@@ -390,7 +399,7 @@ private static TFE_Context allocate(boolean async, int devicePlacementPolicy, Co
390
399
TFE_ContextOptionsSetConfig (opts , configBytes , configBytes .capacity (), status );
391
400
status .throwExceptionIfNotOK ();
392
401
}
393
- TFE_ContextOptionsSetAsync (opts , (byte )(async ? 1 : 0 ));
402
+ TFE_ContextOptionsSetAsync (opts , (byte ) (async ? 1 : 0 ));
394
403
TFE_ContextOptionsSetDevicePlacementPolicy (opts , devicePlacementPolicy );
395
404
TFE_Context context = TFE_NewContext (opts , status );
396
405
status .throwExceptionIfNotOK ();
0 commit comments