Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Wrapped disposes in a finally to ensure they are called.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayres committed Jun 11, 2018
1 parent 471b7a9 commit 6e00d62
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,25 @@ 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) {
val ndSliced = nd.slice(0, realSize)
list += ndSliced.copy()
ndSliced.dispose()
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()
}
batch.dispose()
}
// TODO(Yizhi): we can use Symbol.concat to do the same thing. Can it be more efficient?
val results = outputs.map(NDArray.concatenate(_))
Expand Down

0 comments on commit 6e00d62

Please sign in to comment.