Search in sources :

Example 31 with IntDoubleSparseVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage in project angel by Tencent.

the class MixedBinaryOutAllExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntDummyVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (!op.isKeepStorage()) {
        for (int i = 0; i < parts.length; i++) {
            if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
                resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
            }
        }
    }
    int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
    for (int i = 0; i < v1.getDim(); i++) {
        int pidx = (int) (i / subDim);
        int subidx = i % subDim;
        ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
    }
    IntDoubleVector[] res = new IntDoubleVector[parts.length];
    int i = 0;
    for (IntDoubleVector part : parts) {
        res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
        i++;
    }
    return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector)

Example 32 with IntDoubleSparseVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage in project angel by Tencent.

the class MixedBinaryOutAllExecutor method apply.

private static Vector apply(CompIntDoubleVector v1, IntFloatVector v2, Binary op) {
    IntDoubleVector[] parts = v1.getPartitions();
    Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
    if (v2.isDense()) {
        float[] v2Values = v2.getStorage().getValues();
        int base = 0, k = 0;
        for (IntDoubleVector part : parts) {
            IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[k];
            if (part.isDense()) {
                double[] partValue = part.getStorage().getValues();
                double[] resPartValues = resPart.getValues();
                for (int i = 0; i < partValue.length; i++) {
                    int idx = i;
                    resPartValues[i] = op.apply(partValue[i], v2Values[idx + base]);
                }
            } else if (part.isSparse()) {
                if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                    for (int i = 0; i < part.getDim(); i++) {
                        resPart.set(i, op.apply(0, v2Values[i + base]));
                    }
                    ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
                    while (iter.hasNext()) {
                        Int2DoubleMap.Entry entry = iter.next();
                        int idx = entry.getIntKey();
                        resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
                    }
                } else {
                    for (int i = 0; i < resPart.size(); i++) {
                        if (part.getStorage().hasKey(i)) {
                            resPart.set(i, op.apply(part.get(i), v2Values[i + base]));
                        } else {
                            resPart.set(i, op.apply(0, v2Values[i + base]));
                        }
                    }
                }
            } else {
                // sorted
                if (op.isKeepStorage()) {
                    int[] resPartIndices = resPart.getIndices();
                    double[] resPartValues = resPart.getValues();
                    if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        for (int i = 0; i < part.getDim(); i++) {
                            resPartIndices[i] = i;
                            resPartValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntDoubleVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < resPartValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                resPartIndices[i] = i;
                                resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                resPartIndices[i] = i;
                                resPartValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                } else {
                    double[] resPartValues = resPart.getValues();
                    if (part.size() < Constant.denseLoopThreshold * part.getDim()) {
                        int[] partIndices = part.getStorage().getIndices();
                        double[] partValues = part.getStorage().getValues();
                        for (int i = 0; i < part.getDim(); i++) {
                            resPartValues[i] = op.apply(0, v2Values[i + base]);
                        }
                        int size = part.size();
                        for (int i = 0; i < size; i++) {
                            int idx = partIndices[i];
                            resPartValues[idx] = op.apply(partValues[i], v2Values[idx + base]);
                        }
                    } else {
                        IntDoubleVectorStorage partStorage = part.getStorage();
                        for (int i = 0; i < resPartValues.length; i++) {
                            if (partStorage.hasKey(i)) {
                                resPartValues[i] = op.apply(partStorage.get(i), v2Values[i + base]);
                            } else {
                                resPartValues[i] = op.apply(0, v2Values[i + base]);
                            }
                        }
                    }
                }
            }
            base += part.getDim();
            k++;
        }
    } else {
        if (!op.isKeepStorage()) {
            for (int i = 0; i < parts.length; i++) {
                if (parts[i].getStorage() instanceof IntDoubleSortedVectorStorage) {
                    resParts[i] = new IntDoubleSparseVectorStorage(parts[i].getDim(), parts[i].getStorage().getIndices(), parts[i].getStorage().getValues());
                }
            }
        }
        int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
        for (int i = 0; i < v1.getDim(); i++) {
            int pidx = (int) (i / subDim);
            int subidx = i % subDim;
            if (v2.getStorage().hasKey(i)) {
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2.get(i)));
            } else {
                ((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), 0));
            }
        }
    }
    IntDoubleVector[] res = new IntDoubleVector[parts.length];
    int i = 0;
    for (IntDoubleVector part : parts) {
        res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
        i++;
    }
    return new CompIntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), res, v1.getSubDim());
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) IntIntVectorStorage(com.tencent.angel.ml.math2.storage.IntIntVectorStorage) Storage(com.tencent.angel.ml.math2.storage.Storage) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage) LongIntVectorStorage(com.tencent.angel.ml.math2.storage.LongIntVectorStorage) LongLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSparseVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) LongDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage) LongDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage) LongLongVectorStorage(com.tencent.angel.ml.math2.storage.LongLongVectorStorage) LongFloatVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatVectorStorage) IntLongVectorStorage(com.tencent.angel.ml.math2.storage.IntLongVectorStorage) IntIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage) LongIntSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSortedVectorStorage) IntLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage) IntLongSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage) LongIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongIntSparseVectorStorage) IntFloatVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatVectorStorage) IntFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage) LongLongSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongLongSortedVectorStorage) LongDoubleVectorStorage(com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntIntSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage) IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) LongFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage) LongFloatSortedVectorStorage(com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage) IntDoubleVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage) IntDoubleSortedVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage) CompIntDoubleVector(com.tencent.angel.ml.math2.vector.CompIntDoubleVector)

