-
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 <ydrml@hotmail.com>
- Loading branch information
Showing
6 changed files
with
140 additions
and
89 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include "extra_padding.cuh" | ||
|
||
namespace refactor::kernel { | ||
|
||
std::optional<ExtraPadding> | ||
ExtraPadding::build(DataType dt, int const *shape, int const *pads) { | ||
if (pads[0] == pads[2] && pads[1] == pads[3]) { | ||
return std::nullopt; | ||
} | ||
int padH = pads[0] - pads[2], padW = pads[1] - pads[3]; | ||
return ExtraPadding{ | ||
dt, | ||
shape[0] * shape[1], | ||
(shape[2] + std::abs(padH)) * (shape[3] + std::abs(padW)), | ||
shape[3] + std::abs(padW), | ||
shape[2], | ||
shape[3], | ||
padH, | ||
padW}; | ||
} | ||
|
||
size_t | ||
ExtraPadding::workspace() const { | ||
return nc * sohw * dt.size(); | ||
} | ||
|
||
|
||
void const * | ||
ExtraPadding::operator()(void const *src, void *workspace_) const { | ||
auto extra = reinterpret_cast<uint8_t *>(workspace_); | ||
|
||
#define CASE(T) \ | ||
case DataType::T: { \ | ||
using T_ = primitive<DataType::T>::type; \ | ||
thrust::tabulate(thrust::device, \ | ||
reinterpret_cast<T_ *>(extra), \ | ||
reinterpret_cast<T_ *>(extra + workspace()), \ | ||
ExtraPaddingFunctor<T_>{*this, src}); \ | ||
} break; | ||
|
||
switch (dt) { | ||
CASE(F32) | ||
CASE(F64) | ||
default: | ||
UNREACHABLE(); | ||
} | ||
return workspace_; | ||
} | ||
|
||
}// 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,52 @@ | ||
#ifndef KERNEL_EXTRA_PADDING_CUH | ||
#define KERNEL_EXTRA_PADDING_CUH | ||
|
||
#include "common.h" | ||
#include <optional> | ||
#include <thrust/execution_policy.h> | ||
#include <thrust/tabulate.h> | ||
|
||
namespace refactor::kernel { | ||
|
||
struct ExtraPadding { | ||
DataType dt; | ||
int nc, sohw, sow, h, w, padH, padW; | ||
|
||
static std::optional<ExtraPadding> build(DataType dt, int const *shape, int const *pads); | ||
|
||
size_t workspace() const; | ||
|
||
void const *operator()(void const *src, void *workspace) const; | ||
}; | ||
|
||
template<class T> | ||
struct ExtraPaddingFunctor { | ||
ExtraPadding info; | ||
void const *src; | ||
|
||
__device__ T operator()(size_t i) const noexcept { | ||
auto h = i / info.sow, | ||
w = i % info.sow; | ||
if (0 < info.padH) { | ||
if (h < info.padH) { | ||
return 0; | ||
} | ||
h -= info.padH; | ||
} else if (h >= info.h) { | ||
return 0; | ||
} | ||
if (0 < info.padW) { | ||
if (w < info.padW) { | ||
return 0; | ||
} | ||
w -= info.padW; | ||
} else if (w >= info.w) { | ||
return 0; | ||
} | ||
return reinterpret_cast<T const *>(src)[i / info.sohw * info.h * info.w + h * info.w + w]; | ||
} | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_EXTRA_PADDING_CUH |
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