use of com.tencent.angel.ml.GBDT.psf.HistAggrParam in project angel by Tencent.
the class GBDTController method findSplit.
// find split
public void findSplit() throws Exception {
LOG.info("------Find split------");
long startTime = System.currentTimeMillis();
// 1. find responsible tree node, using RR scheme
List<Integer> responsibleTNode = new ArrayList<>();
int activeTNodeNum = 0;
for (int nid = 0; nid < this.activeNode.length; nid++) {
int isActive = this.activeNode[nid];
if (isActive == 1) {
if (this.taskContext.getTaskIndex() == activeTNodeNum) {
responsibleTNode.add(nid);
}
if (++activeTNodeNum >= taskContext.getTotalTaskNum()) {
activeTNodeNum = 0;
}
}
}
int[] tNodeId = Maths.intList2Arr(responsibleTNode);
LOG.info(String.format("Task[%d] responsible tree node: %s", this.taskContext.getTaskId().getIndex(), responsibleTNode.toString()));
// 2. pull gradient histogram
// the updated indices of the parameter on PS
int[] updatedIndices = new int[tNodeId.length];
// the updated split features
int[] updatedSplitFid = new int[tNodeId.length];
// the updated split value
double[] updatedSplitFvalue = new double[tNodeId.length];
// the updated split gain
double[] updatedSplitGain = new double[tNodeId.length];
boolean isServerSplit = taskContext.getConf().getBoolean(MLConf.ML_GBDT_SERVER_SPLIT(), MLConf.DEFAULT_ML_GBDT_SERVER_SPLIT());
int splitNum = taskContext.getConf().getInt(MLConf.ML_GBDT_SPLIT_NUM(), MLConf.DEFAULT_ML_GBDT_SPLIT_NUM());
for (int i = 0; i < tNodeId.length; i++) {
int nid = tNodeId[i];
LOG.debug(String.format("Task[%d] find best split of tree node: %d", this.taskContext.getTaskIndex(), nid));
// 2.1. get the name of this node's gradient histogram on PS
String gradHistName = this.param.gradHistNamePrefix + nid;
// 2.2. pull the histogram
long pullStartTime = System.currentTimeMillis();
PSModel histMat = model.getPSModel(gradHistName);
TIntDoubleVector histogram = null;
SplitEntry splitEntry = null;
if (isServerSplit) {
int matrixId = histMat.getMatrixId();
GBDTGradHistGetRowFunc func = new GBDTGradHistGetRowFunc(new HistAggrParam(matrixId, 0, param.numSplit, param.minChildWeight, param.regAlpha, param.regLambda));
// histogram = (TDoubleVector) ((GetRowResult) histMat.get(func)).getRow();
splitEntry = ((GBDTGradHistGetRowResult) histMat.get(func)).getSplitEntry();
} else {
histogram = (TIntDoubleVector) histMat.getRow(0);
LOG.debug("Get grad histogram without server split mode, histogram size" + histogram.getDimension());
}
LOG.info(String.format("Pull histogram from PS cost %d ms", System.currentTimeMillis() - pullStartTime));
GradHistHelper histHelper = new GradHistHelper(this, nid);
// 2.3. find best split result of this tree node
if (this.param.isServerSplit) {
// 2.3.1 using server split
if (splitEntry.getFid() != -1) {
int trueSplitFid = this.fSet[splitEntry.getFid()];
int splitIdx = (int) splitEntry.getFvalue();
float trueSplitValue = this.sketches[trueSplitFid * this.param.numSplit + splitIdx];
LOG.info(String.format("Best split of node[%d]: feature[%d], value[%f], " + "true feature[%d], true value[%f], losschg[%f]", nid, splitEntry.getFid(), splitEntry.getFvalue(), trueSplitFid, trueSplitValue, splitEntry.getLossChg()));
splitEntry.setFid(trueSplitFid);
splitEntry.setFvalue(trueSplitValue);
}
// update the grad stats of the root node on PS, only called once by leader worker
if (nid == 0) {
GradStats rootStats = new GradStats(splitEntry.leftGradStat);
rootStats.add(splitEntry.rightGradStat);
this.updateNodeGradStats(nid, rootStats);
}
// update the grad stats of children node
if (splitEntry.fid != -1) {
// update the left child
this.updateNodeGradStats(2 * nid + 1, splitEntry.leftGradStat);
// update the right child
this.updateNodeGradStats(2 * nid + 2, splitEntry.rightGradStat);
}
// 2.3.2 the updated split result (tree node/feature/value/gain) on PS,
updatedIndices[i] = nid;
updatedSplitFid[i] = splitEntry.fid;
updatedSplitFvalue[i] = splitEntry.fvalue;
updatedSplitGain[i] = splitEntry.lossChg;
} else {
// 2.3.3 otherwise, the returned histogram contains the gradient info
splitEntry = histHelper.findBestSplit(histogram);
LOG.info(String.format("Best split of node[%d]: feature[%d], value[%f], losschg[%f]", nid, splitEntry.getFid(), splitEntry.getFvalue(), splitEntry.getLossChg()));
// 2.3.4 the updated split result (tree node/feature/value/gain) on PS,
updatedIndices[i] = nid;
updatedSplitFid[i] = splitEntry.fid;
updatedSplitFvalue[i] = splitEntry.fvalue;
updatedSplitGain[i] = splitEntry.lossChg;
}
// 2.3.5 reset this tree node's gradient histogram to 0
histMat.zero();
}
// 3. push split feature to PS
DenseIntVector splitFeatureVector = new DenseIntVector(this.activeNode.length);
// 4. push split value to PS
DenseDoubleVector splitValueVector = new DenseDoubleVector(this.activeNode.length);
// 5. push split gain to PS
DenseDoubleVector splitGainVector = new DenseDoubleVector(this.activeNode.length);
for (int i = 0; i < updatedIndices.length; i++) {
splitFeatureVector.set(updatedIndices[i], updatedSplitFid[i]);
splitValueVector.set(updatedIndices[i], updatedSplitFvalue[i]);
splitGainVector.set(updatedIndices[i], updatedSplitGain[i]);
}
PSModel splitFeat = model.getPSModel(this.param.splitFeaturesName);
splitFeat.increment(this.currentTree, splitFeatureVector);
PSModel splitValue = model.getPSModel(this.param.splitValuesName);
splitValue.increment(this.currentTree, splitValueVector);
PSModel splitGain = model.getPSModel(this.param.splitGainsName);
splitGain.increment(this.currentTree, splitGainVector);
// 6. set phase to AFTER_SPLIT
this.phase = GBDTPhase.AFTER_SPLIT;
LOG.info(String.format("Find split cost: %d ms", System.currentTimeMillis() - startTime));
// clock
Set<String> needFlushMatrixSet = new HashSet<String>(3);
needFlushMatrixSet.add(this.param.splitFeaturesName);
needFlushMatrixSet.add(this.param.splitValuesName);
needFlushMatrixSet.add(this.param.splitGainsName);
needFlushMatrixSet.add(this.param.nodeGradStatsName);
clockAllMatrix(needFlushMatrixSet, true);
}
Aggregations