[structural binding][11/N] Replace std::tie with structural binding (#130830)

Follows  #130784

Pull Request resolved: https://github.com/pytorch/pytorch/pull/130830
Approved by: https://github.com/janeyx99
This commit is contained in:
cyy
2024-07-18 00:45:05 +00:00
committed by PyTorch MergeBot
parent e14d1d10ef
commit 73d0f484b3
6 changed files with 42 additions and 73 deletions

View File

@ -314,8 +314,7 @@ TEST_F(ModulesTest, MaxPool1d) {
TEST_F(ModulesTest, MaxPool1dReturnIndices) {
MaxPool1d model(MaxPool1dOptions(3).stride(2));
auto x = torch::ones({1, 1, 5}, torch::requires_grad());
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 3);
ASSERT_TRUE(torch::allclose(y, torch::ones({1, 1, 2})));
@ -355,8 +354,7 @@ TEST_F(ModulesTest, MaxPool2dUneven) {
TEST_F(ModulesTest, MaxPool2dReturnIndices) {
MaxPool2d model(MaxPool2dOptions(3).stride(2));
auto x = torch::ones({2, 5, 5}, torch::requires_grad());
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 3);
ASSERT_TRUE(torch::allclose(y, torch::ones({2, 2, 2})));
@ -383,8 +381,7 @@ TEST_F(ModulesTest, MaxPool3d) {
TEST_F(ModulesTest, MaxPool3dReturnIndices) {
MaxPool3d model(MaxPool3dOptions(3).stride(2));
auto x = torch::ones({2, 5, 5, 5}, torch::requires_grad());
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 4);
ASSERT_TRUE(torch::allclose(y, torch::ones({2, 2, 2, 2})));
@ -467,8 +464,7 @@ TEST_F(ModulesTest, FractionalMaxPool2d) {
TEST_F(ModulesTest, FractionalMaxPool2dReturnIndices) {
FractionalMaxPool2d model(FractionalMaxPool2dOptions(3).output_size(2));
auto x = torch::ones({2, 5, 5}, torch::requires_grad());
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 3);
ASSERT_TRUE(torch::allclose(y, torch::ones({2, 2, 2})));
@ -494,8 +490,7 @@ TEST_F(ModulesTest, FractionalMaxPool3d) {
TEST_F(ModulesTest, FractionalMaxPool3dReturnIndices) {
FractionalMaxPool3d model(FractionalMaxPool3dOptions(3).output_size(2));
auto x = torch::ones({2, 5, 5, 5}, torch::requires_grad());
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 4);
ASSERT_TRUE(torch::allclose(y, torch::ones({2, 2, 2, 2})));
@ -655,8 +650,7 @@ TEST_F(ModulesTest, AdaptiveMaxPool1dReturnIndices) {
AdaptiveMaxPool1d model(3);
auto x = torch::tensor(
{{{1, 2, 3, 4, 5}}}, torch::dtype(torch::kFloat).requires_grad(true));
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
ASSERT_EQ(y.dim(), 3);
ASSERT_TRUE(torch::allclose(y, torch::tensor({{{2, 4, 5}}}, torch::kFloat)));
@ -712,8 +706,7 @@ TEST_F(ModulesTest, AdaptiveMaxPool2dReturnIndicesEven) {
AdaptiveMaxPool2d model(3);
auto x = torch::arange(0., 50);
x.resize_({2, 5, 5}).set_requires_grad(true);
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
torch::Tensor s = y.sum();
s.backward();
@ -746,8 +739,7 @@ TEST_F(ModulesTest, AdaptiveMaxPool2dReturnIndicesUneven) {
AdaptiveMaxPool2d model(AdaptiveMaxPool2dOptions({3, 2}));
auto x = torch::arange(0., 40);
x.resize_({2, 5, 4}).set_requires_grad(true);
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
torch::Tensor s = y.sum();
s.backward();
@ -803,8 +795,7 @@ TEST_F(ModulesTest, AdaptiveMaxPool3dReturnIndices) {
AdaptiveMaxPool3d model(3);
auto x = torch::arange(0., 64);
x.resize_({1, 4, 4, 4}).set_requires_grad(true);
torch::Tensor y, indices;
std::tie(y, indices) = model->forward_with_indices(x);
auto [y, indices] = model->forward_with_indices(x);
torch::Tensor s = y.sum();
s.backward();
@ -946,8 +937,7 @@ TEST_F(ModulesTest, MaxPool1d_MaxUnpool1d) {
MaxPool1d pool{MaxPool1dOptions(2).stride(2)};
MaxUnpool1d unpool{MaxUnpool1dOptions(2).stride(2)};
auto input = torch::tensor({{{1, 2, 3, 4, 5, 6, 7, 8}}}, torch::kFloat);
torch::Tensor output, indices;
std::tie(output, indices) = pool->forward_with_indices(input);
auto [output, indices] = pool->forward_with_indices(input);
ASSERT_TRUE(torch::allclose(
unpool(output, indices),
torch::tensor({{{0, 2, 0, 4, 0, 6, 0, 8}}}, torch::kFloat)));
@ -999,8 +989,7 @@ TEST_F(ModulesTest, MaxPool2d_MaxUnpool2d) {
auto input = torch::tensor(
{{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}}},
torch::kFloat);
torch::Tensor output, indices;
std::tie(output, indices) = pool->forward_with_indices(input);
auto [output, indices] = pool->forward_with_indices(input);
ASSERT_TRUE(torch::allclose(
unpool(output, indices),
torch::tensor(
@ -1061,8 +1050,7 @@ TEST_F(ModulesTest, MaxPool3d_MaxUnpool3d) {
MaxPool3d pool{MaxPool3dOptions(3).stride(2)};
MaxUnpool3d unpool{MaxUnpool3dOptions(3).stride(2)};
auto input = torch::randn({20, 16, 51, 33, 15});
torch::Tensor output, indices;
std::tie(output, indices) = pool->forward_with_indices(input);
auto [output, indices] = pool->forward_with_indices(input);
auto unpooled_output = unpool(output, indices);
ASSERT_EQ(
unpooled_output.sizes(), std::vector<int64_t>({20, 16, 51, 33, 15}));
@ -3755,9 +3743,7 @@ void _multihead_attn_test_helper(
/*dim=*/1);
}
}
torch::Tensor attn_heads;
torch::Tensor ref_attn_weight;
std::tie(attn_heads, ref_attn_weight) = _scaled_dot_attn_ref(
auto [attn_heads, ref_attn_weight] = _scaled_dot_attn_ref(
Q_split,
K_split,
V_split,