未验证 提交 1716db63 编写于 作者: Z zhupengyang 提交者: GitHub

[xpu] fix xpu unittests: cast, transpose, pool, layer_norm, elementwise, multiclass_nms (#4259)

上级 ad097764
......@@ -88,7 +88,8 @@ int CastConverter(void* ctx, OpLite* op, KernelBase* kernel) {
// Cast node
graph->Add(
out_name,
graph->builder_.CreateCast(*x_node->data(), CvtPrecisionType(out_ptype)));
graph->builder_.CreateCast(*x_node->data(), CvtPrecisionType(out_ptype)),
PrecisionType(out_ptype));
return SUCCESS;
}
......
......@@ -54,6 +54,7 @@ bool SubgraphEngine::BuildDeviceProgram() {
return false;
}
}
// Collect the input and output nodes of the XPU IR graph
std::vector<xtcl::xExpr*> device_inodes;
std::vector<xtcl::xExpr*> device_onodes;
......@@ -62,16 +63,20 @@ bool SubgraphEngine::BuildDeviceProgram() {
CHECK(graph.Get(input_names_[i])->is_data());
device_inodes.push_back(graph.Get(input_names_[i])->data().get());
}
std::vector<std::string> valid_output_names;
for (size_t i = 0; i < output_names_.size(); i++) {
if (graph.Has(output_names_[i])) {
device_onodes.push_back(graph.Get(output_names_[i])->data().get());
valid_output_names.push_back(output_names_[i]);
} else {
// update output_names_ and origin_otensors because some outputs may be
// useless
output_names_.erase(output_names_.begin() + i);
origin_otensors_.erase(origin_otensors_.begin() + i);
i--;
}
}
// update output_names_ because some outputs may be useless
output_names_ = valid_output_names;
CHECK_GT(output_names_.size(), 0);
CHECK_EQ(output_names_.size(), origin_otensors_.size());
// Build the XPU IR graph to the XPU runtime for inference
device_program_ = lite::xpu::Device::Global().Build(
&graph.builder_, &graph.params_, &device_onodes);
......
......@@ -135,8 +135,8 @@ TEST(Cast, precision) {
float abs_error = 2e-5;
#if defined(LITE_WITH_ARM)
place = TARGET(kARM);
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
// place = TARGET(kXPU);
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
place = TARGET(kXPU);
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
place = TARGET(kHuaweiAscendNPU);
abs_error = 1e-2; // precision_mode default is force_fp16
......
......@@ -182,6 +182,12 @@ void TestElt(Place place,
std::vector<int64_t> y_shape,
int axis,
std::string act_type = "") {
#if defined(LITE_WITH_XPU)
if ((y_shape.size() != 1 && x_shape.size() != y_shape.size()) ||
elt_type != std::string("add") || !act_type.empty()) {
return;
}
#endif
std::unique_ptr<arena::TestCase> tester(new ElementwiseComputeTester(
place, "def", elt_type, x_shape, y_shape, axis, act_type));
arena::Arena arena(std::move(tester), place, abs_error);
......@@ -231,8 +237,8 @@ TEST(Elementwise, precision) {
abs_error = 1e-2; // precision_mode default is force_fp16
#elif defined(LITE_WITH_ARM)
place = TARGET(kARM);
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
// place = TARGET(kXPU);
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
place = TARGET(kXPU);
#else
return;
#endif
......
......@@ -156,8 +156,8 @@ TEST(LayerNorm, precision) {
#elif defined(LITE_WITH_ARM)
place = TARGET(kARM);
abs_error = 6e-5;
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
// place = TARGET(kXPU);
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
place = TARGET(kXPU);
#else
return;
#endif
......
......@@ -478,8 +478,6 @@ TEST(multiclass_nms, precision) {
Place place;
#if defined(LITE_WITH_ARM)
place = TARGET(kHost);
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
// place = TARGET(kXPU);
#else
return;
#endif
......
......@@ -313,6 +313,7 @@ void TestPoolPaddings(Place place, float abs_error = 2e-5) {
for (auto pooling_type : {"max", "avg"}) {
TestPoolHelper(
place, abs_error, {2, 3, 6, 7}, pooling_type, {1, 1}, {0, 0}, {2, 2});
#if !defined(LITE_WITH_XPU)
TestPoolHelper(
place, abs_error, {2, 3, 6, 7}, pooling_type, {1, 1}, {1, 1}, {2, 2});
TestPoolHelper(place,
......@@ -336,6 +337,7 @@ void TestPoolPaddings(Place place, float abs_error = 2e-5) {
{1, 1},
{1, 0, 0, 1},
{2, 2});
#endif
}
}
......@@ -349,6 +351,7 @@ void TestPoolKsize(Place place, float abs_error = 2e-5) {
{1, 1},
{0, 0},
{ksize, ksize});
#if !defined(LITE_WITH_XPU)
TestPoolHelper(place,
abs_error,
{2, 3, 6, 7},
......@@ -356,12 +359,16 @@ void TestPoolKsize(Place place, float abs_error = 2e-5) {
{2, 2},
{1, 1},
{ksize, ksize});
#endif
}
}
}
void TestPoolCeilMode(Place place, float abs_error = 2e-5) {
for (auto pooling_type : {"max", "avg"}) {
#if defined(LITE_WITH_XPU)
if (pooling_type == std::string("max")) continue;
#endif
TestPoolHelper(place,
abs_error,
{2, 3, 6, 6},
......@@ -384,8 +391,8 @@ TEST(Pool, precision) {
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
place = TARGET(kHuaweiAscendNPU);
abs_error = 1e-2; // precision_mode default is force_fp16
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL) // NOLINT
// place = TARGET(kXPU);
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL) // NOLINT
place = TARGET(kXPU);
#else
return;
#endif
......
......@@ -127,7 +127,12 @@ class TransposeComputeTester : public arena::TestCase {
void TestTranspose2D(Place place, float abs_error) {
DDim x_dims{{4, 5}};
std::vector<std::vector<int>> axes{{0, 1}, {1, 0}};
std::vector<std::vector<int>> axes {
#if !defined(LITE_WITH_XPU)
{0, 1},
#endif
{1, 0},
};
for (auto axis : axes) {
std::unique_ptr<arena::TestCase> tester(
new TransposeComputeTester(place, "def", x_dims, axis));
......@@ -138,8 +143,12 @@ void TestTranspose2D(Place place, float abs_error) {
void TestTranspose3D(Place place, float abs_error) {
DDim x_dims{{3, 4, 5}};
std::vector<std::vector<int>> axes{
{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {2, 1, 0}};
std::vector<std::vector<int>> axes {
#if !defined(LITE_WITH_XPU)
{0, 1, 2},
#endif
{0, 2, 1}, {1, 0, 2}, {2, 1, 0},
};
for (auto axis : axes) {
std::unique_ptr<arena::TestCase> tester(
new TransposeComputeTester(place, "def", x_dims, axis));
......@@ -150,8 +159,12 @@ void TestTranspose3D(Place place, float abs_error) {
void TestTranspose4D(Place place, float abs_error) {
DDim x_dims{{2, 3, 4, 5}};
std::vector<std::vector<int>> axes{
{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {3, 1, 2, 0}, {3, 1, 0, 2}};
std::vector<std::vector<int>> axes {
#if !defined(LITE_WITH_XPU)
{0, 1, 2, 3}, {0, 1, 3, 2}, {0, 2, 1, 3}, {3, 1, 2, 0}, {3, 1, 0, 2},
#endif
{0, 2, 3, 1}, {0, 3, 1, 2},
};
for (auto axis : axes) {
std::unique_ptr<arena::TestCase> tester(
new TransposeComputeTester(place, "def", x_dims, axis));
......@@ -170,8 +183,8 @@ TEST(Transpose, precision) {
#elif defined(LITE_WITH_HUAWEI_ASCEND_NPU)
place = TARGET(kHuaweiAscendNPU);
abs_error = 1e-2; // precision_mode default is force_fp16
// #elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL) // NOLINT
// place = TARGET(kXPU);
#elif defined(LITE_WITH_XPU) && defined(LITE_WITH_XTCL)
place = TARGET(kXPU);
#else
return;
#endif
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册