Browse Source

fix adaptive pooling vulkan, second try

tags/20211122
nihui 4 years ago
parent
commit
a5b846371a
3 changed files with 21 additions and 29 deletions
  1. +7
    -10
      src/layer/vulkan/shader/pooling_adaptive.comp
  2. +7
    -10
      src/layer/vulkan/shader/pooling_adaptive_pack4.comp
  3. +7
    -9
      src/layer/vulkan/shader/pooling_adaptive_pack8.comp

+ 7
- 10
src/layer/vulkan/shader/pooling_adaptive.comp View File

@@ -73,12 +73,12 @@ void main()
afp res;

// calculate adaptive kernel size
const int iw0 = int(floor((afp(psc(w)) / afp(psc(outw))) * afp(gx)));
const int iw1 = int(ceil((afp(psc(w)) / afp(psc(outw))) * afp(gx + 1)));
const int kernel_w = iw1 - iw0;
const int ih0 = int(floor((afp(psc(h)) / afp(psc(outh))) * afp(gy)));
const int ih1 = int(ceil((afp(psc(h)) / afp(psc(outh))) * afp(gy + 1)));
const int kernel_h = ih1 - ih0;
const int sx = psc(w) * gx / psc(outw);
const int ex = (psc(w) * (gx + 1) + psc(outw) - 1) / psc(outw);
const int kernel_w = ex - sx;
const int sy = psc(h) * gy / psc(outh);
const int ey = (psc(h) * (gy + 1) + psc(outh) - 1) / psc(outh);
const int kernel_h = ey - sy;

if (pooling_type == 0)
{
@@ -112,7 +112,6 @@ void main()
if (pooling_type == 1)
{
float res_fp32 = 0.f; // force accumulation in fp32
int area = 0;

#if NCNN_image_shader
for (int y = 0; y < kernel_h; y++)
@@ -120,7 +119,6 @@ void main()
for (int x = 0; x < kernel_w; x++)
{
res_fp32 += image3d_ld1(bottom_blob, ivec3(sx + x, sy + y, gz));
area += 1;
}
}
#else
@@ -131,14 +129,13 @@ void main()
for (int x = 0; x < kernel_w; x++)
{
res_fp32 += buffer_ld1(bottom_blob_data, v_offset + x);
area += 1;
}

v_offset += psc(w);
}
#endif

res_fp32 /= float(area);
res_fp32 /= float(kernel_h * kernel_w);
res = afp(res_fp32); // cast to fp16 if possible
}



+ 7
- 10
src/layer/vulkan/shader/pooling_adaptive_pack4.comp View File

@@ -73,12 +73,12 @@ void main()
afpvec4 res;

// calculate adaptive kernel size
const int iw0 = int(floor((afp(psc(w)) / afp(psc(outw))) * afp(gx)));
const int iw1 = int(ceil((afp(psc(w)) / afp(psc(outw))) * afp(gx + 1)));
const int kernel_w = iw1 - iw0;
const int ih0 = int(floor((afp(psc(h)) / afp(psc(outh))) * afp(gy)));
const int ih1 = int(ceil((afp(psc(h)) / afp(psc(outh))) * afp(gy + 1)));
const int kernel_h = ih1 - ih0;
const int sx = psc(w) * gx / psc(outw);
const int ex = (psc(w) * (gx + 1) + psc(outw) - 1) / psc(outw);
const int kernel_w = ex - sx;
const int sy = psc(h) * gy / psc(outh);
const int ey = (psc(h) * (gy + 1) + psc(outh) - 1) / psc(outh);
const int kernel_h = ey - sy;

if (pooling_type == 0)
{
@@ -112,7 +112,6 @@ void main()
else if (pooling_type == 1)
{
vec4 res_fp32 = vec4(0.f); // force accumulation in fp32
int area = 0;

#if NCNN_image_shader
for (int y = 0; y < kernel_h; y++)
@@ -120,7 +119,6 @@ void main()
for (int x = 0; x < kernel_w; x++)
{
res_fp32 += image3d_ld4(bottom_blob, ivec3(sx + x, sy + y, gz));
area += 1;
}
}
#else
@@ -131,14 +129,13 @@ void main()
for (int x = 0; x < kernel_w; x++)
{
res_fp32 += buffer_ld4(bottom_blob_data, v_offset + x);
area += 1;
}

v_offset += psc(w);
}
#endif

res_fp32 /= float(area);
res_fp32 /= float(kernel_h * kernel_w);
res = afpvec4(res_fp32); // cast to fp16 if possible
}



+ 7
- 9
src/layer/vulkan/shader/pooling_adaptive_pack8.comp View File

@@ -74,12 +74,12 @@ void main()
afpvec8 res;

// calculate adaptive kernel size
const int iw0 = int(floor((afp(psc(w)) / afp(psc(outw))) * afp(gx)));
const int iw1 = int(ceil((afp(psc(w)) / afp(psc(outw))) * afp(gx + 1)));
const int kernel_w = iw1 - iw0;
const int ih0 = int(floor((afp(psc(h)) / afp(psc(outh))) * afp(gy)));
const int ih1 = int(ceil((afp(psc(h)) / afp(psc(outh))) * afp(gy + 1)));
const int kernel_h = ih1 - ih0;
const int sx = psc(w) * gx / psc(outw);
const int ex = (psc(w) * (gx + 1) + psc(outw) - 1) / psc(outw);
const int kernel_w = ex - sx;
const int sy = psc(h) * gy / psc(outh);
const int ey = (psc(h) * (gy + 1) + psc(outh) - 1) / psc(outh);
const int kernel_h = ey - sy;

if (pooling_type == 0)
{
@@ -115,7 +115,6 @@ void main()
else if (pooling_type == 1)
{
mat2x4 res_fp32 = mat2x4(vec4(0.f), vec4(0.f)); // force accumulation in fp32
int area = 0;
#if NCNN_image_shader
for (int y = 0; y < kernel_h; y++)
@@ -125,7 +124,6 @@ void main()
afpvec8 v = image3d_ld8(bottom_blob, ivec3(sx + x, sy + y, gz));
res_fp32[0] += v[0];
res_fp32[1] += v[1];
area += 1;
}
}
#else
@@ -138,13 +136,13 @@ void main()
afpvec8 v = buffer_ld8(bottom_blob_data, v_offset + x);
res_fp32[0] += v[0];
res_fp32[1] += v[1];
area += 1;
}

v_offset += psc(w);
}
#endif

int area = kernel_h * kernel_w;
res_fp32[0] /= float(area);
res_fp32[1] /= float(area);
res = afpvec8(res_fp32); // cast to fp16 if possible


Loading…
Cancel
Save