mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-11-05 17:34:21 +00:00
Migrate concat layer
This commit is contained in:
@@ -13,7 +13,7 @@
|
||||
#include "add.hpp"
|
||||
#include "avg_pooling.hpp"
|
||||
#include "batch_norm.cuh"
|
||||
#include "concat.cuh"
|
||||
#include "concat.hpp"
|
||||
#include "conv2d.cuh"
|
||||
#include "dense.hpp"
|
||||
#include "input.cuh"
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef CUDANET_CONCAT_LAYER_H
|
||||
#define CUDANET_CONCAT_LAYER_H
|
||||
|
||||
#include "layer.hpp"
|
||||
|
||||
namespace CUDANet::Layers {
|
||||
|
||||
/**
|
||||
@@ -39,7 +41,15 @@ class Concat {
|
||||
int inputASize;
|
||||
int inputBSize;
|
||||
|
||||
float* forwardCPU(const float* input_A, const float* input_B);
|
||||
|
||||
#ifdef USE_CUDA
|
||||
float* d_output;
|
||||
float* forwardCUDA(const float* d_input_A, const float* d_input_B);
|
||||
|
||||
void initCUDA();
|
||||
void delCUDA();
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace CUDANet::Layers
|
||||
31
src/backends/cuda/layers/concat.cu
Normal file
31
src/backends/cuda/layers/concat.cu
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "concat.hpp"
|
||||
#include "cuda_helper.cuh"
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
void Concat::initCUDA() {
|
||||
d_output = nullptr;
|
||||
CUDA_CHECK(
|
||||
cudaMalloc((void**)&d_output, sizeof(float) * (inputASize + inputBSize))
|
||||
);
|
||||
}
|
||||
|
||||
void Concat::delCUDA() {
|
||||
cudaFree(d_output);
|
||||
}
|
||||
|
||||
float* Concat::forwardCUDA(const float* d_input_A, const float* d_input_B) {
|
||||
CUDA_CHECK(cudaMemcpy(
|
||||
d_output, d_input_A, sizeof(float) * inputASize,
|
||||
cudaMemcpyDeviceToDevice
|
||||
));
|
||||
|
||||
CUDA_CHECK(cudaMemcpy(
|
||||
d_output + inputASize, d_input_B, sizeof(float) * inputBSize,
|
||||
cudaMemcpyDeviceToDevice
|
||||
));
|
||||
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
return d_output;
|
||||
}
|
||||
34
src/layers/concat.cpp
Normal file
34
src/layers/concat.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <stdexcept>
|
||||
|
||||
#include "concat.hpp"
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
Concat::Concat(const int inputASize, const int inputBSize)
|
||||
: inputASize(inputASize), inputBSize(inputBSize) {
|
||||
#ifdef USE_CUDA
|
||||
initCUDA();
|
||||
#endif
|
||||
}
|
||||
|
||||
Concat::~Concat() {
|
||||
#ifdef USE_CUDA
|
||||
delCUDA();
|
||||
#endif
|
||||
}
|
||||
|
||||
float* Concat::forwardCPU(const float* input_A, const float* input_B) {
|
||||
throw std::logic_error("Not implemented");
|
||||
}
|
||||
|
||||
float* Concat::forward(const float* input_A, const float* input_B) {
|
||||
#ifdef USE_CUDA
|
||||
return forwardCUDA(input_A, input_B);
|
||||
#else
|
||||
return forwardCPU(input_A, input_B);
|
||||
#endif
|
||||
}
|
||||
|
||||
int Concat::getOutputSize() {
|
||||
return inputASize + inputBSize;
|
||||
};
|
||||
@@ -1,37 +0,0 @@
|
||||
#include "concat.cuh"
|
||||
#include "cuda_helper.cuh"
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
|
||||
Concat::Concat(const int inputASize, const int inputBSize)
|
||||
: inputASize(inputASize), inputBSize(inputBSize) {
|
||||
|
||||
d_output = nullptr;
|
||||
CUDA_CHECK(cudaMalloc(
|
||||
(void**)&d_output, sizeof(float) * (inputASize + inputBSize)
|
||||
));
|
||||
}
|
||||
|
||||
Concat::~Concat() {
|
||||
cudaFree(d_output);
|
||||
}
|
||||
|
||||
float* Concat::forward(const float* d_input_A, const float* d_input_B) {
|
||||
CUDA_CHECK(cudaMemcpy(
|
||||
d_output, d_input_A, sizeof(float) * inputASize, cudaMemcpyDeviceToDevice
|
||||
));
|
||||
|
||||
CUDA_CHECK(cudaMemcpy(
|
||||
d_output + inputASize, d_input_B,
|
||||
sizeof(float) * inputBSize, cudaMemcpyDeviceToDevice
|
||||
));
|
||||
|
||||
CUDA_CHECK(cudaDeviceSynchronize());
|
||||
|
||||
return d_output;
|
||||
}
|
||||
|
||||
int Concat::getOutputSize() {
|
||||
return inputASize + inputBSize;
|
||||
};
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "concat.cuh"
|
||||
#include "concat.hpp"
|
||||
#include <gtest/gtest.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <vector>
|
||||
|
||||
Reference in New Issue
Block a user