我们使用XGboost4J进行ML预测。我们使用平静的web服务开发了预测器,以便在平台内各个组件可以调用ML预测器。例如从产品标题和描述中找出产品类别树。XGBoost4J同步问题?
只是描述了我们实现的基本方式的代码。
//这是在 initialize方法中完成的,对于每个模型都有一个singleton Booster对象加载。
Class Predictor{
private Booster xgboost;
//init call from Serivice initialization while injecting Predictor
public void init(final String modelFile, final Integer numThreads){
if (!(new File(modelFile).exists())) {
throw new IOException("Modelfile " + modelFile + " does not exist");
}
// we use a util class Params to handle parameters as example
final Iterable<Entry<String, Object>> param = new Params() {
{
put("nthread", numThreads);
}
};
xgboost = new Booster(param, modelFile);
}
//Predict method
public String predict(final String predictionString){
final String dummyLabel = "-1";
final String x_n = dummyLabel + "\t" + x_n_libsvm_idxStr;
final DataLoader.CSRSparseData spData = XGboostSparseData.format(x_n);
final DMatrix x_n_dmatrix = new DMatrix(spData.rowHeaders,
spData.colIndex, spData.data, DMatrix.SparseType.CSR);
final float[][] predict = xgboost.predict(x_n_dmatrix);
// Then there is conversion logic of predict to predicted model result which returns predictions
String prediction = getPrediction(predict);
return prediction
}
}
以上预测类的WebServices服务类单注入 所以对于每一个服务调用线程调用的
service.predict(predictionString);
有一个在Tomcat容器的问题时,多个并发线程调用预测方法助推器方法是同步的
private synchronized float[][] pred(DMatrix data, boolean outPutMargin, long treeLimit, boolean predLeaf) throws XGBoostError {
byte optionMask = 0;
if(outPutMargin) {
optionMask = 1;
}
if(predLeaf) {
optionMask = 2;
}
float[][] rawPredicts = new float[1][];
ErrorHandle.checkCall(XgboostJNI.XGBoosterPredict(this.handle, data.getHandle(), optionMask, treeLimit, rawPredicts));
int row = (int)data.rowNum();
int col = rawPredicts[0].length/row;
float[][] predicts = new float[row][col];
for(int i = 0; i < rawPredicts[0].length; ++i) {
int r = i/col;
int c = i % col;
predicts[r][c] = rawPredicts[0][i];
}
return predicts;
}
此创建的线程由于同步块和等待而被锁定和锁定s导致web服务不可扩展。
我们尝试从XGboost4J源代码和编译的jar中删除同步,但它在1-2分钟内崩溃。堆转储显示在下面一行的崩溃而做本地调用XgboostJNI
ErrorHandle.checkCall(XgboostJNI.XGBoosterPredict(this.handle, data.getHandle(), optionMask, treeLimit, rawPredicts));
任何人都知道实施Xgboost4J为高度可扩展的Web服务方法使用Java的更好的办法?
感谢乔治·Heiler!我会尝试使用jpmml-xgboost! – Ravi