Skip to content

Commit

Permalink
simplify vulkan conv1d (#5095)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui authored Oct 23, 2023
1 parent efcfe95 commit 7afdbfa
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 225 deletions.
50 changes: 14 additions & 36 deletions src/layer/vulkan/convolution1d_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ int Convolution1D_vulkan::create_pipeline(const Option& _opt)
}

{
std::vector<vk_specialization_type> specializations(7 + 10);
std::vector<vk_specialization_type> specializations(7 + 4);
specializations[0].i = kernel_w;
specializations[1].i = dilation_w;
specializations[2].i = stride_w;
Expand All @@ -110,12 +110,6 @@ int Convolution1D_vulkan::create_pipeline(const Option& _opt)
specializations[7 + 1].i = 0;
specializations[7 + 2].i = 0;
specializations[7 + 3].i = 0;
specializations[7 + 4].i = 0;
specializations[7 + 5].i = 0;
specializations[7 + 6].i = 0;
specializations[7 + 7].i = 0;
specializations[7 + 8].i = 0;
specializations[7 + 9].i = 0;

int shader_type_index = -1;
if (elempack == 1 && out_elempack == 1) shader_type_index = LayerShaderType::convolution1d;
Expand Down Expand Up @@ -189,8 +183,6 @@ int Convolution1D_vulkan::upload_model(VkTransfer& cmd, const Option& opt)
int Convolution1D_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
{
int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;
size_t elemsize = bottom_blob.elemsize;
int elempack = bottom_blob.elempack;

Expand Down Expand Up @@ -281,22 +273,16 @@ int Convolution1D_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkC
bindings[2] = weight_data_gpu;
bindings[3] = bias_data_gpu;

std::vector<vk_constant_type> constants(10);
constants[0].i = bottom_blob_bordered.dims;
constants[1].i = bottom_blob_bordered.w;
constants[2].i = bottom_blob_bordered.h;
constants[3].i = bottom_blob_bordered.c;
constants[4].i = bottom_blob_bordered.cstep;
constants[5].i = top_blob.dims;
constants[6].i = top_blob.w;
constants[7].i = top_blob.h;
constants[8].i = top_blob.c;
constants[9].i = top_blob.cstep;
std::vector<vk_constant_type> constants(4);
constants[0].i = bottom_blob_bordered.w;
constants[1].i = bottom_blob_bordered.h;
constants[2].i = top_blob.w;
constants[3].i = top_blob.h;

VkMat dispatcher;
dispatcher.w = (top_blob.w + 1) / 2;
dispatcher.h = (top_blob.h + 1) / 2;
dispatcher.c = (top_blob.c + 1) / 2;
dispatcher.c = 1;

cmd.record_pipeline(pipeline_convolution1d, bindings, constants, dispatcher);

Expand All @@ -306,8 +292,6 @@ int Convolution1D_vulkan::forward(const VkMat& bottom_blob, VkMat& top_blob, VkC
int Convolution1D_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top_blob, VkCompute& cmd, const Option& opt) const
{
int w = bottom_blob.w;
int h = bottom_blob.h;
int channels = bottom_blob.c;
size_t elemsize = bottom_blob.elemsize;
int elempack = bottom_blob.elempack;

Expand Down Expand Up @@ -398,26 +382,20 @@ int Convolution1D_vulkan::forward(const VkImageMat& bottom_blob, VkImageMat& top
bindings[2] = weight_data_gpu_image;
bindings[3] = bias_data_gpu_image;

std::vector<vk_constant_type> constants(10);
constants[0].i = bottom_blob_bordered.dims;
constants[1].i = bottom_blob_bordered.w;
constants[2].i = bottom_blob_bordered.h;
constants[3].i = bottom_blob_bordered.c;
constants[4].i = 0; //bottom_blob_bordered.cstep;
constants[5].i = top_blob.dims;
constants[6].i = top_blob.w;
constants[7].i = top_blob.h;
constants[8].i = top_blob.c;
constants[9].i = 0; //top_blob.cstep;
std::vector<vk_constant_type> constants(4);
constants[0].i = bottom_blob_bordered.w;
constants[1].i = bottom_blob_bordered.h;
constants[2].i = top_blob.w;
constants[3].i = top_blob.h;

VkImageMat dispatcher;
dispatcher.w = (top_blob.w + 1) / 2;
dispatcher.h = (top_blob.h + 1) / 2;
dispatcher.c = (top_blob.c + 1) / 2;
dispatcher.c = 1;

cmd.record_pipeline(pipeline_convolution1d, bindings, constants, dispatcher);

return 0;
}

} // namespace ncnn
} // namespace ncnn
30 changes: 9 additions & 21 deletions src/layer/vulkan/shader/convolution1d.comp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ layout (constant_id = 5) const float activation_param_0 = 0;
layout (constant_id = 6) const float activation_param_1 = 0;

#define shape_constant_id_offset 7
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;

layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;

#if NCNN_image_shader
layout (binding = 0) uniform unfp sampler3D bottom_blob;
Expand All @@ -59,24 +53,18 @@ layout (binding = 3) readonly buffer bias_blob { sfp bias_data[]; };

layout (push_constant) uniform parameter
{
int dims;
int w;
int h;
int c;
int cstep;

int outdims;
int outw;
int outh;
int outc;
int outcstep;
} p;

void main()
{
int gx = int(gl_GlobalInvocationID.x) * 2;
int gy = int(gl_GlobalInvocationID.y) * 2;

if (gx >= psc(outw) || gy >= psc(outh))
return;

Expand Down Expand Up @@ -150,7 +138,7 @@ void main()
w_offset += kernel_w;
}

#endif
#endif

sum0 = activation_afp(sum0, activation_type, activation_param_0, activation_param_1);
sum1 = activation_afp(sum1, activation_type, activation_param_0, activation_param_1);
Expand All @@ -165,7 +153,7 @@ void main()
image3d_st1(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);

#else

const int gi = gy * psc(outw) + gx;

buffer_st1(top_blob_data, gi, sum0);
Expand All @@ -174,4 +162,4 @@ void main()
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st1(top_blob_data, gi + psc(outw) + 1, sum3);

#endif
}
}
30 changes: 9 additions & 21 deletions src/layer/vulkan/shader/convolution1d_pack1to4.comp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ layout (constant_id = 5) const float activation_param_0 = 0;
layout (constant_id = 6) const float activation_param_1 = 0;

