Skip to content
Merged
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
67 changes: 34 additions & 33 deletions aten/src/TH/generic/THTensorMath.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,39 @@
} \
}

// Used for `scatter` and `scatterAdd`
// Assumes TENSOR1 is real
// TENSOR2 is src
// TENSOR3 is index
// Tests:
// 1. index->size[d] <= src->size[d] for all d
// 2. index->size[d] <= real->size[d] for all d != dim
#define TH_TENSOR_DIM_APPLY3_SIZE_SCATTER(TENSOR1, TENSOR2, TENSOR3, DIMENSION) \
{ \
int shape_check_flag = 0; \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < TENSOR1->nDimension; TH_TENSOR_DIM_APPLY_i++) \
{ \
int64_t TENSOR3##_dim_size = TENSOR3->size[TH_TENSOR_DIM_APPLY_i]; \
if (TH_TENSOR_DIM_APPLY_i != DIMENSION) { \
if (TENSOR3##_dim_size > TENSOR1->size[TH_TENSOR_DIM_APPLY_i]) { \
shape_check_flag = 1; \
break; \
} \
} \
if (TENSOR3##_dim_size > TENSOR2->size[TH_TENSOR_DIM_APPLY_i]) { \
shape_check_flag = 1; \
break; \
} \
} \
if (shape_check_flag == 1) { \
THDescBuff T1buff = _THSizeDesc(TENSOR1->size, TENSOR1->nDimension); \
THDescBuff T2buff = _THSizeDesc(TENSOR2->size, TENSOR2->nDimension); \
THDescBuff T3buff = _THSizeDesc(TENSOR3->size, TENSOR3->nDimension); \
THError("Expected %s %s to be smaller size than %s %s and to be smaller than %s %s apart from dimension %d", \
#TENSOR3, T3buff.str, #TENSOR2, T2buff.str, #TENSOR1, T1buff.str, DIMENSION); \
} \
}

static inline real THTensor_(powOne)(real x, real y) {
#if defined(TH_REAL_IS_FLOAT)
return powf(x, y);
Expand Down Expand Up @@ -580,38 +613,6 @@ void THTensor_(scatter)(THTensor *tensor, int dim, THLongTensor *index, THTensor

elems_per_row = THLongTensor_size(index, dim);

// Assumes TENSOR1 is real
// TENSOR2 is src
// TENSOR3 is index
// Tests:
// 1. index->size[d] <= src->size[d] for all d
// 2. index->size[d] <= real->size[d] for all d != dim
#define TH_TENSOR_DIM_APPLY3_SIZE_SCATTER(TENSOR1, TENSOR2, TENSOR3, DIMENSION) \
{ \
int shape_check_flag = 0; \
for(TH_TENSOR_DIM_APPLY_i = 0; TH_TENSOR_DIM_APPLY_i < TENSOR1->nDimension; TH_TENSOR_DIM_APPLY_i++) \
{ \
int64_t TENSOR3##_dim_size = TENSOR3->size[TH_TENSOR_DIM_APPLY_i]; \
if (TH_TENSOR_DIM_APPLY_i != DIMENSION) { \
if (TENSOR3##_dim_size > TENSOR1->size[TH_TENSOR_DIM_APPLY_i]) { \
shape_check_flag = 1; \
break; \
} \
} \
if (TENSOR3##_dim_size > TENSOR2->size[TH_TENSOR_DIM_APPLY_i]) { \
shape_check_flag = 1; \
break; \
} \
} \
if (shape_check_flag == 1) { \
THDescBuff T1buff = _THSizeDesc(TENSOR1->size, TENSOR1->nDimension); \
THDescBuff T2buff = _THSizeDesc(TENSOR2->size, TENSOR2->nDimension); \
THDescBuff T3buff = _THSizeDesc(TENSOR3->size, TENSOR3->nDimension); \
THError("Expected %s %s to be smaller size than %s %s and to be smaller than %s %s apart from dimension %d", \
#TENSOR3, T3buff.str, #TENSOR2, T2buff.str, #TENSOR1, T1buff.str, DIMENSION); \
} \
}

TH_TENSOR_DIM_APPLY3(real, tensor, real, src, int64_t, index, dim,
TH_TENSOR_DIM_APPLY3_SIZE_SCATTER,
for (i = 0; i < elems_per_row; ++i)
Expand Down Expand Up @@ -639,7 +640,7 @@ void THTensor_(scatterAdd)(THTensor *tensor, int dim, THLongTensor *index, THTen
elems_per_row = THLongTensor_size(index, dim);

TH_TENSOR_DIM_APPLY3(real, tensor, real, src, int64_t, index, dim,
TH_TENSOR_DIM_APPLY3_SIZE_EQ_EXCEPT_DIM,
TH_TENSOR_DIM_APPLY3_SIZE_SCATTER,
for (i = 0; i < elems_per_row; ++i)
{
idx = *(index_data + i*index_stride);
Expand Down