mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-13 17:55:48 +00:00
cpu direct conv
This commit is contained in:
@@ -1,31 +1,67 @@
|
||||
#include <iostream>
|
||||
#include "tensor.hpp"
|
||||
|
||||
template <typename T>
|
||||
void direct_convolution(const Tensor<T>& in,
|
||||
const Tensor<T>& wei,
|
||||
Tensor<T>& out,
|
||||
std::size_t num_thread)
|
||||
{
|
||||
auto f = [&](auto n, auto k, auto ho, auto wo) {
|
||||
double v = 0;
|
||||
for(int c = 0; c < wei.mDesc.GetLengths()[1]; ++c)
|
||||
{
|
||||
for(int y = 0; y < wei.mDesc.GetLengths()[2]; ++y)
|
||||
{
|
||||
int hi = ho + y;
|
||||
for(int x = 0; x < wei.mDesc.GetLengths()[3]; ++x)
|
||||
{
|
||||
int wi = wo + x;
|
||||
v += in(n, c, hi, wi) * wei(k, c, y, x);
|
||||
}
|
||||
}
|
||||
}
|
||||
out(n, k, ho, wo) = v;
|
||||
};
|
||||
|
||||
auto f_par = make_ParallelTensorFunctor(f,
|
||||
out.mDesc.GetLengths()[0],
|
||||
out.mDesc.GetLengths()[1],
|
||||
out.mDesc.GetLengths()[2],
|
||||
out.mDesc.GetLengths()[3]);
|
||||
|
||||
f_par(num_thread);
|
||||
}
|
||||
|
||||
template <class T>
|
||||
struct Generator
|
||||
{
|
||||
|
||||
template <class... Is>
|
||||
T operator()(Is... is)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Tensor<float> in({3, 16, 128, 128});
|
||||
Tensor<float> wei({4, 16, 3, 3});
|
||||
Tensor<float> out({3, 4, 126, 126});
|
||||
|
||||
int len_in = 100;
|
||||
int len_wei = 3;
|
||||
int len_out = len_in - len_wei + 1;
|
||||
int num_thread = std::thread::hardware_concurrency();
|
||||
|
||||
std::vector<float> in(len_in, 1);
|
||||
std::vector<float> wei(len_wei, 1);
|
||||
std::vector<float> out(len_out, 1);
|
||||
std::cout << __func__ << ": num_thread " << num_thread << std::endl;
|
||||
|
||||
direct_convolution(in.data(), wei.data(), out.data(), len_in, len_wei);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void direct_convolution(const T* in, const T* wei, T* out, const int len_in, const int len_wei)
|
||||
{
|
||||
int len_out = len_in - len_wei + 1;
|
||||
|
||||
for(int i_out = 0; i_out < len_out++ i_out)
|
||||
{
|
||||
double acc = 0;
|
||||
for(int i_wei = 0; i_wei < len_wei; ++i_wei)
|
||||
{
|
||||
acc += in[i_out + i_wei] * *wei[i_wei];
|
||||
}
|
||||
out[i_out] = acc;
|
||||
}
|
||||
in.GenerateTensorValue(Generator<float>{}, num_thread);
|
||||
wei.GenerateTensorValue(Generator<float>{}, num_thread);
|
||||
|
||||
direct_convolution(in, wei, out, num_thread);
|
||||
|
||||
std::cout << __func__ << ": done" << std::endl;
|
||||
|
||||
LogRange(std::cout, in.mData, ",") << std::endl;
|
||||
LogRange(std::cout, wei.mData, ",") << std::endl;
|
||||
LogRange(std::cout, out.mData, ",") << std::endl;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user