Skip to content

Commit 068f7e7

Browse files
r-barnespytorchmergebot
authored andcommitted
torch::optional -> std::optional (pytorch#138987)
Pull Request resolved: pytorch#138987 Approved by: https://github.com/Skylion007
1 parent 228963a commit 068f7e7

File tree

12 files changed

+64
-64
lines changed

12 files changed

+64
-64
lines changed

test/cpp/api/dataloader.cpp

+38-38
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct DummyDataset : datasets::Dataset<DummyDataset, int> {
3333
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
3434
return 1 + index;
3535
}
36-
torch::optional<size_t> size() const override {
36+
std::optional<size_t> size() const override {
3737
return size_;
3838
}
3939

@@ -151,8 +151,8 @@ struct InfiniteStreamDataset
151151
return batch;
152152
}
153153

154-
torch::optional<size_t> size() const override {
155-
return torch::nullopt;
154+
std::optional<size_t> size() const override {
155+
return std::nullopt;
156156
}
157157

158158
size_t counter = 0;
@@ -459,7 +459,7 @@ TEST(DataTest, StackTransformWorksForExample) {
459459
return {tensor[index], 1 + tensor[index]};
460460
}
461461

462-
torch::optional<size_t> size() const override {
462+
std::optional<size_t> size() const override {
463463
return tensor.size(0);
464464
}
465465

@@ -503,7 +503,7 @@ struct TensorStringDataset
503503
return {torch::tensor(static_cast<double>(index)), std::to_string(index)};
504504
}
505505

506-
torch::optional<size_t> size() const override {
506+
std::optional<size_t> size() const override {
507507
return 100;
508508
}
509509
};
@@ -542,7 +542,7 @@ struct DummyTensorDataset
542542
return {tensor, static_cast<int>(channels)};
543543
}
544544

545-
torch::optional<size_t> size() const override {
545+
std::optional<size_t> size() const override {
546546
return 100;
547547
}
548548
};
@@ -624,7 +624,7 @@ struct UnCopyableDataset : public datasets::Dataset<UnCopyableDataset> {
624624
torch::tensor({static_cast<int64_t>(index)})};
625625
}
626626

627-
torch::optional<size_t> size() const override {
627+
std::optional<size_t> size() const override {
628628
return 100;
629629
}
630630
};
@@ -753,7 +753,7 @@ struct UncopyableDataset : datasets::Dataset<UncopyableDataset, int> {
753753
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
754754
return 1 + index;
755755
}
756-
torch::optional<size_t> size() const override {
756+
std::optional<size_t> size() const override {
757757
return 100;
758758
}
759759
};
@@ -806,18 +806,18 @@ struct TestIndexDataset
806806
}
807807
return batch;
808808
}
809-
torch::optional<size_t> size() const override {
809+
std::optional<size_t> size() const override {
810810
return data.size();
811811
}
812812
std::vector<int> data;
813813
};
814814

