Files
ncnn/src/layer.cpp
T
nihui 10b8ac68cc [WIP] vulkan compute (#618)
* vulkan infrastructure

* vkallocator and vkmat

* layer interface for vulkan compute

* wip...

* default vulkan device, command wrapper, upload model weight in load_model to simplify layer interface

* simplify command api, vkmat holds staging buffer, relu works

* initialize specialization constant, simplify command dispatch, fix staging buffer copy with different shape, convolution works

* init extension functions

* dynamic local size and group count

* group count=1 is invalid

* regard device max workgroup size limit

* fix relu oooops

* decouple command record and staging allocation

* create result blob

* add pooling shader

* buffer is faster than image :)

* fix pooling shader

* add innerproduct shader

* readonly writeonly decoration

* simplify buffer creation

* decouple command and layer, VK_KHR_descriptor_update_template extension makes descriptor binding update easy :D

* fix vulkan building issues in visual studio (#1)

* fix building issues on visual studio

* ignore benchmark

* cancel changes

* ... ...

* decouple paramdict and vulkandevice

* fix staging buffer destroy in model loading

* remove vkdev member in option

* add padding shader

* simplify vulkan layer creation, simplify convolution and pooling shader for no padding, less debug output

* add convolutiondepthwise and softmax shader

* specialization float type, add leakyrelu

* add dropout shader

* add batchnorm shader

* split vulkan forward

* add scale shader

* push constant type can be int or float

* set_optimal_local_size_xyz

* add eltwise shader

* concat vulkan forward

* fix convolution without bias

* add dummy shader for concat and split, more fix ...

* optional VK_KHR_descriptor_update_template and VK_KHR_push_descriptor

* check VK_KHR_push_descriptor for vkCmdPushDescriptorSetWithTemplateKHR

* binaryop and unaryop shader

* hide raw command buffer

* simple vkbenchncnn benchmark

* create device with transfer queue

* rename command to vkcompute, add vktransfer and layer upload_model interface

* external VkMat, copy and map wrt buffer offset

* command copy respect offset and size

* decouple weight upload and load, simplify upload weight api, use one big staging buffer for uploading weights

* fix build on android

* binding count can not vary :(

* barrier check state, fix sub-op destruction

* declare local_size_xyz constant, fix crash on radv

* fix local_size_xyz, second try

* more barrier and state fix

* fix softmax

* reconstruct buffer memory allocator, reuse blob buffer, less verbose output

* find unified memory type index

* weight staging buffer allocator and weight buffer allocator, respect descriptor buffer offset alignment

* use VK_KHR_descriptor_update_template for faster descriptor update if available, multithread pipeline creation

* find more useful vulkan extensions and enable them

* fix msvc build

* respect VK_KHR_dedicated_allocation for weight buffer allocation

* fix android build

* fix bias name conflicts with metal

* decouple pipeline and layer, building shader sources into shader module, dedicated create_pipeline api, simplify pipeline recording

* drop dummy shader, inplace softmax, multiple shader module works

* fix unique queue family index error

* flatten support vulkan

* mnasnet run

* find shader module by name, each entry point per shader module, fix attribute/id conflict on moltenvk

* some minor changes

* add some high level api

* use dedicated transfer queue to upload weight model

* prefer mappable buffer on unified memory

* global pooling and convolution fc, reuse staging buffer

* implement ring-buffer style blob allocator, add VkBufferMemory capacity

* use blob allocator for workspace blob, it works fine :)

* vulkan option off

* Update layer.cpp

* fix build with vulkan off

* less verbose output, fix crash on vulkan_compute off

* merge benchncnn tool

* allocator clear api, use new weight buffer allocator per net

* add default locked allocator

* mapped mat ptr api, persistent mapped memory works generally :)

* travis ci linux vulkan

* travis ci vulkan wip ...

* more gpu wip ...

* more gpu wip ...

* wip...

* wip...

* wip... ...

* wip... ios vulkan build...

* find glslangValidator on ios build

* use dynamic moltenvk library

* travis ci wip ...

* ios simulator does not support metal at all

* fix cpu only extractor

* optimize workgroup size, first try

* optimize workgroup size, second try

* conv1x1s1d1 vec4

* revert build system

* fix ncnn2mem build

* fix ncnn2mem build
2019-01-23 16:21:17 +08:00

251 lines
5.5 KiB
C++

