Skip to content

Commit

Permalink
let paddle hold the creation buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Qing Lan committed Aug 11, 2021
1 parent ea814b4 commit 53907ad
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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;
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 @@ -148,6 +152,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

0 comments on commit 53907ad

Please sign in to comment.