CUDNN Frontend API  8.3.0
cudnn_frontend_Reorder_Tensor.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <algorithm>
26 #include <array>
27 #include <functional>
28 #include <memory>
29 #include <sstream>
30 #include <iostream>
31 #include <utility>
32 
33 #include "cudnn_frontend_Tensor.h"
34 #include "cudnn_frontend_ConvDesc.h"
35 
36 namespace cudnn_frontend {
37 
38 static cudnnStatus_t
39 cudnnReorderFilterAndBiasInt8x32(cudnnHandle_t handle,
40  const Tensor_v8 &tensor,
41  const ConvDesc_v8 &conv_desc,
42  void *dev_filter_ptr, void *reordered_filter_ptr,
43  void *dev_bias_ptr, void *reordered_bias_ptr) {
44 
45  auto cudnn_status = CUDNN_STATUS_SUCCESS;
46 
47  if (dev_filter_ptr && reordered_filter_ptr == nullptr) {
48  return CUDNN_STATUS_BAD_PARAM;
49  }
50  if (dev_bias_ptr && reordered_bias_ptr == nullptr) {
51  return CUDNN_STATUS_BAD_PARAM;
52  }
53 
54  cudnnFilterDescriptor_t filterDesc = nullptr;
55 
56  cudnn_status = cudnnCreateFilterDescriptor(&filterDesc);
57  if (cudnn_status != CUDNN_STATUS_SUCCESS) {return cudnn_status;}
58 
59  auto conv_dims = conv_desc.getDimensionCount();
60  auto tensor_dims = tensor.getDimensionCount();
61  auto non_shape_dims = tensor_dims - conv_dims;
62 
63  if (non_shape_dims != 2 && non_shape_dims != 3) {
64  return CUDNN_STATUS_BAD_PARAM;
65  }
66 
67  if (conv_dims != 2 && conv_dims != 3) {
68  return CUDNN_STATUS_BAD_PARAM;
69  }
70 
71  int filter_dims_[5] = {1,1,1,1,1};
72  int64_t const * filter_dims = tensor.getDimArray();
73  filter_dims_[0] = static_cast<int> (filter_dims[0]); // n
74  filter_dims_[1] = static_cast<int> ((non_shape_dims == 2) ? filter_dims[1] : filter_dims[2]) * 32; // c
75  filter_dims_[2] = static_cast<int> ((non_shape_dims == 2) ? filter_dims[2] : filter_dims[3]); // d/h
76  filter_dims_[3] = static_cast<int> ((non_shape_dims == 2) ? filter_dims[3] : filter_dims[4]); // h/w
77  if (conv_dims == 3) {
78  filter_dims_[4] = static_cast<int> ((non_shape_dims == 2) ? filter_dims[4] : filter_dims[5]); // w
79  }
80 
81  cudnn_status = cudnnSetFilterNdDescriptor(filterDesc, CUDNN_DATA_INT8x32, CUDNN_TENSOR_NCHW_VECT_C, conv_dims + 2, filter_dims_);
82 
83  if (cudnn_status != CUDNN_STATUS_SUCCESS) {return cudnn_status;}
84 
85  int reorderBias = (dev_bias_ptr != nullptr);
86 
87  cudnn_status = cudnnReorderFilterAndBias(handle,
88  filterDesc, CUDNN_DEFAULT_REORDER, dev_filter_ptr, reordered_filter_ptr, reorderBias, dev_bias_ptr, reordered_bias_ptr);
89 
90  cudnnDestroyFilterDescriptor(filterDesc);
91  return cudnn_status;
92 }
93 }
int64_t const * getDimArray() const
static cudnnStatus_t cudnnReorderFilterAndBiasInt8x32(cudnnHandle_t handle, const Tensor_v8 &tensor, const ConvDesc_v8 &conv_desc, void *dev_filter_ptr, void *reordered_filter_ptr, void *dev_bias_ptr, void *reordered_bias_ptr)