nswamy closed pull request #11204: Scala inference memory leak fix
URL: https://github.com/apache/incubator-mxnet/pull/11204
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/scala-package/core/src/main/scala/org/apache/mxnet/FeedForward.scala
b/scala-package/core/src/main/scala/org/apache/mxnet/FeedForward.scala
index 7289df19712..87c9bc72be0 100644
--- a/scala-package/core/src/main/scala/org/apache/mxnet/FeedForward.scala
+++ b/scala-package/core/src/main/scala/org/apache/mxnet/FeedForward.scala
@@ -224,13 +224,24 @@ class FeedForward private(
var i = 0
while (data.hasNext && i != numBatch) {
val batch = data.next()
- i += 1
- ExecutorManager.loadData(batch, dataArrays)
- predExec.forward(isTrain = false)
- val padded = batch.pad
- val realSize = batchSize - padded
- for ((list, nd) <- outputs zip predExec.outputs) {
- list += nd.slice(0, realSize).copy()
+ try {
+ i += 1
+ ExecutorManager.loadData(batch, dataArrays)
+ predExec.forward(isTrain = false)
+ val padded = batch.pad
+ val realSize = batchSize - padded
+ for ((list, nd) <- outputs zip predExec.outputs) {
+ // The slice is being written to a value so that dispose can be
called after the copy.
+ // The one liner nd.slice().copy() leads to leaking the memory of
the slice.
+ val ndSliced = nd.slice(0, realSize)
+ try {
+ list += ndSliced.copy()
+ } finally {
+ ndSliced.dispose()
+ }
+ }
+ } finally {
+ batch.dispose()
}
}
// TODO(Yizhi): we can use Symbol.concat to do the same thing. Can it be
more efficient?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services