Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fuse adaptive pool dynamic output size, implement ncnn adaptive pooling dynamic outsize #5043

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/layer/pooling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,16 @@ int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) c

if (adaptive_pooling)
{
top_blob.create(out_w, out_h, channels, elemsize, opt.blob_allocator);
int _out_w = out_w == -233 ? w : out_w;
int _out_h = out_h == -233 ? h : out_h;

if (_out_w == w && _out_h == h)
{
top_blob = bottom_blob;
return 0;
}

top_blob.create(_out_w, _out_h, channels, elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;

Expand All @@ -116,18 +125,18 @@ int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) c
const float* inptr = bottom_blob.channel(q);
float* outptr = top_blob.channel(q);

for (int i = 0; i < out_h; i++)
for (int i = 0; i < _out_h; i++)
{
// floor div
const int ih0 = h * i / out_h;
const int ih0 = h * i / _out_h;
// ceil div
const int ih1 = (h * (i + 1) + out_h - 1) / out_h;
for (int j = 0; j < out_w; j++)
const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h;
for (int j = 0; j < _out_w; j++)
{
// floor div
const int iw0 = w * j / out_w;
const int iw0 = w * j / _out_w;
// ceil div
const int iw1 = (w * (j + 1) + out_w - 1) / out_w;
const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w;

float max = inptr[ih0 * w + iw0];
for (int ih = ih0; ih < ih1; ih++)
Expand All @@ -140,7 +149,7 @@ int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) c

outptr[j] = max;
}
outptr += out_w;
outptr += _out_w;
}
}
}
Expand All @@ -152,19 +161,19 @@ int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) c
const float* inptr = bottom_blob.channel(q);
float* outptr = top_blob.channel(q);

for (int i = 0; i < out_h; i++)
for (int i = 0; i < _out_h; i++)
{
// floor div
const int ih0 = h * i / out_h;
const int ih0 = h * i / _out_h;
// ceil div
const int ih1 = (h * (i + 1) + out_h - 1) / out_h;
const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h;
const int hk = ih1 - ih0;
for (int j = 0; j < out_w; j++)
for (int j = 0; j < _out_w; j++)
{
// floor div
const int iw0 = w * j / out_w;
const int iw0 = w * j / _out_w;
// ceil div
const int iw1 = (w * (j + 1) + out_w - 1) / out_w;
const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w;
const int wk = iw1 - iw0;

float sum = 0;
Expand All @@ -179,7 +188,7 @@ int Pooling::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) c
outptr[j] = sum / hk / wk;
}

outptr += out_w;
outptr += _out_w;
}
}
}
Expand Down
52 changes: 31 additions & 21 deletions src/layer/pooling3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,17 @@ int Pooling3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)

if (adaptive_pooling)
{
top_blob.create(out_w, out_h, out_d, channels, elemsize, opt.blob_allocator);
int _out_w = out_w == -233 ? w : out_w;
int _out_h = out_h == -233 ? h : out_h;
int _out_d = out_d == -233 ? d : out_d;

if (_out_w == w && _out_h == h && _out_d == d)
{
top_blob = bottom_blob;
return 0;
}

top_blob.create(_out_w, _out_h, _out_d, channels, elemsize, opt.blob_allocator);
if (top_blob.empty())
return -100;

Expand All @@ -120,24 +130,24 @@ int Pooling3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
const float* inptr = bottom_blob.channel(q);
float* outptr = top_blob.channel(q);

for (int z = 0; z < out_d; z++)
for (int z = 0; z < _out_d; z++)
{
// floor div
const int id0 = d * z / out_d;
const int id0 = d * z / _out_d;
// ceil div
const int id1 = (d * (z + 1) + out_d - 1) / out_d;
for (int i = 0; i < out_h; i++)
const int id1 = (d * (z + 1) + _out_d - 1) / _out_d;
for (int i = 0; i < _out_h; i++)
{
// floor div
const int ih0 = h * i / out_h;
const int ih0 = h * i / _out_h;
// ceil div
const int ih1 = (h * (i + 1) + out_h - 1) / out_h;
for (int j = 0; j < out_w; j++)
const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h;
for (int j = 0; j < _out_w; j++)
{
// floor div
const int iw0 = w * j / out_w;
const int iw0 = w * j / _out_w;
// ceil div
const int iw1 = (w * (j + 1) + out_w - 1) / out_w;
const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w;

float max_value = inptr[id0 * w * h + ih0 * w + iw0];

Expand All @@ -155,7 +165,7 @@ int Pooling3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
outptr[j] = max_value;
}

outptr += out_w;
outptr += _out_w;
}
}
}
Expand All @@ -168,26 +178,26 @@ int Pooling3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
const float* inptr = bottom_blob.channel(q);
float* outptr = top_blob.channel(q);

