Skip to content

Commit

Permalink
[codemod] c10::optional -> std::optional in pytorch/audio/src/libtorc…
Browse files Browse the repository at this point in the history
…haudio/sox/effects.cpp +20

Differential Revision: D57294298

Pull Request resolved: #3791
  • Loading branch information
r-barnes authored May 14, 2024
1 parent 9f10306 commit 1980f8a
Show file tree
Hide file tree
Showing 20 changed files with 76 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/libtorchaudio/sox/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ auto apply_effects_tensor(
auto apply_effects_file(
const std::string& path,
const std::vector<std::vector<std::string>>& effects,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format)
std::optional<bool> normalize,
std::optional<bool> channels_first,
const std::optional<std::string>& format)
-> std::tuple<torch::Tensor, int64_t> {
// Open input file
SoxFormat sf(sox_open_read(
Expand Down
6 changes: 3 additions & 3 deletions src/libtorchaudio/sox/effects.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ auto apply_effects_tensor(
auto apply_effects_file(
const std::string& path,
const std::vector<std::vector<std::string>>& effects,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format)
std::optional<bool> normalize,
std::optional<bool> channels_first,
const std::optional<std::string>& format)
-> std::tuple<torch::Tensor, int64_t>;

} // namespace torchaudio::sox
Expand Down
24 changes: 12 additions & 12 deletions src/libtorchaudio/sox/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace torchaudio::sox {

std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
const std::string& path,
const c10::optional<std::string>& format) {
const std::optional<std::string>& format) {
SoxFormat sf(sox_open_read(
path.c_str(),
/*signal=*/nullptr,
Expand All @@ -28,8 +28,8 @@ std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
}

std::vector<std::vector<std::string>> get_effects(
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames) {
const std::optional<int64_t>& frame_offset,
const std::optional<int64_t>& num_frames) {
const auto offset = frame_offset.value_or(0);
TORCH_CHECK(
offset >= 0,
Expand Down Expand Up @@ -57,11 +57,11 @@ std::vector<std::vector<std::string>> get_effects(

std::tuple<torch::Tensor, int64_t> load_audio_file(
const std::string& path,
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format) {
const std::optional<int64_t>& frame_offset,
const std::optional<int64_t>& num_frames,
std::optional<bool> normalize,
std::optional<bool> channels_first,
const std::optional<std::string>& format) {
auto effects = get_effects(frame_offset, num_frames);
return apply_effects_file(path, effects, normalize, channels_first, format);
}
Expand All @@ -71,10 +71,10 @@ void save_audio_file(
torch::Tensor tensor,
int64_t sample_rate,
bool channels_first,
c10::optional<double> compression,
c10::optional<std::string> format,
c10::optional<std::string> encoding,
c10::optional<int64_t> bits_per_sample) {
std::optional<double> compression,
std::optional<std::string> format,
std::optional<std::string> encoding,
std::optional<int64_t> bits_per_sample) {
validate_input_tensor(tensor);

const auto filetype = [&]() {
Expand Down
24 changes: 12 additions & 12 deletions src/libtorchaudio/sox/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
namespace torchaudio::sox {

auto get_effects(
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames)
const std::optional<int64_t>& frame_offset,
const std::optional<int64_t>& num_frames)
-> std::vector<std::vector<std::string>>;

std::tuple<int64_t, int64_t, int64_t, int64_t, std::string> get_info_file(
const std::string& path,
const c10::optional<std::string>& format);
const std::optional<std::string>& format);

std::tuple<torch::Tensor, int64_t> load_audio_file(
const std::string& path,
const c10::optional<int64_t>& frame_offset,
const c10::optional<int64_t>& num_frames,
c10::optional<bool> normalize,
c10::optional<bool> channels_first,
const c10::optional<std::string>& format);
const std::optional<int64_t>& frame_offset,
const std::optional<int64_t>& num_frames,
std::optional<bool> normalize,
std::optional<bool> channels_first,
const std::optional<std::string>& format);

void save_audio_file(
const std::string& path,
torch::Tensor tensor,
int64_t sample_rate,
bool channels_first,
c10::optional<double> compression,
c10::optional<std::string> format,
c10::optional<std::string> encoding,
c10::optional<int64_t> bits_per_sample);
std::optional<double> compression,
std::optional<std::string> format,
std::optional<std::string> encoding,
std::optional<int64_t> bits_per_sample);

} // namespace torchaudio::sox

Expand Down
4 changes: 2 additions & 2 deletions src/libtorchaudio/sox/types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::string to_string(Encoding v) {
}
}

Encoding get_encoding_from_option(const c10::optional<std::string>& encoding) {
Encoding get_encoding_from_option(const std::optional<std::string>& encoding) {
if (!encoding.has_value())
return Encoding::NOT_PROVIDED;
std::string v = encoding.value();
Expand All @@ -84,7 +84,7 @@ Encoding get_encoding_from_option(const c10::optional<std::string>& encoding) {
TORCH_CHECK(false, "Internal Error: unexpected encoding value: ", v);
}

BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth) {
BitDepth get_bit_depth_from_option(const std::optional<int64_t>& bit_depth) {
if (!bit_depth.has_value())
return BitDepth::NOT_PROVIDED;
int64_t v = bit_depth.value();
Expand Down
4 changes: 2 additions & 2 deletions src/libtorchaudio/sox/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ enum class Encoding {
};

std::string to_string(Encoding v);
Encoding get_encoding_from_option(const c10::optional<std::string>& encoding);
Encoding get_encoding_from_option(const std::optional<std::string>& encoding);

enum class BitDepth : unsigned {
NOT_PROVIDED = 0,
Expand All @@ -49,7 +49,7 @@ enum class BitDepth : unsigned {
B64 = 64,
};

BitDepth get_bit_depth_from_option(const c10::optional<int64_t>& bit_depth);
BitDepth get_bit_depth_from_option(const std::optional<int64_t>& bit_depth);

std::string get_encoding(sox_encoding_t encoding);

Expand Down
10 changes: 5 additions & 5 deletions src/libtorchaudio/sox/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ std::tuple<sox_encoding_t, unsigned> get_save_encoding_for_wav(
std::tuple<sox_encoding_t, unsigned> get_save_encoding(
const std::string& format,
const caffe2::TypeMeta& dtype,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample) {
const std::optional<std::string>& encoding,
const std::optional<int64_t>& bits_per_sample) {
const Format fmt = get_format_from_string(format);
const Encoding enc = get_encoding_from_option(encoding);
const BitDepth bps = get_bit_depth_from_option(bits_per_sample);
Expand Down Expand Up @@ -492,9 +492,9 @@ sox_encodinginfo_t get_tensor_encodinginfo(caffe2::TypeMeta dtype) {
sox_encodinginfo_t get_encodinginfo_for_save(
const std::string& format,
const caffe2::TypeMeta& dtype,
const c10::optional<double>& compression,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample) {
const std::optional<double>& compression,
const std::optional<std::string>& encoding,
const std::optional<int64_t>& bits_per_sample) {
auto enc = get_save_encoding(format, dtype, encoding, bits_per_sample);
return sox_encodinginfo_t{
/*encoding=*/std::get<0>(enc),
Expand Down
6 changes: 3 additions & 3 deletions src/libtorchaudio/sox/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ sox_encodinginfo_t get_tensor_encodinginfo(const caffe2::TypeMeta dtype);
sox_encodinginfo_t get_encodinginfo_for_save(
const std::string& format,
const caffe2::TypeMeta& dtype,
const c10::optional<double>& compression,
const c10::optional<std::string>& encoding,
const c10::optional<int64_t>& bits_per_sample);
const std::optional<double>& compression,
const std::optional<std::string>& encoding,
const std::optional<int64_t>& bits_per_sample);

} // namespace torchaudio::sox
#endif
2 changes: 1 addition & 1 deletion src/libtorchaudio/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool is_align_available() {
#endif
}

c10::optional<int64_t> cuda_version() {
std::optional<int64_t> cuda_version() {
#ifdef USE_CUDA
return CUDA_VERSION;
#else
Expand Down
2 changes: 1 addition & 1 deletion src/libtorchaudio/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
namespace torchaudio {
bool is_rir_available();
bool is_align_available();
c10::optional<int64_t> cuda_version();
std::optional<int64_t> cuda_version();
} // namespace torchaudio
2 changes: 1 addition & 1 deletion src/libtorio/ffmpeg/ffmpeg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace torio::io {
////////////////////////////////////////////////////////////////////////////////
// AVDictionary
////////////////////////////////////////////////////////////////////////////////
AVDictionary* get_option_dict(const c10::optional<OptionDict>& option) {
AVDictionary* get_option_dict(const std::optional<OptionDict>& option) {
AVDictionary* opt = nullptr;
if (option) {
for (auto const& [key, value] : option.value()) {
Expand Down
2 changes: 1 addition & 1 deletion src/libtorio/ffmpeg/ffmpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Wrapper {
// IIRC-semantic. Instead we provide helper functions.

// Convert standard dict to FFmpeg native type
AVDictionary* get_option_dict(const c10::optional<OptionDict>& option);
AVDictionary* get_option_dict(const std::optional<OptionDict>& option);

// Clean up the dict after use. If there is an unsed key, throw runtime error
void clean_up_dict(AVDictionary* p);
Expand Down
34 changes: 17 additions & 17 deletions src/libtorio/ffmpeg/pybind/pybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ struct StreamingMediaDecoderFileObj : private FileObj,
public StreamingMediaDecoderCustomIO {
StreamingMediaDecoderFileObj(
py::object fileobj,
const c10::optional<std::string>& format,
const c10::optional<std::map<std::string, std::string>>& option,
const std::optional<std::string>& format,
const std::optional<std::map<std::string, std::string>>& option,
int buffer_size)
: FileObj{fileobj, buffer_size},
StreamingMediaDecoderCustomIO(
Expand All @@ -177,7 +177,7 @@ struct StreamingMediaEncoderFileObj : private FileObj,
public StreamingMediaEncoderCustomIO {
StreamingMediaEncoderFileObj(
py::object fileobj,
const c10::optional<std::string>& format,
const std::optional<std::string>& format,
int buffer_size)
: FileObj{fileobj, buffer_size},
StreamingMediaEncoderCustomIO(
Expand Down Expand Up @@ -231,8 +231,8 @@ struct StreamingMediaDecoderBytes : private BytesWrapper,
public StreamingMediaDecoderCustomIO {
StreamingMediaDecoderBytes(
std::string_view src,
const c10::optional<std::string>& format,
const c10::optional<std::map<std::string, std::string>>& option,
const std::optional<std::string>& format,
const std::optional<std::map<std::string, std::string>>& option,
int64_t buffer_size)
: BytesWrapper{src},
StreamingMediaDecoderCustomIO(
Expand Down Expand Up @@ -278,10 +278,10 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def_readwrite("frames", &Chunk::frames)
.def_readwrite("pts", &Chunk::pts);
py::class_<CodecConfig>(m, "CodecConfig", py::module_local())
.def(py::init<int, int, const c10::optional<int>&, int, int>());
.def(py::init<int, int, const std::optional<int>&, int, int>());
py::class_<StreamingMediaEncoder>(
m, "StreamingMediaEncoder", py::module_local())
.def(py::init<const std::string&, const c10::optional<std::string>&>())
.def(py::init<const std::string&, const std::optional<std::string>&>())
.def("set_metadata", &StreamingMediaEncoder::set_metadata)
.def("add_audio_stream", &StreamingMediaEncoder::add_audio_stream)
.def("add_video_stream", &StreamingMediaEncoder::add_video_stream)
Expand All @@ -293,7 +293,7 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def("close", &StreamingMediaEncoder::close);
py::class_<StreamingMediaEncoderFileObj>(
m, "StreamingMediaEncoderFileObj", py::module_local())
.def(py::init<py::object, const c10::optional<std::string>&, int64_t>())
.def(py::init<py::object, const std::optional<std::string>&, int64_t>())
.def("set_metadata", &StreamingMediaEncoderFileObj::set_metadata)
.def("add_audio_stream", &StreamingMediaEncoderFileObj::add_audio_stream)
.def("add_video_stream", &StreamingMediaEncoderFileObj::add_video_stream)
Expand Down Expand Up @@ -366,8 +366,8 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
m, "StreamingMediaDecoder", py::module_local())
.def(py::init<
const std::string&,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&>())
const std::optional<std::string>&,
const std::optional<OptionDict>&>())
.def("num_src_streams", &StreamingMediaDecoder::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoder::num_out_streams)
.def(
Expand All @@ -385,7 +385,7 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def("remove_stream", &StreamingMediaDecoder::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
py::overload_cast<const std::optional<double>&, const double>(
&StreamingMediaDecoder::process_packet))
.def("process_all_packets", &StreamingMediaDecoder::process_all_packets)
.def("fill_buffer", &StreamingMediaDecoder::fill_buffer)
Expand All @@ -395,8 +395,8 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
m, "StreamingMediaDecoderFileObj", py::module_local())
.def(py::init<
py::object,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&,
const std::optional<std::string>&,
const std::optional<OptionDict>&,
int64_t>())
.def("num_src_streams", &StreamingMediaDecoderFileObj::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoderFileObj::num_out_streams)
Expand All @@ -419,7 +419,7 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def("remove_stream", &StreamingMediaDecoderFileObj::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
py::overload_cast<const std::optional<double>&, const double>(
&StreamingMediaDecoder::process_packet))
.def(
"process_all_packets",
Expand All @@ -431,8 +431,8 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
m, "StreamingMediaDecoderBytes", py::module_local())
.def(py::init<
std::string_view,
const c10::optional<std::string>&,
const c10::optional<OptionDict>&,
const std::optional<std::string>&,
const std::optional<OptionDict>&,
int64_t>())
.def("num_src_streams", &StreamingMediaDecoderBytes::num_src_streams)
.def("num_out_streams", &StreamingMediaDecoderBytes::num_out_streams)
Expand All @@ -455,7 +455,7 @@ PYBIND11_MODULE(TORIO_FFMPEG_EXT_NAME, m) {
.def("remove_stream", &StreamingMediaDecoderBytes::remove_stream)
.def(
"process_packet",
py::overload_cast<const c10::optional<double>&, const double>(
py::overload_cast<const std::optional<double>&, const double>(
&StreamingMediaDecoder::process_packet))
.def(
"process_all_packets",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void ChunkedBuffer::push_frame(torch::Tensor frame, int64_t pts_) {
}
}

c10::optional<Chunk> ChunkedBuffer::pop_chunk() {
std::optional<Chunk> ChunkedBuffer::pop_chunk() {
using namespace torch::indexing;
if (!num_buffered_frames) {
return {};
Expand Down
2 changes: 1 addition & 1 deletion src/libtorio/ffmpeg/stream_reader/buffer/chunked_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ChunkedBuffer {

bool is_ready() const;
void flush();
c10::optional<Chunk> pop_chunk();
std::optional<Chunk> pop_chunk();
void push_frame(torch::Tensor frame, int64_t pts_);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void UnchunkedBuffer::push_frame(torch::Tensor frame, int64_t pts_) {
chunks.push_back(frame);
}

c10::optional<Chunk> UnchunkedBuffer::pop_chunk() {
std::optional<Chunk> UnchunkedBuffer::pop_chunk() {
if (chunks.size() == 0) {
return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UnchunkedBuffer {
explicit UnchunkedBuffer(AVRational time_base);
bool is_ready() const;
void push_frame(torch::Tensor frame, int64_t pts_);
c10::optional<Chunk> pop_chunk();
std::optional<Chunk> pop_chunk();
void flush();
};

Expand Down
2 changes: 1 addition & 1 deletion src/libtorio/ffmpeg/stream_reader/post_process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ struct ProcessImpl : public IPostDecodeProcess {
return ret;
}

c10::optional<Chunk> pop_chunk() override {
std::optional<Chunk> pop_chunk() override {
return buffer.pop_chunk();
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/libtorio/ffmpeg/stream_reader/post_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct IPostDecodeProcess {
virtual ~IPostDecodeProcess() = default;

virtual int process_frame(AVFrame* frame) = 0;
virtual c10::optional<Chunk> pop_chunk() = 0;
virtual std::optional<Chunk> pop_chunk() = 0;
virtual bool is_buffer_ready() const = 0;
virtual const std::string& get_filter_desc() const = 0;
virtual FilterGraphOutputInfo get_filter_output_info() const = 0;
Expand Down
Loading

0 comments on commit 1980f8a

Please sign in to comment.