New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Some memory improvements #6883
Merged
Merged
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
5531a87
initial commit
raver119 907ee5a
not closeable for IsAttached()
raver119 fdd50cc
couple of simple tests
raver119 0f4eca0
one more test
raver119 e18632d
CUDA backend
raver119 bc4ef94
Merge branch 'master' into r119_close_that
raver119 5d935d8
batched_gemm additional validation
raver119 2ae3704
normalize_moments same mode fix
raver119 7ecda26
- varargs for few Nd4j.createFromArray signatures
raver119 8f34253
one more test
raver119 cc19ee2
one more test
raver119 9056b94
workspace attached
raver119 97d4081
one more test
raver119 de786e0
- legacy tracking
raver119 d9f71a5
cpu constant space
raver119 d1eccdd
minor tweaks
raver119 f822e1a
important pico fix
raver119 0aeef0a
cpu workspace tracking
raver119 1720d44
separate tracking for workspace allocations
raver119 bd60ee6
temporary ignored test pack
raver119 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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
88 changes: 88 additions & 0 deletions
88
nd4j/nd4j-backends/nd4j-tests/src/test/java/org/nd4j/linalg/memory/CloseableTests.java
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2015-2018 Skymind, Inc. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
******************************************************************************/ | ||
|
||
package org.nd4j.linalg.memory; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.nd4j.linalg.BaseNd4jTest; | ||
import org.nd4j.linalg.api.memory.conf.WorkspaceConfiguration; | ||
import org.nd4j.linalg.factory.Nd4j; | ||
import org.nd4j.linalg.factory.Nd4jBackend; | ||
import org.nd4j.linalg.indexing.NDArrayIndex; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* @author raver119@gmail.com | ||
*/ | ||
@Slf4j | ||
@RunWith(Parameterized.class) | ||
public class CloseableTests extends BaseNd4jTest { | ||
public CloseableTests(Nd4jBackend backend) { | ||
super(backend); | ||
} | ||
|
||
@Test | ||
public void testSimpleRelease_1() { | ||
val array = Nd4j.createFromArray(new float[]{1, 2, 3, 4, 5}); | ||
assertTrue(array.closeable()); | ||
|
||
array.close(); | ||
|
||
assertFalse(array.closeable()); | ||
} | ||
|
||
@Test | ||
public void testCyclicRelease_1() { | ||
for (int e = 0; e < 1000; e++) { | ||
try (val array = Nd4j.createFromArray(new float[]{1, 2, 3, 4, 5})) { | ||
array.addi(1.0f); | ||
} | ||
System.gc(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testViewRelease_1() { | ||
val array = Nd4j.create(5, 5); | ||
assertTrue(array.closeable()); | ||
|
||
val view = array.get(NDArrayIndex.point(1), NDArrayIndex.all()); | ||
|
||
assertTrue(array.closeable()); | ||
assertFalse(view.closeable()); | ||
} | ||
|
||
@Test | ||
public void testAttachedRelease_1() { | ||
val wsconf = WorkspaceConfiguration.builder().build(); | ||
|
||
try (val ws = Nd4j.getWorkspaceManager().getAndActivateWorkspace(wsconf, "haha72yjhfdfs")) { | ||
val array = Nd4j.create(5, 5); | ||
assertFalse(array.closeable()); | ||
} | ||
} | ||
|
||
@Override | ||
public char ordering() { | ||
return 'c'; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
import org.nd4j.linalg.api.buffer.util.AllocUtil; | ||
import org.nd4j.linalg.api.buffer.util.DataTypeUtil; | ||
import org.nd4j.linalg.api.memory.MemoryWorkspace; | ||
import org.nd4j.linalg.primitives.AtomicBoolean; | ||
import org.nd4j.linalg.primitives.AtomicDouble; | ||
import org.nd4j.linalg.primitives.Triple; | ||
import org.nd4j.linalg.util.ArrayUtil; | ||
|
@@ -35,6 +36,7 @@ | |
import java.nio.FloatBuffer; | ||
import java.nio.IntBuffer; | ||
import java.nio.LongBuffer; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
@@ -94,6 +96,10 @@ public abstract class BaseDataBuffer implements DataBuffer { | |
protected transient Long trackingPoint; | ||
|
||
protected transient boolean constant = false; | ||
protected transient boolean released = false; | ||
|
||
protected transient AtomicBoolean referenced = new AtomicBoolean(false); | ||
protected transient Collection<BaseDataBuffer> references = new ArrayList<>(); | ||
|
||
public BaseDataBuffer() {} | ||
|
||
|
@@ -168,6 +174,8 @@ protected BaseDataBuffer(DataBuffer underlyingBuffer, long length, long offset) | |
this.elementSize = (byte) underlyingBuffer.getElementSize(); | ||
this.underlyingLength = underlyingBuffer.underlyingLength(); | ||
this.wrappedDataBuffer = underlyingBuffer; | ||
((BaseDataBuffer) underlyingBuffer).referenced.compareAndSet(false, true); | ||
((BaseDataBuffer) underlyingBuffer).references.add(this); | ||
|
||
// Adding link to original databuffer | ||
if (underlyingBuffer.originalDataBuffer() == null) { | ||
|
@@ -2246,4 +2254,40 @@ public DataBuffer reallocate(long length) { | |
public long capacity() { | ||
return pointer().capacity(); | ||
} | ||
|
||
@Override | ||
public boolean closeable() { | ||
if (released || isAttached() || isConstant()) | ||
return false; | ||
|
||
if (wrappedDataBuffer != null && wrappedDataBuffer != this) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
protected void markReleased() { | ||
this.released = true; | ||
|
||
for (val r:references) | ||
r.markReleased(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
if (!closeable()) | ||
throw new IllegalStateException("Can't release this data buffer"); | ||
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. Would be great to know why it is not closeable. |
||
|
||
// notifying other databuffers that their underlying | ||
for (val r:references) | ||
r.markReleased(); | ||
|
||
release(); | ||
} | ||
|
||
protected void release() { | ||
this.pointer.deallocate(); | ||
this.indexer = null; | ||
this.pointer = null; | ||
} | ||
} |
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
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.
Would be great to know why it is not closeable.