5
5
#include < test/cpp/api/support.h>
6
6
7
7
#include < c10/util/ArrayRef.h>
8
+ #include < c10/util/irange.h>
8
9
#include < c10/util/tempfile.h>
9
10
10
11
#include < algorithm>
@@ -173,7 +174,7 @@ TEST(DataTest, InfiniteStreamDataset) {
173
174
for (auto & batch : *data_loader) {
174
175
ASSERT_LT (batch_index, 3 );
175
176
ASSERT_EQ (batch.size (), kBatchSize );
176
- for (size_t j = 0 ; j < kBatchSize ; ++j ) {
177
+ for (const auto j : c10::irange ( kBatchSize ) ) {
177
178
ASSERT_EQ (batch.at (j), 1 + (batch_index * kBatchSize ) + j);
178
179
}
179
180
batch_index += 1 ;
@@ -837,7 +838,7 @@ TEST(DataTest, CanUseCustomTypeAsIndexType) {
837
838
838
839
size_t i = 0 ;
839
840
for (auto batch : *data_loader) {
840
- for (int j = 0 ; j < kBatchSize ; ++j ) {
841
+ for (const auto j : c10::irange ( kBatchSize ) ) {
841
842
ASSERT_EQ (batch.at (j), 10 + j);
842
843
}
843
844
i += 1 ;
@@ -857,7 +858,7 @@ TEST(DataTest, DistributedRandomSamplerSingleReplicaProduceCorrectSamples) {
857
858
ASSERT_EQ (res.size (), sample_count);
858
859
859
860
std::sort (res.begin (), res.end ());
860
- for (size_t i = 0 ; i < res.size (); ++i ) {
861
+ for (const auto i : c10::irange ( res.size ()) ) {
861
862
ASSERT_EQ (res[i], i);
862
863
}
863
864
}
@@ -872,14 +873,14 @@ TEST(DataTest, DistributedRandomSamplerMultiReplicaProduceCorrectSamples) {
872
873
size_t batch_size) {
873
874
std::vector<std::unique_ptr<samplers::DistributedRandomSampler>> samplers;
874
875
875
- for (size_t i = 0 ; i < num_replicas; ++i ) {
876
+ for (const auto i : c10::irange ( num_replicas) ) {
876
877
samplers.emplace_back (
877
878
torch::make_unique<samplers::DistributedRandomSampler>(
878
879
sample_count, num_replicas, i, allow_duplicates));
879
880
}
880
881
881
882
std::vector<size_t > res;
882
- for (size_t i = 0 ; i < num_replicas; ++i ) {
883
+ for (const auto i : c10::irange ( num_replicas) ) {
883
884
(*samplers[i]).reset ();
884
885
torch::optional<std::vector<size_t >> idx;
885
886
while ((idx = (*samplers[i]).next (batch_size)).has_value ()) {
@@ -953,7 +954,7 @@ TEST(DataTest, DistributedSequentialSamplerSingleReplicaProduceCorrectSamples) {
953
954
ASSERT_EQ (res.size (), sample_count);
954
955
955
956
std::sort (res.begin (), res.end ());
956
- for (size_t i = 0 ; i < res.size (); ++i ) {
957
+ for (const auto i : c10::irange ( res.size ()) ) {
957
958
ASSERT_EQ (res[i], i);
958
959
}
959
960
}
@@ -969,14 +970,14 @@ TEST(DataTest, DistributedSequentialSamplerMultiReplicaProduceCorrectSamples) {
969
970
std::vector<std::unique_ptr<samplers::DistributedSequentialSampler>>
970
971
samplers;
971
972
972
- for (size_t i = 0 ; i < num_replicas; ++i ) {
973
+ for (const auto i : c10::irange ( num_replicas) ) {
973
974
samplers.emplace_back (
974
975
torch::make_unique<samplers::DistributedSequentialSampler>(
975
976
sample_count, num_replicas, i, allow_duplicates));
976
977
}
977
978
978
979
std::vector<size_t > res;
979
- for (size_t i = 0 ; i < num_replicas; ++i ) {
980
+ for (const auto i : c10::irange ( num_replicas) ) {
980
981
(*samplers[i]).reset ();
981
982
torch::optional<std::vector<size_t >> idx;
982
983
while ((idx = (*samplers[i]).next (batch_size)).has_value ()) {
@@ -1490,7 +1491,7 @@ TEST(DataLoaderTest, StatefulDatasetWithNoWorkers) {
1490
1491
1491
1492
auto data_loader = torch::data::make_data_loader (D{});
1492
1493
1493
- for (size_t i = 0 ; i < 10 ; ++i ) {
1494
+ for (const auto i : c10::irange ( 10 ) ) {
1494
1495
const auto number_of_iterations =
1495
1496
std::distance (data_loader->begin (), data_loader->end ());
1496
1497
ASSERT_EQ (
@@ -1531,7 +1532,7 @@ TEST(DataLoaderTest, StatefulDatasetWithManyWorkers) {
1531
1532
torch::data::datasets::make_shared_dataset<D>(),
1532
1533
DataLoaderOptions ().workers (kNumberOfWorkers ));
1533
1534
1534
- for (size_t i = 0 ; i < 10 ; ++i ) {
1535
+ for (const auto i : c10::irange ( 10 ) ) {
1535
1536
const auto number_of_iterations =
1536
1537
std::distance (data_loader->begin (), data_loader->end ());
1537
1538
ASSERT_EQ (
@@ -1574,7 +1575,7 @@ TEST(DataLoaderTest, StatefulDatasetWithMap) {
1574
1575
})),
1575
1576
DataLoaderOptions{});
1576
1577
1577
- for (size_t i = 0 ; i < 10 ; ++i ) {
1578
+ for (const auto i : c10::irange ( 10 ) ) {
1578
1579
const auto number_of_iterations =
1579
1580
std::distance (data_loader->begin (), data_loader->end ());
1580
1581
ASSERT_EQ (
@@ -1675,7 +1676,8 @@ TEST(DataLoaderTest, ChunkDataSetGetBatch) {
1675
1676
dataset,
1676
1677
DataLoaderOptions (batch_size).workers (dataloader_worker_count));
1677
1678
1678
- for (int epoch_index = 0 ; epoch_index < epoch_count; ++epoch_index) {
1679
+ for (const auto epoch_index : c10::irange (epoch_count)) {
1680
+ (void )epoch_index; // Suppress unused variable warning
1679
1681
std::vector<bool > result (total_example_count, false );
1680
1682
int iteration_count = 0 ;
1681
1683
for (auto iterator = data_loader->begin ();
@@ -1687,11 +1689,11 @@ TEST(DataLoaderTest, ChunkDataSetGetBatch) {
1687
1689
// When prefetch_count is equal to 1 and no worker thread, the batch
1688
1690
// order is deterministic. So we can verify elements in each batch.
1689
1691
if (prefetch_count == 1 && dataloader_worker_count == 0 ) {
1690
- for (size_t j = 0 ; j < batch_size; ++j ) {
1692
+ for (const auto j : c10::irange ( batch_size) ) {
1691
1693
ASSERT_EQ (batch[j], iteration_count * batch_size + j);
1692
1694
}
1693
1695
}
1694
- for (size_t j = 0 ; j < batch_size; ++j ) {
1696
+ for (const auto j : c10::irange ( batch_size) ) {
1695
1697
result[batch[j]] = true ;
1696
1698
}
1697
1699
}
@@ -1978,7 +1980,8 @@ TEST(DataLoaderTest, ChunkDatasetSave) {
1978
1980
dataset,
1979
1981
DataLoaderOptions (batch_size).workers (dataloader_worker_count));
1980
1982
1981
- for (int epoch_index = 0 ; epoch_index < epoch_count; ++epoch_index) {
1983
+ for (const auto epoch_index : c10::irange (epoch_count)) {
1984
+ (void )epoch_index; // Suppress unused variable warning
1982
1985
int iteration_count = 0 ;
1983
1986
for (auto iterator = data_loader->begin (); iterator != data_loader->end ();
1984
1987
++iterator, ++iteration_count) {
@@ -2079,7 +2082,7 @@ TEST(DataLoaderTest, ChunkDatasetLoad) {
2079
2082
auto data_loader = torch::data::make_data_loader (
2080
2083
dataset, DataLoaderOptions (batch_size).workers (dataloader_worker_count));
2081
2084
2082
- for (int epoch_index = 0 ; epoch_index < epoch_count; ++epoch_index ) {
2085
+ for (const auto epoch_index : c10::irange ( epoch_count) ) {
2083
2086
int iteration_count = 0 ;
2084
2087
2085
2088
// For the first epoch, the returned batch should be returned from the
@@ -2128,7 +2131,7 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
2128
2131
size_t index = 0 ;
2129
2132
2130
2133
// Repeatly sample every 5 indices.
2131
- for (size_t i = 0 ; i < batch_size; ++i ) {
2134
+ for (const auto i : c10::irange ( batch_size) ) {
2132
2135
for (size_t j = 0 ; j < size_ / batch_size; ++j) {
2133
2136
indices_[index ++] = i + batch_size * j;
2134
2137
}
@@ -2222,11 +2225,11 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
2222
2225
// construct expected result
2223
2226
int offset = 0 ;
2224
2227
2225
- for (int i = 0 ; i < (chunk_count + cross_chunk_shuffle_count - 1 ) /
2226
- cross_chunk_shuffle_count;
2227
- i++ ) {
2228
- for ( int j = 0 ; j < chunk_size; ++j) {
2229
- for (int k = 0 ; k < cross_chunk_shuffle_count; ++k ) {
2228
+ for (const auto i : c10::irange ( (chunk_count + cross_chunk_shuffle_count - 1 ) /
2229
+ cross_chunk_shuffle_count)) {
2230
+ for ( const auto j : c10::irange (chunk_size) ) {
2231
+ ( void )j; // Suppress unused variable warning
2232
+ for (const auto k : c10::irange ( cross_chunk_shuffle_count) ) {
2230
2233
if (i * cross_chunk_shuffle_count + k < chunk_count) {
2231
2234
expected_result.push_back (i * cross_chunk_shuffle_count + k);
2232
2235
}
0 commit comments