This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
NativeResource Management in Scala #12647
Merged
+778
−124
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
db87a2b
add Generic MXNetHandle trait and MXNetHandlePhantomRef class that wi…
nswamy cba8a43
use nswamy@ personal repo for mac testing
nswamy 34106a4
Generic Handle with AutoCloseable
nswamy 373ac78
add NativeResource and NativeResourceManager with Periodic GC calling
nswamy e0016d7
use NativeResource trait in NDArray, Symbol and Executor
nswamy 5cd3cd3
add run train mnist script
nswamy ef4bfe8
create a Generic ResourceScope that can collect all NativeResources t…
nswamy c04e4f0
modify NativeResource and ResourceScope, extend NativeResource in NDA…
nswamy bb36934
remove GCExecutor
nswamy 9d92dc1
deRegister PhantomReferences by when calling dispose()
nswamy 1465717
add Finalizer(temporary) to NativeResource
nswamy e9b4b70
refactor NativeResource.dispose() method
nswamy dd294f0
update NativeResource/add Unit Test for NativeResource
nswamy 980db5a
updates to NativeResource/NativeResourceRef and unit tests to NativeR…
nswamy 2b0b073
remove redundant code added because of the object equality that was n…
nswamy 18b1175
add ResourceScope
nswamy e2d5c99
Fix NativeResource to not remove from Scope, add Unit Tests to Resour…
nswamy 21140cf
cleanup log/print debug statements
nswamy 362ae18
use TreeSet inplace of ArrayBuffer to speedup removal of resources fr…
nswamy d78f571
fix segfault that was happening because of NDArray creation on the fl…
nswamy f0e873b
Add comments for dispose(param:Boolean)
nswamy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -562,16 +562,20 @@ object NDArray extends NDArrayBase { | |
*/ | ||
class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle, | ||
val writable: Boolean = true, | ||
addToCollector: Boolean = true) extends WarnIfNotDisposed { | ||
addToCollector: Boolean = true) extends NativeResource { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why extend this? is that possible to make it composite (such as create a NativeResource Object in NDArray)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the right patten. NDArray |
||
if (addToCollector) { | ||
NDArrayCollector.collect(this) | ||
} | ||
|
||
override def nativeAddress: CPtrAddress = handle | ||
override def nativeDeAllocator: (CPtrAddress => Int) = _LIB.mxNDArrayFree | ||
override val bytesAllocated: Long = DType.numOfBytes(this.dtype) * this.shape.product | ||
|
||
override val ref: NativeResourceRef = super.register() | ||
|
||
// record arrays who construct this array instance | ||
// we use weak reference to prevent gc blocking | ||
private[mxnet] val dependencies = mutable.HashMap.empty[Long, WeakReference[NDArray]] | ||
@volatile private var disposed = false | ||
def isDisposed: Boolean = disposed | ||
|
||
def serialize(): Array[Byte] = { | ||
val buf = ArrayBuffer.empty[Byte] | ||
|
@@ -584,11 +588,10 @@ class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle, | |
* The NDArrays it depends on will NOT be disposed. <br /> | ||
* The object shall never be used after it is disposed. | ||
*/ | ||
def dispose(): Unit = { | ||
if (!disposed) { | ||
_LIB.mxNDArrayFree(handle) | ||
override def dispose(): Unit = { | ||
if (!super.isDisposed) { | ||
super.dispose() | ||
dependencies.clear() | ||
disposed = true | ||
} | ||
} | ||
|
||
|
@@ -1034,6 +1037,7 @@ class NDArray private[mxnet](private[mxnet] val handle: NDArrayHandle, | |
// TODO: naive implementation | ||
shape.hashCode + toArray.hashCode | ||
} | ||
|
||
} | ||
|
||
private[mxnet] object NDArrayConversions { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this module for the Unittest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes