Skip to content

Commit d705ad9

Browse files
committed
test(ATen): refine take index cases
1 parent 0409fd8 commit d705ad9

1 file changed

Lines changed: 10 additions & 20 deletions

File tree

test/ATen/ops/TakeTest.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <ATen/ATen.h>
22
#include <ATen/core/Tensor.h>
3+
#include <ATen/ops/full.h>
34
#include <ATen/ops/take.h>
5+
#include <ATen/ops/tensor.h>
46
#include <ATen/ops/zeros.h>
57
#include <c10/util/Exception.h>
68
#include <gtest/gtest.h>
@@ -74,19 +76,13 @@ static at::Tensor make_test_tensor(at::ScalarType dtype) {
7476
}
7577

7678
static at::Tensor make_index_tensor(const std::vector<int64_t>& values) {
77-
auto t = at::empty({static_cast<int64_t>(values.size())},
78-
at::TensorOptions().dtype(at::kLong));
79-
int64_t* data = t.data_ptr<int64_t>();
80-
for (size_t i = 0; i < values.size(); ++i) data[i] = values[i];
81-
return t;
79+
return at::tensor(at::ArrayRef<int64_t>(values),
80+
at::TensorOptions().dtype(at::kLong));
8281
}
8382

8483
static at::Tensor make_int_index_tensor(const std::vector<int32_t>& values) {
85-
auto t = at::empty({static_cast<int64_t>(values.size())},
86-
at::TensorOptions().dtype(at::kInt));
87-
int32_t* data = t.data_ptr<int32_t>();
88-
for (size_t i = 0; i < values.size(); ++i) data[i] = values[i];
89-
return t;
84+
return at::tensor(at::ArrayRef<int32_t>(values),
85+
at::TensorOptions().dtype(at::kInt));
9086
}
9187

9288
// Shape: small 1D index
@@ -149,12 +145,7 @@ TEST_F(TakeTest, TakeLongSmall) {
149145
// Shape: multi-dimensional index
150146
TEST_F(TakeTest, TakeFloatMultiDimIndex) {
151147
at::Tensor t = make_test_tensor(at::kFloat);
152-
at::Tensor index = at::empty({2, 2}, at::TensorOptions().dtype(at::kLong));
153-
int64_t* data = index.data_ptr<int64_t>();
154-
data[0] = 0;
155-
data[1] = 3;
156-
data[2] = 7;
157-
data[3] = 10;
148+
at::Tensor index = make_index_tensor({0, 3, 7, 10}).reshape({2, 2});
158149
at::Tensor result = at::take(t, index);
159150

160151
auto file_name = g_custom_param.get();
@@ -169,8 +160,7 @@ TEST_F(TakeTest, TakeFloatMultiDimIndex) {
169160
// Shape: scalar index (0-dim)
170161
TEST_F(TakeTest, TakeFloatScalarIndex) {
171162
at::Tensor t = make_test_tensor(at::kFloat);
172-
at::Tensor index = at::empty({}, at::TensorOptions().dtype(at::kLong));
173-
index.data_ptr<int64_t>()[0] = 7;
163+
at::Tensor index = at::full({}, 7, at::kLong);
174164
at::Tensor result = at::take(t, index);
175165

176166
auto file_name = g_custom_param.get();
@@ -224,8 +214,8 @@ TEST_F(TakeTest, TakeExceptionOutOfRange) {
224214
try {
225215
at::Tensor result = at::take(t, index);
226216
write_result_to_file(&file, result);
227-
} catch (const std::exception& e) {
228-
file << "exception: ";
217+
} catch (const std::exception&) {
218+
file << "exception ";
229219
}
230220
file << "\n";
231221
file.saveFile();

0 commit comments

Comments
 (0)