Example 33 with IntDoubleSparseVectorStorage

use of com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage in project angel by Tencent.

the class GBDTController method mergeCateFeatSketch.

public void mergeCateFeatSketch() throws Exception {
    LOG.info("------Merge categorical features------");
    Set<String> needFlushMatrixSet = new HashSet<String>(1);
    // the leader worker
    if (!this.cateFeatList.isEmpty() && this.taskContext.getTaskIndex() == 0) {
        PSModel cateFeat = model.getPSModel(this.param.cateFeatureName);
        PSModel sketch = model.getPSModel(this.param.sketchName);
        Set<Double>[] featSet = new HashSet[cateFeatList.size()];
        for (int i = 0; i < cateFeatList.size(); i++) {
            featSet[i] = new HashSet<>();
        }
        int workerNum = this.taskContext.getConf().getInt(AngelConf.ANGEL_WORKERGROUP_ACTUAL_NUM, 1);
        // merge categorical features
        for (int worker = 0; worker < workerNum; worker++) {
            IntDoubleVector vec = (IntDoubleVector) cateFeat.getRow(worker);
            for (int i = 0; i < cateFeatList.size(); i++) {
                int fid = cateFeatList.get(i);
                int start = i * this.param.numSplit;
                for (int j = 0; j < this.param.numSplit; j++) {
                    double fvalue = vec.get(start + j);
                    featSet[i].add(fvalue);
                }
            }
        }
        // create updates
        IntDoubleVector cateFeatVec = new IntDoubleVector(this.param.numFeature * this.param.numSplit, new IntDoubleSparseVectorStorage(this.param.numFeature * this.param.numSplit));
        for (int i = 0; i < cateFeatList.size(); i++) {
            int fid = cateFeatList.get(i);
            int start = fid * this.param.numSplit;
            List<Double> sortedValue = new ArrayList<>(featSet[i]);
            Collections.sort(sortedValue);
            assert sortedValue.size() < this.param.numSplit;
            for (int j = 0; j < sortedValue.size(); j++) {
                cateFeatVec.set(start + j, sortedValue.get(j));
            }
        }
        sketch.increment(0, cateFeatVec);
        needFlushMatrixSet.add(this.param.sketchName);
    }
    clockAllMatrix(needFlushMatrixSet, true);
}
Also used : PSModel(com.tencent.angel.ml.model.PSModel) IntDoubleVector(com.tencent.angel.ml.math2.vector.IntDoubleVector) IntDoubleSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)

Aggregations

IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)33 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)30 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)23 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)23 CompIntDoubleVector (com.tencent.angel.ml.math2.vector.CompIntDoubleVector)21 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)20 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)20 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)20 IntLongSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSortedVectorStorage)20 IntLongSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntLongSparseVectorStorage)20 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)20 LongDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSortedVectorStorage)20 LongDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleSparseVectorStorage)20 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)20 LongFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSortedVectorStorage)20 LongFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatSparseVectorStorage)20 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)20