mirror of
https://github.com/lordmathis/CUDANet.git
synced 2025-12-22 14:24:22 +00:00
Migrate concat layer
This commit is contained in:
@@ -84,6 +84,12 @@ class Backend {
|
|||||||
CUDANet::Tensor& running_var,
|
CUDANet::Tensor& running_var,
|
||||||
CUDANet::Tensor& epsilon
|
CUDANet::Tensor& epsilon
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
|
virtual CUDANet::Tensor& concat(
|
||||||
|
CUDANet::Tensor& input_a,
|
||||||
|
CUDANet::Tensor& input_b,
|
||||||
|
CUDANet::Tensor& output
|
||||||
|
) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CUDANet
|
} // namespace CUDANet
|
||||||
@@ -80,6 +80,12 @@ class CUDA : public Backend {
|
|||||||
CUDANet::Tensor& running_var,
|
CUDANet::Tensor& running_var,
|
||||||
CUDANet::Tensor& epsilon
|
CUDANet::Tensor& epsilon
|
||||||
) override;
|
) override;
|
||||||
|
|
||||||
|
CUDANet::Tensor& concat(
|
||||||
|
CUDANet::Tensor& input_a,
|
||||||
|
CUDANet::Tensor& input_b,
|
||||||
|
CUDANet::Tensor& output
|
||||||
|
) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CUDANet::Backend
|
} // namespace CUDANet::Backend
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
#ifndef CUDANET_CONCAT_LAYER_H
|
#pragma once
|
||||||
#define CUDANET_CONCAT_LAYER_H
|
|
||||||
|
|
||||||
#include "layer.hpp"
|
#include "layer.hpp"
|
||||||
|
|
||||||
@@ -11,47 +10,24 @@ namespace CUDANet::Layers {
|
|||||||
*/
|
*/
|
||||||
class Concat {
|
class Concat {
|
||||||
public:
|
public:
|
||||||
/**
|
|
||||||
* @brief Create a new Concat layer
|
|
||||||
*
|
|
||||||
* @param inputASize Size of the first input
|
|
||||||
* @param inputBSize Size of the second input
|
|
||||||
*/
|
|
||||||
Concat(const int inputASize, const int inputBSize);
|
|
||||||
|
|
||||||
/**
|
Concat(const CUDANet::Shape a_shape, const CUDANet::Shape b_shape, CUDANet::Backend *backend);
|
||||||
* @brief Destroy the Concat layer
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
~Concat();
|
~Concat();
|
||||||
|
|
||||||
/**
|
CUDANet::Tensor& forward(CUDANet::Tensor& input_a, CUDANet::Tensor& input_b);
|
||||||
* @brief Concatenates the two inputs
|
|
||||||
*
|
|
||||||
* @param d_input_A Device pointer to the first input
|
|
||||||
* @param d_input_B Device pointer to the second input
|
|
||||||
*
|
|
||||||
* @return Device pointer to the output
|
|
||||||
*/
|
|
||||||
float* forward(const float* d_input_A, const float* d_input_B);
|
|
||||||
|
|
||||||
int getOutputSize();
|
CUDANet::Shape output_shape();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int inputASize;
|
CUDANet::Shape a_shape;
|
||||||
int inputBSize;
|
CUDANet::Shape b_shape;
|
||||||
|
|
||||||
float* forwardCPU(const float* input_A, const float* input_B);
|
CUDANet::Shape out_shape;
|
||||||
|
CUDANet::Tensor output;
|
||||||
|
|
||||||
#ifdef USE_CUDA
|
CUDANet::Backend *backend;
|
||||||
float* d_output;
|
|
||||||
float* forwardCUDA(const float* d_input_A, const float* d_input_B);
|
|
||||||
|
|
||||||
void initCUDA();
|
|
||||||
void delCUDA();
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CUDANet::Layers
|
} // namespace CUDANet::Layers
|
||||||
|
|
||||||
#endif // CUDANET_CONCAT_LAYER_H
|
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "activation.hpp"
|
|
||||||
#include "layer.hpp"
|
#include "layer.hpp"
|
||||||
|
|
||||||
namespace CUDANet::Layers {
|
namespace CUDANet::Layers {
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "backend.hpp"
|
#include "backend.hpp"
|
||||||
#include "layer.hpp"
|
#include "layer.hpp"
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,30 @@ class InvalidShapeException : public std::runtime_error {
|
|||||||
actual
|
actual
|
||||||
)
|
)
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
InvalidShapeException(
|
||||||
|
const std::string& message,
|
||||||
|
const Shape& shape_a,
|
||||||
|
const Shape& shape_b
|
||||||
|
)
|
||||||
|
: std::runtime_error(
|
||||||
|
std::format(
|
||||||
|
"{}. Shape A: [{}], Shape B: [{}]",
|
||||||
|
message,
|
||||||
|
format_shape(shape_a),
|
||||||
|
format_shape(shape_b)
|
||||||
|
)
|
||||||
|
) {}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static std::string format_shape(const Shape& shape) {
|
||||||
|
std::string result;
|
||||||
|
for (size_t i = 0; i < shape.size(); ++i) {
|
||||||
|
if (i > 0) result += ", ";
|
||||||
|
result += std::to_string(shape[i]);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace CUDANet
|
} // namespace CUDANet
|
||||||
|
|||||||
@@ -211,4 +211,24 @@ CUDANet::Tensor& CUDA::batch_norm(
|
|||||||
);
|
);
|
||||||
CUDA_CHECK(cudaGetLastError());
|
CUDA_CHECK(cudaGetLastError());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CUDANet::Tensor& CUDA::concat(
|
||||||
|
CUDANet::Tensor& input_a,
|
||||||
|
CUDANet::Tensor& input_b,
|
||||||
|
CUDANet::Tensor& output
|
||||||
|
) {
|
||||||
|
CUDA_CHECK(cudaMemcpy(
|
||||||
|
output.data<float>(), input_a.data<float>(), input_a.size(),
|
||||||
|
cudaMemcpyDeviceToDevice
|
||||||
|
));
|
||||||
|
|
||||||
|
CUDA_CHECK(cudaMemcpy(
|
||||||
|
output.data<float>() + input_a.numel(), input_b.data<float>(), input_b.size(),
|
||||||
|
cudaMemcpyDeviceToDevice
|
||||||
|
));
|
||||||
|
|
||||||
|
CUDA_CHECK(cudaDeviceSynchronize());
|
||||||
|
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
@@ -1,31 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
@@ -1,34 +1,32 @@
|
|||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include "concat.hpp"
|
#include "concat.hpp"
|
||||||
|
|
||||||
using namespace CUDANet::Layers;
|
using namespace CUDANet::Layers;
|
||||||
|
|
||||||
Concat::Concat(const int inputASize, const int inputBSize)
|
Concat::Concat(const CUDANet::Shape a_shape, const CUDANet::Shape b_shape, CUDANet::Backend *backend)
|
||||||
: inputASize(inputASize), inputBSize(inputBSize) {
|
: a_shape(a_shape), b_shape(b_shape), backend(backend) {
|
||||||
#ifdef USE_CUDA
|
if (a_shape[0] != b_shape[0] || a_shape[1] != b_shape[1]) {
|
||||||
initCUDA();
|
throw InvalidShapeException(
|
||||||
#endif
|
"Concat requires matching batch and height dimensions", a_shape,
|
||||||
|
b_shape
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
out_shape = {a_shape[0], a_shape[1], a_shape[2] + b_shape[2]};
|
||||||
|
output = CUDANet::Tensor(out_shape, CUDANet::DType::FLOAT32, backend);
|
||||||
}
|
}
|
||||||
|
|
||||||
Concat::~Concat() {
|
Concat::~Concat() {}
|
||||||
#ifdef USE_CUDA
|
|
||||||
delCUDA();
|
CUDANet::Tensor& Concat::forward(CUDANet::Tensor& input_a, CUDANet::Tensor& input_b) {
|
||||||
#endif
|
output.zero();
|
||||||
|
backend->concat(
|
||||||
|
input_a,
|
||||||
|
input_b,
|
||||||
|
output
|
||||||
|
);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
float* Concat::forwardCPU(const float* input_A, const float* input_B) {
|
CUDANet::Shape Concat::output_shape() {
|
||||||
throw std::logic_error("Not implemented");
|
return out_shape;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user