selected_rows.h 2.8 KB
Newer Older
朔-望's avatar
朔-望 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
==============================================================================*/

#pragma once

#include <vector>

#include "lod_tensor.h"
#include "tensor.h"

namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
27
namespace framework {
朔-望's avatar
朔-望 已提交
28

朔-望's avatar
朔-望 已提交
29 30 31 32 33 34
class SelectedRows {
  public:
    SelectedRows(const std::vector<int64_t> &rows, const int64_t &height)
        : rows_(rows), height_(height) {
        value_.reset(new Tensor());
    }
朔-望's avatar
朔-望 已提交
35

朔-望's avatar
朔-望 已提交
36 37 38 39
    SelectedRows() {
        height_ = 0;
        value_.reset(new Tensor());
    }
朔-望's avatar
朔-望 已提交
40

朔-望's avatar
朔-望 已提交
41
    const Tensor &value() const { return *value_; }
朔-望's avatar
朔-望 已提交
42

朔-望's avatar
朔-望 已提交
43
    Tensor *mutable_value() { return value_.get(); }
朔-望's avatar
朔-望 已提交
44

朔-望's avatar
朔-望 已提交
45
    int64_t height() const { return height_; }
朔-望's avatar
朔-望 已提交
46

朔-望's avatar
朔-望 已提交
47
    void set_height(int64_t height) { height_ = height; }
朔-望's avatar
朔-望 已提交
48

朔-望's avatar
朔-望 已提交
49
    const std::vector<int64_t> &rows() const { return rows_; }
朔-望's avatar
朔-望 已提交
50

朔-望's avatar
朔-望 已提交
51
    std::vector<int64_t> *mutable_rows() { return &rows_; }
朔-望's avatar
朔-望 已提交
52

朔-望's avatar
朔-望 已提交
53
    void set_rows(const std::vector<int64_t> &rows) { rows_ = rows; }
朔-望's avatar
朔-望 已提交
54

朔-望's avatar
朔-望 已提交
55 56 57 58 59 60 61 62
    /**
     * get the index of id in rows
     */
    int64_t index(int64_t id) const {
        auto it = std::find(rows_.begin(), rows_.end(), id);
        //    PADDLE_ENFORCE(it != rows_.end(), "id should be in rows");
        return static_cast<int64_t>(std::distance(rows_.begin(), it));
    }
朔-望's avatar
朔-望 已提交
63

朔-望's avatar
朔-望 已提交
64 65 66 67 68
    DDim GetCompleteDims() const {
        std::vector<int64_t> dims = vectorize(value_->dims());
        dims[0] = height_;
        return make_ddim(dims);
    }
朔-望's avatar
朔-望 已提交
69

朔-望's avatar
朔-望 已提交
70 71 72 73 74 75 76 77 78
  private:
    // Notice: rows can be duplicate. We can have {0, 4, 7, 0, 5, 7, 9}
    // here.
    // SelectedRows are simply concated when adding together. Until a
    // SelectedRows add a Tensor, will the duplicate rows be handled.
    std::vector<int64_t> rows_;
    std::unique_ptr<Tensor> value_{nullptr};
    int64_t height_;
};
朔-望's avatar
朔-望 已提交
79

朔-望's avatar
朔-望 已提交
80
} // namespace framework
L
liuruilong 已提交
81
} // namespace paddle_mobile