Skip to content
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

let paddle hold the creation buffer #1162

Merged
merged 2 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@
public class PpNDArray extends NativeResource<Long> implements NDArrayAdapter {

private PpNDManager manager;
// we keep the data to prevent GC from early collecting native memory
private ByteBuffer data;
lanking520 marked this conversation as resolved.
Show resolved Hide resolved
private Shape shape;
private DataType dataType;

/**
* Constructs an PpNDArray from a native handle (internal. Use {@link NDManager} instead).
*
* @param manager the manager to attach the new array to
* @param data bytebuffer that holds the native memory
* @param handle the pointer to the native MxNDArray memory
*/
public PpNDArray(PpNDManager manager, long handle) {
public PpNDArray(PpNDManager manager, ByteBuffer data, long handle) {
super(handle);
this.manager = manager;
this.data = data;
manager.attachInternal(getUid(), this);
}

Expand Down Expand Up @@ -130,7 +134,11 @@ public void detach() {
/** {@inheritDoc} */
@Override
public ByteBuffer toByteBuffer() {
return JniUtils.getByteBufferFromNd(this);
if (data == null) {
data = JniUtils.getByteBufferFromNd(this);
}
data.rewind();
return data;
}

/** {@inheritDoc} */
Expand All @@ -148,6 +156,7 @@ public void close() {
Long pointer = handle.getAndSet(null);
if (pointer != null) {
JniUtils.deleteNd(pointer);
data = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static PpNDArray createNdArray(
long handle =
PaddleLibrary.LIB.paddleCreateTensor(
data, data.remaining(), intShape, PpDataType.toPaddlePaddle(dtype));
return new PpNDArray(manager, handle);
return new PpNDArray(manager, data, handle);
}

public static DataType getDTypeFromNd(PpNDArray array) {
Expand Down Expand Up @@ -119,7 +119,7 @@ public static PpNDArray[] predictorForward(
PpNDManager manager = (PpNDManager) inputs[0].getManager();
PpNDArray[] arrays = new PpNDArray[outputs.length];
for (int i = 0; i < outputs.length; i++) {
arrays[i] = new PpNDArray(manager, outputs[i]);
arrays[i] = new PpNDArray(manager, null, outputs[i]);
}
return arrays;
}
Expand Down