mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-06 09:44:28 +00:00
Implement Add layer
This commit is contained in:
@@ -12,6 +12,7 @@ set(LIBRARY_SOURCES
|
|||||||
src/kernels/activation_functions.cu
|
src/kernels/activation_functions.cu
|
||||||
src/kernels/convolution.cu
|
src/kernels/convolution.cu
|
||||||
src/kernels/matmul.cu
|
src/kernels/matmul.cu
|
||||||
|
src/layers/add.cu
|
||||||
src/layers/dense.cu
|
src/layers/dense.cu
|
||||||
src/layers/conv2d.cu
|
src/layers/conv2d.cu
|
||||||
src/layers/concat.cu
|
src/layers/concat.cu
|
||||||
|
|||||||
40
include/layers/add.cuh
Normal file
40
include/layers/add.cuh
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef CUDANET_ADD_LAYER_H
|
||||||
|
#define CUDANET_ADD_LAYER_H
|
||||||
|
|
||||||
|
namespace CUDANet::Layers {
|
||||||
|
|
||||||
|
class Add {
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* @brief Create a new Add layer
|
||||||
|
*
|
||||||
|
* @param inputSize Size of the input arrays
|
||||||
|
*/
|
||||||
|
Add(int inputSize);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destroy the Add layer
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
~Add();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Adds the two inputs
|
||||||
|
*
|
||||||
|
* @param d_inputA Device pointer to the first input
|
||||||
|
* @param d_inputB Device pointer to the second input
|
||||||
|
*
|
||||||
|
* @return Device pointer to the output
|
||||||
|
*/
|
||||||
|
float* forward(const float* d_inputA, const float* d_inputB);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int inputSize;
|
||||||
|
int gridSize;
|
||||||
|
|
||||||
|
float* d_output;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace CUDANet::Layers
|
||||||
|
|
||||||
|
#endif // CUDANET_ADD_LAYER_H
|
||||||
29
src/layers/add.cu
Normal file
29
src/layers/add.cu
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include "add.cuh"
|
||||||
|
#include "matmul.cuh"
|
||||||
|
#include "cuda_helper.cuh"
|
||||||
|
|
||||||
|
using namespace CUDANet;
|
||||||
|
|
||||||
|
|
||||||
|
Layers::Add::Add(int inputSize)
|
||||||
|
: inputSize(inputSize) {
|
||||||
|
|
||||||
|
d_output = nullptr;
|
||||||
|
CUDA_CHECK(cudaMalloc((void**)&d_output, sizeof(float) * inputSize));
|
||||||
|
|
||||||
|
gridSize = (inputSize + BLOCK_SIZE - 1) / BLOCK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Layers::Add::~Add() {
|
||||||
|
cudaFree(d_output);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
float* Layers::Add::forward(const float* d_inputA, const float* d_inputB) {
|
||||||
|
|
||||||
|
Kernels::vec_vec_add<<<gridSize, BLOCK_SIZE>>>(
|
||||||
|
d_inputA, d_inputB, d_output, inputSize
|
||||||
|
);
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user