mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-24 07:14:22 +00:00
Add avgPool2d implementation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "cuda_helper.cuh"
|
||||
#include "layer.hpp"
|
||||
#include "pooling.cuh"
|
||||
#include "pool.cuh"
|
||||
|
||||
using namespace CUDANet;
|
||||
|
||||
@@ -135,5 +135,31 @@ CUDANet::Tensor& CUDA::maxPool2d(
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
CUDANet::Tensor& CUDA::avgPool2d(
|
||||
const CUDANet::Tensor& input,
|
||||
CUDANet::Tensor& output,
|
||||
CUDANet::Shape input_shape,
|
||||
CUDANet::Shape pool_shape,
|
||||
CUDANet::Shape stride_shape,
|
||||
CUDANet::Shape padding_shape,
|
||||
CUDANet::Shape output_shape
|
||||
) {
|
||||
dim3 block(8, 8, 8);
|
||||
dim3 grid(
|
||||
(output_shape[0] + block.x - 1) / block.x,
|
||||
(output_shape[1] + block.y - 1) / block.y,
|
||||
(output_shape[2] + block.z - 1) / block.z
|
||||
);
|
||||
|
||||
Kernels::avg_pool<<<grid, block>>>(
|
||||
input.data<float>(), output.data<float>(), input_shape, output_shape, pool_shape,
|
||||
stride_shape, padding_shape
|
||||
);
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
#include "avg_pooling.hpp"
|
||||
#include "cuda_helper.cuh"
|
||||
#include "pooling.cuh"
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
void AvgPooling2d::initCUDA() {
|
||||
d_output = nullptr;
|
||||
CUDA_CHECK(cudaMalloc(
|
||||
(void**)&d_output,
|
||||
sizeof(float) * outputSize.first * outputSize.second * nChannels
|
||||
));
|
||||
}
|
||||
|
||||
void AvgPooling2d::delCUDA() {
|
||||
cudaFree(d_output);
|
||||
}
|
||||
|
||||
float* AvgPooling2d::forwardCUDA(const float* d_input) {
|
||||
dim3 block(8, 8, 8);
|
||||
dim3 grid(
|
||||
(outputSize.first + block.x - 1) / block.x,
|
||||
(outputSize.second + block.y - 1) / block.y,
|
||||
(nChannels + block.z - 1) / block.z
|
||||
);
|
||||
|
||||
Kernels::avg_pooling<<<grid, block>>>(
|
||||
d_input, d_output, inputSize, outputSize, nChannels, poolingSize,
|
||||
stride, padding
|
||||
);
|
||||
CUDA_CHECK(cudaGetLastError());
|
||||
|
||||
activation->activate(d_output);
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
return d_output;
|
||||
}
|
||||
|
||||
void AdaptiveAvgPooling2d::initCUDA() {
|
||||
cudaFree(d_output);
|
||||
cudaMalloc(
|
||||
(void**)&d_output,
|
||||
sizeof(float) * outputSize.first * outputSize.second * nChannels
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user