for (int z = 0; z < out_d; z++)
for (int z = 0; z < _out_d; z++)
{
// floor div
const int id0 = d * z / out_d;
const int id0 = d * z / _out_d;
// ceil div
const int id1 = (d * (z + 1) + out_d - 1) / out_d;
const int id1 = (d * (z + 1) + _out_d - 1) / _out_d;
int dk = id1 - id0;
for (int i = 0; i < out_h; i++)
for (int i = 0; i < _out_h; i++)
{
// floor div
const int ih0 = h * i / out_h;
const int ih0 = h * i / _out_h;
// ceil div
const int ih1 = (h * (i + 1) + out_h - 1) / out_h;
const int ih1 = (h * (i + 1) + _out_h - 1) / _out_h;
int hk = ih1 - ih0;
for (int j = 0; j < out_w; j++)
for (int j = 0; j < _out_w; j++)
{
// floor div
const int iw0 = w * j / out_w;
const int iw0 = w * j / _out_w;
// ceil div
const int iw1 = (w * (j + 1) + out_w - 1) / out_w;
const int iw1 = (w * (j + 1) + _out_w - 1) / _out_w;
int wk = iw1 - iw0;

float sum = 0;
Expand All @@ -205,7 +215,7 @@ int Pooling3D::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt)
outptr[j] = sum / hk / wk / dk;
}

outptr += out_w;
outptr += _out_w;
}
}
}
Expand Down
22 changes: 20 additions & 2 deletions src/layer/vulkan/pooling_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,16 @@ int Pooling_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute

