Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

op unittest for scatter_add, remove scatter/scatter_nd operator #1500

Merged
merged 11 commits into from
Jun 5, 2023
2 changes: 0 additions & 2 deletions cinn/auto_schedule/measure/simple_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ static const std::unordered_map<std::string, std::vector<int>> kInitWithZeroPara
{"lookup_table", {1}},
{"gather", {1}},
{"gather_nd", {1}},
{"scatter", {1}},
{"scatter_nd", {1}},
{"scatter_assign", {2}},
{"scatter_add", {2}},
};
Expand Down
27 changes: 0 additions & 27 deletions cinn/frontend/net_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -430,33 +430,6 @@ Variable NetBuilder::GatherNd(const Variable& x, const Variable& index) {
return CustomInstr("gather_nd", {x, index}, {}).front();
}

Variable NetBuilder::Scatter(const Variable& src, const Variable& index, const Variable& out, const int& axis) {
return CustomInstr("scatter", {src, index, out}, {{"axis", axis}}).front();
}
Variable NetBuilder::Scatter(const Variable& src,
const Variable& index,
const std::vector<int>& shape,
const float& default_value,
const int& axis) {
auto out = FillConstant(shape, default_value, UniqName("fill_constant"), "float", false);
return Scatter(src, index, out, axis);
}

Variable NetBuilder::ScatterNd(const Variable& src,
const Variable& index,
const Variable& out,
const std::vector<int>& axes) {
return CustomInstr("scatter_nd", {src, index, out}, {{"axes", axes}}).front();
}
Variable NetBuilder::ScatterNd(const Variable& src,
const Variable& index,
const std::vector<int>& shape,
const float& default_value,
const std::vector<int>& axes) {
auto out = FillConstant(shape, default_value, UniqName("fill_constant"), "float", false);
return ScatterNd(src, index, out, axes);
}

Variable NetBuilder::Cast(const Variable& operand, const std::string& dtype) {
return CustomInstr("cast", {operand}, {{"dtype", dtype}}).front();
}
Expand Down
151 changes: 0 additions & 151 deletions cinn/frontend/net_builder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -354,157 +354,6 @@ TEST(net_build, program_execute_gather_nd) {
}
}

TEST(net_build, program_execute_scatter) {
const float default_value = 3.14;
const int B = 3;
const int H_IN = 4;
const int H_OUT = 11;

NetBuilder builder("net_builder");
Placeholder input1 = builder.CreateInput(Float(32), {B, H_IN}, "In1");
Placeholder input2 = builder.CreateInput(Int(32), {B, H_IN}, "In2");
Variable output = builder.Scatter(input1, input2, {B, H_OUT}, default_value, 1);
auto program = builder.Build();

#ifdef CINN_WITH_CUDA
Target target = common::DefaultNVGPUTarget();
#else
Target target = common::DefaultHostTarget();
#endif
std::unordered_set<std::string> fetch_ids;
auto graph = Optimize(&program, fetch_ids, target);

auto scope = BuildScope(target, graph);
hlir::framework::GraphCompiler gc(target, scope, graph);
auto runtime_program = gc.Build();

scope->Var<hlir::framework::Tensor>(std::string(input1.id()));
scope->Var<hlir::framework::Tensor>(std::string(input2.id()));
scope->Var<hlir::framework::Tensor>(std::string(output->id));

auto input1_tensor = scope->GetTensor(std::string(input1.id()));
SetRandData<float>(input1_tensor, target);
std::vector<float> input1_data = GetTensorData<float>(input1_tensor, target);

auto input2_tensor = scope->GetTensor(std::string(input2.id()));
SetRandInt(input2_tensor, target, -1, 0, H_IN);

std::vector<int> input2_data = GetTensorData<int>(input2_tensor, target);

runtime_program->Execute();

auto output_tensor = scope->GetTensor(std::string(output->id));
const std::vector<int>& output_shape = output_tensor->shape().data();
EXPECT_EQ(output_tensor->type(), Float(32));
EXPECT_EQ(output_shape.size(), 2UL);
EXPECT_EQ(output_shape[0], B);
EXPECT_EQ(output_shape[1], H_OUT);

float true_data[B * H_OUT];
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_OUT; ++h) {
int index = h + H_OUT * b;
true_data[index] = default_value;
}
}
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_IN; ++h) {
int index = h + H_IN * b;
true_data[input2_data[index] + H_OUT * b] = input1_data[index];
}
}

std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
VLOG(6) << "Visualize output_data";
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_OUT; ++h) {
std::string line;
int index = h + H_OUT * b;
float t_data = true_data[index];
float out_data = output_data[index];
line += (std::to_string(out_data) + ", ");
EXPECT_EQ(t_data, out_data);
VLOG(6) << line;
}
}
}

TEST(net_build, program_execute_scatter_nd) {
Context::Global().ResetNameId();
const float default_value = 3.14;
const int B = 3;
const int H_IN = 4;
const int H_OUT = 11;

NetBuilder builder("net_builder");
Placeholder input1 = builder.CreateInput(Float(32), {B, H_IN}, "In1");
Placeholder input2 = builder.CreateInput(Int(32), {B, H_IN, 1}, "In2");
Variable output = builder.ScatterNd(input1, input2, {B, H_OUT}, default_value, {1});
auto program = builder.Build();

#ifdef CINN_WITH_CUDA
Target target = common::DefaultNVGPUTarget();
#else
Target target = common::DefaultHostTarget();
#endif
std::unordered_set<std::string> fetch_ids;
auto graph = Optimize(&program, fetch_ids, target);

auto scope = BuildScope(target, graph);
hlir::framework::GraphCompiler gc(target, scope, graph);
auto runtime_program = gc.Build();

scope->Var<hlir::framework::Tensor>(std::string(input1.id()));
scope->Var<hlir::framework::Tensor>(std::string(input2.id()));
scope->Var<hlir::framework::Tensor>(std::string(output->id));

auto input1_tensor = scope->GetTensor(std::string(input1.id()));
SetRandData<float>(input1_tensor, target);

auto input2_tensor = scope->GetTensor(std::string(input2.id()));
SetRandInt(input2_tensor, target);

runtime_program->Execute();

std::vector<float> input1_data = GetTensorData<float>(input1_tensor, target);
std::vector<int> input2_data = GetTensorData<int>(input2_tensor, target);

auto output_tensor = scope->GetTensor(std::string(output->id));
const std::vector<int>& output_shape = output_tensor->shape().data();
EXPECT_EQ(output_tensor->type(), Float(32));
EXPECT_EQ(output_shape.size(), 2UL);
EXPECT_EQ(output_shape[0], B);
EXPECT_EQ(output_shape[1], H_OUT);

float true_data[B * H_OUT];
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_OUT; ++h) {
int index = h + H_OUT * b;
true_data[index] = default_value;
}
}
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_IN; ++h) {
int index = h + H_IN * b;
true_data[input2_data[index] + H_OUT * b] = input1_data[index];
}
}

std::vector<float> output_data = GetTensorData<float>(output_tensor, target);
VLOG(6) << "Visualize output_data";
for (int b = 0; b < B; ++b) {
for (int h = 0; h < H_OUT; ++h) {
std::string line;
int index = h + H_OUT * b;
float t_data = true_data[index];
float out_data = output_data[index];
line += (std::to_string(out_data) + ", ");
EXPECT_EQ(t_data, out_data);
VLOG(6) << line;
}
}
}

TEST(net_build, program_execute_cast) {
const int B = 4;
const int H = 7;
Expand Down
2 changes: 0 additions & 2 deletions cinn/hlir/op/contrib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ core_gather_headers()

gather_srcs(cinnapi_src SRCS
gather_nd.cc
scatter.cc
flip.cc
sort.cc
argmin.cc
Expand All @@ -22,7 +21,6 @@ gather_srcs(cinnapi_src SRCS
)

cc_test(test_gather_nd SRCS gather_nd_test.cc DEPS cinncore)
cc_test(test_scatter SRCS scatter_test.cc DEPS cinncore)
cc_test(test_sort SRCS sort_test.cc DEPS cinncore)
cc_test(test_argmin SRCS argmin_test.cc DEPS cinncore)
cc_test(test_argmax SRCS argmax_test.cc DEPS cinncore)
Expand Down
Loading