-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
11 changed files
with
241 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "cpu_kernel.hh" | ||
#include <execution> | ||
|
||
namespace refactor::kernel { | ||
using K = SliceCpu; | ||
|
||
K::SliceCpu(SliceInfo info_) noexcept | ||
: Kernel(), info(std::move(info_)) {} | ||
|
||
auto K::build(SliceInfo info) noexcept -> KernelBox { | ||
return std::make_unique<K>(std::move(info)); | ||
} | ||
auto K::typeId() noexcept -> size_t { | ||
static uint8_t ID = 1; | ||
return reinterpret_cast<size_t>(&ID); | ||
} | ||
|
||
auto K::kernelTypeId() const noexcept -> size_t { | ||
return typeId(); | ||
} | ||
auto K::description() const noexcept -> std::string_view { | ||
return "Performing slice operation on generic cpu"; | ||
} | ||
|
||
Routine K::lower(Resources &) const noexcept { | ||
using namespace runtime; | ||
return [info = this->info](Resources &, void const **inputs, void **outputs) { | ||
auto src = reinterpret_cast<uint8_t const *>(inputs[0]) + info.baseOffset; | ||
auto dst = reinterpret_cast<uint8_t *>(outputs[0]); | ||
std::for_each_n(std::execution::par_unseq, | ||
natural_t(0), info.blockCount, | ||
[=, &info](auto i) { | ||
long rem = i; | ||
auto src_ = src; | ||
auto dst_ = dst + i * info.blockSize; | ||
for (auto const &dim : info.dims) { | ||
auto d = std::div(rem, dim.countStride); | ||
src_ += d.quot * dim.sizeStride + dim.sizeStart; | ||
rem = d.rem; | ||
} | ||
std::memcpy(dst_, src_, info.blockSize); | ||
}); | ||
}; | ||
} | ||
|
||
}// namespace refactor::kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef KERNEL_SPLIT_CPU_KERNEL_HH | ||
#define KERNEL_SPLIT_CPU_KERNEL_HH | ||
|
||
#include "kernel/attributes/slice_info.h" | ||
#include "kernel/kernel.h" | ||
|
||
namespace refactor::kernel { | ||
|
||
struct SliceCpu final : public Kernel { | ||
SliceInfo info; | ||
|
||
explicit SliceCpu(SliceInfo) noexcept; | ||
|
||
static KernelBox build(SliceInfo) noexcept; | ||
static size_t typeId() noexcept; | ||
|
||
size_t kernelTypeId() const noexcept final; | ||
std::string_view description() const noexcept final; | ||
Routine lower(Resources &) const noexcept final; | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_SPLIT_CPU_KERNEL_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "cuda_kernel.hh" | ||
|
||
namespace refactor::kernel { | ||
using K = SliceCuda; | ||
|
||
K::SliceCuda(SliceInfo info_) noexcept | ||
: Kernel(), info(std::move(info_)) {} | ||
|
||
auto K::build(SliceInfo info) noexcept -> KernelBox { | ||
#ifndef USE_CUDA | ||
return nullptr; | ||
#endif | ||
return std::make_unique<K>(std::move(info)); | ||
} | ||
auto K::typeId() noexcept -> size_t { | ||
static uint8_t ID = 1; | ||
return reinterpret_cast<size_t>(&ID); | ||
} | ||
|
||
auto K::kernelTypeId() const noexcept -> size_t { | ||
return typeId(); | ||
} | ||
auto K::description() const noexcept -> std::string_view { | ||
return "Performing slice operation using CUDA"; | ||
} | ||
|
||
}// namespace refactor::kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "cuda_kernel.hh" | ||
#include "kernel/cuda/split.cuh" | ||
#include "mem_manager/foreign_blob.hh" | ||
#include "runtime/mem_manager.hh" | ||
#include <thrust/device_vector.h> | ||
|
||
namespace refactor::kernel { | ||
using namespace runtime; | ||
|
||
Routine SliceCuda::lower(Resources &) const noexcept { | ||
return [](Resources &, void const **inputs, void **outputs) { | ||
}; | ||
} | ||
|
||
}// namespace refactor::kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef KERNEL_SPLIT_CUDA_KERNEL_HH | ||
#define KERNEL_SPLIT_CUDA_KERNEL_HH | ||
|
||
#include "kernel/attributes/slice_info.h" | ||
#include "kernel/kernel.h" | ||
|
||
namespace refactor::kernel { | ||
|
||
struct SliceCuda final : public Kernel { | ||
SliceInfo info; | ||
|
||
explicit SliceCuda(SliceInfo) noexcept; | ||
|
||
static KernelBox build(SliceInfo) noexcept; | ||
static size_t typeId() noexcept; | ||
|
||
size_t kernelTypeId() const noexcept final; | ||
std::string_view description() const noexcept final; | ||
#ifdef USE_CUDA | ||
Routine lower(Resources &) const noexcept final; | ||
#endif | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_SPLIT_CUDA_KERNEL_HH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "../../../src/kernels/slice/cpu_kernel.hh" | ||
#include <gtest/gtest.h> | ||
#include <numeric> | ||
|
||
using namespace refactor; | ||
using namespace kernel; | ||
|
||
TEST(kernel, SliceCpu) { | ||
// build routine | ||
Dimensions dims{ | ||
{5, -2, 3},// 7 -> {5, 3, 1} -> {108, 900, -360} | ||
{2, 3, 2}, // 6 -> {2, 5} -> { 36, 60, 90} | ||
{1, 1, 3}, // 5 -> {1, 2, 3} -> { 18, 6, 30} | ||
{0, 1, 1}, // 1 -> {0} | ||
{0, 1, 2}, // 2 -> {0, 1} | ||
{0, 1, 3}, // 3 -> {0, 1, 2} | ||
}; | ||
auto input = Tensor::share(DataType::F32, Shape{7, 6, 5, 1, 2, 3}), | ||
output = Tensor::share(DataType::F32, Shape{3, 2, 3, 1, 2, 3}); | ||
auto kernel = SliceCpu::build(SliceInfo(dims, *input)); | ||
ASSERT_TRUE(kernel); | ||
auto res = runtime::Resources(); | ||
auto routine = kernel->lower(res); | ||
// put input data | ||
std::vector<float> | ||
data(input->elementsSize()), | ||
result(output->elementsSize()); | ||
std::iota(data.begin(), data.end(), 0); | ||
// inference | ||
void const *inputs[]{data.data()}; | ||
void *outputs[]{result.data()}; | ||
routine(res, inputs, outputs); | ||
// check | ||
dim_t | ||
di[]{5, 3, 1}, | ||
dj[]{2, 5}, | ||
dk[]{1, 2, 3}; | ||
auto n = 6; | ||
for (auto i : range0_(3)) { | ||
for (auto j : range0_(2)) { | ||
for (auto k : range0_(3)) { | ||
// clang-format off | ||
auto src = di[i] * 6 * 5 * n + dj[j] * 5 * n + dk[k] * n; | ||
auto dst = i * 2 * 3 * n + j * 3 * n + k * n; | ||
// clang-format on | ||
for (auto l : range0_(n)) { | ||
EXPECT_EQ(data[src + l], result[dst + l]); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifdef USE_CUDA | ||
|
||
#include "../../../src/kernels/slice/cpu_kernel.hh" | ||
#include "../../../src/kernels/slice/cuda_kernel.hh" | ||
#include "kernel/target.h" | ||
#include "runtime/mem_manager.hh" | ||
#include <gtest/gtest.h> | ||
#include <numeric> | ||
|
||
using namespace refactor; | ||
using namespace kernel; | ||
|
||
TEST(kernel, SliceCuda) { | ||
} | ||
|
||
#endif |