• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "src/extendrt/delegate/tensorrt/cuda_impl/logical.cuh"
18 #include "src/extendrt/delegate/tensorrt/cuda_impl/cuda_helper.h"
19 
20 template <typename T>
LogicalNotKernel(const T * input1,T * output,int element_cnt)21 __global__ void LogicalNotKernel(const T *input1, T *output, int element_cnt) {
22   for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < element_cnt; pos += blockDim.x * gridDim.x) {
23     output[pos] = static_cast<T>(input1[pos] == 0);
24   }
25 }
26 
27 template <typename T>
LogicalAndKernel(const T * input_addr1,const T * input_addr2,T * output,int size)28 __global__ void LogicalAndKernel(const T *input_addr1, const T *input_addr2, T *output, int size) {
29   for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < size; pos += blockDim.x * gridDim.x) {
30     output[pos] = input_addr1[pos] * input_addr2[pos];
31   }
32 }
33 
34 template <typename T>
LogicalOrKernel(const T * input_addr1,const T * input_addr2,T * output,int size)35 __global__ void LogicalOrKernel(const T *input_addr1, const T *input_addr2, T *output, int size) {
36   for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < size; pos += blockDim.x * gridDim.x) {
37     T sum = input_addr1[pos] + input_addr2[pos];
38     output[pos] = static_cast<T>(sum > 0);
39   }
40 }
41 
42 template <typename T>
GreaterOrEqualKernal(const T * input1,const T * input2,T * output,int element_cnt)43 __global__ void GreaterOrEqualKernal(const T *input1, const T *input2, T *output, int element_cnt) {
44   for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < element_cnt; pos += blockDim.x * gridDim.x) {
45     output[pos] = (input1[pos] >= input2[pos]);
46   }
47 }
48 
49 template <typename T>
LessOrEqualKernal(const T * input1,const T * input2,T * output,int element_cnt)50 __global__ void LessOrEqualKernal(const T *input1, const T *input2, T *output, int element_cnt) {
51   for (int pos = blockIdx.x * blockDim.x + threadIdx.x; pos < element_cnt; pos += blockDim.x * gridDim.x) {
52     output[pos] = (input1[pos] <= input2[pos]);
53   }
54 }
55 
56 template <typename T>
LogicalNot(const T * input1,T * output,int element_cnt,cudaStream_t stream)57 void LogicalNot(const T *input1, T *output, int element_cnt, cudaStream_t stream) {
58   LogicalNotKernel<<<GET_BLOCKS(element_cnt), GET_THREADS, 0, stream>>>(input1, output, element_cnt);
59 }
60 
61 template <typename T>
LogicalAnd(const T * input1,const T * input2,T * output,int element_cnt,cudaStream_t stream)62 void LogicalAnd(const T *input1, const T *input2, T *output, int element_cnt, cudaStream_t stream) {
63   LogicalAndKernel<<<GET_BLOCKS(element_cnt), GET_THREADS, 0, stream>>>(input1, input2, output, element_cnt);
64 }
65 
66 template <typename T>
LogicalOr(const T * input1,const T * input2,T * output,int element_cnt,cudaStream_t stream)67 void LogicalOr(const T *input1, const T *input2, T *output, int element_cnt, cudaStream_t stream) {
68   LogicalOrKernel<<<GET_BLOCKS(element_cnt), GET_THREADS, 0, stream>>>(input1, input2, output, element_cnt);
69 }
70 
71 template <typename T>
GreaterOrEqual(const T * input1,const T * input2,T * output,int element_cnt,cudaStream_t stream)72 void GreaterOrEqual(const T *input1, const T *input2, T *output, int element_cnt, cudaStream_t stream) {
73   GreaterOrEqualKernal<<<GET_BLOCKS(element_cnt), GET_THREADS, 0, stream>>>(input1, input2, output, element_cnt);
74 }
75 
76 template <typename T>
LessOrEqual(const T * input1,const T * input2,T * output,int element_cnt,cudaStream_t stream)77 void LessOrEqual(const T *input1, const T *input2, T *output, int element_cnt, cudaStream_t stream) {
78   LessOrEqualKernal<<<GET_BLOCKS(element_cnt), GET_THREADS, 0, stream>>>(input1, input2, output, element_cnt);
79 }
80 
81 template void GreaterOrEqual(const float *input1, const float *input2, float *output, int element_cnt,
82                              cudaStream_t stream);
83 
84 template void GreaterOrEqual(const int *input1, const int *input2, int *output, int element_cnt, cudaStream_t stream);
85 
86 template void LessOrEqual(const float *input1, const float *input2, float *output, int element_cnt,
87                           cudaStream_t stream);
88 
89 template void LessOrEqual(const int *input1, const int *input2, int *output, int element_cnt, cudaStream_t stream);
90 
91 template void LogicalNot(const int32_t *input1, int32_t *output, int element_cnt, cudaStream_t stream);
92 
93 template void LogicalAnd(const int32_t *input1, const int32_t *input2, int32_t *output, int element_cnt,
94                          cudaStream_t stream);
95 
96 template void LogicalOr(const int32_t *input1, const int32_t *input2, int32_t *output, int element_cnt,
97                         cudaStream_t stream);
98