815815
struct TestIndexSampler : public samplers::Sampler<TestIndex> {
816816
explicit TestIndexSampler(size_t size) : size_(size) {}
817-
void reset(torch::optional<size_t> new_size = torch::nullopt) override {}
818-
torch::optional<TestIndex> next(size_t batch_size) override {
817+
void reset(std::optional<size_t> new_size = std::nullopt) override {}
818+
std::optional<TestIndex> next(size_t batch_size) override {
819819
if (index_ >= size_) {
820-
return torch::nullopt;
820+
return std::nullopt;
821821
}
822822
std::vector<size_t> indices(batch_size);
823823
std::iota(indices.begin(), indices.end(), size_t(0));
@@ -847,7 +847,7 @@ TEST(DataTest, DistributedRandomSamplerSingleReplicaProduceCorrectSamples) {
847847
samplers::DistributedRandomSampler drs(sample_count);
848848

849849
std::vector<size_t> res;
850-
torch::optional<std::vector<size_t>> idx;
850+
std::optional<std::vector<size_t>> idx;
851851
while ((idx = drs.next(3)).has_value()) {
852852
res.insert(std::end(res), std::begin(*idx), std::end(*idx));
853853
}
@@ -879,7 +879,7 @@ TEST(DataTest, DistributedRandomSamplerMultiReplicaProduceCorrectSamples) {
879879
std::vector<size_t> res;
880880
for (const auto i : c10::irange(num_replicas)) {
881881
(*samplers[i]).reset();
882-
torch::optional<std::vector<size_t>> idx;
882+
std::optional<std::vector<size_t>> idx;
883883
while ((idx = (*samplers[i]).next(batch_size)).has_value()) {
884884
res.insert(std::end(res), std::begin(*idx), std::end(*idx));
885885
}
@@ -943,7 +943,7 @@ TEST(DataTest, DistributedSequentialSamplerSingleReplicaProduceCorrectSamples) {
943943
samplers::DistributedSequentialSampler dss(sample_count);
944944

945945
std::vector<size_t> res;
946-
torch::optional<std::vector<size_t>> idx;
946+
std::optional<std::vector<size_t>> idx;
947947
while ((idx = dss.next(batch_size)).has_value()) {
948948
res.insert(std::end(res), std::begin(*idx), std::end(*idx));
949949
}
@@ -976,7 +976,7 @@ TEST(DataTest, DistributedSequentialSamplerMultiReplicaProduceCorrectSamples) {
976976
std::vector<size_t> res;
977977
for (const auto i : c10::irange(num_replicas)) {
978978
(*samplers[i]).reset();
979-
torch::optional<std::vector<size_t>> idx;
979+
std::optional<std::vector<size_t>> idx;
980980
while ((idx = (*samplers[i]).next(batch_size)).has_value()) {
981981
res.insert(std::end(res), std::begin(*idx), std::end(*idx));
982982
}
@@ -1052,8 +1052,8 @@ struct UnsizedDataset : public datasets::Dataset<UnsizedDataset> {
10521052
torch::data::Example<> get(size_t i) override {
10531053
return {torch::ones(i), torch::ones(i)};
10541054
}
1055-
torch::optional<size_t> size() const noexcept override {
1056-
return torch::nullopt;
1055+
std::optional<size_t> size() const noexcept override {
1056+
return std::nullopt;
10571057
}
10581058
};
10591059

@@ -1150,7 +1150,7 @@ TEST(DataLoaderTest, CanUseIteratorAlgorithms) {
11501150
// NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions)
11511151
return 1 + indices.front();
11521152
}
1153-
torch::optional<size_t> size() const override {
1153+
std::optional<size_t> size() const override {
11541154
return 10;
11551155
}
11561156
};
@@ -1270,7 +1270,7 @@ TEST(DataLoaderTest, RespectsTimeout) {
12701270
baton->cv.wait_for(lock, 1000 * kMillisecond);
12711271
return 0;
12721272
}
1273-
torch::optional<size_t> size() const override {
1273+
std::optional<size_t> size() const override {
12741274
return 100;
12751275
}
12761276
std::shared_ptr<Baton> baton;
@@ -1388,7 +1388,7 @@ struct Dataset : datasets::BatchDataset<Dataset, size_t> {
13881388
return indices.front();
13891389
}
13901390

1391-
torch::optional<size_t> size() const override {
1391+
std::optional<size_t> size() const override {
13921392
return kNumberOfWorkers;
13931393
}
13941394

@@ -1441,7 +1441,7 @@ TEST(DataLoaderTest, TestExceptionsArePropagatedFromWorkers) {
14411441
int get(size_t index) override {
14421442
throw std::invalid_argument("badness");
14431443
}
1444-
torch::optional<size_t> size() const override {
1444+
std::optional<size_t> size() const override {
14451445
return 100;
14461446
}
14471447
};
@@ -1467,13 +1467,13 @@ TEST(DataLoaderTest, StatefulDatasetWithNoWorkers) {
14671467
const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10;
14681468

14691469
struct D : datasets::StatefulDataset<D, int, size_t> {
1470-
torch::optional<int> get_batch(size_t) override {
1470+
std::optional<int> get_batch(size_t) override {
14711471
if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) {
14721472
return counter++;
14731473
}
1474-
return torch::nullopt;
1474+
return std::nullopt;
14751475
}
1476-
torch::optional<size_t> size() const override {
1476+
std::optional<size_t> size() const override {
14771477
return 100;
14781478
}
14791479
void reset() override {
@@ -1504,14 +1504,14 @@ TEST(DataLoaderTest, StatefulDatasetWithManyWorkers) {
15041504
const int kNumberOfWorkers = 4;
15051505

15061506
struct D : datasets::StatefulDataset<D, int, size_t> {
1507-
torch::optional<int> get_batch(size_t) override {
1507+
std::optional<int> get_batch(size_t) override {
15081508
std::lock_guard<std::mutex> lock(mutex);
15091509
if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) {
15101510
return counter++;
15111511
}
1512-
return torch::nullopt;
1512+
return std::nullopt;
15131513
}
1514-
torch::optional<size_t> size() const override {
1514+
std::optional<size_t> size() const override {
15151515
return 100;
15161516
}
15171517
void reset() override {
@@ -1544,13 +1544,13 @@ TEST(DataLoaderTest, StatefulDatasetWithMap) {
15441544
const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10;
15451545

15461546
struct D : datasets::StatefulDataset<D, int, size_t> {
1547-
torch::optional<int> get_batch(size_t) override {
1547+
std::optional<int> get_batch(size_t) override {
15481548
if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) {
15491549
return counter++;
15501550
}
1551-
return torch::nullopt;
1551+
return std::nullopt;
15521552
}
1553-
torch::optional<size_t> size() const override {
1553+
std::optional<size_t> size() const override {
15541554
return 100;
15551555
}
15561556
void reset() override {
@@ -1587,7 +1587,7 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) {
15871587
const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10;
15881588

15891589
struct D : datasets::StatefulDataset<D> {
1590-
torch::optional<std::vector<Example<>>> get_batch(
1590+
std::optional<std::vector<Example<>>> get_batch(
15911591
size_t batch_size) override {
15921592
if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) {
15931593
counter += batch_size;
@@ -1597,9 +1597,9 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) {
15971597
torch::ones(batch_size + 1), torch::zeros(batch_size - 1)});
15981598
return batch;
15991599
}
1600-
return torch::nullopt;
1600+
return std::nullopt;
16011601
}
1602-
torch::optional<size_t> size() const override {
1602+
std::optional<size_t> size() const override {
16031603
return 100;
16041604
}
16051605
void reset() override {
@@ -1616,7 +1616,7 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) {
16161616

16171617
// Notice that the `get_batch()` of the dataset returns a vector<Example>, but
16181618
// the `Stack` collation stacks the tensors into one.
1619-
torch::optional<Example<>> batch = d.get_batch(kBatchSize);
1619+
std::optional<Example<>> batch = d.get_batch(kBatchSize);
16201620
ASSERT_TRUE(batch.has_value());
16211621
ASSERT_EQ(batch->data.size(0), kBatchSize);
16221622
ASSERT_EQ(batch->data.size(1), kBatchSize + 1);
@@ -2117,7 +2117,7 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
21172117
public:
21182118
explicit S(size_t size) : size_(size), index_(0){};
21192119

2120-
void reset(torch::optional<size_t> new_size = torch::nullopt) override {
2120+
void reset(std::optional<size_t> new_size = std::nullopt) override {
21212121
if (new_size.has_value()) {
21222122
size_ = *new_size;
21232123
}
@@ -2134,10 +2134,10 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) {
21342134
}
21352135

21362136
// Returns the next batch of indices.
2137-
torch::optional<std::vector<size_t>> next(size_t batch_size) override {
2137+
std::optional<std::vector<size_t>> next(size_t batch_size) override {
21382138
const auto remaining_indices = size_ - index_;
21392139
if (remaining_indices == 0) {
2140-
return torch::nullopt;
2140+
return std::nullopt;
21412141
}
21422142
auto return_size = std::min(batch_size, remaining_indices);
21432143
std::vector<size_t> index_batch(

test/cpp/api/functional.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,7 @@ TEST_F(FunctionalTest, Interpolate) {
23292329
auto tensor = torch::rand({2, 3, 32, 32});
23302330
std::vector<int64_t> osize = {8, 10};
23312331
auto expected =
2332-
at::native::_upsample_nearest_exact2d(tensor, osize, torch::nullopt);
2332+
at::native::_upsample_nearest_exact2d(tensor, osize, std::nullopt);
23332333

23342334
auto options = F::InterpolateFuncOptions()
23352335
.size(osize)
@@ -2342,8 +2342,8 @@ TEST_F(FunctionalTest, Interpolate) {
23422342
{
23432343
auto tensor = torch::rand({2, 3, 32, 32});
23442344
std::vector<int64_t> osize = {8, 10};
2345-
auto expected = at::native::_upsample_bilinear2d_aa(
2346-
tensor, osize, false, torch::nullopt);
2345+
auto expected =
2346+
at::native::_upsample_bilinear2d_aa(tensor, osize, false, std::nullopt);
23472347

23482348
auto options = F::InterpolateFuncOptions()
23492349
.size(osize)
@@ -2356,8 +2356,8 @@ TEST_F(FunctionalTest, Interpolate) {
23562356
{
23572357
auto tensor = torch::rand({2, 3, 32, 32});
23582358
std::vector<int64_t> osize = {8, 10};
2359-
auto expected = at::native::_upsample_bicubic2d_aa(
2360-
tensor, osize, false, torch::nullopt);
2359+
auto expected =
2360+
at::native::_upsample_bicubic2d_aa(tensor, osize, false, std::nullopt);
23612361

23622362
auto options = F::InterpolateFuncOptions()
23632363
.size(osize)

test/cpp/api/module.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ TEST_F(ModuleTest, CallingCloneOnModuleThatDoesNotOverrideCloneThrows) {
381381
TEST_F(ModuleTest, CallingCloneOnModuleThatDoesOverrideCloneDoesNotThrow) {
382382
struct Cloneable : Module {
383383
std::shared_ptr<Module> clone(
384-
const torch::optional<torch::Device>& device =
385-
torch::nullopt) const override {
384+
const std::optional<torch::Device>& device =
385+
std::nullopt) const override {
386386
return nullptr;
387387
}
388388
};

test/cpp/api/parallel.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ TEST_F(
190190
auto output = parallel::data_parallel(
191191
m,
192192
input,
193-
/*devices=*/torch::nullopt,
193+
/*devices=*/std::nullopt,
194194
/*output_device=*/torch::Device(torch::kCUDA, 1));
195195
ASSERT_TRUE(output.defined());
196196
ASSERT_TRUE(output.device().is_cuda());

test/cpp/api/rnn.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ TEST_F(RNNTest, UsePackedSequenceAsInput) {
750750
std::get<0>(rnn_output).data(), expected_output, 1e-05, 2e-04));
751751

752752
// Test passing optional argument to `LSTM::forward_with_packed_input`
753-
rnn_output = m->forward_with_packed_input(packed_input, torch::nullopt);
753+
rnn_output = m->forward_with_packed_input(packed_input, std::nullopt);
754754
ASSERT_TRUE(torch::allclose(
755755
std::get<0>(rnn_output).data(), expected_output, 1e-05, 2e-04));
756756
}

test/cpp/jit/test_lite_trainer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ struct DummyDataset : torch::data::datasets::Dataset<DummyDataset, int> {
317317
// NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions)
318318
return 1 + index;
319319
}
320-
torch::optional<size_t> size() const override {
320+
std::optional<size_t> size() const override {
321321
return size_;
322322
}
323323

torch/csrc/api/include/torch/data/datasets/chunk.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ template <
5050
class BatchDataBuffer {
5151
public:
5252
using UnwrappedBatchType = UnwrappedBatch;
53-
using BatchType = torch::optional<UnwrappedBatchType>;
53+
using BatchType = std::optional<UnwrappedBatchType>;
5454
using BatchRequestType = typename ExampleSampler::BatchRequestType;
5555

5656
BatchDataBuffer(
@@ -316,7 +316,7 @@ class ChunkDataset final
316316
typename ChunkReader::BatchType,
317317
size_t> {
318318
public:
319-
using BatchType = torch::optional<typename ChunkReader::BatchType>;
319+
using BatchType = std::optional<typename ChunkReader::BatchType>;
320320
using UnwrappedBatchType = typename ChunkReader::BatchType;
321321
using BatchRequestType = size_t;
322322
using ChunkSamplerType = ChunkSampler;
@@ -404,7 +404,7 @@ class ChunkDataset final
404404

405405
/// size is not used for chunk dataset.
406406
std::optional<size_t> size() const override {
407-
return torch::nullopt;
407+
return std::nullopt;
408408
}
409409

410410
// provide a references to chunk sampler. Used mainly in distributed data

torch/csrc/api/include/torch/data/datasets/map.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace torch::data::datasets {
1313
namespace detail {
1414
template <bool C, typename T>
15-
using optional_if_t = std::conditional_t<C, torch::optional<T>, T>;
15+
using optional_if_t = std::conditional_t<C, std::optional<T>, T>;
1616
} // namespace detail
1717

1818
/// A `MapDataset` is a dataset that applies a transform to a source dataset.

0 commit comments

Comments
 (0)