File: conv_api_strided_tensors.cpp

package info (click to toggle)
miopen 6.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,788 kB
  • sloc: cpp: 300,511; lisp: 29,731; ansic: 2,683; sh: 471; python: 323; makefile: 155
file content (198 lines) | stat: -rw-r--r-- 8,184 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2023 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 *******************************************************************************/

// Test Suite for convolution with strided tensor descriptors

#include <gtest/gtest.h>
#include <miopen/miopen.h>
#include "platform.hpp"
#include "../workspace.hpp"

#define MIOPEN_CHECK_RET(val) ASSERT_EQ(val, miopenStatusSuccess)

class GPU_ConvStridedTensors_FP32 : public ::testing::Test
{
protected:
    void SetUp() override
    {
        // MIOpen handle
        MIOPEN_CHECK_RET(miopenCreate(&handle));

        // Tensor descriptors
        MIOPEN_CHECK_RET(miopenCreateTensorDescriptor(&input_descr));
        MIOPEN_CHECK_RET(miopenSetTensorDescriptor(
            input_descr, miopenFloat, input_dims.size(), input_dims.data(), input_strides.data()));

        MIOPEN_CHECK_RET(miopenCreateTensorDescriptor(&filter_descr));
        MIOPEN_CHECK_RET(miopenSetTensorDescriptor(filter_descr,
                                                   miopenFloat,
                                                   filter_dims.size(),
                                                   filter_dims.data(),
                                                   filter_strides.data()));

        MIOPEN_CHECK_RET(miopenCreateTensorDescriptor(&output_descr));
        MIOPEN_CHECK_RET(miopenSetTensorDescriptor(output_descr,
                                                   miopenFloat,
                                                   output_dims.size(),
                                                   output_dims.data(),
                                                   output_strides.data()));

        // Convolution descriptor
        MIOPEN_CHECK_RET(miopenCreateConvolutionDescriptor(&conv_descr));
        MIOPEN_CHECK_RET(miopenInitConvolutionNdDescriptor(
            conv_descr, pad.size(), pad.data(), stride.data(), dilation.data(), miopenConvolution));
        MIOPEN_CHECK_RET(miopenSetConvolutionGroupCount(conv_descr, 1));

        // Workspace
        size_t sz = 0;
        MIOPEN_CHECK_RET(miopenConvolutionForwardGetWorkSpaceSize(
            handle, filter_descr, input_descr, conv_descr, output_descr, &sz));

        // Data
        wspace.resize(sz);
        h_input.resize(input_size);
        h_filter.resize(filter_size);
        h_output.resize(output_size);
    }

    void TearDown() override
    {
        // Convolution descriptor
        if(conv_descr != nullptr)
        {
            MIOPEN_CHECK_RET(miopenDestroyConvolutionDescriptor(conv_descr));
        }

        // Tensor descriptors
        if(output_descr != nullptr)
        {
            MIOPEN_CHECK_RET(miopenDestroyTensorDescriptor(output_descr));
        }
        if(filter_descr != nullptr)
        {
            MIOPEN_CHECK_RET(miopenDestroyTensorDescriptor(filter_descr));
        }
        if(input_descr != nullptr)
        {
            MIOPEN_CHECK_RET(miopenDestroyTensorDescriptor(input_descr));
        }

        // MIOpen handle
        if(handle != nullptr)
        {
            MIOPEN_CHECK_RET(miopenDestroy(handle));
        }
    }

    // MIOpen handle
    miopenHandle_t handle = nullptr;

    // Tensor descriptors
    miopenTensorDescriptor_t input_descr  = nullptr;
    miopenTensorDescriptor_t filter_descr = nullptr;
    miopenTensorDescriptor_t output_descr = nullptr;
    std::vector<int> input_dims           = {4, 4, 16, 9, 16};
    std::vector<int> input_strides        = {10240, 2560, 160, 16, 1};
    std::vector<int> filter_dims          = {8, 4, 3, 3, 3};
    std::vector<int> filter_strides       = {108, 27, 9, 3, 1};
    std::vector<int> output_dims          = {4, 8, 8, 4, 8};
    std::vector<int> output_strides       = {2048, 256, 32, 8, 1};

    // Convolution descriptor
    miopenConvolutionDescriptor_t conv_descr = nullptr;
    std::vector<int> pad                     = {1, 0, 1};
    std::vector<int> stride                  = {2, 2, 2};
    std::vector<int> dilation                = {1, 1, 1};

    // Workspace
    Workspace wspace{};

    // Data
    const size_t input_size   = input_dims[0] * input_strides[0];
    const size_t filter_size  = filter_dims[0] * filter_strides[0];
    const size_t output_size  = output_dims[0] * output_strides[0];
    const size_t input_bytes  = input_size * sizeof(float);
    const size_t filter_bytes = filter_size * sizeof(float);
    const size_t output_bytes = output_size * sizeof(float);
    std::vector<float> h_input;
    std::vector<float> h_filter;
    std::vector<float> h_output;
};

TEST_F(GPU_ConvStridedTensors_FP32, ConvStridedTensorsNotImplemented)
{
    auto device = Device(handle);

    auto d_input  = device.Malloc(input_bytes);
    auto d_filter = device.Malloc(filter_bytes);
    auto d_output = device.Malloc(output_bytes);

    std::fill_n(h_input.begin(), h_input.size(), 1.f);
    ASSERT_TRUE(d_input.CopyToDevice(h_input.data(), input_bytes));

    std::fill_n(h_filter.begin(), h_filter.size(), 1.f);
    ASSERT_TRUE(d_filter.CopyToDevice(h_filter.data(), filter_bytes));

    miopenConvAlgoPerf_t perf_results[10];
    int perf_results_count;

    ASSERT_EQ(miopenFindConvolutionForwardAlgorithm(handle,
                                                    input_descr,
                                                    d_input.Data(),
                                                    filter_descr,
                                                    d_filter.Data(),
                                                    conv_descr,
                                                    output_descr,
                                                    d_output.Data(),
                                                    sizeof(perf_results) / sizeof(perf_results[0]),
                                                    &perf_results_count,
                                                    perf_results,
                                                    wspace.ptr(),
                                                    wspace.size(),
                                                    true),
              miopenStatusSuccess);
    ASSERT_GT(perf_results_count, 0);

    const float alpha = 1.f;
    const float beta  = 0.f;

    ASSERT_TRUE(device.Synchronize());
    ASSERT_EQ(miopenConvolutionForward(handle,
                                       &alpha,
                                       input_descr,
                                       d_input.Data(),
                                       filter_descr,
                                       d_filter.Data(),
                                       conv_descr,
                                       perf_results[0].fwd_algo,
                                       &beta,
                                       output_descr,
                                       d_output.Data(),
                                       wspace.ptr(),
                                       wspace.size()),
              miopenStatusSuccess);
    ASSERT_TRUE(device.Synchronize());
}