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

Layer test #354

Closed
wants to merge 11 commits into from
Closed
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
1,746 changes: 0 additions & 1,746 deletions 3rdparty/include/webgpu/include/webgpu_cpp.cpp

This file was deleted.

Binary file removed 3rdparty/include/webgpu/lib/libshaderc.so
Binary file not shown.
1,746 changes: 0 additions & 1,746 deletions 3rdparty/include/webgpu/lib/webgpu_cpp.cpp

This file was deleted.

6 changes: 3 additions & 3 deletions modules/dnn/src/dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class BackendRegistry
#ifdef HAVE_WEBGPU
if(haveWGPU())
backends.push_back(std::make_pair(DNN_BACKEND_WGPU, DNN_TARGET_WGPU));
#endif
#endif // HAVE_WEBGPU

#ifdef HAVE_CUDA
if (haveCUDA()) {
Expand Down Expand Up @@ -1283,7 +1283,7 @@ struct Net::Impl : public detail::NetImplBase
{
#ifdef HAVE_WEBGPU
return Ptr<BackendWrapper>(new WGPUBackendWrapper(baseBuffer, host));
#endif
#endif // HAVE_WEBGPU
}
else if (preferableBackend == DNN_BACKEND_CUDA)
{
Expand Down Expand Up @@ -2417,7 +2417,7 @@ struct Net::Impl : public detail::NetImplBase
ld.backendNodes[DNN_BACKEND_WGPU] = Ptr<BackendNode>();
}
}
#endif
#endif // HAVE_WEBGPU
}

void initCUDABackend() {
Expand Down
16 changes: 15 additions & 1 deletion modules/dnn/src/layers/concat_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "../op_inf_engine.hpp"
#include "../ie_ngraph.hpp"
#include "../op_vkcom.hpp"
#include "../op_webgpu.hpp"

#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
Expand Down Expand Up @@ -116,7 +117,8 @@ class ConcatLayerImpl CV_FINAL : public ConcatLayer
(backendId == DNN_BACKEND_HALIDE && haveHalide() && axis == 1 && !padding) || // By channels
(backendId == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 && haveInfEngine() && !padding) ||
backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH ||
(backendId == DNN_BACKEND_VKCOM && haveVulkan() && !padding);
(backendId == DNN_BACKEND_VKCOM && haveVulkan() && !padding) ||
(backendId == DNN_BACKEND_WGPU && haveWGPU() && !padding);
}

class ChannelConcatInvoker : public ParallelLoopBody
Expand Down Expand Up @@ -312,6 +314,18 @@ class ConcatLayerImpl CV_FINAL : public ConcatLayer
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initWGPU(const std::vector<Ptr<BackendWrapper> > &input) CV_OVERRIDE
{
#ifdef HAVE_WEBGPU
webgpu::Tensor in = WGPUTensor(input[0]);
int cAxis = clamp(axis, in.dimNum());
std::shared_ptr<webgpu::OpBase> op(new webgpu::OpConcat(cAxis));
return Ptr<BackendNode>(new WGPUBackendNode(input, op));
#endif // HAVE_WEBGPU
return Ptr<BackendNode>();
}


virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &input) CV_OVERRIDE
{
#ifdef HAVE_HALIDE
Expand Down
69 changes: 68 additions & 1 deletion modules/dnn/src/layers/convolution_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "../op_inf_engine.hpp"
#include "../ie_ngraph.hpp"
#include "../op_vkcom.hpp"
#include "../op_webgpu.hpp"

#include "opencv2/core/hal/hal.hpp"
#include "opencv2/core/hal/intrin.hpp"
Expand Down Expand Up @@ -303,7 +304,8 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
else if (kernel_size.size() == 2)
return backendId == DNN_BACKEND_OPENCV ||
backendId == DNN_BACKEND_HALIDE ||
(backendId == DNN_BACKEND_VKCOM && haveVulkan());
(backendId == DNN_BACKEND_VKCOM && haveVulkan()) ||
(backendId == DNN_BACKEND_WGPU && haveWGPU());
else
return false;
}
Expand Down Expand Up @@ -581,6 +583,71 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initWGPU(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
{
#ifdef HAVE_WEBGPU
int out_channel = blobs[0].size[0];
bool has_bias = hasBias() || fusedBias;
int filter_size[2] = {kernel.height, kernel.width};
int pad_size[2] = {pad.height, pad.width};
int stride_size[2] = {stride.height, stride.width};
int dilation_size[2] = {dilation.height, dilation.width};
int activation = 0;
webgpu::Tensor input_tensor = WGPUTensor(inputs[0]);
int in_channel = input_tensor.dimSize(1);
int group = in_channel / blobs[0].size[1];

// TODO: support group > 1
if (group != 1)
return Ptr<BackendNode>();

int padding_mode;
if (padMode.empty())
{
padding_mode = webgpu::wPaddingModeCaffe;
}
else if (padMode == "VALID")
{
padding_mode = webgpu::wPaddingModeValid;
}
else if (padMode == "SAME")
{
padding_mode = webgpu::wPaddingModeSame;
}
else
CV_Error(Error::StsError, "Unsupported padding mode " + padMode);

std::shared_ptr<webgpu::OpBase> op(new webgpu::OpConv(out_channel, has_bias,
filter_size, pad_size,
stride_size, dilation_size,
activation, group,
padding_mode));

std::vector<Ptr<BackendWrapper> > blobsWrapper;

if (fusedWeights)
{
Mat wm;
weightsMat.copyTo(wm); // to handle the case of isContinuous() == false
wm = wm.reshape(1, blobs[0].dims, blobs[0].size);
blobsWrapper.push_back(Ptr<BackendWrapper>(new WGPUBackendWrapper(wm)));
}
else
{
blobsWrapper.push_back(Ptr<BackendWrapper>(new WGPUBackendWrapper(blobs[0])));
}

if (has_bias)
{
Mat biasesMat({out_channel}, CV_32F, &biasvec[0]);
blobsWrapper.push_back(Ptr<BackendWrapper>(new WGPUBackendWrapper(biasesMat)));
}

return Ptr<BackendNode>(new WGPUBackendNode(inputs, op, blobsWrapper));
#endif // HAVE_WEBGPU
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
{
#ifdef HAVE_HALIDE
Expand Down
100 changes: 99 additions & 1 deletion modules/dnn/src/layers/elementwise_layers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "../op_inf_engine.hpp"
#include "../ie_ngraph.hpp"
#include "../op_vkcom.hpp"
#include "../op_webgpu.hpp"

#include <opencv2/dnn/shape_utils.hpp>
#include <iostream>
Expand Down Expand Up @@ -189,6 +190,14 @@ class ElementWiseLayer : public Func::Layer
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initWGPU(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
{
#ifdef HAVE_WEBGPU
return Ptr<BackendNode>(new WGPUBackendNode(inputs, func.initWGPU()));
#endif // HAVE_WEBGPU
return Ptr<BackendNode>();
}

virtual bool tryFuse(Ptr<dnn::Layer>& top) CV_OVERRIDE
{
return func.tryFuse(top);
Expand Down Expand Up @@ -310,7 +319,8 @@ struct ReLUFunctor : public BaseFunctor
return backendId == DNN_BACKEND_OPENCV ||
backendId == DNN_BACKEND_CUDA ||
backendId == DNN_BACKEND_HALIDE ||
backendId == DNN_BACKEND_VKCOM;
backendId == DNN_BACKEND_VKCOM ||
backendId == DNN_BACKEND_WGPU;
}

void apply(const float* srcptr, float* dstptr, int len, size_t planeSize, int cn0, int cn1) const
Expand Down Expand Up @@ -436,6 +446,14 @@ struct ReLUFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
std::shared_ptr<webgpu::OpBase> op(new webgpu::OpReLU(slope));
return op;
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 1; }
};

Expand Down Expand Up @@ -559,6 +577,14 @@ struct ReLU6Functor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 2; }
};

Expand Down Expand Up @@ -651,6 +677,14 @@ struct TanHFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 1; }
};

Expand Down Expand Up @@ -743,6 +777,14 @@ struct SwishFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 3; }
};

Expand Down Expand Up @@ -840,6 +882,14 @@ struct MishFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 3; }
};

Expand Down Expand Up @@ -932,6 +982,14 @@ struct SigmoidFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 3; }
};

Expand Down Expand Up @@ -1024,6 +1082,14 @@ struct ELUFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 2; }
};

Expand Down Expand Up @@ -1122,6 +1188,14 @@ struct AbsValFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 1; }
};

Expand Down Expand Up @@ -1215,6 +1289,14 @@ struct BNLLFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 5; }
};

Expand Down Expand Up @@ -1363,6 +1445,14 @@ struct PowerFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

bool tryFuse(Ptr<dnn::Layer>& top)
{
if (power != 1.0f && shift != 0.0f)
Expand Down Expand Up @@ -1526,6 +1616,14 @@ struct ChannelsPReLUFunctor : public BaseFunctor
}
#endif // HAVE_VULKAN

#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> initWGPU()
{
// TODO: add webgpu implementation
return std::shared_ptr<webgpu::OpBase>();
}
#endif // HAVE_WEBGPU

int64 getFLOPSPerElement() const { return 1; }
};

Expand Down
13 changes: 12 additions & 1 deletion modules/dnn/src/layers/lrn_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "../op_inf_engine.hpp"
#include "../ie_ngraph.hpp"
#include "../op_vkcom.hpp"
#include "../op_webgpu.hpp"

#include "opencv2/imgproc.hpp"
#include "opencv2/dnn/shape_utils.hpp"
Expand Down Expand Up @@ -108,7 +109,8 @@ class LRNLayerImpl CV_FINAL : public LRNLayer
return backendId == DNN_BACKEND_OPENCV ||
backendId == DNN_BACKEND_CUDA ||
backendId == DNN_BACKEND_HALIDE ||
(backendId == DNN_BACKEND_VKCOM && haveVulkan() && (size % 2 == 1) && (type == CHANNEL_NRM));
(backendId == DNN_BACKEND_VKCOM && haveVulkan() && (size % 2 == 1) && (type == CHANNEL_NRM))
|| (backendId == DNN_BACKEND_WGPU && haveWGPU() && (size % 2 == 1) && (type == CHANNEL_NRM));
}

#ifdef HAVE_OPENCL
Expand Down Expand Up @@ -371,6 +373,15 @@ class LRNLayerImpl CV_FINAL : public LRNLayer
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initWGPU(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
{
#ifdef HAVE_WEBGPU
std::shared_ptr<webgpu::OpBase> op(new webgpu::OpLRN(size / 2, bias, alpha, beta, normBySize));
return Ptr<BackendNode>(new WGPUBackendNode(inputs, op));
#endif // HAVE_WEBGPU
return Ptr<BackendNode>();
}

virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> > &inputs) CV_OVERRIDE
{
#ifdef HAVE_HALIDE
Expand Down
Loading