#define shape_constant_id_offset 7
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;

layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;

#if NCNN_image_shader
layout (binding = 0) uniform unfp sampler3D bottom_blob;
Expand All @@ -59,24 +53,18 @@ layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };

layout (push_constant) uniform parameter
{
int dims;
int w;
int h;
int c;
int cstep;

int outdims;
int outw;
int outh;
int outc;
int outcstep;
} p;

void main()
{
int gx = int(gl_GlobalInvocationID.x) * 2;
int gy = int(gl_GlobalInvocationID.y) * 2;

if (gx >= psc(outw) || gy >= psc(outh))
return;

Expand Down Expand Up @@ -150,7 +138,7 @@ void main()
w_offset += kernel_w;
}

#endif
#endif

sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
Expand All @@ -165,7 +153,7 @@ void main()
image3d_st4(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);

#else

const int gi = gy * psc(outw) + gx;

buffer_st4(top_blob_data, gi, sum0);
Expand All @@ -174,4 +162,4 @@ void main()
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + psc(outw) + 1, sum3);

#endif
}
}
30 changes: 9 additions & 21 deletions src/layer/vulkan/shader/convolution1d_pack1to8.comp
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,11 @@ layout (constant_id = 5) const float activation_param_0 = 0;
layout (constant_id = 6) const float activation_param_1 = 0;

#define shape_constant_id_offset 7
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;

layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;

#if NCNN_image_shader
layout (binding = 0) uniform unfp sampler3D bottom_blob;
Expand All @@ -60,24 +54,18 @@ layout (binding = 3) readonly buffer bias_blob { sfpvec8 bias_data[]; };

