Skip to content

Commit b215101

Browse files
committed
test(ATen): add scatter_reduce index dtype cases
1 parent 88da8fe commit b215101

1 file changed

Lines changed: 114 additions & 0 deletions

File tree

test/ATen/ops/ScatterReduceTest.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <ATen/ATen.h>
22
#include <ATen/core/Tensor.h>
3+
#include <ATen/ops/scatter_reduce.h>
34
#include <gtest/gtest.h>
45

56
#include <string>
@@ -489,6 +490,102 @@ TEST_F(ScatterReduceTest, ScatterReduceInplaceNegativeIndex) {
489490
file.saveFile();
490491
}
491492

493+
// PyTorch C++ scatter_reduce accepts int32 index tensors.
494+
TEST_F(ScatterReduceTest, ScatterReduceIntIndex) {
495+
auto file_name = g_custom_param.get();
496+
FileManerger file(file_name);
497+
file.openAppend();
498+
file << "ScatterReduceIntIndex ";
499+
500+
at::Tensor self = at::zeros({3, 5}, at::kFloat);
501+
at::Tensor index = at::zeros({1, 5}, at::kInt);
502+
index.data_ptr<int>()[0] = 0;
503+
index.data_ptr<int>()[1] = 1;
504+
index.data_ptr<int>()[2] = 2;
505+
index.data_ptr<int>()[3] = 0;
506+
index.data_ptr<int>()[4] = 0;
507+
at::Tensor src = at::full({1, 5}, 1.0f, at::kFloat);
508+
src.data_ptr<float>()[0] = 1.0f;
509+
src.data_ptr<float>()[1] = 2.0f;
510+
src.data_ptr<float>()[2] = 3.0f;
511+
src.data_ptr<float>()[3] = 4.0f;
512+
src.data_ptr<float>()[4] = 5.0f;
513+
at::Tensor result = self.scatter_reduce(0, index, src, "sum");
514+
write_scatter_reduce_result_to_file(&file, result);
515+
516+
file << "\n";
517+
file.saveFile();
518+
}
519+
520+
// PyTorch C++ scatter_reduce_ accepts int32 index tensors.
521+
TEST_F(ScatterReduceTest, ScatterReduceInplaceIntIndex) {
522+
auto file_name = g_custom_param.get();
523+
FileManerger file(file_name);
524+
file.openAppend();
525+
file << "ScatterReduceInplaceIntIndex ";
526+
527+
at::Tensor self = at::zeros({3, 5}, at::kFloat);
528+
at::Tensor index = at::zeros({1, 5}, at::kInt);
529+
index.data_ptr<int>()[0] = 0;
530+
index.data_ptr<int>()[1] = 1;
531+
index.data_ptr<int>()[2] = 2;
532+
index.data_ptr<int>()[3] = 0;
533+
index.data_ptr<int>()[4] = 0;
534+
at::Tensor src = at::full({1, 5}, 1.0f, at::kFloat);
535+
src.data_ptr<float>()[0] = 1.0f;
536+
src.data_ptr<float>()[1] = 2.0f;
537+
src.data_ptr<float>()[2] = 3.0f;
538+
src.data_ptr<float>()[3] = 4.0f;
539+
src.data_ptr<float>()[4] = 5.0f;
540+
self.scatter_reduce_(0, index, src, "sum");
541+
write_scatter_reduce_result_to_file(&file, self);
542+
543+
file << "\n";
544+
file.saveFile();
545+
}
546+
547+
// PyTorch C++ scatter_reduce rejects non-empty floating index tensors.
548+
TEST_F(ScatterReduceTest, ScatterReduceFloatIndex) {
549+
auto file_name = g_custom_param.get();
550+
FileManerger file(file_name);
551+
file.openAppend();
552+
file << "ScatterReduceFloatIndex ";
553+
554+
try {
555+
at::Tensor self = at::zeros({2, 2}, at::kFloat);
556+
at::Tensor index = at::zeros({1, 2}, at::kFloat);
557+
at::Tensor src = at::ones({1, 2}, at::kFloat);
558+
at::Tensor result = self.scatter_reduce(0, index, src, "sum");
559+
write_scatter_reduce_result_to_file(&file, result);
560+
} catch (const std::exception&) {
561+
file << "exception ";
562+
}
563+
564+
file << "\n";
565+
file.saveFile();
566+
}
567+
568+
// PyTorch C++ scatter_reduce_ rejects non-empty floating index tensors.
569+
TEST_F(ScatterReduceTest, ScatterReduceInplaceFloatIndex) {
570+
auto file_name = g_custom_param.get();
571+
FileManerger file(file_name);
572+
file.openAppend();
573+
file << "ScatterReduceInplaceFloatIndex ";
574+
575+
try {
576+
at::Tensor self = at::zeros({2, 2}, at::kFloat);
577+
at::Tensor index = at::zeros({1, 2}, at::kFloat);
578+
at::Tensor src = at::ones({1, 2}, at::kFloat);
579+
self.scatter_reduce_(0, index, src, "sum");
580+
write_scatter_reduce_result_to_file(&file, self);
581+
} catch (const std::exception&) {
582+
file << "exception ";
583+
}
584+
585+
file << "\n";
586+
file.saveFile();
587+
}
588+
492589
// Shape: small 2D, Dtype: kFloat, In-place scatter_reduce_
493590
TEST_F(ScatterReduceTest, ScatterReduceInplaceFloatSmall) {
494591
auto file_name = g_custom_param.get();
@@ -528,6 +625,23 @@ TEST_F(ScatterReduceTest, ScatterReduceEmpty) {
528625
file.saveFile();
529626
}
530627

628+
// Boundary: empty floating index tensor is accepted by PyTorch C++.
629+
TEST_F(ScatterReduceTest, ScatterReduceEmptyFloatIndex) {
630+
auto file_name = g_custom_param.get();
631+
FileManerger file(file_name);
632+
file.openAppend();
633+
file << "ScatterReduceEmptyFloatIndex ";
634+
635+
at::Tensor self = at::zeros({0, 2}, at::kFloat);
636+
at::Tensor index = at::empty({0, 2}, at::kFloat);
637+
at::Tensor src = at::empty({0, 2}, at::kFloat);
638+
at::Tensor result = self.scatter_reduce(0, index, src, "sum");
639+
write_scatter_reduce_result_to_file(&file, result);
640+
641+
file << "\n";
642+
file.saveFile();
643+
}
644+
531645
// Shape: small 2D, Dtype: kFloat, Reduce: prod, include_self=false
532646
TEST_F(ScatterReduceTest, ScatterReduceProdFloatNoIncludeSelf) {
533647
auto file_name = g_custom_param.get();

0 commit comments

Comments
 (0)