@@ -408,9 +408,9 @@ static void build_index_op(
408
408
config.set_check_mem_overlap (false )
409
409
.check_all_same_dtype (false )
410
410
.add_output (result)
411
- .add_owned_input (info.src );
411
+ .add_owned_const_input (info.src );
412
412
for (auto & index : info.indices ) {
413
- config.add_owned_input (index );
413
+ config.add_owned_const_input (index );
414
414
}
415
415
if (!result.defined ()) {
416
416
config.declare_static_dtype_and_device (info.src .scalar_type (), info.src .device ());
@@ -614,9 +614,9 @@ static TensorIterator make_index_put_iterator(const AdvancedIndex& info, const T
614
614
config.resize_outputs (false );
615
615
config.check_all_same_dtype (false );
616
616
config.add_output (info.src );
617
- config.add_input (value);
617
+ config.add_const_input (value);
618
618
for (auto & index : info.indices ) {
619
- config.add_input (index );
619
+ config.add_const_input (index );
620
620
}
621
621
return config.build ();
622
622
}
@@ -689,8 +689,8 @@ Tensor & put_(Tensor & self, const Tensor& index, const Tensor & source, const b
689
689
auto iter = TensorIteratorConfig ()
690
690
.set_check_mem_overlap (false )
691
691
.check_all_same_dtype (false )
692
- .add_input (source)
693
- .add_input (index_reshaped)
692
+ .add_const_input (source)
693
+ .add_const_input (index_reshaped)
694
694
.build ();
695
695
696
696
put_stub (iter.device_type (), iter, self, accumulate);
@@ -769,7 +769,7 @@ Tensor& take_out(const Tensor& self, const Tensor& index, Tensor& out) {
769
769
.set_check_mem_overlap (false )
770
770
.check_all_same_dtype (false )
771
771
.add_output (out)
772
- .add_input (index )
772
+ .add_const_input (index )
773
773
.build ();
774
774
775
775
// Early return after out has been resized
@@ -848,8 +848,8 @@ TORCH_IMPL_FUNC(index_copy_out)
848
848
.check_all_same_dtype (false )
849
849
.resize_outputs (false )
850
850
.add_output (result_restrided)
851
- .add_input (index_restrided)
852
- .add_input (source_nonzero)
851
+ .add_const_input (index_restrided)
852
+ .add_const_input (source_nonzero)
853
853
.build ();
854
854
855
855
auto result_dim_size = result_nonzero.size (dim);
@@ -943,15 +943,15 @@ TORCH_IMPL_FUNC(index_add_cpu_out)
943
943
auto iter = TensorIterator::borrowing_binary_op (selfSlice, selfSlice, sourceSlice);
944
944
945
945
AT_DISPATCH_INDEX_TYPES (index .scalar_type (), " index_add_cpu_" , [&] () {
946
- auto index_data = index_contig.data_ptr <index_t >();
946
+ auto index_data = index_contig.const_data_ptr <index_t >();
947
947
for (const auto i : c10::irange (numel)) {
948
948
auto self_i = index_data[i];
949
949
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_dim_size), " index out of range in self" );
950
950
auto self_data = static_cast <char *>(selfSlice.data_ptr ()) + self_i * self_stride_bytes;
951
- auto source_data = static_cast <char *>(sourceSlice.data_ptr ()) + i * source_stride_bytes;
951
+ auto source_data = static_cast <const char *>(sourceSlice.const_data_ptr ()) + i * source_stride_bytes;
952
952
iter.unsafe_replace_operand (0 , self_data);
953
953
iter.unsafe_replace_operand (1 , self_data);
954
- iter.unsafe_replace_operand (2 , source_data);
954
+ iter.unsafe_replace_operand (2 , const_cast < char *>( source_data) );
955
955
add_stub (iter.device_type (), iter, alpha);
956
956
}
957
957
});
@@ -967,10 +967,10 @@ TORCH_IMPL_FUNC(index_add_cpu_out)
967
967
auto source_stride = source.dim () == 0 ? 1 : source.stride (dim);
968
968
// TODO: Maybe TensorAccessor can be used here?
969
969
auto * result_ptr = result.data_ptr <scalar_t >();
970
- auto * source_ptr = source.data_ptr <scalar_t >();
970
+ auto * source_ptr = source.const_data_ptr <scalar_t >();
971
971
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_add_cpu_" ,
972
972
[&index_contig, &numel, &result, &result_ptr, &result_stride, &source_ptr, &source_stride, &alpha_value] {
973
- auto index_data = index_contig.data_ptr <index_t >();
973
+ auto index_data = index_contig.const_data_ptr <index_t >();
974
974
for (const auto i : c10::irange (numel)) {
975
975
auto self_i = index_data[i];
976
976
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < result.numel ()), " index out of range in self" );
@@ -1040,15 +1040,15 @@ static void index_reduce_func_impl(
1040
1040
auto iter = TensorIterator::borrowing_binary_op (selfSlice, selfSlice, sourceSlice);
1041
1041
1042
1042
AT_DISPATCH_INDEX_TYPES (index .scalar_type (), " index_func_cpu_" , [&] () {
1043
- auto index_data = index_contig.data_ptr <index_t >();
1043
+ auto index_data = index_contig.const_data_ptr <index_t >();
1044
1044
for (const auto i : c10::irange (numel)) {
1045
1045
auto self_i = index_data[i];
1046
1046
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_dim_size), " index out of range in self" );
1047
1047
auto self_data = static_cast <char *>(selfSlice.data_ptr ()) + self_i * self_stride_bytes;
1048
- auto source_data = static_cast <char *>(sourceSlice.data_ptr ()) + i * source_stride_bytes;
1048
+ auto source_data = static_cast <const char *>(sourceSlice.const_data_ptr ()) + i * source_stride_bytes;
1049
1049
iter.unsafe_replace_operand (0 , self_data);
1050
1050
iter.unsafe_replace_operand (1 , self_data);
1051
- iter.unsafe_replace_operand (2 , source_data);
1051
+ iter.unsafe_replace_operand (2 , const_cast < char *>( source_data) );
1052
1052
1053
1053
switch (op) {
1054
1054
case ReductionType::PROD :
@@ -1090,11 +1090,11 @@ static void index_reduce_func_impl(
1090
1090
auto counts_stride = counts.dim () == 0 ? 1 : counts.stride (dim);
1091
1091
// TODO: Maybe TensorAccessor can be used here?
1092
1092
auto * result_ptr = result.data_ptr <scalar_t >();
1093
- auto * source_ptr = source.data_ptr <scalar_t >();
1093
+ auto * source_ptr = source.const_data_ptr <scalar_t >();
1094
1094
auto counts_ptr = counts.data_ptr <scalar_t >();
1095
1095
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_func_cpu_" ,
1096
1096
[&index_contig, &numel, &result, &result_ptr, &result_stride, &source_ptr, &source_stride, &op, &counts_ptr, &counts_stride] {
1097
- auto index_data = index_contig.data_ptr <index_t >();
1097
+ auto index_data = index_contig.const_data_ptr <index_t >();
1098
1098
for (const auto i : c10::irange (numel)) {
1099
1099
auto self_i = index_data[i];
1100
1100
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < result.numel ()), " index out of range in self" );
@@ -1175,7 +1175,7 @@ static Tensor & index_select_out_cpu_dim1_(
1175
1175
1176
1176
auto out = static_cast <char *>(result_contig.data_ptr ());
1177
1177
1178
- auto src_base = static_cast <const char *>(self_contig.data_ptr ());
1178
+ auto src_base = static_cast <const char *>(self_contig.const_data_ptr ());
1179
1179
1180
1180
auto self_sizes = self_contig.sizes ();
1181
1181
auto outer_dims_product = c10::size_to_dim_ (1 , self_sizes);
@@ -1191,7 +1191,7 @@ static Tensor & index_select_out_cpu_dim1_(
1191
1191
AT_DISPATCH_INDEX_TYPES (
1192
1192
index_contig.scalar_type (), " batch_index_select_compute" , [&]() {
1193
1193
1194
- const auto * idxs = index_contig.data_ptr <index_t >();
1194
+ const auto * idxs = index_contig.const_data_ptr <index_t >();
1195
1195
check_indexarray_range<index_t >(idxs, N, src_indexing_axis_dim);
1196
1196
1197
1197
// Special-case single-float copy for efficiency
@@ -1256,7 +1256,7 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1256
1256
" index_select(): self indexing axis dim should be positive" );
1257
1257
AT_DISPATCH_INDEX_TYPES (
1258
1258
index_contig.scalar_type (), " index_select_empty_self_bound_check" , [&]() {
1259
- const auto * idxs = index_contig.data_ptr <index_t >();
1259
+ const auto * idxs = index_contig.const_data_ptr <index_t >();
1260
1260
check_indexarray_range<index_t >(idxs, numel, src_indexing_axis_dim);
1261
1261
});
1262
1262
return result;
@@ -1280,7 +1280,7 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1280
1280
.check_all_same_dtype (false )
1281
1281
.resize_outputs (false )
1282
1282
.add_output (resultSlice)
1283
- .add_input (selfSlice)
1283
+ .add_const_input (selfSlice)
1284
1284
.build ();
1285
1285
1286
1286
auto grain_size = at::internal::GRAIN_SIZE;
@@ -1293,7 +1293,7 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1293
1293
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_select_out_cpu_" ,
1294
1294
[&index_contig, &start, &end, &sub_iter, &self_dim_size, &selfSlice_data, &self_stride_bytes,
1295
1295
&resultSlice_data, &result_stride_bytes] () {
1296
- auto index_data = index_contig.data_ptr <index_t >();
1296
+ auto index_data = index_contig.const_data_ptr <index_t >();
1297
1297
for (const auto i : c10::irange (start, end)) {
1298
1298
auto self_i = index_data[i];
1299
1299
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_dim_size), " index out of range in self" );
@@ -1322,7 +1322,7 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1322
1322
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_select_out_cpu_" ,
1323
1323
[&index_contig, &slice_size_bytes, &self_dim_size, &selfSlice_data,
1324
1324
&self_stride_bytes, &resultSlice_data, &result_stride_bytes, &start, &end] () {
1325
- auto index_data = index_contig.data_ptr <index_t >();
1325
+ auto index_data = index_contig.const_data_ptr <index_t >();
1326
1326
for (const auto i : c10::irange (start, end)) {
1327
1327
auto self_i = index_data[i];
1328
1328
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_dim_size), " index out of range in self" );
@@ -1344,16 +1344,16 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1344
1344
AT_DISPATCH_QINT_TYPES (self.scalar_type (), " index_select_quant" , [&index_contig, &self, &result, &dim, &numel] {
1345
1345
auto self_stride = self.dim () == 0 ? 1 : self.stride (dim);
1346
1346
auto result_stride = result.dim () == 0 ? 1 : result.stride (dim);
1347
- auto self_data_ptr = self.data_ptr <scalar_t >();
1347
+ auto self_data_ptr = self.const_data_ptr <scalar_t >();
1348
1348
auto result_data_ptr = result.data_ptr <scalar_t >();
1349
1349
auto self_numel = self.numel ();
1350
1350
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_select_out_cpu_quant_" ,
1351
1351
[&index_contig, &numel, &self_numel, &self_data_ptr, &self_stride, &result_data_ptr, &result_stride] {
1352
- auto index_data = index_contig.data_ptr <index_t >();
1352
+ auto index_data = index_contig.const_data_ptr <index_t >();
1353
1353
for (const auto i : c10::irange (numel)) {
1354
1354
auto self_i = index_data[i];
1355
1355
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_numel), " index out of range in self" );
1356
- scalar_t *self_ip = self_data_ptr + self_i * self_stride;
1356
+ const scalar_t *self_ip = self_data_ptr + self_i * self_stride;
1357
1357
*(result_data_ptr + i * result_stride) = *self_ip;
1358
1358
}
1359
1359
});
@@ -1364,16 +1364,16 @@ Tensor & index_select_out_cpu_(const Tensor & self, int64_t dim, const Tensor &
1364
1364
auto self_stride = self.dim () == 0 ? 1 : self.stride (dim);
1365
1365
auto result_stride = result.dim () == 0 ? 1 : result.stride (dim);
1366
1366
1367
- auto self_data_ptr = self.data_ptr <scalar_t >();
1367
+ auto self_data_ptr = self.const_data_ptr <scalar_t >();
1368
1368
auto result_data_ptr = result.data_ptr <scalar_t >();
1369
1369
auto self_numel = self.numel ();
1370
1370
AT_DISPATCH_INDEX_TYPES (index_contig.scalar_type (), " index_select_out_cpu_" ,
1371
1371
[&index_contig, &numel, &self_numel, &self_data_ptr, &self_stride, &result_data_ptr, &result_stride] {
1372
- auto index_data = index_contig.data_ptr <index_t >();
1372
+ auto index_data = index_contig.const_data_ptr <index_t >();
1373
1373
for (const auto i : c10::irange (numel)) {
1374
1374
auto self_i = index_data[i];
1375
1375
TORCH_CHECK_INDEX ((self_i >= 0 ) && (self_i < self_numel), " index out of range in self" );
1376
- scalar_t *self_ip = self_data_ptr + self_i * self_stride;
1376
+ const scalar_t *self_ip = self_data_ptr + self_i * self_stride;
1377
1377
*(result_data_ptr + i * result_stride) = *self_ip;
1378
1378
}
1379
1379
});
@@ -1462,7 +1462,7 @@ Tensor & index_fill_(Tensor & self, int64_t dim, const Tensor & index, const Sca
1462
1462
.check_all_same_dtype (false )
1463
1463
.resize_outputs (false )
1464
1464
.add_output (self_restrided)
1465
- .add_input (index_restrided)
1465
+ .add_const_input (index_restrided)
1466
1466
.build ();
1467
1467
1468
1468
auto self_dim_size = (self_nonzero_dim.sizes ())[dim];
@@ -1924,7 +1924,7 @@ static Tensor & masked_fill_impl_cpu(Tensor & self, const Tensor & mask, const S
1924
1924
.check_all_same_dtype (false )
1925
1925
.resize_outputs (false )
1926
1926
.add_output (self)
1927
- .add_input (mask)
1927
+ .add_const_input (mask)
1928
1928
.build ();
1929
1929
1930
1930
masked_fill_stub (iter.device_type (), iter, value);
@@ -2017,8 +2017,8 @@ static Tensor & masked_select_out_impl_cpu(Tensor & result, const Tensor & self,
2017
2017
.check_all_same_dtype (false )
2018
2018
.resize_outputs (false )
2019
2019
.add_output (result_strided)
2020
- .add_input (*_self)
2021
- .add_input (*_mask)
2020
+ .add_const_input (*_self)
2021
+ .add_const_input (*_mask)
2022
2022
.build ();
2023
2023
2024
2024
masked_select_serial_stub (iter.device_type (), iter, orig_stride);
@@ -2041,9 +2041,9 @@ static Tensor & masked_select_out_impl_cpu(Tensor & result, const Tensor & self,
2041
2041
.check_all_same_dtype (false )
2042
2042
.resize_outputs (false )
2043
2043
.add_output (result_strided)
2044
- .add_input (*_self)
2045
- .add_input (*_mask)
2046
- .add_input (mask_prefix_sum)
2044
+ .add_const_input (*_self)
2045
+ .add_const_input (*_mask)
2046
+ .add_const_input (mask_prefix_sum)
2047
2047
.build ();
2048
2048
2049
2049
masked_select_stub (iter.device_type (), iter, orig_stride);
@@ -2228,7 +2228,7 @@ Tensor count_nonzero_cpu(const Tensor& self, IntArrayRef dims){
2228
2228
2229
2229
// Optimized all-reduce
2230
2230
auto iter = TensorIteratorConfig ()
2231
- .add_input (self)
2231
+ .add_const_input (self)
2232
2232
.build ();
2233
2233
2234
2234
const auto num_threads = at::get_num_threads ();
@@ -2267,7 +2267,7 @@ Tensor& nonzero_out_cpu(const Tensor& self, Tensor& result) {
2267
2267
at::assert_no_overlap (result, self);
2268
2268
2269
2269
auto iter = TensorIteratorConfig ()
2270
- .add_input (self)
2270
+ .add_const_input (self)
2271
2271
.enforce_linear_iteration ()
2272
2272
.build ();
2273
2273
@@ -2495,7 +2495,7 @@ Tensor & masked_scatter__cpu(Tensor& self, const Tensor & mask, const Tensor & s
2495
2495
// order of indexing matters
2496
2496
.enforce_linear_iteration ()
2497
2497
.add_output (self)
2498
- .add_input (*b_mask)
2498
+ .add_const_input (*b_mask)
2499
2499
.build ();
2500
2500
2501
2501
masked_scatter_stub (iter.device_type (), iter, src_cont);
0 commit comments