Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue described by #106769 #108340

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aten/src/ATen/native/RNN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ std::tuple<Tensor, Tensor> lstm_cell(
check_rnn_cell_forward_input(input, w_ih.sym_size(1));
auto hidden_size = w_hh.sym_size(1);
check_rnn_cell_forward_hidden(input, hx[0], hidden_size, 0);
check_rnn_cell_forward_hidden(input, hx[1], std::move(hidden_size), 0);
check_rnn_cell_forward_hidden(input, hx[1], std::move(hidden_size), 1);
static at::Tensor undefined;
return LSTMCell<CellParams>{}(input, std::make_tuple(hx[0], hx[1]), CellParams{w_ih, w_hh, b_ih, b_hh, undefined});
}
Expand Down
150 changes: 144 additions & 6 deletions test/cpp/api/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4355,6 +4355,7 @@ TEST_F(ModulesTest, CrossMapLRN2d) {
TEST_F(ModulesTest, RNNCell) {
torch::manual_seed(0);
auto rnn = RNNCell(1, 2);

auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 2});
auto output = rnn(input, hx);
Expand All @@ -4366,15 +4367,51 @@ TEST_F(ModulesTest, RNNCell) {
expected =
torch::tensor({{-0.0775, 0.6688}, {-0.0734, 0.4759}, {-0.0725, 0.4225}});
ASSERT_TRUE(torch::allclose(output, expected, 1e-05, 2e-04));

input = torch::randn({1});
hx = torch::randn({2});
output = rnn(input, hx);
expected = torch::tensor({0.2808, 0.6505});
ASSERT_TRUE(torch::allclose(output, expected, 1e-05, 2e-04));

{
auto input = torch::randn({3, 2});
auto hx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
rnn(input, hx), "input has inconsistent input_size: got 2 expected 1");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1});
ASSERT_THROWS_WITH(
rnn(input, hx),
"hidden0 has inconsistent hidden_size: got 1, expected 2");
}

{
auto input = torch::randn({3, 1, 1, 1, 1});
auto hx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
rnn(input, hx), "Expected input to be 1D or 2D, got 5D instead");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1, 1, 1, 2});
ASSERT_THROWS_WITH(
rnn(input, hx), "Expected hidden to be 1D or 2D, got 5D instead");
}
}

TEST_F(ModulesTest, LSTMCell) {
torch::manual_seed(0);
auto rnn = LSTMCell(1, 2);
auto lstm = LSTMCell(1, 2);

auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 2});
auto cx = torch::randn({3, 2});
auto output = rnn(input, std::make_tuple(hx, cx));
auto output = lstm(input, std::make_tuple(hx, cx));
auto output_hx = std::get<0>(output);
auto output_cx = std::get<1>(output);
auto expected_hx =
Expand All @@ -4384,7 +4421,7 @@ TEST_F(ModulesTest, LSTMCell) {
ASSERT_TRUE(torch::allclose(output_hx, expected_hx, 1e-05, 2e-04));
ASSERT_TRUE(torch::allclose(output_cx, expected_cx, 1e-05, 2e-04));

output = rnn(input);
output = lstm(input);
output_hx = std::get<0>(output);
output_cx = std::get<1>(output);
expected_hx =
Expand All @@ -4393,22 +4430,123 @@ TEST_F(ModulesTest, LSTMCell) {
torch::tensor({{-0.2679, 0.2180}, {-0.3049, 0.3493}, {-0.2896, 0.2853}});
ASSERT_TRUE(torch::allclose(output_hx, expected_hx, 1e-05, 2e-04));
ASSERT_TRUE(torch::allclose(output_cx, expected_cx, 1e-05, 2e-04));

input = torch::randn({1});
hx = torch::randn({2});
cx = torch::randn({2});
output = lstm(input, std::make_tuple(hx, cx));
output_hx = std::get<0>(output);
output_cx = std::get<1>(output);
expected_hx = torch::tensor({-0.0443, 0.1537});
expected_cx = torch::tensor({-0.1195, 0.2144});
ASSERT_TRUE(torch::allclose(output_hx, expected_hx, 1e-05, 2e-04));
ASSERT_TRUE(torch::allclose(output_cx, expected_cx, 1e-05, 2e-04));

{
auto input = torch::randn({3, 2});
auto hx = torch::randn({3, 2});
auto cx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"input has inconsistent input_size: got 2 expected 1");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1});
auto cx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"hidden0 has inconsistent hidden_size: got 1, expected 2");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 2});
auto cx = torch::randn({3, 1});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"hidden1 has inconsistent hidden_size: got 1, expected 2");
}

{
auto input = torch::randn({3, 1, 1, 1, 1});
auto hx = torch::randn({3, 1});
auto cx = torch::randn({3, 1});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"Expected input to be 1D or 2D, got 5D instead");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1, 1, 1, 2});
auto cx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"Expected hx[0] to be 1D or 2D, got 5D instead");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 2});
auto cx = torch::randn({3, 1, 1, 1, 2});
ASSERT_THROWS_WITH(
lstm(input, std::make_tuple(hx, cx)),
"Expected hx[1] to be 1D or 2D, got 5D instead");
}
}

TEST_F(ModulesTest, GRUCell) {
torch::manual_seed(0);
auto rnn = GRUCell(1, 2);
auto gru = GRUCell(1, 2);

auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 2});
auto output = rnn(input, hx);
auto output = gru(input, hx);
auto expected =
torch::tensor({{1.0243, 0.3227}, {-0.5659, 0.0330}, {-0.4030, -0.2800}});
ASSERT_TRUE(torch::allclose(output, expected, 1e-05, 2e-04));

output = rnn(input);
output = gru(input);
expected =
torch::tensor({{-0.0085, 0.1095}, {-0.1291, 0.2675}, {-0.1339, 0.2725}});
ASSERT_TRUE(torch::allclose(output, expected, 1e-05, 2e-04));

input = torch::randn({1});
hx = torch::randn({2});
output = gru(input, hx);
expected = torch::tensor({-1.0058, -0.3025});
ASSERT_TRUE(torch::allclose(output, expected, 1e-05, 2e-04));

{
auto input = torch::randn({3, 2});
auto hx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
gru(input, hx), "input has inconsistent input_size: got 2 expected 1");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1});
ASSERT_THROWS_WITH(
gru(input, hx),
"hidden0 has inconsistent hidden_size: got 1, expected 2");
}

{
auto input = torch::randn({3, 1, 1, 1, 1});
auto hx = torch::randn({3, 2});
ASSERT_THROWS_WITH(
gru(input, hx), "Expected input to be 1D or 2D, got 5D instead");
}

{
auto input = torch::randn({3, 1});
auto hx = torch::randn({3, 1, 1, 1, 2});
ASSERT_THROWS_WITH(
gru(input, hx), "Expected hidden to be 1D or 2D, got 5D instead");
}
}

TEST_F(ModulesTest, PrettyPrintLinear) {
Expand Down
6 changes: 1 addition & 5 deletions torch/csrc/api/include/torch/nn/modules/rnn.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,7 @@ class TORCH_API RNNCellImplBase : public torch::nn::Cloneable<Derived> {
Tensor bias_hh;

protected:
void check_forward_input(const Tensor& input) const;
void check_forward_hidden(
const Tensor& input,
const Tensor& hx,
std::string hidden_label) const;
void check_forward_input(const Tensor& input, const std::string name) const;
virtual std::string get_nonlinearity_str() const;
};
} // namespace detail
Expand Down
Loading
Loading