mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
new QNNPACK dwconv support and tests (#13652)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/13652 new dwconv 3x3 5x5 tests provided Reviewed By: Maratyszcza Differential Revision: D12951866 fbshipit-source-id: f853bb7412a724de594ed36c6b2b69ec268d6464
This commit is contained in:
parent
1413dd4bfc
commit
94fe8faa00
|
|
@ -617,6 +617,128 @@ TEST(Int8, DepthwiseConv) {
|
|||
EXPECT_TENSOR_APPROX_EQ(*YA, YE, 1.0e-5);
|
||||
}
|
||||
|
||||
TEST(Int8, DepthwiseConv3x3) {
|
||||
auto XQ = q({1, 3, 3, 3});
|
||||
XQ->scale = 0.5;
|
||||
XQ->zero_point = 127;
|
||||
setq(XQ.get(), std::vector<float>{1, 4, 3, 2, 9, 3, 8, 2, 6,
|
||||
7, 8, 2, 3, 4, 5, 2, 4, 4,
|
||||
9, 8, 7, 6, 5, 4, 3, 2, 1});
|
||||
|
||||
auto WQ = q({3, 3, 3, 1});
|
||||
WQ->scale = 0.5;
|
||||
WQ->zero_point = 127;
|
||||
setq(WQ.get(), std::vector<float>{1, -4, 3, 2, -9, 3, -8, 2, 6,
|
||||
7, 8, -2, -3, 4, -5, -2, 4, 4,
|
||||
-9, 8, -7, 6, -5, 4, 3, -2, 1});
|
||||
auto BQ = biasq({3}, XQ->scale * WQ->scale);
|
||||
biassetq(BQ.get(), {1, 2, 3});
|
||||
auto X = dq(*XQ);
|
||||
auto W = dq(*WQ);
|
||||
auto B = biasdq(*BQ);
|
||||
auto xop = CreateOperatorDef(
|
||||
"Conv",
|
||||
"",
|
||||
{"XT", "WT", "B"},
|
||||
{"YT"},
|
||||
{MakeArgument<int>("kernel", 3),
|
||||
MakeArgument<string>("order", "NCHW"),
|
||||
MakeArgument<int>("group", 3)});
|
||||
auto op = CreateOperatorDef(
|
||||
"Int8Conv",
|
||||
"",
|
||||
{"XQ", "WQ", "BQ"},
|
||||
{"YQ"},
|
||||
{MakeArgument<int>("kernel", 3),
|
||||
MakeArgument<string>("order", "NHWC"),
|
||||
MakeArgument<int>("group", 3),
|
||||
MakeArgument<int>("Y_zero_point", 127),
|
||||
MakeArgument<float>("Y_scale", 1.0)});
|
||||
Workspace ws;
|
||||
int8Copy(ws.CreateBlob("XQ")->GetMutable<int8::Int8TensorCPU>(), *XQ);
|
||||
int8Copy(ws.CreateBlob("WQ")->GetMutable<int8::Int8TensorCPU>(), *WQ);
|
||||
int8Copy(ws.CreateBlob("BQ")->GetMutable<int8::Int8TensorCPU>(), *BQ);
|
||||
BlobGetMutableTensor(ws.CreateBlob("X"), CPU)->CopyFrom(*X);
|
||||
BlobGetMutableTensor(ws.CreateBlob("W"), CPU)->CopyFrom(*W);
|
||||
BlobGetMutableTensor(ws.CreateBlob("B"), CPU)->CopyFrom(*B);
|
||||
ws.RunOperatorOnce(op);
|
||||
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NHWC2NCHW", "", {"X"}, {"XT"}));
|
||||
// Need to transpose MxKHxKWx1 to Mx1xKHxKW
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NHWC2NCHW", "", {"W"}, {"WT"}));
|
||||
ws.RunOperatorOnce(xop);
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NCHW2NHWC", "", {"YT"}, {"Y"}));
|
||||
const auto& YQ = ws.GetBlob("YQ")->Get<int8::Int8TensorCPU>();
|
||||
auto YA = dq(YQ);
|
||||
const auto& YE = ws.GetBlob("Y")->Get<TensorCPU>();
|
||||
for (auto i = 0; i < YA->numel(); ++i) {
|
||||
LOG(INFO) << YA->data<float>()[i];
|
||||
LOG(INFO) << YE.data<float>()[i];
|
||||
}
|
||||
EXPECT_TENSOR_APPROX_EQ(*YA, YE, 1.0e-5);
|
||||
}
|
||||
|
||||
TEST(Int8, DepthwiseConv5x5) {
|
||||
auto XQ = q({1, 5, 5, 1});
|
||||
XQ->scale = 0.5;
|
||||
XQ->zero_point = 127;
|
||||
setq(XQ.get(), std::vector<float>{1, 4, 3, 2, 9, 3, 8, 2, 6,
|
||||
7, 8, 2, 3, 4, 5, 2, 4, 4,
|
||||
9, 8, 7, 6, 5, 4, 3});
|
||||
|
||||
auto WQ = q({1, 5, 5, 1});
|
||||
WQ->scale = 0.5;
|
||||
WQ->zero_point = 127;
|
||||
setq(WQ.get(), std::vector<float>{1, -4, 3, 2, -9, 3, -8, 2, 6,
|
||||
7, 8, -2, -3, 4, -5, -2, 4, 4,
|
||||
-9, 8, -7, 6, -5, 4, 3});
|
||||
auto BQ = biasq({1}, XQ->scale * WQ->scale);
|
||||
biassetq(BQ.get(), {1});
|
||||
auto X = dq(*XQ);
|
||||
auto W = dq(*WQ);
|
||||
auto B = biasdq(*BQ);
|
||||
auto xop = CreateOperatorDef(
|
||||
"Conv",
|
||||
"",
|
||||
{"XT", "WT", "B"},
|
||||
{"YT"},
|
||||
{MakeArgument<int>("kernel", 5),
|
||||
MakeArgument<string>("order", "NCHW"),
|
||||
MakeArgument<int>("group", 1)});
|
||||
auto op = CreateOperatorDef(
|
||||
"Int8Conv",
|
||||
"",
|
||||
{"XQ", "WQ", "BQ"},
|
||||
{"YQ"},
|
||||
{MakeArgument<int>("kernel", 5),
|
||||
MakeArgument<string>("order", "NHWC"),
|
||||
MakeArgument<int>("group", 1),
|
||||
MakeArgument<int>("Y_zero_point", 127),
|
||||
MakeArgument<float>("Y_scale", 1.0)});
|
||||
Workspace ws;
|
||||
int8Copy(ws.CreateBlob("XQ")->GetMutable<int8::Int8TensorCPU>(), *XQ);
|
||||
int8Copy(ws.CreateBlob("WQ")->GetMutable<int8::Int8TensorCPU>(), *WQ);
|
||||
int8Copy(ws.CreateBlob("BQ")->GetMutable<int8::Int8TensorCPU>(), *BQ);
|
||||
BlobGetMutableTensor(ws.CreateBlob("X"), CPU)->CopyFrom(*X);
|
||||
BlobGetMutableTensor(ws.CreateBlob("W"), CPU)->CopyFrom(*W);
|
||||
BlobGetMutableTensor(ws.CreateBlob("B"), CPU)->CopyFrom(*B);
|
||||
ws.RunOperatorOnce(op);
|
||||
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NHWC2NCHW", "", {"X"}, {"XT"}));
|
||||
// Need to transpose MxKHxKWx1 to Mx1xKHxKW
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NHWC2NCHW", "", {"W"}, {"WT"}));
|
||||
ws.RunOperatorOnce(xop);
|
||||
ws.RunOperatorOnce(CreateOperatorDef("NCHW2NHWC", "", {"YT"}, {"Y"}));
|
||||
const auto& YQ = ws.GetBlob("YQ")->Get<int8::Int8TensorCPU>();
|
||||
auto YA = dq(YQ);
|
||||
const auto& YE = ws.GetBlob("Y")->Get<TensorCPU>();
|
||||
for (auto i = 0; i < YA->numel(); ++i) {
|
||||
LOG(INFO) << YA->data<float>()[i];
|
||||
LOG(INFO) << YE.data<float>()[i];
|
||||
}
|
||||
EXPECT_TENSOR_APPROX_EQ(*YA, YE, 1.0e-5);
|
||||
}
|
||||
|
||||
TEST(Int8, ConvTranspose) {
|
||||
auto XQ = q({1, 3, 6, 1});
|
||||
XQ->scale = 0.5;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user