|
| 1 | +// Copyright (C) 2021-2023 Intel Corporation |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// |
| 4 | + |
| 5 | +#include "sign.hpp" |
| 6 | + |
| 7 | +namespace ov { |
| 8 | +namespace nvidia_gpu { |
| 9 | +namespace kernel { |
| 10 | + |
| 11 | +namespace cumath = CUDA::math; |
| 12 | + |
| 13 | +template <typename T, typename Enable = void> |
| 14 | +struct SignOpImpl { |
| 15 | + __device__ static inline T op(T x); |
| 16 | +}; |
| 17 | + |
| 18 | +template <> |
| 19 | +struct SignOpImpl<char> { |
| 20 | + __device__ static inline char op(char x) { |
| 21 | + return cumath::sign_int(x); |
| 22 | + } |
| 23 | +}; |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +struct SignOpImpl<T, typename std::enable_if<std::is_integral<T>::value && |
| 27 | + !std::is_unsigned<T>::value>::type> { |
| 28 | + __device__ static inline T op(T x) { |
| 29 | + return cumath::sign_int(x); |
| 30 | + } |
| 31 | +}; |
| 32 | + |
| 33 | +template <typename T> |
| 34 | +struct SignOpImpl<T, typename std::enable_if<std::is_integral<T>::value && |
| 35 | + std::is_unsigned<T>::value>::type> { |
| 36 | + __device__ static inline T op(T x) { |
| 37 | + return cumath::sign_uint(x); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +struct SignOpImpl<T, typename std::enable_if<std::is_floating_point<T>::value>::type> { |
| 43 | + __device__ static inline T op(T x) { |
| 44 | + return cumath::sign_float(x); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +template <> |
| 49 | +struct SignOpImpl<__nv_bfloat16> { |
| 50 | + __device__ static inline __nv_bfloat16 op(__nv_bfloat16 x) { |
| 51 | + return cumath::sign_float(x); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +template <> |
| 56 | +struct SignOpImpl<__half> { |
| 57 | + __device__ static inline __half op(__half x) { |
| 58 | + return cumath::sign_float(x); |
| 59 | + } |
| 60 | +}; |
| 61 | + |
| 62 | +Sign::Sign(Type_t element_type, size_t max_threads_per_block, size_t num_elements) |
| 63 | + : impl_{element_type, max_threads_per_block, num_elements} {} |
| 64 | + |
| 65 | +void Sign::operator()(cudaStream_t stream, const void* in0, void* out) const { |
| 66 | + impl_(stream, in0, out); |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace kernel |
| 70 | +} // namespace nvidia_gpu |
| 71 | +} // namespace ov |
0 commit comments