-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMiopenConvDescriptor.cpp
More file actions
156 lines (132 loc) · 5.43 KB
/
Copy pathMiopenConvDescriptor.cpp
File metadata and controls
156 lines (132 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <algorithm>
#include <limits>
#include <vector>
#include <hipdnn_sdk/plugin/PluginException.hpp>
#include "MiopenConvDescriptor.hpp"
#include "MiopenUtils.hpp"
namespace miopen_legacy_plugin
{
namespace
{
void copyWithCheck(const flatbuffers::Vector<int64_t>* src,
std::vector<int>& dst,
size_t expectedSize,
const char* name,
const char* expectedSizeName)
{
if(src->size() != expectedSize)
{
throw hipdnn_plugin::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: " + std::string(name)
+ " size must be equal to "
+ std::string(expectedSizeName));
}
if(!std::all_of(src->begin(), src->end(), [](int64_t v) {
return v <= std::numeric_limits<int>::max();
}))
{
throw hipdnn_plugin::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: " + std::string(name)
+ " values must be less than INT_MAX");
}
std::copy(src->begin(), src->end(), dst.begin());
}
}
MiopenConvDescriptor::MiopenConvDescriptor(
size_t spatialDimCount, const hipdnn_sdk::data_objects::ConvolutionFwdAttributes& attributes)
{
if(spatialDimCount > std::numeric_limits<int>::max())
{
throw hipdnn_plugin::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: spatialDimCount must be not greater than INT_MAX");
}
const auto convMode = attributes.conv_mode();
if(convMode != hipdnn_sdk::data_objects::ConvMode::CROSS_CORRELATION)
{
throw hipdnn_plugin::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: only ConvMode::CROSS_CORRELATION is supported");
}
const auto attrPrePadding = attributes.pre_padding();
if(attrPrePadding == nullptr)
{
throw hipdnn_plugin::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: pre_padding must be set");
}
const auto attrPostPadding = attributes.post_padding();
if(attrPostPadding == nullptr)
{
throw hipdnn_plugin::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM, "MiopenConvDescriptor: post_padding must be set");
}
const auto attrStride = attributes.stride();
if(attrStride == nullptr)
{
throw hipdnn_plugin::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: stride must be set");
}
const auto attrDilation = attributes.dilation();
if(attrDilation == nullptr)
{
throw hipdnn_plugin::HipdnnPluginException(HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: dilation must be set");
}
if(attrPrePadding->size() != attrPostPadding->size())
{
throw hipdnn_plugin::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: pre_padding and post_padding sizes must be equal");
}
if(!std::equal(attrPrePadding->begin(), attrPrePadding->end(), attrPostPadding->begin()))
{
throw hipdnn_plugin::HipdnnPluginException(
HIPDNN_PLUGIN_STATUS_BAD_PARAM,
"MiopenConvDescriptor: asymmetric padding is not supported");
}
std::vector<int> padding(spatialDimCount);
std::vector<int> stride(spatialDimCount);
std::vector<int> dilation(spatialDimCount);
copyWithCheck(attrPrePadding, padding, spatialDimCount, "attrPadding", "spatialDimCount");
copyWithCheck(attrStride, stride, spatialDimCount, "attrStride", "spatialDimCount");
copyWithCheck(attrDilation, dilation, spatialDimCount, "attrDilation", "spatialDimCount");
THROW_ON_MIOPEN_FAILURE(miopenCreateConvolutionDescriptor(&_descriptor));
THROW_ON_MIOPEN_FAILURE(miopenInitConvolutionNdDescriptor(_descriptor,
static_cast<int>(spatialDimCount),
padding.data(),
stride.data(),
dilation.data(),
miopenConvolution));
}
MiopenConvDescriptor::MiopenConvDescriptor(MiopenConvDescriptor&& other) noexcept
: _descriptor(other._descriptor)
{
other._descriptor = nullptr;
}
MiopenConvDescriptor& MiopenConvDescriptor::operator=(MiopenConvDescriptor&& other) noexcept
{
if(this != &other)
{
if(_descriptor != nullptr)
{
LOG_ON_MIOPEN_FAILURE(miopenDestroyConvolutionDescriptor(_descriptor));
}
_descriptor = other._descriptor;
other._descriptor = nullptr;
}
return *this;
}
MiopenConvDescriptor::~MiopenConvDescriptor()
{
if(_descriptor != nullptr)
{
LOG_ON_MIOPEN_FAILURE(miopenDestroyConvolutionDescriptor(_descriptor));
}
}
miopenConvolutionDescriptor_t MiopenConvDescriptor::convDescriptor() const
{
return _descriptor;
}
}