Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions csrc/cpu/reducer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,34 @@ const std::map<std::string, ReductionType> reduce2REDUCE = {
[&] { \
switch (reduce2REDUCE.at(reduce)) { \
case SUM: { \
const ReductionType REDUCE = SUM; \
static constexpr ReductionType REDUCE = SUM; \
return __VA_ARGS__(); \
} \
case MEAN: { \
const ReductionType REDUCE = MEAN; \
static constexpr ReductionType REDUCE = MEAN; \
return __VA_ARGS__(); \
} \
case MUL: { \
const ReductionType REDUCE = MUL; \
static constexpr ReductionType REDUCE = MUL; \
return __VA_ARGS__(); \
} \
case DIV: { \
const ReductionType REDUCE = DIV; \
static constexpr ReductionType REDUCE = DIV; \
return __VA_ARGS__(); \
} \
case MIN: { \
const ReductionType REDUCE = MIN; \
static constexpr ReductionType REDUCE = MIN; \
return __VA_ARGS__(); \
} \
case MAX: { \
const ReductionType REDUCE = MAX; \
static constexpr ReductionType REDUCE = MAX; \
return __VA_ARGS__(); \
} \
} \
}()

template <typename scalar_t> struct Reducer {
static inline scalar_t init(ReductionType REDUCE) {
template <typename scalar_t, ReductionType REDUCE> struct Reducer {
static inline scalar_t init() {
if (REDUCE == MUL || REDUCE == DIV)
return (scalar_t)1;
else if (REDUCE == MIN)
Expand All @@ -52,8 +52,8 @@ template <typename scalar_t> struct Reducer {
return (scalar_t)0;
}

static inline void update(ReductionType REDUCE, scalar_t *val,
scalar_t new_val, int64_t *arg, int64_t new_arg) {
static inline void update(scalar_t *val, scalar_t new_val, int64_t *arg,
int64_t new_arg) {
if (REDUCE == SUM || REDUCE == MEAN)
*val = *val + new_val;
else if (REDUCE == MUL)
Expand All @@ -67,9 +67,8 @@ template <typename scalar_t> struct Reducer {
}
}

static inline void write(ReductionType REDUCE, scalar_t *address,
scalar_t val, int64_t *arg_address, int64_t arg,
int count) {
static inline void write(scalar_t *address, scalar_t val,
int64_t *arg_address, int64_t arg, int count) {
if (REDUCE == SUM || REDUCE == MUL || REDUCE == DIV)
*address = val;
else if (REDUCE == MEAN)
Expand Down
8 changes: 4 additions & 4 deletions csrc/cpu/scatter_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ scatter_cpu(torch::Tensor src, torch::Tensor index, int64_t dim,
int64_t i, idx;
AT_DISPATCH_REDUCTION_TYPES(reduce, [&] {
if (!optional_out.has_value())
out.fill_(Reducer<scalar_t>::init(REDUCE));
out.fill_(Reducer<scalar_t, REDUCE>::init());

for (auto b = 0; b < B; b++) {
for (auto e = 0; e < E; e++) {
for (auto k = 0; k < K; k++) {
i = b * E * K + e * K + k;
idx = index_info.data[IndexToOffset<int64_t>::get(i, index_info)];
Reducer<scalar_t>::update(
REDUCE, out_data + b * N * K + idx * K + k, src_data[i],
Reducer<scalar_t, REDUCE>::update(
out_data + b * N * K + idx * K + k, src_data[i],
arg_out_data + b * N * K + idx * K + k, e);
}
}
}

if (!optional_out.has_value() && (REDUCE == MIN || REDUCE == MAX))
out.masked_fill_(out == Reducer<scalar_t>::init(REDUCE), (scalar_t)0);
out.masked_fill_(out == Reducer<scalar_t, REDUCE>::init(), (scalar_t)0);
});
});

Expand Down
16 changes: 8 additions & 8 deletions csrc/cpu/segment_coo_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ segment_coo_cpu(torch::Tensor src, torch::Tensor index,
int64_t idx, next_idx, row_start;
AT_DISPATCH_REDUCTION_TYPES(reduce, [&] {
if (!optional_out.has_value())
out.fill_(Reducer<scalar_t>::init(REDUCE));
out.fill_(Reducer<scalar_t, REDUCE>::init());
if (REDUCE == MEAN)
count_data = arg_out.value().data_ptr<scalar_t>();

Expand All @@ -87,13 +87,13 @@ segment_coo_cpu(torch::Tensor src, torch::Tensor index,
for (auto e = 0; e < E; e++) {

for (auto k = 0; k < K; k++)
Reducer<scalar_t>::update(
REDUCE, &vals[k], src_data[b * E * K + e * K + k], &args[k], e);
Reducer<scalar_t, REDUCE>::update(
&vals[k], src_data[b * E * K + e * K + k], &args[k], e);

if (e == E - 1) {
for (auto k = 0; k < K; k++)
Reducer<scalar_t>::write(
REDUCE, out_data + b * N * K + idx * K + k, vals[k],
Reducer<scalar_t, REDUCE>::write(
out_data + b * N * K + idx * K + k, vals[k],
arg_out_data + b * N * K + idx * K + k, args[k],
e + 1 - row_start);
if (REDUCE == MEAN)
Expand All @@ -104,8 +104,8 @@ segment_coo_cpu(torch::Tensor src, torch::Tensor index,

if (idx != next_idx) {
for (auto k = 0; k < K; k++) {
Reducer<scalar_t>::write(
REDUCE, out_data + b * N * K + idx * K + k, vals[k],
Reducer<scalar_t, REDUCE>::write(
out_data + b * N * K + idx * K + k, vals[k],
arg_out_data + b * N * K + idx * K + k, args[k],
e + 1 - row_start);

Expand All @@ -121,7 +121,7 @@ segment_coo_cpu(torch::Tensor src, torch::Tensor index,
}
}
if (!optional_out.has_value() && (REDUCE == MIN || REDUCE == MAX))
out.masked_fill_(out == Reducer<scalar_t>::init(REDUCE), (scalar_t)0);
out.masked_fill_(out == Reducer<scalar_t, REDUCE>::init(), (scalar_t)0);

if (REDUCE == MEAN)
arg_out.value().clamp_(1);
Expand Down
12 changes: 6 additions & 6 deletions csrc/cpu/segment_csr_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,17 @@ segment_csr_cpu(torch::Tensor src, torch::Tensor indptr,

offset = (n / (indptr.size(-1) - 1)) * E * K;
for (auto k = 0; k < K; k++)
vals[k] = Reducer<scalar_t>::init(REDUCE);
vals[k] = Reducer<scalar_t, REDUCE>::init();

for (auto e = row_start; e < row_end; e++)
for (auto k = 0; k < K; k++)
Reducer<scalar_t>::update(
REDUCE, &vals[k], src_data[offset + e * K + k], &args[k], e);
Reducer<scalar_t, REDUCE>::update(
&vals[k], src_data[offset + e * K + k], &args[k], e);

for (auto k = 0; k < K; k++)
Reducer<scalar_t>::write(REDUCE, out_data + n * K + k, vals[k],
arg_out_data + n * K + k, args[k],
row_end - row_start);
Reducer<scalar_t, REDUCE>::write(out_data + n * K + k, vals[k],
arg_out_data + n * K + k, args[k],
row_end - row_start);
}
});
});
Expand Down