@@ -71,57 +71,6 @@ public enum DevicePlacementPolicy {
7171 private final int code ;
7272 }
7373
74- /**
75- * Controls how TensorFlow resources are cleaned up when they are no longer needed.
76- *
77- * <p>All resources allocated during an {@code EagerSession} are deleted when the session is
78- * closed. To prevent out-of-memory errors, it is also strongly suggest to cleanup those resources
79- * during the session. For example, executing n operations in a loop of m iterations will allocate
80- * a minimum of n*m resources while in most cases, only resources of the last iteration are still
81- * being used.
82- *
83- * <p>{@code EagerSession} instances can be notified in different ways when TensorFlow objects are
84- * no longer being referred, so they can proceed to the cleanup of any resources they owned.
85- */
86- public enum ResourceCleanupStrategy {
87-
88- /**
89- * Monitor and delete unused resources from a new thread running in background.
90- *
91- * <p>This is the most reliable approach to cleanup TensorFlow resources, at the cost of
92- * starting and running an additional thread dedicated to this task. Each {@code EagerSession}
93- * instance has its own thread, which is stopped only when the session is closed.
94- *
95- * <p>This strategy is used by default.
96- */
97- IN_BACKGROUND ,
98-
99- /**
100- * Monitor and delete unused resources from existing threads, before or after they complete
101- * another task.
102- *
103- * <p>Unused resources are released when a call to the TensorFlow library reaches a safe point
104- * for cleanup. This is done synchronously and might block for a short period of time the thread
105- * who triggered that call.
106- *
107- * <p>This strategy should be used only if, for some reasons, no additional thread should be
108- * allocated for cleanup. Otherwise, {@link #IN_BACKGROUND} should be preferred.
109- */
110- ON_SAFE_POINTS ,
111-
112- /**
113- * Only delete resources when the session is closed.
114- *
115- * <p>All resources allocated during the session will remained in memory until the session is
116- * explicitly closed (or via the traditional `try-with-resource` technique). No extra task for
117- * resource cleanup will be attempted.
118- *
119- * <p>This strategy can lead up to out-of-memory errors and its usage is not recommended, unless
120- * the scope of the session is limited to execute only a small amount of operations.
121- */
122- ON_SESSION_CLOSE ,
123- }
124-
12574 public static class Options {
12675
12776 /**
@@ -154,19 +103,6 @@ public Options devicePlacementPolicy(DevicePlacementPolicy value) {
154103 return this ;
155104 }
156105
157- /**
158- * Controls how TensorFlow resources are cleaned up when no longer needed.
159- *
160- * <p>{@link ResourceCleanupStrategy#IN_BACKGROUND} is used by default.
161- *
162- * @param value strategy to use
163- * @see ResourceCleanupStrategy
164- */
165- public Options resourceCleanupStrategy (ResourceCleanupStrategy value ) {
166- resourceCleanupStrategy = value ;
167- return this ;
168- }
169-
170106 /**
171107 * Configures the session based on the data found in the provided configuration.
172108 *
@@ -186,13 +122,11 @@ public EagerSession build() {
186122
187123 private boolean async ;
188124 private DevicePlacementPolicy devicePlacementPolicy ;
189- private ResourceCleanupStrategy resourceCleanupStrategy ;
190125 private ConfigProto config ;
191126
192127 private Options () {
193128 async = false ;
194129 devicePlacementPolicy = DevicePlacementPolicy .SILENT ;
195- resourceCleanupStrategy = ResourceCleanupStrategy .IN_BACKGROUND ;
196130 config = null ;
197131 }
198132 }
@@ -337,9 +271,6 @@ static void closeDefaultForTest() {
337271
338272 @ Override
339273 public OperationBuilder opBuilder (String type , String name ) {
340- if (resourceCleanupStrategy == ResourceCleanupStrategy .ON_SAFE_POINTS ) {
341- nativeRefs .close ();
342- }
343274 checkSession ();
344275 return new EagerOperationBuilder (this , type , name );
345276 }
@@ -349,10 +280,6 @@ TFE_Context nativeHandle() {
349280 return nativeHandle ;
350281 }
351282
352- ResourceCleanupStrategy resourceCleanupStrategy () {
353- return resourceCleanupStrategy ;
354- }
355-
356283 void attach (Pointer ... resources ) {
357284 checkSession ();
358285 for (Pointer r : resources ) {
@@ -363,27 +290,19 @@ void attach(Pointer... resources) {
363290 void detach (Pointer ... resources ) {
364291 checkSession ();
365292 for (Pointer r : resources ) {
366- if (resourceCleanupStrategy == ResourceCleanupStrategy .ON_SAFE_POINTS ) {
367- nativeRefs .attach (r );
368- }
369293 nativeResources .detach (r );
370294 }
371295 }
372296
373297 private static volatile EagerSession defaultSession = null ;
374298
375- private final PointerScope nativeRefs ;
376299 private final PointerScope nativeResources ;
377- private final ResourceCleanupStrategy resourceCleanupStrategy ;
378300 private TFE_Context nativeHandle ;
379301
380302 private EagerSession (Options options ) {
381- this .nativeRefs = new PointerScope ();
382303 this .nativeResources = new PointerScope ();
383304 this .nativeHandle = allocate (options .async , options .devicePlacementPolicy .code , options .config );
384- this .resourceCleanupStrategy = options .resourceCleanupStrategy ;
385305
386- nativeRefs .close (); // remove from stack
387306 nativeResources .close (); // remove from stack
388307 }
389308
@@ -395,7 +314,6 @@ private void checkSession() {
395314
396315 private synchronized void doClose () {
397316 if (nativeHandle != null && !nativeHandle .isNull ()) {
398- nativeRefs .close ();
399317 nativeResources .close ();
400318 delete (nativeHandle );
401319 nativeHandle = null ;
0 commit comments