Skip to content

Commit a3f5b7a

Browse files
saudetkarllessard
authored andcommitted
Remove no longer useful ResourceCleanupStrategy
1 parent 9c7833d commit a3f5b7a

2 files changed

Lines changed: 3 additions & 113 deletions

File tree

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

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

tensorflow-core/tensorflow-core-api/src/test/java/org/tensorflow/EagerSessionTest.java

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.junit.Test;
2727
import org.junit.runner.RunWith;
2828
import org.junit.runners.JUnit4;
29-
import org.tensorflow.EagerSession.ResourceCleanupStrategy;
3029

3130
@RunWith(JUnit4.class)
3231
public class EagerSessionTest {
@@ -41,10 +40,7 @@ public void closeSessionTwiceDoesNotFail() {
4140
@Test
4241
public void cleanupResourceOnSessionClose() {
4342
Pointer ref;
44-
try (EagerSession s =
45-
EagerSession.options()
46-
.resourceCleanupStrategy(ResourceCleanupStrategy.ON_SESSION_CLOSE)
47-
.build()) {
43+
try (EagerSession s = EagerSession.create()) {
4844
s.attach(ref = new IntPointer(1));
4945
assertFalse(ref.isNull());
5046

@@ -55,31 +51,9 @@ public void cleanupResourceOnSessionClose() {
5551
assertTrue(ref.isNull());
5652
}
5753

58-
@Test
59-
public void cleanupResourceOnSafePoints() {
60-
try (EagerSession s =
61-
EagerSession.options()
62-
.resourceCleanupStrategy(ResourceCleanupStrategy.ON_SAFE_POINTS)
63-
.build()) {
64-
65-
Pointer ref = new IntPointer(1);
66-
s.attach(ref);
67-
assertFalse(ref.isNull());
68-
69-
// garbage collecting the reference won't release until we reached safe point
70-
s.detach(ref);
71-
assertFalse(ref.isNull());
72-
buildOp(s); // safe point
73-
assertTrue(ref.isNull());
74-
}
75-
}
76-
7754
@Test
7855
public void cleanupResourceInBackground() {
79-
try (EagerSession s =
80-
EagerSession.options()
81-
.resourceCleanupStrategy(ResourceCleanupStrategy.IN_BACKGROUND)
82-
.build()) {
56+
try (EagerSession s = EagerSession.create()) {
8357

8458
Pointer ref = new IntPointer(1024 * 1024);
8559
s.attach(ref);
@@ -134,12 +108,10 @@ public void addingReferenceToClosedSessionFails() {
134108
@Test
135109
public void defaultSession() throws Exception {
136110
EagerSession.closeDefaultForTest();
137-
EagerSession.Options options =
138-
EagerSession.options().resourceCleanupStrategy(ResourceCleanupStrategy.ON_SESSION_CLOSE);
111+
EagerSession.Options options = EagerSession.options();
139112
EagerSession.initDefault(options);
140113
EagerSession session = EagerSession.getDefault();
141114
assertNotNull(session);
142-
assertEquals(ResourceCleanupStrategy.ON_SESSION_CLOSE, session.resourceCleanupStrategy());
143115
try {
144116
EagerSession.initDefault(options);
145117
fail();

0 commit comments

Comments
 (0)