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

[Lgbm] fix LgbmNDArray replaced.close() release data problem #3174

Merged
merged 2 commits into from
May 12, 2024
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 @@ -39,8 +39,8 @@ public class LgbmNDArray extends NDArrayAdapter {

private AtomicReference<SWIGTYPE_p_void> handle;
private int typeConstant;
private SWIGTYPE_p_float floatData;
private SWIGTYPE_p_double doubleData;
private AtomicReference<SWIGTYPE_p_float> floatDataRef;
private AtomicReference<SWIGTYPE_p_double> doubleDataRef;

LgbmNDArray(
NDManager manager,
Expand All @@ -53,6 +53,8 @@ public class LgbmNDArray extends NDArrayAdapter {
this.format = SparseFormat.DENSE;
manager.attachInternal(uid, this);
handle = new AtomicReference<>();
floatDataRef = new AtomicReference<>();
doubleDataRef = new AtomicReference<>();
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -82,19 +84,19 @@ public SWIGTYPE_p_void getHandle() {
if (getDataType() == DataType.FLOAT32) {
typeConstant = lightgbmlibConstants.C_API_DTYPE_FLOAT32;
FloatBuffer d1 = toByteBuffer().asFloatBuffer();
floatData = lightgbmlib.new_floatArray(size);
floatDataRef.set(lightgbmlib.new_floatArray(size));
for (int i = 0; i < size; i++) {
lightgbmlib.floatArray_setitem(floatData, i, d1.get(i));
lightgbmlib.floatArray_setitem(floatDataRef.get(), i, d1.get(i));
}
handle.set(lightgbmlib.float_to_voidp_ptr(floatData));
handle.set(lightgbmlib.float_to_voidp_ptr(floatDataRef.get()));
} else if (getDataType() == DataType.FLOAT64) {
typeConstant = lightgbmlibConstants.C_API_DTYPE_FLOAT64;
DoubleBuffer d1 = toByteBuffer().asDoubleBuffer();
doubleData = lightgbmlib.new_doubleArray(size);
doubleDataRef.set(lightgbmlib.new_doubleArray(size));
for (int i = 0; i < size; i++) {
lightgbmlib.doubleArray_setitem(doubleData, i, d1.get(i));
lightgbmlib.doubleArray_setitem(doubleDataRef.get(), i, d1.get(i));
}
handle.set(lightgbmlib.double_to_voidp_ptr(doubleData));
handle.set(lightgbmlib.double_to_voidp_ptr(doubleDataRef.get()));
} else {
throw new IllegalArgumentException(
"The LightGBM operation can only be performed with a Float32 or Float64"
Expand Down Expand Up @@ -151,18 +153,19 @@ public ByteBuffer toByteBuffer() {
/** {@inheritDoc} */
@Override
public void intern(NDArray replaced) {
LgbmNDArray array = (LgbmNDArray) replaced;

SWIGTYPE_p_float floatData = floatDataRef.getAndSet(array.floatDataRef.getAndSet(null));
if (floatData != null) {
lightgbmlib.delete_floatArray(floatData);
}
SWIGTYPE_p_double doubleData = doubleDataRef.getAndSet(array.doubleDataRef.getAndSet(null));
if (doubleData != null) {
lightgbmlib.delete_doubleArray(doubleData);
}
LgbmNDArray array = (LgbmNDArray) replaced;
handle.set(array.handle.getAndSet(null));
data = array.data;
handle = array.handle;
format = array.format;
floatData = array.floatData;
doubleData = array.doubleData;
typeConstant = array.typeConstant;
shape = array.shape;
dataType = array.dataType;
Expand All @@ -180,11 +183,15 @@ public void detach() {
@Override
public void close() {
super.close();
SWIGTYPE_p_float floatData = floatDataRef.getAndSet(null);
if (floatData != null) {
lightgbmlib.delete_floatArray(floatData);
}
SWIGTYPE_p_double doubleData = doubleDataRef.getAndSet(null);
if (doubleData != null) {
lightgbmlib.delete_doubleArray(doubleData);
}
handle.set(null);
data = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ JNIEXPORT jlong JNICALL Java_ai_djl_pytorch_jni_PyTorchLibrary_torchAtan(JNIEnv*
}

JNIEXPORT jlong JNICALL Java_ai_djl_pytorch_jni_PyTorchLibrary_torchAtan2(
JNIEnv* env, jobject jthis, jlong jself, jlong jother) {
JNIEnv* env, jobject jthis, jlong jself, jlong jother) {
API_BEGIN()
const auto* self_ptr = reinterpret_cast<torch::Tensor*>(jself);
const auto* other_ptr = reinterpret_cast<torch::Tensor*>(jother);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
#include <jni.h>
#include <torch/csrc/api/include/torch/enum.h>
#include <torch/script.h>
#include <variant>

#include <iostream>
#include <variant>

#include "djl_pytorch_jni_log.h"

Expand Down
Loading