From 068f7e7a78e25b960d17a27fc5e7c3bcc0d724e1 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Mon, 28 Oct 2024 19:09:43 +0000 Subject: [PATCH] torch::optional -> std::optional (#138987) Pull Request resolved: https://github.com/pytorch/pytorch/pull/138987 Approved by: https://github.com/Skylion007 --- test/cpp/api/dataloader.cpp | 76 +++++++++---------- test/cpp/api/functional.cpp | 10 +-- test/cpp/api/module.cpp | 4 +- test/cpp/api/parallel.cpp | 2 +- test/cpp/api/rnn.cpp | 2 +- test/cpp/jit/test_lite_trainer.cpp | 2 +- .../api/include/torch/data/datasets/chunk.h | 6 +- .../api/include/torch/data/datasets/map.h | 2 +- torch/csrc/api/include/torch/nn/modules/rnn.h | 12 +-- torch/csrc/api/include/torch/nn/utils/rnn.h | 2 +- torch/csrc/api/src/nn/modules/rnn.cpp | 8 +- torchgen/executorch/api/et_cpp.py | 2 +- 12 files changed, 64 insertions(+), 64 deletions(-) diff --git a/test/cpp/api/dataloader.cpp b/test/cpp/api/dataloader.cpp index e592f10df8d..0b50e898901 100644 --- a/test/cpp/api/dataloader.cpp +++ b/test/cpp/api/dataloader.cpp @@ -33,7 +33,7 @@ struct DummyDataset : datasets::Dataset { // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) return 1 + index; } - torch::optional size() const override { + std::optional size() const override { return size_; } @@ -151,8 +151,8 @@ struct InfiniteStreamDataset return batch; } - torch::optional size() const override { - return torch::nullopt; + std::optional size() const override { + return std::nullopt; } size_t counter = 0; @@ -459,7 +459,7 @@ TEST(DataTest, StackTransformWorksForExample) { return {tensor[index], 1 + tensor[index]}; } - torch::optional size() const override { + std::optional size() const override { return tensor.size(0); } @@ -503,7 +503,7 @@ struct TensorStringDataset return {torch::tensor(static_cast(index)), std::to_string(index)}; } - torch::optional size() const override { + std::optional size() const override { return 100; } }; @@ -542,7 +542,7 @@ struct DummyTensorDataset return {tensor, static_cast(channels)}; } - torch::optional size() const override { + std::optional size() const override { return 100; } }; @@ -624,7 +624,7 @@ struct UnCopyableDataset : public datasets::Dataset { torch::tensor({static_cast(index)})}; } - torch::optional size() const override { + std::optional size() const override { return 100; } }; @@ -753,7 +753,7 @@ struct UncopyableDataset : datasets::Dataset { // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) return 1 + index; } - torch::optional size() const override { + std::optional size() const override { return 100; } }; @@ -806,7 +806,7 @@ struct TestIndexDataset } return batch; } - torch::optional size() const override { + std::optional size() const override { return data.size(); } std::vector data; @@ -814,10 +814,10 @@ struct TestIndexDataset struct TestIndexSampler : public samplers::Sampler { explicit TestIndexSampler(size_t size) : size_(size) {} - void reset(torch::optional new_size = torch::nullopt) override {} - torch::optional next(size_t batch_size) override { + void reset(std::optional new_size = std::nullopt) override {} + std::optional next(size_t batch_size) override { if (index_ >= size_) { - return torch::nullopt; + return std::nullopt; } std::vector indices(batch_size); std::iota(indices.begin(), indices.end(), size_t(0)); @@ -847,7 +847,7 @@ TEST(DataTest, DistributedRandomSamplerSingleReplicaProduceCorrectSamples) { samplers::DistributedRandomSampler drs(sample_count); std::vector res; - torch::optional> idx; + std::optional> idx; while ((idx = drs.next(3)).has_value()) { res.insert(std::end(res), std::begin(*idx), std::end(*idx)); } @@ -879,7 +879,7 @@ TEST(DataTest, DistributedRandomSamplerMultiReplicaProduceCorrectSamples) { std::vector res; for (const auto i : c10::irange(num_replicas)) { (*samplers[i]).reset(); - torch::optional> idx; + std::optional> idx; while ((idx = (*samplers[i]).next(batch_size)).has_value()) { res.insert(std::end(res), std::begin(*idx), std::end(*idx)); } @@ -943,7 +943,7 @@ TEST(DataTest, DistributedSequentialSamplerSingleReplicaProduceCorrectSamples) { samplers::DistributedSequentialSampler dss(sample_count); std::vector res; - torch::optional> idx; + std::optional> idx; while ((idx = dss.next(batch_size)).has_value()) { res.insert(std::end(res), std::begin(*idx), std::end(*idx)); } @@ -976,7 +976,7 @@ TEST(DataTest, DistributedSequentialSamplerMultiReplicaProduceCorrectSamples) { std::vector res; for (const auto i : c10::irange(num_replicas)) { (*samplers[i]).reset(); - torch::optional> idx; + std::optional> idx; while ((idx = (*samplers[i]).next(batch_size)).has_value()) { res.insert(std::end(res), std::begin(*idx), std::end(*idx)); } @@ -1052,8 +1052,8 @@ struct UnsizedDataset : public datasets::Dataset { torch::data::Example<> get(size_t i) override { return {torch::ones(i), torch::ones(i)}; } - torch::optional size() const noexcept override { - return torch::nullopt; + std::optional size() const noexcept override { + return std::nullopt; } }; @@ -1150,7 +1150,7 @@ TEST(DataLoaderTest, CanUseIteratorAlgorithms) { // NOLINTNEXTLINE(cppcoreguidelines-narrowing-conversions,bugprone-narrowing-conversions) return 1 + indices.front(); } - torch::optional size() const override { + std::optional size() const override { return 10; } }; @@ -1270,7 +1270,7 @@ TEST(DataLoaderTest, RespectsTimeout) { baton->cv.wait_for(lock, 1000 * kMillisecond); return 0; } - torch::optional size() const override { + std::optional size() const override { return 100; } std::shared_ptr baton; @@ -1388,7 +1388,7 @@ struct Dataset : datasets::BatchDataset { return indices.front(); } - torch::optional size() const override { + std::optional size() const override { return kNumberOfWorkers; } @@ -1441,7 +1441,7 @@ TEST(DataLoaderTest, TestExceptionsArePropagatedFromWorkers) { int get(size_t index) override { throw std::invalid_argument("badness"); } - torch::optional size() const override { + std::optional size() const override { return 100; } }; @@ -1467,13 +1467,13 @@ TEST(DataLoaderTest, StatefulDatasetWithNoWorkers) { const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10; struct D : datasets::StatefulDataset { - torch::optional get_batch(size_t) override { + std::optional get_batch(size_t) override { if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) { return counter++; } - return torch::nullopt; + return std::nullopt; } - torch::optional size() const override { + std::optional size() const override { return 100; } void reset() override { @@ -1504,14 +1504,14 @@ TEST(DataLoaderTest, StatefulDatasetWithManyWorkers) { const int kNumberOfWorkers = 4; struct D : datasets::StatefulDataset { - torch::optional get_batch(size_t) override { + std::optional get_batch(size_t) override { std::lock_guard lock(mutex); if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) { return counter++; } - return torch::nullopt; + return std::nullopt; } - torch::optional size() const override { + std::optional size() const override { return 100; } void reset() override { @@ -1544,13 +1544,13 @@ TEST(DataLoaderTest, StatefulDatasetWithMap) { const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10; struct D : datasets::StatefulDataset { - torch::optional get_batch(size_t) override { + std::optional get_batch(size_t) override { if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) { return counter++; } - return torch::nullopt; + return std::nullopt; } - torch::optional size() const override { + std::optional size() const override { return 100; } void reset() override { @@ -1587,7 +1587,7 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) { const int kNumberOfExamplesAfterWhichTheDatasetExhausts = 10; struct D : datasets::StatefulDataset { - torch::optional>> get_batch( + std::optional>> get_batch( size_t batch_size) override { if (counter < kNumberOfExamplesAfterWhichTheDatasetExhausts) { counter += batch_size; @@ -1597,9 +1597,9 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) { torch::ones(batch_size + 1), torch::zeros(batch_size - 1)}); return batch; } - return torch::nullopt; + return std::nullopt; } - torch::optional size() const override { + std::optional size() const override { return 100; } void reset() override { @@ -1616,7 +1616,7 @@ TEST(DataLoaderTest, StatefulDatasetWithCollate) { // Notice that the `get_batch()` of the dataset returns a vector, but // the `Stack` collation stacks the tensors into one. - torch::optional> batch = d.get_batch(kBatchSize); + std::optional> batch = d.get_batch(kBatchSize); ASSERT_TRUE(batch.has_value()); ASSERT_EQ(batch->data.size(0), kBatchSize); ASSERT_EQ(batch->data.size(1), kBatchSize + 1); @@ -2117,7 +2117,7 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) { public: explicit S(size_t size) : size_(size), index_(0){}; - void reset(torch::optional new_size = torch::nullopt) override { + void reset(std::optional new_size = std::nullopt) override { if (new_size.has_value()) { size_ = *new_size; } @@ -2134,10 +2134,10 @@ TEST(DataLoaderTest, ChunkDatasetCrossChunkShuffle) { } // Returns the next batch of indices. - torch::optional> next(size_t batch_size) override { + std::optional> next(size_t batch_size) override { const auto remaining_indices = size_ - index_; if (remaining_indices == 0) { - return torch::nullopt; + return std::nullopt; } auto return_size = std::min(batch_size, remaining_indices); std::vector index_batch( diff --git a/test/cpp/api/functional.cpp b/test/cpp/api/functional.cpp index 8dc29be38ea..83c5cd2900e 100644 --- a/test/cpp/api/functional.cpp +++ b/test/cpp/api/functional.cpp @@ -2329,7 +2329,7 @@ TEST_F(FunctionalTest, Interpolate) { auto tensor = torch::rand({2, 3, 32, 32}); std::vector osize = {8, 10}; auto expected = - at::native::_upsample_nearest_exact2d(tensor, osize, torch::nullopt); + at::native::_upsample_nearest_exact2d(tensor, osize, std::nullopt); auto options = F::InterpolateFuncOptions() .size(osize) @@ -2342,8 +2342,8 @@ TEST_F(FunctionalTest, Interpolate) { { auto tensor = torch::rand({2, 3, 32, 32}); std::vector osize = {8, 10}; - auto expected = at::native::_upsample_bilinear2d_aa( - tensor, osize, false, torch::nullopt); + auto expected = + at::native::_upsample_bilinear2d_aa(tensor, osize, false, std::nullopt); auto options = F::InterpolateFuncOptions() .size(osize) @@ -2356,8 +2356,8 @@ TEST_F(FunctionalTest, Interpolate) { { auto tensor = torch::rand({2, 3, 32, 32}); std::vector osize = {8, 10}; - auto expected = at::native::_upsample_bicubic2d_aa( - tensor, osize, false, torch::nullopt); + auto expected = + at::native::_upsample_bicubic2d_aa(tensor, osize, false, std::nullopt); auto options = F::InterpolateFuncOptions() .size(osize) diff --git a/test/cpp/api/module.cpp b/test/cpp/api/module.cpp index 28f17f10ff4..e46ffcb27dc 100644 --- a/test/cpp/api/module.cpp +++ b/test/cpp/api/module.cpp @@ -381,8 +381,8 @@ TEST_F(ModuleTest, CallingCloneOnModuleThatDoesNotOverrideCloneThrows) { TEST_F(ModuleTest, CallingCloneOnModuleThatDoesOverrideCloneDoesNotThrow) { struct Cloneable : Module { std::shared_ptr clone( - const torch::optional& device = - torch::nullopt) const override { + const std::optional& device = + std::nullopt) const override { return nullptr; } }; diff --git a/test/cpp/api/parallel.cpp b/test/cpp/api/parallel.cpp index ca5e9ad566a..1ec7c463a59 100644 --- a/test/cpp/api/parallel.cpp +++ b/test/cpp/api/parallel.cpp @@ -190,7 +190,7 @@ TEST_F( auto output = parallel::data_parallel( m, input, - /*devices=*/torch::nullopt, + /*devices=*/std::nullopt, /*output_device=*/torch::Device(torch::kCUDA, 1)); ASSERT_TRUE(output.defined()); ASSERT_TRUE(output.device().is_cuda()); diff --git a/test/cpp/api/rnn.cpp b/test/cpp/api/rnn.cpp index 6036c7477b8..0a120a113c1 100644 --- a/test/cpp/api/rnn.cpp +++ b/test/cpp/api/rnn.cpp @@ -750,7 +750,7 @@ TEST_F(RNNTest, UsePackedSequenceAsInput) { std::get<0>(rnn_output).data(), expected_output, 1e-05, 2e-04)); // Test passing optional argument to `LSTM::forward_with_packed_input` - rnn_output = m->forward_with_packed_input(packed_input, torch::nullopt); + rnn_output = m->forward_with_packed_input(packed_input, std::nullopt); ASSERT_TRUE(torch::allclose( std::get<0>(rnn_output).data(), expected_output, 1e-05, 2e-04)); } diff --git a/test/cpp/jit/test_lite_trainer.cpp b/test/cpp/jit/test_lite_trainer.cpp index c88775aac31..a0937406530 100644 --- a/test/cpp/jit/test_lite_trainer.cpp +++ b/test/cpp/jit/test_lite_trainer.cpp @@ -317,7 +317,7 @@ struct DummyDataset : torch::data::datasets::Dataset { // NOLINTNEXTLINE(bugprone-narrowing-conversions,cppcoreguidelines-narrowing-conversions) return 1 + index; } - torch::optional size() const override { + std::optional size() const override { return size_; } diff --git a/torch/csrc/api/include/torch/data/datasets/chunk.h b/torch/csrc/api/include/torch/data/datasets/chunk.h index c51822536d9..755d62c0306 100644 --- a/torch/csrc/api/include/torch/data/datasets/chunk.h +++ b/torch/csrc/api/include/torch/data/datasets/chunk.h @@ -50,7 +50,7 @@ template < class BatchDataBuffer { public: using UnwrappedBatchType = UnwrappedBatch; - using BatchType = torch::optional; + using BatchType = std::optional; using BatchRequestType = typename ExampleSampler::BatchRequestType; BatchDataBuffer( @@ -316,7 +316,7 @@ class ChunkDataset final typename ChunkReader::BatchType, size_t> { public: - using BatchType = torch::optional; + using BatchType = std::optional; using UnwrappedBatchType = typename ChunkReader::BatchType; using BatchRequestType = size_t; using ChunkSamplerType = ChunkSampler; @@ -404,7 +404,7 @@ class ChunkDataset final /// size is not used for chunk dataset. std::optional size() const override { - return torch::nullopt; + return std::nullopt; } // provide a references to chunk sampler. Used mainly in distributed data diff --git a/torch/csrc/api/include/torch/data/datasets/map.h b/torch/csrc/api/include/torch/data/datasets/map.h index b23e881391a..f0a09fb4532 100644 --- a/torch/csrc/api/include/torch/data/datasets/map.h +++ b/torch/csrc/api/include/torch/data/datasets/map.h @@ -12,7 +12,7 @@ namespace torch::data::datasets { namespace detail { template -using optional_if_t = std::conditional_t, T>; +using optional_if_t = std::conditional_t, T>; } // namespace detail /// A `MapDataset` is a dataset that applies a transform to a source dataset. diff --git a/torch/csrc/api/include/torch/nn/modules/rnn.h b/torch/csrc/api/include/torch/nn/modules/rnn.h index a2a251b4136..4d30ea149ba 100644 --- a/torch/csrc/api/include/torch/nn/modules/rnn.h +++ b/torch/csrc/api/include/torch/nn/modules/rnn.h @@ -158,17 +158,17 @@ class TORCH_API LSTMImpl : public detail::RNNImplBase { std::tuple> forward( const Tensor& input, - torch::optional> hx_opt = {}); + std::optional> hx_opt = {}); protected: FORWARD_HAS_DEFAULT_ARGS( - {1, AnyValue(torch::optional>())}) + {1, AnyValue(std::optional>())}) public: std::tuple> forward_with_packed_input( const torch::nn::utils::rnn::PackedSequence& packed_input, - torch::optional> hx_opt = {}); + std::optional> hx_opt = {}); LSTMOptions options; @@ -191,7 +191,7 @@ class TORCH_API LSTMImpl : public detail::RNNImplBase { const Tensor& batch_sizes, const Tensor& sorted_indices, int64_t max_batch_size, - torch::optional> hx_opt); + std::optional> hx_opt); }; /// A `ModuleHolder` subclass for `LSTMImpl`. @@ -343,11 +343,11 @@ class TORCH_API LSTMCellImpl : public detail::RNNCellImplBase { std::tuple forward( const Tensor& input, - torch::optional> hx_opt = {}); + std::optional> hx_opt = {}); protected: FORWARD_HAS_DEFAULT_ARGS( - {1, AnyValue(torch::optional>())}) + {1, AnyValue(std::optional>())}) public: LSTMCellOptions options; diff --git a/torch/csrc/api/include/torch/nn/utils/rnn.h b/torch/csrc/api/include/torch/nn/utils/rnn.h index 53c378c0289..84c639708ee 100644 --- a/torch/csrc/api/include/torch/nn/utils/rnn.h +++ b/torch/csrc/api/include/torch/nn/utils/rnn.h @@ -244,7 +244,7 @@ inline std::tuple pad_packed_sequence( const PackedSequence& sequence, bool batch_first = false, double padding_value = 0.0, - std::optional total_length = torch::nullopt) { + std::optional total_length = std::nullopt) { int64_t max_seq_length = sequence.batch_sizes().size(0); if (total_length.has_value()) { int64_t total_length_val = total_length.value(); diff --git a/torch/csrc/api/src/nn/modules/rnn.cpp b/torch/csrc/api/src/nn/modules/rnn.cpp index 23e90e56163..272d1db8a64 100644 --- a/torch/csrc/api/src/nn/modules/rnn.cpp +++ b/torch/csrc/api/src/nn/modules/rnn.cpp @@ -607,7 +607,7 @@ std::tuple> LSTMImpl::forward_helper( const Tensor& batch_sizes, const Tensor& sorted_indices, int64_t max_batch_size, - torch::optional> hx_opt) { + std::optional> hx_opt) { std::tuple hx; if (!hx_opt.has_value()) { int64_t num_directions = options.bidirectional() ? 2 : 1; @@ -664,7 +664,7 @@ std::tuple> LSTMImpl::forward_helper( std::tuple> LSTMImpl::forward( const Tensor& input, - torch::optional> hx_opt) { + std::optional> hx_opt) { auto batch_sizes = torch::Tensor(); auto max_batch_size = options.batch_first() ? input.size(0) : input.size(1); auto sorted_indices = torch::Tensor(); @@ -680,7 +680,7 @@ std::tuple> LSTMImpl::forward( std::tuple> LSTMImpl:: forward_with_packed_input( const PackedSequence& packed_input, - torch::optional> hx_opt) { + std::optional> hx_opt) { const auto& input = packed_input.data(); const auto& batch_sizes = packed_input.batch_sizes(); const auto& sorted_indices = packed_input.sorted_indices(); @@ -945,7 +945,7 @@ LSTMCellImpl::LSTMCellImpl(const LSTMCellOptions& options_) std::tuple LSTMCellImpl::forward( const Tensor& input, - torch::optional> hx_opt) { + std::optional> hx_opt) { this->check_forward_input(input, "input"); if (hx_opt.has_value()) { this->check_forward_input(std::get<0>(hx_opt.value()), "hx[0]"); diff --git a/torchgen/executorch/api/et_cpp.py b/torchgen/executorch/api/et_cpp.py index 1d7672715a6..e4e92ff58d1 100644 --- a/torchgen/executorch/api/et_cpp.py +++ b/torchgen/executorch/api/et_cpp.py @@ -242,7 +242,7 @@ def return_names(f: NativeFunction, *, fallback_name: str = "result") -> Sequenc JIT_TO_CPP_DEFAULT = { "False": "false", "True": "true", - "None": "torch::executorch::nullopt", # UGH this one is type directed + "None": "torch::execustd::nullopt", # UGH this one is type directed "[]": "{}", "contiguous_format": "torch::executorch::MemoryFormat::Contiguous", "long": "torch::executorch::kLong",