Browse Source

reorg shader

tags/20190320
nihuini 7 years ago
parent
commit
14efdd8e00
5 changed files with 339 additions and 0 deletions
  1. +108
    -0
      src/layer/reorg.cpp
  2. +13
    -0
      src/layer/reorg.h
  3. +60
    -0
      src/layer/shader/reorg.comp
  4. +67
    -0
      src/layer/shader/reorg_pack1to4.comp
  5. +91
    -0
      src/layer/shader/reorg_pack4.comp

+ 108
- 0
src/layer/reorg.cpp View File

@@ -22,6 +22,13 @@ Reorg::Reorg()
{
one_blob_only = true;
support_inplace = false;
support_vulkan = true;

#if NCNN_VULKAN
pipeline_reorg = 0;
pipeline_reorg_pack4 = 0;
pipeline_reorg_pack1to4 = 0;
#endif // NCNN_VULKAN
}

int Reorg::load_param(const ParamDict& pd)
@@ -75,4 +82,105 @@ int Reorg::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) con
return 0;
}

#if NCNN_VULKAN
int Reorg::create_pipeline()
{
std::vector<vk_specialization_type> specializations(1);
specializations[0].i = stride;

pipeline_reorg = new Pipeline(vkdev);
pipeline_reorg->set_optimal_local_size_xyz();
pipeline_reorg->create("reorg", specializations, 2, 10);

// pack4
{
pipeline_reorg_pack4 = new Pipeline(vkdev);
pipeline_reorg_pack4->set_optimal_local_size_xyz();
pipeline_reorg_pack4->create("reorg_pack4", specializations, 2, 10);
}

// pack1to4
{
pipeline_reorg_pack1to4 = new Pipeline(vkdev);
pipeline_reorg_pack1to4->set_optimal_local_size_xyz();
pipeline_reorg_pack1to4->create("reorg_pack1to4", specializations, 2, 10);
}

return 0;
}

int Reorg::destroy_pipeline()
{
delete pipeline_reorg;
pipeline_reorg = 0;

delete pipeline_reorg_pack4;
pipeline_reorg_pack4 = 0;

delete pipeline_reorg_pack1to4;
pipeline_reorg_pack1to4 = 0;

return 0;
}

int Reorg::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 packing = bottom_blob.packing;

int outw = w / stride;
int outh = h / stride;
int outc = channels * packing * stride * stride;

int out_packing = outc % 4 == 0 ? 4 : 1;
size_t out_elemsize = elemsize / packing * out_packing;

top_blob.create(outw, outh, outc / out_packing, out_elemsize, out_packing, opt.blob_vkallocator, opt.staging_vkallocator);
if (top_blob.empty())
return -100;

// fprintf(stderr, "Reorg::forward %p %p\n", bottom_blob.buffer(), top_blob.buffer());

std::vector<VkMat> bindings(2);
bindings[0] = bottom_blob;
bindings[1] = top_blob;

std::vector<vk_constant_type> constants(10);
constants[0].i = bottom_blob.dims;
constants[1].i = bottom_blob.w;
constants[2].i = bottom_blob.h;
constants[3].i = bottom_blob.c;
constants[4].i = bottom_blob.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;

const Pipeline* pipeline = 0;
if (packing == 1 && out_packing == 1)
{
pipeline = pipeline_reorg;
}
else if (packing == 4) // assert out_packing == 4
{
pipeline = pipeline_reorg_pack4;
}
else if (packing == 1 && out_packing == 4)
{
pipeline = pipeline_reorg_pack1to4;
}

// record
cmd.record_prepare_compute_barrier(bottom_blob);
cmd.record_prepare_compute_barrier(top_blob);
cmd.record_pipeline(pipeline, bindings, constants, top_blob);

return 0;
}
#endif // NCNN_VULKAN

} // namespace ncnn

+ 13
- 0
src/layer/reorg.h View File

@@ -28,8 +28,21 @@ public:

virtual int forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const;

#if NCNN_VULKAN
virtual int create_pipeline();
virtual int destroy_pipeline();

virtual int forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const;
#endif // NCNN_VULKAN

private:
int stride;

#if NCNN_VULKAN
Pipeline* pipeline_reorg;
Pipeline* pipeline_reorg_pack4;
Pipeline* pipeline_reorg_pack1to4;
#endif // NCNN_VULKAN
};

} // namespace ncnn


+ 60
- 0
src/layer/shader/reorg.comp View File

@@ -0,0 +1,60 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#version 450

layout (constant_id = 0) const int stride = 0;

layout (local_size_x_id = 233) in;
layout (local_size_y_id = 234) in;
layout (local_size_z_id = 235) in;