if (adaptive_pooling)
{
top_blob.create(out_w, out_h, channels, elemsize, elempack, opt.blob_vkallocator);
int _out_w = out_w == -233 ? w : out_w;
int _out_h = out_h == -233 ? h : out_h;

if (_out_w == w && _out_h == h)
{
top_blob = bottom_blob;
return 0;
}

top_blob.create(_out_w, _out_h, channels, elemsize, elempack, opt.blob_vkallocator);
if (top_blob.empty())
return -100;

Expand Down Expand Up @@ -610,7 +619,16 @@ int Pooling_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob,

if (adaptive_pooling)
{
top_blob.create(out_w, out_h, channels, elemsize, elempack, opt.blob_vkallocator);
int _out_w = out_w == -233 ? w : out_w;
int _out_h = out_h == -233 ? h : out_h;

if (_out_w == w && _out_h == h)
{
top_blob = bottom_blob;
return 0;
}

top_blob.create(_out_w, _out_h, channels, elemsize, elempack, opt.blob_vkallocator);
if (top_blob.empty())
return -100;

Expand Down
1 change: 1 addition & 0 deletions tools/pnnx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ set(pnnx_pass_level3_SRCS
pass_level3/expand_quantization_modules.cpp
pass_level3/fuse_opnto1_tensors.cpp
pass_level3/fuse_op1ton_unpack.cpp
pass_level3/fuse_dynamic_adaptive_pool.cpp
pass_level3/fuse_einsum_operands.cpp
pass_level3/fuse_expression.cpp
pass_level3/fuse_index_expression.cpp
Expand Down
54 changes: 52 additions & 2 deletions tools/pnnx/src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,13 @@ Parameter::Parameter(const torch::jit::Node* value_node)
type = 5;
for (const auto& x : value_node->inputs())
{
if (!x->node()->hasAttribute(torch::jit::attr::value))
{
fprintf(stderr, "no attribute value in int list\n");
ai.push_back(0);
continue;
}

ai.push_back((int)x->node()->i(torch::jit::attr::value));
}
break;
Expand All @@ -300,6 +307,13 @@ Parameter::Parameter(const torch::jit::Node* value_node)
type = 6;
for (const auto& x : value_node->inputs())
{
if (!x->node()->hasAttribute(torch::jit::attr::value))
{
fprintf(stderr, "no attribute value in float list\n");
af.push_back(0.f);
continue;
}

af.push_back((float)x->node()->f(torch::jit::attr::value));
}
break;
Expand All @@ -309,6 +323,13 @@ Parameter::Parameter(const torch::jit::Node* value_node)
type = 7;
for (const auto& x : value_node->inputs())
{
if (!x->node()->hasAttribute(torch::jit::attr::value))
{
fprintf(stderr, "no attribute value in string list\n");
as.push_back("");
continue;
}

as.push_back(x->node()->s(torch::jit::attr::value));
}
break;
Expand All @@ -319,6 +340,13 @@ Parameter::Parameter(const torch::jit::Node* value_node)
type = 11;
for (const auto& x : value_node->inputs())
{
if (!x->node()->hasAttribute(torch::jit::attr::value))
{
fprintf(stderr, "no attribute value in complex list\n");
ac.push_back(std::complex<float>(0.f, 0.f));
continue;
}

ac.push_back(std::complex<float>(x->node()->c(torch::jit::attr::value)));
}
break;
Expand Down Expand Up @@ -1629,7 +1657,18 @@ int Graph::python(const std::string& pypath, const std::string& pnnxbinpath)
fprintf(pyfp, "(");
for (size_t i = 0; i < param.ai.size(); i++)
{
fprintf(pyfp, "%d", param.ai[i]);
if ((op->type == "nn.AdaptiveAvgPool2d"
|| op->type == "nn.AdaptiveAvgPool3d"
|| op->type == "nn.AdaptiveMaxPool2d"
|| op->type == "nn.AdaptiveMaxPool3d")
&& it.first == "output_size" && param.ai[i] == 0)
{
fprintf(pyfp, "None");
}
else
{
fprintf(pyfp, "%d", param.ai[i]);
}
if (i + 1 != param.ai.size() || param.ai.size() == 1)
fprintf(pyfp, ",");
}
Expand Down Expand Up @@ -2299,7 +2338,18 @@ int Graph::python(const std::string& pypath, const std::string& pnnxbinpath)
fprintf(pyfp, "(");
for (size_t i = 0; i < param.ai.size(); i++)
{
fprintf(pyfp, "%d", param.ai[i]);
if ((op->type == "F.adaptive_avg_pool2d"
|| op->type == "F.adaptive_avg_pool3d"
|| op->type == "F.adaptive_max_pool2d"
|| op->type == "F.adaptive_max_pool3d")
&& it.first == "output_size" && param.ai[i] == 0)
{
fprintf(pyfp, "None");
}
else
{
fprintf(pyfp, "%d", param.ai[i]);
}
if (i + 1 != param.ai.size() || param.ai.size() == 1)
fprintf(pyfp, ",");
}
Expand Down
6 changes: 3 additions & 3 deletions tools/pnnx/src/pass_level2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ bool GraphRewriterPass::match(const std::map<std::string, Parameter>& captured_p
return match(captured_params);
}

bool GraphRewriterPass::match(const std::map<std::string, const Operator*>& /*matched_operators*/) const
bool GraphRewriterPass::match(const std::map<std::string, const Operator*>& /*matched_operators*/, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
{
return true;
return match(captured_params, captured_attrs);
}

void GraphRewriterPass::write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
Expand Down Expand Up @@ -893,7 +893,7 @@ void pnnx_graph_rewrite(Graph& graph, const GraphRewriterPass* pass, int& opinde
break;
}

if (matched && (!pass->match(captured_params, captured_attrs) || !pass->match(matched_operators)))
if (matched && !pass->match(matched_operators, captured_params, captured_attrs))
{
matched_operators.clear();
matched_inputs.clear();
Expand Down
2 changes: 1 addition & 1 deletion tools/pnnx/src/pass_level2.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GraphRewriterPass

virtual bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const;

virtual bool match(const std::map<std::string, const Operator*>& matched_operators) const;
virtual bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const;

virtual void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const;

Expand Down
3 changes: 3 additions & 0 deletions tools/pnnx/src/pass_level3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "pass_level3/expand_quantization_modules.h"
#include "pass_level3/fuse_opnto1_tensors.h"
#include "pass_level3/fuse_op1ton_unpack.h"
#include "pass_level3/fuse_dynamic_adaptive_pool.h"
#include "pass_level3/fuse_einsum_operands.h"
#include "pass_level3/fuse_expression.h"
#include "pass_level3/fuse_index_expression.h"
Expand Down Expand Up @@ -52,6 +53,8 @@ void pass_level3(Graph& g, const std::set<std::string>& foldable_constants, cons

fuse_rnn_unpack(g);

fuse_dynamic_adaptive_pool(g);

expand_quantization_modules(g);

eliminate_tuple_pair(g);
Expand Down
Loading
Loading