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
7678static 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
8483static 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
150146TEST_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)
170161TEST_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 ();
@@ -197,6 +187,24 @@ TEST_F(TakeTest, TakeFloatEmptyIndex) {
197187 file.saveFile ();
198188}
199189
190+ TEST_F (TakeTest, TakeEmptyInputNonEmptyIndexThrows) {
191+ at::Tensor t = at::empty ({0 }, at::TensorOptions ().dtype (at::kFloat ));
192+ at::Tensor index = make_index_tensor ({0 });
193+
194+ auto file_name = g_custom_param.get ();
195+ FileManerger file (file_name);
196+ file.openAppend ();
197+ file << " TakeEmptyInputNonEmptyIndexThrows " ;
198+ try {
199+ at::Tensor result = at::take (t, index);
200+ write_result_to_file (&file, result);
201+ } catch (const std::exception&) {
202+ file << " exception " ;
203+ }
204+ file << " \n " ;
205+ file.saveFile ();
206+ }
207+
200208// Shape: duplicate indices
201209TEST_F (TakeTest, TakeFloatDuplicateIndices) {
202210 at::Tensor t = make_test_tensor (at::kFloat );
@@ -224,8 +232,8 @@ TEST_F(TakeTest, TakeExceptionOutOfRange) {
224232 try {
225233 at::Tensor result = at::take (t, index);
226234 write_result_to_file (&file, result);
227- } catch (const std::exception& e ) {
228- file << " exception: " ;
235+ } catch (const std::exception&) {
236+ file << " exception " ;
229237 }
230238 file << " \n " ;
231239 file.saveFile ();
0 commit comments