layout (push_constant) uniform parameter
{
int dims;
int w;
int h;
int c;
int cstep;

int outdims;
int outw;
int outh;
int outc;
int outcstep;
} p;

void main()
{
int gx = int(gl_GlobalInvocationID.x) * 2;
int gy = int(gl_GlobalInvocationID.y) * 2;

if (gx >= psc(outw) || gy >= psc(outh))
return;

Expand Down Expand Up @@ -159,7 +147,7 @@ void main()
w_offset += kernel_w;
}

#endif
#endif

sum0 = activation_afpvec8(sum0, activation_type, activation_param_0, activation_param_1);
sum1 = activation_afpvec8(sum1, activation_type, activation_param_0, activation_param_1);
Expand All @@ -174,7 +162,7 @@ void main()
image3d_st8(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);

#else

const int gi = gy * psc(outw) + gx;

buffer_st8(top_blob_data, gi, sum0);
Expand All @@ -183,4 +171,4 @@ void main()
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st8(top_blob_data, gi + psc(outw) + 1, sum3);

#endif
}
}
30 changes: 9 additions & 21 deletions src/layer/vulkan/shader/convolution1d_pack4.comp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,11 @@ layout (constant_id = 5) const float activation_param_0 = 0;
layout (constant_id = 6) const float activation_param_1 = 0;

#define shape_constant_id_offset 7
layout (constant_id = shape_constant_id_offset + 0) const int dims = 0;
layout (constant_id = shape_constant_id_offset + 1) const int w = 0;
layout (constant_id = shape_constant_id_offset + 2) const int h = 0;
layout (constant_id = shape_constant_id_offset + 3) const int c = 0;
layout (constant_id = shape_constant_id_offset + 4) const int cstep = 0;

layout (constant_id = shape_constant_id_offset + 5) const int outdims = 0;
layout (constant_id = shape_constant_id_offset + 6) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 7) const int outh = 0;
layout (constant_id = shape_constant_id_offset + 8) const int outc = 0;
layout (constant_id = shape_constant_id_offset + 9) const int outcstep = 0;
layout (constant_id = shape_constant_id_offset + 0) const int w = 0;
layout (constant_id = shape_constant_id_offset + 1) const int h = 0;

layout (constant_id = shape_constant_id_offset + 2) const int outw = 0;
layout (constant_id = shape_constant_id_offset + 3) const int outh = 0;

#if NCNN_image_shader
layout (binding = 0) uniform unfp sampler3D bottom_blob;
Expand All @@ -64,24 +58,18 @@ layout (binding = 3) readonly buffer bias_blob { sfpvec4 bias_data[]; };

layout (push_constant) uniform parameter
{
int dims;
int w;
int h;
int c;
int cstep;

int outdims;
int outw;
int outh;
int outc;
int outcstep;
} p;

void main()
{
int gx = int(gl_GlobalInvocationID.x) * 2;
int gy = int(gl_GlobalInvocationID.y) * 2;

if (gx >= psc(outw) || gy >= psc(outh))
return;

Expand Down Expand Up @@ -181,7 +169,7 @@ void main()
w_offset += kernel_w;
}

#endif
#endif

sum0 = activation_afpvec4(sum0, activation_type, activation_param_0, activation_param_1);
sum1 = activation_afpvec4(sum1, activation_type, activation_param_0, activation_param_1);
Expand All @@ -196,7 +184,7 @@ void main()
image3d_st4(top_blob, ivec3(gx2.y, gy2.y, 0), sum3);

#else

const int gi = gy * psc(outw) + gx;

buffer_st4(top_blob_data, gi, sum0);
Expand All @@ -205,4 +193,4 @@ void main()
if (gy + 1 < psc(outh) && gx + 1 < psc(outw)) buffer_st4(top_blob_data, gi + psc(outw) + 1, sum3);

#endif
}
}
Loading

0 comments on commit 7afdbfa

Please sign in to comment.