@@ -118,6 +118,15 @@ void ScatterAssign(const platform::DeviceContext& ctx, const Tensor& src,
118118
119119 for (int i = 0 ; i < index_size; ++i) {
120120 IndexT index_ = p_index[i];
121+
122+ PADDLE_ENFORCE_GE (index_, 0 ,
123+ platform::errors::OutOfRange (
124+ " The index is out of bounds, "
125+ " please check whether the dimensions of index and "
126+ " input meet the requirements. It should "
127+ " be greater than or equal to 0, but received [%d]" ,
128+ index_));
129+
121130 memcpy (p_output + index_ * slice_size, p_src + i * slice_size, slice_bytes);
122131 }
123132}
@@ -173,6 +182,15 @@ void ScatterAssignAdd(const framework::ExecutionContext& ctx, const Tensor& src,
173182 // if not in overwrite mode, need to init output data
174183 for (int i = 0 ; i < index_size; ++i) {
175184 const IndexT& index_ = p_index[i];
185+
186+ PADDLE_ENFORCE_GE (index_, 0 ,
187+ platform::errors::OutOfRange (
188+ " The index is out of bounds, "
189+ " please check whether the dimensions of index and "
190+ " input meet the requirements. It should "
191+ " be greater than or equal to 0, but received [%d]" ,
192+ index_));
193+
176194 elementwise_inner_add<T, IndexT>(ctx, p_src, p_output, result_p_output, src,
177195 output, i, index_, slice_size,
178196 slice_bytes);
@@ -233,6 +251,15 @@ void ScatterNdAdd(const framework::ExecutionContext& ctx, const Tensor& update,
233251 IndexT temp = 1 ;
234252 for (int64_t j = end_size - 1 ; j >= 0 ; --j) {
235253 IndexT index_value = p_index[i * end_size + j];
254+ PADDLE_ENFORCE_EQ (
255+ (index_value >= 0 && index_value < output_dims[j]), true ,
256+ platform::errors::OutOfRange (
257+ " The index is out of bounds, "
258+ " please check whether the dimensions of index and "
259+ " input meet the requirements. It should "
260+ " be less than [%d] and greater or equal to 0, but received [%d]" ,
261+ output_dims[j], index_value));
262+
236263 index_ += (index_value * temp);
237264 temp *= output_dims[j];
238265 }
0 commit comments