|
| 1 | +#include "segment_csr_cpu.h" |
| 2 | + |
| 3 | +#include "index_info.h" |
| 4 | +#include "reducer.h" |
| 5 | +#include "utils.h" |
| 6 | + |
| 7 | +std::tuple<torch::Tensor, torch::optional<torch::Tensor>> |
| 8 | +segment_csr_cpu(torch::Tensor src, torch::Tensor indptr, |
| 9 | + torch::optional<torch::Tensor> optional_out, |
| 10 | + std::string reduce) { |
| 11 | + CHECK_CPU(src); |
| 12 | + CHECK_CPU(indptr); |
| 13 | + if (optional_out.has_value()) |
| 14 | + CHECK_CPU(optional_out.value()); |
| 15 | + |
| 16 | + CHECK_INPUT(src.dim() >= indptr.dim()); |
| 17 | + |
| 18 | + auto sizes = indptr.sizes().vec(); |
| 19 | + for (auto i = 0; i < indptr.dim() - 1; i++) |
| 20 | + sizes[i] = src.size(i); |
| 21 | + indptr = indptr.expand(sizes); |
| 22 | + |
| 23 | + auto dim = indptr.dim() - 1; |
| 24 | + |
| 25 | + src = src.contiguous(); |
| 26 | + |
| 27 | + torch::Tensor out; |
| 28 | + if (optional_out.has_value()) { |
| 29 | + out = optional_out.value().contiguous(); |
| 30 | + for (int i = 0; i < out.dim(); i++) |
| 31 | + if (i != dim) |
| 32 | + CHECK_INPUT(src.size(i) == out.size(i)); |
| 33 | + CHECK_INPUT(out.size(dim) == indptr.size(dim) - 1); |
| 34 | + } else { |
| 35 | + sizes = src.sizes().vec(); |
| 36 | + sizes[dim] = indptr.size(dim) - 1; |
| 37 | + out = torch::empty(sizes, src.options()); |
| 38 | + } |
| 39 | + |
| 40 | + torch::optional<torch::Tensor> arg_out = torch::nullopt; |
| 41 | + int64_t *arg_out_data = nullptr; |
| 42 | + if (reduce2REDUCE.at(reduce) == MIN || reduce2REDUCE.at(reduce) == MAX) { |
| 43 | + arg_out = torch::full(out.sizes(), src.size(dim), indptr.options()); |
| 44 | + arg_out_data = arg_out.value().data_ptr<int64_t>(); |
| 45 | + } |
| 46 | + |
| 47 | + auto N = out.size(dim) * (indptr.numel() / indptr.size(-1)); |
| 48 | + auto K = out.numel() / N; |
| 49 | + auto E = src.size(dim); |
| 50 | + |
| 51 | + auto indptr_info = getTensorInfo<int64_t>(indptr); |
| 52 | + auto stride = indptr_info.strides[indptr_info.dims - 1]; |
| 53 | + std::vector<int64_t> args(K); |
| 54 | + AT_DISPATCH_ALL_TYPES(src.scalar_type(), "segment_csr", [&] { |
| 55 | + auto src_data = src.data_ptr<scalar_t>(); |
| 56 | + auto out_data = out.data_ptr<scalar_t>(); |
| 57 | + |
| 58 | + std::vector<scalar_t> vals(K); |
| 59 | + int64_t row_start, row_end; |
| 60 | + AT_DISPATCH_REDUCTION_TYPES(reduce, [&] { |
| 61 | + for (auto n = 0; n < N; n++) { |
| 62 | + auto offset = IndexPtrToOffset<int64_t>::get(n, indptr_info); |
| 63 | + row_start = indptr_info.data[offset]; |
| 64 | + row_end = indptr_info.data[offset + stride]; |
| 65 | + |
| 66 | + offset = (n / (indptr.size(-1) - 1)) * E * K; |
| 67 | + for (auto k = 0; k < K; k++) |
| 68 | + vals[k] = Reducer<scalar_t, REDUCE>::init(); |
| 69 | + |
| 70 | + for (auto e = row_start; e < row_end; e++) { |
| 71 | + CHECK_INPUT(e < E); |
| 72 | + for (auto k = 0; k < K; k++) |
| 73 | + Reducer<scalar_t, REDUCE>::update( |
| 74 | + &vals[k], src_data[offset + e * K + k], &args[k], e); |
| 75 | + } |
| 76 | + |
| 77 | + for (auto k = 0; k < K; k++) |
| 78 | + Reducer<scalar_t, REDUCE>::write(out_data + n * K + k, vals[k], |
| 79 | + arg_out_data + n * K + k, args[k], |
| 80 | + row_end - row_start); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + return std::make_tuple(out, arg_out); |
| 86 | +} |
| 87 | + |
| 88 | +torch::Tensor gather_csr_cpu(torch::Tensor src, torch::Tensor indptr, |
| 89 | + torch::optional<torch::Tensor> optional_out) { |
| 90 | + CHECK_CPU(src); |
| 91 | + CHECK_CPU(indptr); |
| 92 | + if (optional_out.has_value()) |
| 93 | + CHECK_CPU(optional_out.value()); |
| 94 | + |
| 95 | + CHECK_INPUT(src.dim() >= indptr.dim()); |
| 96 | + |
| 97 | + auto sizes = indptr.sizes().vec(); |
| 98 | + for (auto i = 0; i < indptr.dim() - 1; i++) |
| 99 | + sizes[i] = src.size(i); |
| 100 | + indptr = indptr.expand(sizes); |
| 101 | + |
| 102 | + auto dim = indptr.dim() - 1; |
| 103 | + CHECK_INPUT(src.size(dim) == indptr.size(dim) - 1); |
| 104 | + |
| 105 | + src = src.contiguous(); |
| 106 | + |
| 107 | + torch::Tensor out; |
| 108 | + if (optional_out.has_value()) { |
| 109 | + out = optional_out.value().contiguous(); |
| 110 | + for (auto i = 0; i < out.dim(); i++) |
| 111 | + if (i != dim) |
| 112 | + CHECK_INPUT(src.size(i) == out.size(i)); |
| 113 | + } else { |
| 114 | + auto sizes = src.sizes().vec(); |
| 115 | + sizes[dim] = *indptr.flatten()[-1].data_ptr<int64_t>(); |
| 116 | + out = torch::empty(sizes, src.options()); |
| 117 | + } |
| 118 | + |
| 119 | + auto N = src.size(dim) * (indptr.numel() / indptr.size(-1)); |
| 120 | + auto K = src.numel() / N; |
| 121 | + auto E = out.size(dim); |
| 122 | + |
| 123 | + auto indptr_info = getTensorInfo<int64_t>(indptr); |
| 124 | + auto stride = indptr_info.strides[indptr_info.dims - 1]; |
| 125 | + AT_DISPATCH_ALL_TYPES(src.scalar_type(), "gather_csr", [&] { |
| 126 | + auto src_data = src.data_ptr<scalar_t>(); |
| 127 | + auto out_data = out.data_ptr<scalar_t>(); |
| 128 | + |
| 129 | + std::vector<scalar_t> vals(K); |
| 130 | + int64_t row_start, row_end; |
| 131 | + for (int n = 0; n < N; n++) { |
| 132 | + auto offset = IndexPtrToOffset<int64_t>::get(n, indptr_info); |
| 133 | + row_start = indptr_info.data[offset]; |
| 134 | + row_end = indptr_info.data[offset + stride]; |
| 135 | + |
| 136 | + for (auto k = 0; k < K; k++) |
| 137 | + vals[k] = src_data[n * K + k]; |
| 138 | + |
| 139 | + offset = (n / (indptr.size(-1) - 1)) * E * K; |
| 140 | + for (auto e = row_start; e < row_end; e++) |
| 141 | + for (auto k = 0; k < K; k++) |
| 142 | + out_data[offset + e * K + k] = vals[k]; |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + return out; |
| 147 | +} |
0 commit comments