From eb5d28ecefb9d78d4fff5fac099e70e5eb3fbe2e Mon Sep 17 00:00:00 2001 From: Peter Goldsborough Date: Wed, 19 Dec 2018 21:38:00 -0800 Subject: [PATCH] Pretty printing of C++ modules (#15326) Summary: A long outstanding nicety: pretty printing of C++ modules. E.g. ``` Sequential sequential( Linear(10, 3), Conv2d(1, 2, 3), Dropout(0.5), BatchNorm(5), Embedding(4, 10), LSTM(4, 5)); std::cout << sequential; ``` prints ``` torch::nn::Sequential( (0): torch::nn::Linear(in=10, out=3, with_bias=true) (1): torch::nn::Conv2d(input_channels=1, output_channels=2, kernel_size=[3, 3], stride=[1, 1]) (2): torch::nn::Dropout(rate=0.5) (3): torch::nn::BatchNorm(features=5, eps=1e-05, momentum=0.1, affine=true, stateful=true) (4): torch::nn::Embedding(count=4, dimension=10) (5): torch::nn::LSTM(input_size=4, hidden_size=5, layers=1, dropout=0) ) ``` apaszke ebetica ezyang Pull Request resolved: https://github.com/pytorch/pytorch/pull/15326 Differential Revision: D13518986 Pulled By: goldsborough fbshipit-source-id: 63bf753672f0e348951de3645208f263581de5fb --- test/cpp/api/module.cpp | 20 +++++ test/cpp/api/modules.cpp | 82 +++++++++++++++++++ test/cpp/api/rnn.cpp | 12 +++ test/cpp/api/sequential.cpp | 20 +++++ .../csrc/api/include/torch/expanding_array.h | 26 ++++-- torch/csrc/api/include/torch/nn/module.h | 20 +++++ .../api/include/torch/nn/modules/batchnorm.h | 3 + .../csrc/api/include/torch/nn/modules/conv.h | 3 + .../api/include/torch/nn/modules/dropout.h | 11 ++- .../api/include/torch/nn/modules/embedding.h | 3 + .../api/include/torch/nn/modules/functional.h | 3 + .../api/include/torch/nn/modules/linear.h | 3 + torch/csrc/api/include/torch/nn/modules/rnn.h | 8 +- .../api/include/torch/nn/modules/sequential.h | 6 ++ torch/csrc/api/include/torch/nn/pimpl.h | 8 ++ torch/csrc/api/src/nn/module.cpp | 26 ++++++ torch/csrc/api/src/nn/modules/batchnorm.cpp | 9 ++ torch/csrc/api/src/nn/modules/conv.cpp | 9 ++ torch/csrc/api/src/nn/modules/dropout.cpp | 9 ++ torch/csrc/api/src/nn/modules/embedding.cpp | 9 +- torch/csrc/api/src/nn/modules/functional.cpp | 4 + torch/csrc/api/src/nn/modules/linear.cpp | 6 ++ torch/csrc/api/src/nn/modules/rnn.cpp | 19 +++++ 23 files changed, 306 insertions(+), 13 deletions(-) diff --git a/test/cpp/api/module.cpp b/test/cpp/api/module.cpp index 2e77a8dd67a3..77212797bec1 100644 --- a/test/cpp/api/module.cpp +++ b/test/cpp/api/module.cpp @@ -832,3 +832,23 @@ TEST_F(ModuleTest, ThrowsWhenAttemptingtoGetTopLevelModuleAsSharedPtr) { ASSERT_NO_THROW(module->modules()); } } + +struct EmptyModule : torch::nn::Module {}; + +TEST_F(ModuleTest, PrettyPrint) { + struct TestModule : torch::nn::Module { + TestModule(int x, float y) : x_(x), y_(y) {} + + void pretty_print(std::ostream& stream) const { + stream << "TestModule(x=" << x_ << ", y=" << y_ << ")"; + } + + int x_; + float y_; + }; + + using namespace torch::nn; + + ASSERT_EQ(c10::str(EmptyModule{}), "EmptyModule"); + ASSERT_EQ(c10::str(TestModule(1, 3.14)), "TestModule(x=1, y=3.14)"); +} diff --git a/test/cpp/api/modules.cpp b/test/cpp/api/modules.cpp index 6d240c40d8de..6852d1916d84 100644 --- a/test/cpp/api/modules.cpp +++ b/test/cpp/api/modules.cpp @@ -325,3 +325,85 @@ TEST_F(ModulesTest, Linear2_CUDA) { ASSERT_EQ(model->weight.grad().numel(), 2 * 5); } + +TEST_F(ModulesTest, PrettyPrintLinear) { + ASSERT_EQ( + c10::str(Linear(3, 4)), "torch::nn::Linear(in=3, out=4, with_bias=true)"); +} + +TEST_F(ModulesTest, PrettyPrintConv) { + ASSERT_EQ( + c10::str(Conv1d(3, 4, 5)), + "torch::nn::Conv1d(input_channels=3, output_channels=4, kernel_size=5, stride=1)"); + ASSERT_EQ( + c10::str(Conv2d(3, 4, 5)), + "torch::nn::Conv2d(input_channels=3, output_channels=4, kernel_size=[5, 5], stride=[1, 1])"); + ASSERT_EQ( + c10::str(Conv2d(Conv2dOptions(3, 4, 5).stride(2))), + "torch::nn::Conv2d(input_channels=3, output_channels=4, kernel_size=[5, 5], stride=[2, 2])"); + + const auto options = Conv2dOptions(3, 4, torch::IntList{5, 6}).stride({1, 2}); + ASSERT_EQ( + c10::str(Conv2d(options)), + "torch::nn::Conv2d(input_channels=3, output_channels=4, kernel_size=[5, 6], stride=[1, 2])"); +} + +TEST_F(ModulesTest, PrettyPrintDropout) { + ASSERT_EQ(c10::str(Dropout(0.5)), "torch::nn::Dropout(rate=0.5)"); + ASSERT_EQ( + c10::str(FeatureDropout(0.5)), "torch::nn::FeatureDropout(rate=0.5)"); +} + +TEST_F(ModulesTest, PrettyPrintFunctional) { + ASSERT_EQ(c10::str(Functional(torch::relu)), "torch::nn::Functional()"); +} + +TEST_F(ModulesTest, PrettyPrintBatchNorm) { + ASSERT_EQ( + c10::str(BatchNorm( + BatchNormOptions(4).eps(0.5).momentum(0.1).affine(false).stateful( + true))), + "torch::nn::BatchNorm(features=4, eps=0.5, momentum=0.1, affine=false, stateful=true)"); +} + +TEST_F(ModulesTest, PrettyPrintEmbedding) { + ASSERT_EQ( + c10::str(Embedding(10, 2)), + "torch::nn::Embedding(count=10, dimension=2)"); +} + +TEST_F(ModulesTest, PrettyPrintNestedModel) { + struct InnerTestModule : torch::nn::Module { + InnerTestModule() + : torch::nn::Module("InnerTestModule"), + fc(register_module("fc", torch::nn::Linear(3, 4))), + table(register_module("table", torch::nn::Embedding(10, 2))) {} + + torch::nn::Linear fc; + torch::nn::Embedding table; + }; + + struct TestModule : torch::nn::Module { + TestModule() + : torch::nn::Module("TestModule"), + fc(register_module("fc", torch::nn::Linear(4, 5))), + table(register_module("table", torch::nn::Embedding(10, 2))), + inner(register_module("inner", std::make_shared())) { + } + + torch::nn::Linear fc; + torch::nn::Embedding table; + std::shared_ptr inner; + }; + + ASSERT_EQ( + c10::str(TestModule{}), + "TestModule(\n" + " (fc): torch::nn::Linear(in=4, out=5, with_bias=true)\n" + " (table): torch::nn::Embedding(count=10, dimension=2)\n" + " (inner): InnerTestModule(\n" + " (fc): torch::nn::Linear(in=3, out=4, with_bias=true)\n" + " (table): torch::nn::Embedding(count=10, dimension=2)\n" + " )\n" + ")"); +} diff --git a/test/cpp/api/rnn.cpp b/test/cpp/api/rnn.cpp index 5c95851a91c8..86b05df7ede7 100644 --- a/test/cpp/api/rnn.cpp +++ b/test/cpp/api/rnn.cpp @@ -227,3 +227,15 @@ TEST_F(RNNTest, EndToEndRNNTanh_CUDA) { ASSERT_TRUE(test_RNN_xor( [](int s) { return RNN(RNNOptions(s, s).tanh().layers(2)); }, true)); } + +TEST_F(RNNTest, PrettyPrintRNNs) { + ASSERT_EQ( + c10::str(LSTM(LSTMOptions(128, 64).layers(3).dropout(0.2))), + "torch::nn::LSTM(input_size=128, hidden_size=64, layers=3, dropout=0.2)"); + ASSERT_EQ( + c10::str(GRU(GRUOptions(128, 64).layers(3).dropout(0.5))), + "torch::nn::GRU(input_size=128, hidden_size=64, layers=3, dropout=0.5)"); + ASSERT_EQ( + c10::str(RNN(RNNOptions(128, 64).layers(3).dropout(0.2).tanh())), + "torch::nn::RNN(input_size=128, hidden_size=64, layers=3, dropout=0.2, activation=tanh)"); +} diff --git a/test/cpp/api/sequential.cpp b/test/cpp/api/sequential.cpp index f89d034de425..1f7cf5519eed 100644 --- a/test/cpp/api/sequential.cpp +++ b/test/cpp/api/sequential.cpp @@ -323,3 +323,23 @@ TEST_F(SequentialTest, CloneToDevice_CUDA) { ASSERT_EQ(b.device(), device); } } + +TEST_F(SequentialTest, PrettyPrintSequential) { + Sequential sequential( + Linear(10, 3), + Conv2d(1, 2, 3), + Dropout(0.5), + BatchNorm(5), + Embedding(4, 10), + LSTM(4, 5)); + ASSERT_EQ( + c10::str(sequential), + "torch::nn::Sequential(\n" + " (0): torch::nn::Linear(in=10, out=3, with_bias=true)\n" + " (1): torch::nn::Conv2d(input_channels=1, output_channels=2, kernel_size=[3, 3], stride=[1, 1])\n" + " (2): torch::nn::Dropout(rate=0.5)\n" + " (3): torch::nn::BatchNorm(features=5, eps=1e-05, momentum=0.1, affine=true, stateful=true)\n" + " (4): torch::nn::Embedding(count=4, dimension=10)\n" + " (5): torch::nn::LSTM(input_size=4, hidden_size=5, layers=1, dropout=0)\n" + ")"); +} diff --git a/torch/csrc/api/include/torch/expanding_array.h b/torch/csrc/api/include/torch/expanding_array.h index 910590023c54..a840a881d9bb 100644 --- a/torch/csrc/api/include/torch/expanding_array.h +++ b/torch/csrc/api/include/torch/expanding_array.h @@ -24,18 +24,17 @@ class ExpandingArray { /// the length is checked against the `ExpandingArray`'s extent parameter `D` /// at runtime. /*implicit*/ ExpandingArray(std::initializer_list list) - : ExpandingArray(std::vector(list)) {} + : ExpandingArray(at::ArrayRef(list)) {} - /// Constructs an `ExpandingArray` from a `vector`. The extent of the - /// length is checked against the `ExpandingArray`'s extent parameter `D` at - /// runtime. - /*implicit*/ ExpandingArray(const std::vector& values) { + /// Constructs an `ExpandingArray` from an `initializer_list`. The extent of + /// the length is checked against the `ExpandingArray`'s extent parameter `D` + /// at runtime. + /*implicit*/ ExpandingArray(at::ArrayRef values) { + // clang-format off AT_CHECK( values.size() == D, - "Expected ", - D, - " values, but instead got ", - values.size()); + "Expected ", D, " values, but instead got ", values.size()); + // clang-format on std::copy(values.begin(), values.end(), values_.begin()); } @@ -84,4 +83,13 @@ class ExpandingArray { std::array values_; }; +template +std::ostream& operator<<( + std::ostream& stream, + const ExpandingArray& expanding_array) { + if (expanding_array.size() == 1) { + return stream << expanding_array->at(0); + } + return stream << static_cast>(expanding_array); +} } // namespace torch diff --git a/torch/csrc/api/include/torch/nn/module.h b/torch/csrc/api/include/torch/nn/module.h index bb3fa9e1ea3a..91b77fb68a07 100644 --- a/torch/csrc/api/include/torch/nn/module.h +++ b/torch/csrc/api/include/torch/nn/module.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -386,6 +387,15 @@ class TORCH_API Module : public std::enable_shared_from_this { /// Deserializes the `Module` from the given `InputArchive`. virtual void load(serialize::InputArchive& archive); + /// Streams a pretty representation of the `Module` into the given `stream`. + /// By default, this representation will be the name of the module (taken from + /// `name()`), followed by a recursive pretty print of all of the `Module`'s + /// submodules. + /// + /// Override this method to change the pretty print. The input + /// `stream` should be returned from the method, to allow easy chaining. + virtual void pretty_print(std::ostream& stream) const; + protected: /// Registers a parameter with this `Module`. /// @@ -462,6 +472,11 @@ class TORCH_API Module : public std::enable_shared_from_this { template friend class Cloneable; + /// Pretty prints the given `Module` into the `ostream`. + TORCH_API friend std::ostream& operator<<( + std::ostream& stream, + const nn::Module& module); + // Private methods. /// Used in the implementation of `Cloneable`. @@ -471,6 +486,11 @@ class TORCH_API Module : public std::enable_shared_from_this { template void to_impl(Ts&&... ts); + /// Implements pretty printing the module hierarchy. + void pretty_print_recursive( + std::ostream& stream, + const std::string& indentation) const; + /// Applies the `function` to every submodule recursively, starting at this /// `Module`'s children (thus not including the module itself). void apply_to_submodules( diff --git a/torch/csrc/api/include/torch/nn/modules/batchnorm.h b/torch/csrc/api/include/torch/nn/modules/batchnorm.h index 33aabfd66cc8..2f7dd3be1aa7 100644 --- a/torch/csrc/api/include/torch/nn/modules/batchnorm.h +++ b/torch/csrc/api/include/torch/nn/modules/batchnorm.h @@ -53,6 +53,9 @@ class TORCH_API BatchNormImpl : public torch::nn::Cloneable { void reset() override; + /// Pretty prints the `BatchNorm` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Applies batch normalization on the `input` using the stored mean and /// variance. /// diff --git a/torch/csrc/api/include/torch/nn/modules/conv.h b/torch/csrc/api/include/torch/nn/modules/conv.h index a242af19e25a..38cf9b642699 100644 --- a/torch/csrc/api/include/torch/nn/modules/conv.h +++ b/torch/csrc/api/include/torch/nn/modules/conv.h @@ -88,6 +88,9 @@ class ConvImpl : public torch::nn::Cloneable { void reset() override; + /// Pretty prints the `Conv{1,2,3}d` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// The options with which this `Module` was constructed. ConvOptions options; diff --git a/torch/csrc/api/include/torch/nn/modules/dropout.h b/torch/csrc/api/include/torch/nn/modules/dropout.h index 46fb0e3eb4ac..edd7c6259f32 100644 --- a/torch/csrc/api/include/torch/nn/modules/dropout.h +++ b/torch/csrc/api/include/torch/nn/modules/dropout.h @@ -39,9 +39,13 @@ class DropoutImplBase : public torch::nn::Cloneable { class TORCH_API DropoutImpl : public detail::DropoutImplBase { public: using detail::DropoutImplBase::DropoutImplBase; + /// During training, applies a noise mask to the input tensor. /// During evaluation, applies an identity function. Tensor forward(const Tensor& input); + + /// Pretty prints the `Dropout` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; }; /// Applies spatial [Dropout](https://arxiv.org/abs/1207.0580) to inputs with @@ -53,12 +57,17 @@ class TORCH_API DropoutImpl : public detail::DropoutImplBase { /// [Dropout3d](https://pytorch.org/docs/stable/nn.html#torch.nn.Dropout3d) for /// 3-D features. This `FeatureDropout` module can instead deal with both 2-D /// and 3-D features. -class TORCH_API FeatureDropoutImpl : public detail::DropoutImplBase { +class TORCH_API FeatureDropoutImpl + : public detail::DropoutImplBase { public: using detail::DropoutImplBase::DropoutImplBase; + /// During training, applies a noise mask to the input tensor. /// During evaluation, applies an identity function. Tensor forward(const Tensor& input); + + /// Pretty prints the `FeatureDropout` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; }; /// A `ModuleHolder` subclass for `DropoutImpl`. diff --git a/torch/csrc/api/include/torch/nn/modules/embedding.h b/torch/csrc/api/include/torch/nn/modules/embedding.h index 1c94884ebb2c..4c8e60d3f80e 100644 --- a/torch/csrc/api/include/torch/nn/modules/embedding.h +++ b/torch/csrc/api/include/torch/nn/modules/embedding.h @@ -28,6 +28,9 @@ class TORCH_API EmbeddingImpl : public torch::nn::Cloneable { void reset() override; + /// Pretty prints the `Embedding` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Performs a lookup on the embedding table stored in `weight` using the /// `indices` supplied and returns the result. Tensor forward(const Tensor& indices); diff --git a/torch/csrc/api/include/torch/nn/modules/functional.h b/torch/csrc/api/include/torch/nn/modules/functional.h index 553faf622e2a..96378b9a91a1 100644 --- a/torch/csrc/api/include/torch/nn/modules/functional.h +++ b/torch/csrc/api/include/torch/nn/modules/functional.h @@ -79,6 +79,9 @@ class TORCH_API FunctionalImpl : public torch::nn::Cloneable { void reset() override; + /// Pretty prints the `Functional` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Forwards the `input` tensor to the underlying (bound) function object. Tensor forward(Tensor input); diff --git a/torch/csrc/api/include/torch/nn/modules/linear.h b/torch/csrc/api/include/torch/nn/modules/linear.h index 3a115438be25..d11cd93e87d3 100644 --- a/torch/csrc/api/include/torch/nn/modules/linear.h +++ b/torch/csrc/api/include/torch/nn/modules/linear.h @@ -29,6 +29,9 @@ class TORCH_API LinearImpl : public Cloneable { void reset() override; + /// Pretty prints the `Linear` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Transforms the `input` tensor by multiplying with the `weight` and /// optionally adding the `bias`, if `with_bias` is true in the options. Tensor forward(const Tensor& input); diff --git a/torch/csrc/api/include/torch/nn/modules/rnn.h b/torch/csrc/api/include/torch/nn/modules/rnn.h index c418ed8a8217..62602dd467b9 100644 --- a/torch/csrc/api/include/torch/nn/modules/rnn.h +++ b/torch/csrc/api/include/torch/nn/modules/rnn.h @@ -74,6 +74,9 @@ class RNNImplBase : public torch::nn::Cloneable { void to(torch::Dtype dtype, bool non_blocking = false) override; void to(torch::Device device, bool non_blocking = false) override; + /// Pretty prints the RNN module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Modifies the internal storage of weights for optimization purposes. /// /// On CPU, this method should be called if any of the weight or bias vectors @@ -136,7 +139,7 @@ class RNNImplBase : public torch::nn::Cloneable { // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ RNN ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -enum class RNNActivation : uint32_t TORCH_API { ReLU, Tanh }; +enum class RNNActivation : uint32_t TORCH_API{ReLU, Tanh}; /// Options for RNN modules. struct TORCH_API RNNOptions { @@ -177,6 +180,9 @@ class TORCH_API RNNImpl : public detail::RNNImplBase { : RNNImpl(RNNOptions(input_size, hidden_size)) {} explicit RNNImpl(const RNNOptions& options); + /// Pretty prints the `RNN` module into the given `stream`. + void pretty_print(std::ostream& stream) const override; + /// Applies the `RNN` module to an input sequence and input state. /// The `input` should follow a `(sequence, batch, features)` layout unless /// `batch_first` is true, in which case the layout should be `(batch, diff --git a/torch/csrc/api/include/torch/nn/modules/sequential.h b/torch/csrc/api/include/torch/nn/modules/sequential.h index 32cd9137cecb..976e0e351240 100644 --- a/torch/csrc/api/include/torch/nn/modules/sequential.h +++ b/torch/csrc/api/include/torch/nn/modules/sequential.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -116,6 +117,11 @@ class SequentialImpl : public Cloneable { /// its own. void reset() override {} + /// Pretty prints the `Sequential` module into the given `stream`. + void pretty_print(std::ostream& stream) const override { + stream << "torch::nn::Sequential"; + } + /// Feeds `inputs` to the first module and then chains outputs to inputs, /// returning the last output. /// diff --git a/torch/csrc/api/include/torch/nn/pimpl.h b/torch/csrc/api/include/torch/nn/pimpl.h index 790a63d58679..c7cef931a123 100644 --- a/torch/csrc/api/include/torch/nn/pimpl.h +++ b/torch/csrc/api/include/torch/nn/pimpl.h @@ -156,6 +156,14 @@ class ModuleHolder : torch::detail::ModuleHolderIndicator { } }; +/// Pretty prints the given `Module` into the `ostream`. +template +std::ostream& operator<<( + std::ostream& stream, + const nn::ModuleHolder& module) { + return stream << *module; +} + /// Serializes a `ModuleHolder` into an `OutputArchive`. template serialize::OutputArchive& operator<<( diff --git a/torch/csrc/api/src/nn/module.cpp b/torch/csrc/api/src/nn/module.cpp index de526b21196e..5c0629dc3e84 100644 --- a/torch/csrc/api/src/nn/module.cpp +++ b/torch/csrc/api/src/nn/module.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -321,6 +322,26 @@ Tensor& Module::register_buffer(std::string name, Tensor tensor) { return buffers_.insert(std::move(name), std::move(tensor)); } +void Module::pretty_print(std::ostream& stream) const { + stream << name(); +} + +void Module::pretty_print_recursive( + std::ostream& stream, + const std::string& indentation) const { + pretty_print(stream); + if (!children_.is_empty()) { + stream << "(\n"; + const std::string next_indentation = indentation + " "; + for (const auto& child : children_) { + stream << next_indentation << "(" << child.key() << "): "; + child.value()->pretty_print_recursive(stream, next_indentation); + stream << '\n'; + } + stream << indentation << ")"; + } +} + void Module::clone_(Module& other, const optional& device) {} void Module::apply_to_submodules( @@ -351,6 +372,11 @@ std::shared_ptr Module::shared_from_this_checked() const { return std::const_pointer_cast(ptr); } +std::ostream& operator<<(std::ostream& stream, const nn::Module& module) { + module.pretty_print_recursive(stream, ""); + return stream; +} + serialize::OutputArchive& operator<<( serialize::OutputArchive& archive, const std::shared_ptr& module) { diff --git a/torch/csrc/api/src/nn/modules/batchnorm.cpp b/torch/csrc/api/src/nn/modules/batchnorm.cpp index cb1e91ddc045..fa2789c73de8 100644 --- a/torch/csrc/api/src/nn/modules/batchnorm.cpp +++ b/torch/csrc/api/src/nn/modules/batchnorm.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -32,6 +33,14 @@ void BatchNormImpl::reset() { } } +void BatchNormImpl::pretty_print(std::ostream& stream) const { + stream << std::boolalpha + << "torch::nn::BatchNorm(features=" << options.features_ + << ", eps=" << options.eps_ << ", momentum=" << options.momentum_ + << ", affine=" << options.affine_ << ", stateful=" << options.stateful_ + << ")"; +} + Tensor BatchNormImpl::forward(const Tensor& input) { AT_CHECK( options.stateful_, diff --git a/torch/csrc/api/src/nn/modules/conv.cpp b/torch/csrc/api/src/nn/modules/conv.cpp index 581d38d8e6a8..42147438b963 100644 --- a/torch/csrc/api/src/nn/modules/conv.cpp +++ b/torch/csrc/api/src/nn/modules/conv.cpp @@ -59,6 +59,15 @@ void ConvImpl::reset() { } } +template +void ConvImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::Conv" << D << "d" + << "(input_channels=" << options.input_channels_ + << ", output_channels=" << options.output_channels_ + << ", kernel_size=" << options.kernel_size_ + << ", stride=" << options.stride_ << ")"; +} + Tensor Conv1dImpl::forward(const Tensor& input) { if (options.transposed_) { return torch::conv_transpose1d( diff --git a/torch/csrc/api/src/nn/modules/dropout.cpp b/torch/csrc/api/src/nn/modules/dropout.cpp index 2ec5400379c5..c068f70d389c 100644 --- a/torch/csrc/api/src/nn/modules/dropout.cpp +++ b/torch/csrc/api/src/nn/modules/dropout.cpp @@ -5,6 +5,7 @@ #include #include +#include #include namespace torch { @@ -30,8 +31,16 @@ Tensor DropoutImpl::forward(const Tensor& input) { return torch::dropout(input, options.rate_, this->is_training()); } +void DropoutImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::Dropout(rate=" << options.rate_ << ")"; +} + Tensor FeatureDropoutImpl::forward(const Tensor& input) { return torch::feature_dropout(input, options.rate_, this->is_training()); } + +void FeatureDropoutImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::FeatureDropout(rate=" << options.rate_ << ")"; +} } // namespace nn } // namespace torch diff --git a/torch/csrc/api/src/nn/modules/embedding.cpp b/torch/csrc/api/src/nn/modules/embedding.cpp index be972e5ed6b7..786b4272b79d 100644 --- a/torch/csrc/api/src/nn/modules/embedding.cpp +++ b/torch/csrc/api/src/nn/modules/embedding.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -13,8 +14,7 @@ namespace nn { EmbeddingOptions::EmbeddingOptions(int64_t count, int64_t dimension) : count_(count), dimension_(dimension) {} -EmbeddingImpl::EmbeddingImpl(EmbeddingOptions options) - : options(options) { +EmbeddingImpl::EmbeddingImpl(EmbeddingOptions options) : options(options) { reset(); } @@ -25,6 +25,11 @@ void EmbeddingImpl::reset() { weight.normal_(0, 1); } +void EmbeddingImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::Embedding(count=" << options.count_ + << ", dimension=" << options.dimension_ << ")"; +} + Tensor EmbeddingImpl::forward(const Tensor& input) { return torch::embedding(weight, /*indices=*/input); } diff --git a/torch/csrc/api/src/nn/modules/functional.cpp b/torch/csrc/api/src/nn/modules/functional.cpp index e35a2e60d2af..0da0b741d38b 100644 --- a/torch/csrc/api/src/nn/modules/functional.cpp +++ b/torch/csrc/api/src/nn/modules/functional.cpp @@ -12,6 +12,10 @@ FunctionalImpl::FunctionalImpl(Function function) void FunctionalImpl::reset() {} +void FunctionalImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::Functional()"; +} + Tensor FunctionalImpl::forward(Tensor input) { return function_(std::move(input)); } diff --git a/torch/csrc/api/src/nn/modules/linear.cpp b/torch/csrc/api/src/nn/modules/linear.cpp index f9ec0d160f12..8cd6842befc4 100644 --- a/torch/csrc/api/src/nn/modules/linear.cpp +++ b/torch/csrc/api/src/nn/modules/linear.cpp @@ -28,6 +28,12 @@ void LinearImpl::reset() { } } +void LinearImpl::pretty_print(std::ostream& stream) const { + stream << std::boolalpha << "torch::nn::Linear(in=" << options.in_ + << ", out=" << options.out_ << ", with_bias=" << options.with_bias_ + << ")"; +} + Tensor LinearImpl::forward(const Tensor& input) { AT_ASSERT(!options.with_bias_ || bias.defined()); return torch::linear(input, weight, bias); diff --git a/torch/csrc/api/src/nn/modules/rnn.cpp b/torch/csrc/api/src/nn/modules/rnn.cpp index 632e4d46564b..270753690bcb 100644 --- a/torch/csrc/api/src/nn/modules/rnn.cpp +++ b/torch/csrc/api/src/nn/modules/rnn.cpp @@ -97,6 +97,16 @@ void RNNImplBase::to(torch::Device device, bool non_blocking) { flatten_parameters(); } +template +void RNNImplBase::pretty_print(std::ostream& stream) const { + const std::string name = this->name(); + const std::string name_without_impl = name.substr(0, name.size() - 4); + stream << name_without_impl << "(input_size=" << options.input_size_ + << ", hidden_size=" << options.hidden_size_ + << ", layers=" << options.layers_ << ", dropout=" << options.dropout_ + << ")"; +} + template void RNNImplBase::flatten_parameters() { // Cache the flattened weight and bias vector. @@ -203,6 +213,15 @@ RNNImpl::RNNImpl(const RNNOptions& options) static_cast(options.activation_)), options(options) {} +void RNNImpl::pretty_print(std::ostream& stream) const { + stream << "torch::nn::RNN(input_size=" << options.input_size_ + << ", hidden_size=" << options.hidden_size_ + << ", layers=" << options.layers_ << ", dropout=" << options.dropout_ + << ", activation=" + << (options.activation_ == RNNActivation::Tanh ? "tanh" : "relu") + << ")"; +} + RNNOutput RNNImpl::forward(const Tensor& input, Tensor state) { switch (options.activation_) { case RNNActivation::ReLU: