-
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.
fix(kernel): 改正 mat mul integer Signed-off-by: YdrMaster <ydrml@hotmail.com>
- Loading branch information
Showing
10 changed files
with
344 additions
and
54 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
64 changes: 64 additions & 0 deletions
64
src/04kernel/src/kernels/dynamic_quantize_linear/cpu_kernel.cc
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,64 @@ | ||
#include "cpu_kernel.hh" | ||
#include <execution> | ||
#include <numeric> | ||
|
||
namespace refactor::kernel { | ||
using K = DynamicQuantizeLinearCpu; | ||
|
||
K::DynamicQuantizeLinearCpu(decltype(size) size_) noexcept | ||
: Kernel(), size(size_) {} | ||
|
||
auto K::build(decltype(size) size) noexcept -> KernelBox { | ||
return std::make_unique<K>(size); | ||
} | ||
|
||
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 dynamic quantize linear using CPU"; | ||
} | ||
|
||
auto K::lower(Resources &) const noexcept -> RoutineWorkspace { | ||
using namespace runtime; | ||
return [size = size](Resources &, void *, void const *const *inputs, void *const *outputs) { | ||
using TI = float; | ||
using TO = uint8_t; | ||
|
||
constexpr static auto | ||
ZERO = static_cast<TI>(0), | ||
_MIN = std::numeric_limits<TI>::min(), | ||
_MAX = std::numeric_limits<TI>::max(), | ||
QMIN = static_cast<TI>(std::numeric_limits<TO>::min()), | ||
QMAX = static_cast<TI>(std::numeric_limits<TO>::max()), | ||
QLEN = QMAX - QMIN; | ||
|
||
auto x = reinterpret_cast<TI const *>(inputs[0]); | ||
auto [min, max] = std::accumulate( | ||
x, x + size, | ||
std::pair{_MAX, _MIN}, | ||
[](auto acc, auto it) { | ||
auto [min, max] = acc; | ||
return std::pair{ | ||
std::min(min, it), | ||
std::max(max, it), | ||
}; | ||
}); | ||
auto len = std::max(ZERO, max) - std::min(ZERO, min); | ||
auto scale = len / QLEN; | ||
auto zp = static_cast<TO>(std::round(QMIN - min * QLEN / len)); | ||
|
||
std::transform( | ||
std::execution::par_unseq, | ||
x, x + size, | ||
reinterpret_cast<TO *>(outputs[0]), | ||
[=](auto it) { return static_cast<TO>(std::round(it / scale) + zp); }); | ||
*reinterpret_cast<TI *>(outputs[1]) = scale; | ||
*reinterpret_cast<TO *>(outputs[2]) = zp; | ||
}; | ||
} | ||
|
||
}// namespace refactor::kernel |
23 changes: 23 additions & 0 deletions
23
src/04kernel/src/kernels/dynamic_quantize_linear/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,23 @@ | ||
#ifndef KERNEL_DYNAMIC_QUANTIZE_LINEAR_CPU_KERNEL_HH | ||
#define KERNEL_DYNAMIC_QUANTIZE_LINEAR_CPU_KERNEL_HH | ||
|
||
#include "kernel/kernel.h" | ||
|
||
namespace refactor::kernel { | ||
|
||
struct DynamicQuantizeLinearCpu final : public Kernel { | ||
size_t size; | ||
|
||
explicit DynamicQuantizeLinearCpu(decltype(size)) noexcept; | ||
|
||
static KernelBox build(decltype(size)) noexcept; | ||
static size_t typeId() noexcept; | ||
|
||
size_t kernelTypeId() const noexcept final; | ||
std::string_view description() const noexcept final; | ||
RoutineWorkspace lower(Resources &) const noexcept final; | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_SOFTMAX_CPU_KERNEL_HH |
23 changes: 23 additions & 0 deletions
23
src/04kernel/src/kernels/dynamic_quantize_linear/cuda_kernel.cc
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,23 @@ | ||
#include "cuda_kernel.hh" | ||
|
||
namespace refactor::kernel { | ||
using K = DynamicQuantizeLinearCuda; | ||
|
||
K::DynamicQuantizeLinearCuda(decltype(size) size_) noexcept | ||
: Kernel(), size(size_) {} | ||
|
||
auto K::build(decltype(size) size) noexcept -> KernelBox { | ||
return std::make_unique<K>(size); | ||
} | ||
|
||
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 dynamic quantize linear using Nvidia GPU"; | ||
} | ||
|
||
}// namespace refactor::kernel |
16 changes: 16 additions & 0 deletions
16
src/04kernel/src/kernels/dynamic_quantize_linear/cuda_kernel.cu
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 @@ | ||
#include "cuda_kernel.hh" | ||
#include <cub/cub.cuh> | ||
|
||
namespace refactor::kernel { | ||
using K = DynamicQuantizeLinearCuda; | ||
|
||
auto K::lower(Resources &) const noexcept -> RoutineWorkspace { | ||
using namespace runtime; | ||
using TI = float; | ||
using TO = uint8_t; | ||
|
||
return [size = size](Resources &, void *, void const *const *inputs, void *const *outputs) { | ||
}; | ||
} | ||
|
||
}// namespace refactor::kernel |
25 changes: 25 additions & 0 deletions
25
src/04kernel/src/kernels/dynamic_quantize_linear/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef KERNEL_DYNAMIC_QUANTIZE_LINEAR_CUDA_KERNEL_HH | ||
#define KERNEL_DYNAMIC_QUANTIZE_LINEAR_CUDA_KERNEL_HH | ||
|
||
#include "kernel/kernel.h" | ||
|
||
namespace refactor::kernel { | ||
|
||
struct DynamicQuantizeLinearCuda final : public Kernel { | ||
size_t size; | ||
|
||
explicit DynamicQuantizeLinearCuda(decltype(size)) noexcept; | ||
|
||
static KernelBox build(decltype(size)) noexcept; | ||
static size_t typeId() noexcept; | ||
|
||
size_t kernelTypeId() const noexcept final; | ||
std::string_view description() const noexcept final; | ||
#ifdef USE_CUDA | ||
RoutineWorkspace lower(Resources &) const noexcept final; | ||
#endif | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_DYNAMIC_QUANTIZE_LINEAR_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
Oops, something went wrong.