layout (binding = 0) readonly buffer bottom_blob { float bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { float top_blob_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);
int gy = int(gl_GlobalInvocationID.y);
int gz = int(gl_GlobalInvocationID.z);

if (gx >= p.outw || gy >= p.outh || gz >= p.outc)
return;

int z = gz / (stride * stride);
int zi = gz % (stride * stride);
int y = gy * stride + zi / stride;
int x = gx * stride + zi % stride;

int v_offset = z * p.cstep + y * p.w + x;

float v = bottom_blob_data[v_offset];

top_blob_data[gz * p.outcstep + gy * p.outw + gx] = v;
}

+ 67
- 0
src/layer/shader/reorg_pack1to4.comp View File

@@ -0,0 +1,67 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#version 450

layout (constant_id = 0) const int stride = 0;

layout (local_size_x_id = 233) in;
layout (local_size_y_id = 234) in;
layout (local_size_z_id = 235) in;

layout (binding = 0) readonly buffer bottom_blob { float bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { vec4 top_blob_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);
int gy = int(gl_GlobalInvocationID.y);
int gz = int(gl_GlobalInvocationID.z);

if (gx >= p.outw || gy >= p.outh || gz >= p.outc)
return;

ivec4 gz4 = ivec4(gz * 4) + ivec4(0, 1, 2, 3);

ivec4 z4 = gz4 / (stride * stride);
ivec4 zi4 = gz4 % (stride * stride);
ivec4 y4 = gy * stride + zi4 / stride;
ivec4 x4 = gx * stride + zi4 % stride;

ivec4 v_offset = z4 * p.cstep + y4 * p.w + x4;

vec4 v;

v.r = bottom_blob_data[v_offset.r];
v.g = bottom_blob_data[v_offset.g];
v.b = bottom_blob_data[v_offset.b];
v.a = bottom_blob_data[v_offset.a];

top_blob_data[gz * p.outcstep + gy * p.outw + gx] = v;
}

+ 91
- 0
src/layer/shader/reorg_pack4.comp View File

@@ -0,0 +1,91 @@
// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
//
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
// in compliance with the License. You may obtain a copy of the License at
//
// https://opensource.org/licenses/BSD-3-Clause
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

#version 450

layout (constant_id = 0) const int stride = 0;

layout (local_size_x_id = 233) in;
layout (local_size_y_id = 234) in;
layout (local_size_z_id = 235) in;

layout (binding = 0) readonly buffer bottom_blob { vec4 bottom_blob_data[]; };
layout (binding = 1) writeonly buffer top_blob { vec4 top_blob_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);
int gy = int(gl_GlobalInvocationID.y);
int gz = int(gl_GlobalInvocationID.z);

if (gx >= p.outw || gy >= p.outh || gz >= p.outc)
return;

ivec4 gz4 = ivec4(gz * 4) + ivec4(0, 1, 2, 3);

ivec4 z4 = gz4 / (stride * stride);
ivec4 zi4 = gz4 % (stride * stride);
ivec4 y4 = gy * stride + zi4 / stride;
ivec4 x4 = gx * stride + zi4 % stride;

ivec4 v_offset = (z4 / 4) * p.cstep + y4 * p.w + x4;

ivec4 lane4 = z4 % 4;

// v = v4[lane]
vec4 v;

vec4 v4;

v4 = bottom_blob_data[v_offset.r];
if (lane4.r == 0) v.r = v4.r;
else if (lane4.r == 1) v.r = v4.g;
else if (lane4.r == 2) v.r = v4.b;
else /* if (lane4.r == 3) */ v.r = v4.a;

v4 = bottom_blob_data[v_offset.g];
if (lane4.g == 0) v.g = v4.r;
else if (lane4.g == 1) v.g = v4.g;
else if (lane4.g == 2) v.g = v4.b;
else /* if (lane4.g == 3) */ v.g = v4.a;

v4 = bottom_blob_data[v_offset.b];
if (lane4.b == 0) v.b = v4.r;
else if (lane4.b == 1) v.b = v4.g;
else if (lane4.b == 2) v.b = v4.b;
else /* if (lane4.b == 3) */ v.b = v4.a;

v4 = bottom_blob_data[v_offset.a];
if (lane4.a == 0) v.a = v4.r;
else if (lane4.a == 1) v.a = v4.g;
else if (lane4.a == 2) v.a = v4.b;
else /* if (lane4.a == 3) */ v.a = v4.a;

top_blob_data[gz * p.outcstep + gy * p.outw + gx] = v;
}

Loading…
Cancel
Save