// Tencent is pleased to support the open source community by making ncnn available.
//
// Copyright (C) 2017 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.
#include "layer.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include "cpu.h"
namespace ncnn {
Option::Option()
{
lightmode = true;
num_threads = get_cpu_count();
blob_allocator = 0;
workspace_allocator = 0;
#if NCNN_VULKAN
vulkan_compute = true;
blob_vkallocator = 0;
workspace_vkallocator = 0;
staging_vkallocator = 0;
#endif // NCNN_VULKAN
}
static Option g_default_option;
const Option& get_default_option()
{
return g_default_option;
}
int set_default_option(const Option& opt)
{
if (opt.num_threads <= 0)
{
fprintf(stderr, "invalid option num_threads %d\n", opt.num_threads);
return -1;
}
g_default_option = opt;
return 0;
}
Layer::Layer()
{
one_blob_only = false;
support_inplace = false;
support_vulkan = false;
#if NCNN_VULKAN
pipeline = 0;
#endif // NCNN_VULKAN
}
Layer::~Layer()
{
#if NCNN_VULKAN
delete pipeline;
#endif // NCNN_VULKAN
}
int Layer::load_param(const ParamDict& /*pd*/)
{
return 0;
}
int Layer::load_model(const ModelBin& /*mb*/)
{
return 0;
}
int Layer::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blobs = bottom_blobs;
for (int i = 0; i < (int)top_blobs.size(); i++)
{
top_blobs[i] = bottom_blobs[i].clone(opt.blob_allocator);
if (top_blobs[i].empty())
return -100;
}
return forward_inplace(top_blobs, opt);
}
int Layer::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blob = bottom_blob.clone(opt.blob_allocator);
if (top_blob.empty())
return -100;
return forward_inplace(top_blob, opt);
}
int Layer::forward_inplace(std::vector<Mat>& /*bottom_top_blobs*/, const Option& /*opt*/) const
{
return -1;
}
int Layer::forward_inplace(Mat& /*bottom_top_blob*/, const Option& /*opt*/) const
{
return -1;
}
#if NCNN_VULKAN
int Layer::upload_model(VkTransfer& /*cmd*/)
{
return 0;
}
int Layer::create_pipeline()
{
return 0;
}
int Layer::forward(const std::vector<VkMat>& bottom_blobs, std::vector<VkMat>& top_blobs, VkCompute& cmd, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blobs.resize(bottom_blobs.size());
for (int i = 0; i < (int)top_blobs.size(); i++)
{
top_blobs[i].create_like(bottom_blobs[i], bottom_blobs[i].allocator, bottom_blobs[i].staging_allocator);
if (top_blobs[i].empty())
return -100;
cmd.record_prepare_transfer_barrier(bottom_blobs[i]);
cmd.record_clone(bottom_blobs[i], top_blobs[i]);
}
return forward_inplace(top_blobs, cmd, opt);
}
int Layer::forward(const VkMat& bottom_blob, VkMat& top_blob, VkCompute& cmd, const Option& opt) const
{
if (!support_inplace)
return -1;
top_blob.create_like(bottom_blob, bottom_blob.allocator, bottom_blob.staging_allocator);
if (top_blob.empty())
return -100;
cmd.record_prepare_transfer_barrier(bottom_blob);
cmd.record_clone(bottom_blob, top_blob);
return forward_inplace(top_blob, cmd, opt);
}
int Layer::forward_inplace(std::vector<VkMat>& /*bottom_top_blobs*/, VkCompute& /*cmd*/, const Option& /*opt*/) const
{
return -1;
}
int Layer::forward_inplace(VkMat& /*bottom_top_blob*/, VkCompute& /*cmd*/, const Option& /*opt*/) const
{
return -1;
}
#endif // NCNN_VULKAN
#include "layer_declaration.h"
static const layer_registry_entry layer_registry[] =
{
#include "layer_registry.h"
};
static const int layer_registry_entry_count = sizeof(layer_registry) / sizeof(layer_registry_entry);
#if NCNN_STRING
int layer_to_index(const char* type)
{
for (int i=0; i<layer_registry_entry_count; i++)
{
if (strcmp(type, layer_registry[i].name) == 0)
return i;
}
return -1;
}
Layer* create_layer(const char* type)
{
int index = layer_to_index(type);
if (index == -1)
return 0;
return create_layer(index);
}
#endif // NCNN_STRING
Layer* create_layer(int index)
{
if (index < 0 || index >= layer_registry_entry_count)
return 0;
layer_creator_func layer_creator = layer_registry[index].creator;
if (!layer_creator)
return 0;
return layer_creator();
}
#if NCNN_VULKAN
#if NCNN_STRING
Layer* create_layer(const char* type, const VulkanDevice* vkdev)
{
int index = layer_to_index(type);
if (index == -1)
return 0;
return create_layer(index, vkdev);
}
#endif // NCNN_STRING
Layer* create_layer(int index, const VulkanDevice* vkdev)
{
Layer* layer = create_layer(index);
if (!layer)
return 0;
layer->vkdev = vkdev;
layer->pipeline = new Pipeline(vkdev);
return layer;
}
#endif // NCNN_VULKAN
} // namespace ncnn