mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-22 22:34:22 +00:00
Migrate add layer to tensors
This commit is contained in:
@@ -1,44 +1,22 @@
|
||||
#include "add.hpp"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
using namespace CUDANet::Layers;
|
||||
|
||||
|
||||
Add::Add(int inputSize)
|
||||
: inputSize(inputSize) {
|
||||
|
||||
output = new float[inputSize];
|
||||
|
||||
#ifdef USE_CUDA
|
||||
initCUDA();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
Add::~Add() {
|
||||
#ifdef USE_CUDA
|
||||
delCUDA();
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
float* Add::forward(const float* inputA, const float* inputB) {
|
||||
|
||||
#ifdef USE_CUDA
|
||||
return forwardCUDA(inputA, inputB);
|
||||
#else
|
||||
return forwardCPU(inputA, inputB);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
float* Add::forwardCPU(const float* inputA, const float* inputB) {
|
||||
for (size_t i = 0; i < inputSize; i++)
|
||||
{
|
||||
output[i] = inputA[i] + inputB[i];
|
||||
Add::Add(CUDANet::Shape a_shape, CUDANet::Shape b_shape, CUDANet::Backend* backend) : backend(backend) {
|
||||
if (a_shape != b_shape) {
|
||||
throw InvalidShapeException(
|
||||
"Add requires matching dimensions", a_shape, b_shape
|
||||
);
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
out_shape = a_shape;
|
||||
output = CUDANet::Tensor(out_shape, CUDANet::DType::FLOAT32, backend);
|
||||
}
|
||||
|
||||
Add::~Add() {}
|
||||
|
||||
CUDANet::Tensor&
|
||||
Add::forward(CUDANet::Tensor& input_a, CUDANet::Tensor& input_b) {
|
||||
output.zero();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user