File: tensor_api.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 (676 lines) | stat: -rw-r--r-- 22,252 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*******************************************************************************
 *
 * 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 tensor API

#include <gtest/gtest.h>
#include <miopen/miopen.h>

#include <iomanip>
#include <optional>
#include <sstream>
#include <string>
#include <vector>

// Compiler uses undefined behavior sanitizer
// -fsanitize=enum (or -fsanitize=undefined)
#if(defined(__clang__) || defined(__GNUG__)) && !defined(NDEBUG)
#define UBSAN_ENABLED 1
#else
#define UBSAN_ENABLED 0
#endif

// We use out-of-range values for miopenDataType_t and miopenTensorLayout_t
#define USE_OUT_OF_RANGE_ENUM (UBSAN_ENABLED == 0)

// miopenLastDataType must be changed if new data types are added
#define miopenFirstDataType miopenHalf
#define miopenLastDataType miopenInt64

// miopenLastTensorLayout must be changed if new layouts are added
#define miopenFirstTensorLayout miopenTensorNCHW
#define miopenLastTensorLayout miopenTensorNDHWC

namespace {

using testDataType_t     = int;
using testTensorLayout_t = int;

enum class TestStatus
{
    Failed,
    Passed,
    Skipped
};

struct TensorParams
{
    testDataType_t dataType;
    std::optional<testTensorLayout_t> tensorLayout;
    int nbDims;
    int* dimsA;
    int* stridesA;

    friend std::ostream& operator<<(std::ostream& os, const TensorParams& tp)
    {
        os << "(";
        os << "type:" << static_cast<int>(tp.dataType) << ",";
        os << "layout:";
        if(tp.tensorLayout)
            os << static_cast<int>(tp.tensorLayout.value());
        else
            os << "none";
        os << ",";
        os << "ndims:" << tp.nbDims;
        if(tp.nbDims > 0)
        {
            os << ",";
            if(tp.dimsA == nullptr)
            {
                os << "dimsA:none";
            }
            else
            {
                for(unsigned i = 0; i < tp.nbDims; i++)
                {
                    if(i != 0)
                        os << "x";
                    os << tp.dimsA[i];
                }
            }
            os << ",";
            if(tp.stridesA == nullptr)
            {
                os << "stridesA:none";
            }
            else
            {
                for(unsigned i = 0; i < tp.nbDims; i++)
                {
                    if(i != 0)
                        os << "x";
                    os << tp.stridesA[i];
                }
            }
        }
        os << ")";
        return os;
    }
};

struct TestConfig
{
    bool null_tensor_descriptor;
    TensorParams params;
    bool valid;
    bool skip;

    friend std::ostream& operator<<(std::ostream& os, const TestConfig& tc)
    {
        os << "(";
        os << "skip:" << tc.skip << ",";
        os << "valid:" << tc.valid << ",";
        os << "null_td:" << tc.null_tensor_descriptor << ",";
        os << tc.params;
        os << ")";
        return os;
    }
};

template <class L1, class L2>
bool CompareLengths(const L1 l1, const L2 l2, int size)
{
    for(int i = 0; i < size; i++)
    {
        if(l1[i] != l2[i])
            return false;
    }
    return true;
}

// Set tensor descriptor
TestStatus Set4dTensorDescriptor(miopenTensorDescriptor_t tensorDesc,
                                 const TensorParams& params,
                                 bool check_skip)
{
    if(params.tensorLayout && (params.tensorLayout != miopenTensorNCHW))
        return TestStatus::Skipped;
    if(params.nbDims != 4 || params.dimsA == nullptr || params.stridesA != nullptr)
        return TestStatus::Skipped;

    if(check_skip)
        return TestStatus::Passed;

    miopenStatus_t status =
        miopenSet4dTensorDescriptor(tensorDesc,
                                    static_cast<miopenDataType_t>(params.dataType),
                                    params.dimsA[0],
                                    params.dimsA[1],
                                    params.dimsA[2],
                                    params.dimsA[3]);
    if(status == miopenStatusSuccess)
        return TestStatus::Passed;

    return TestStatus::Failed;
}

TestStatus SetNdTensorDescriptorWithLayout(miopenTensorDescriptor_t tensorDesc,
                                           const TensorParams& params,
                                           bool check_skip)
{
    if(!params.tensorLayout || params.stridesA != nullptr)
        return TestStatus::Skipped;

    if(check_skip)
        return TestStatus::Passed;

    miopenStatus_t status = miopenSetNdTensorDescriptorWithLayout(
        tensorDesc,
        static_cast<miopenDataType_t>(params.dataType),
        static_cast<miopenTensorLayout_t>(params.tensorLayout.value()),
        params.dimsA,
        params.nbDims);
    if(status == miopenStatusSuccess)
        return TestStatus::Passed;

    return TestStatus::Failed;
}

TestStatus Set4dTensorDescriptorEx(miopenTensorDescriptor_t tensorDesc,
                                   const TensorParams& params,
                                   bool check_skip)
{
    if(params.tensorLayout || params.nbDims != 4 || params.dimsA == nullptr ||
       params.stridesA == nullptr)
    {
        return TestStatus::Skipped;
    }

    if(check_skip)
        return TestStatus::Passed;

    miopenStatus_t status =
        miopenSet4dTensorDescriptorEx(tensorDesc,
                                      static_cast<miopenDataType_t>(params.dataType),
                                      params.dimsA[0],
                                      params.dimsA[1],
                                      params.dimsA[2],
                                      params.dimsA[3],
                                      params.stridesA[0],
                                      params.stridesA[1],
                                      params.stridesA[2],
                                      params.stridesA[3]);
    if(status == miopenStatusSuccess)
        return TestStatus::Passed;

    return TestStatus::Failed;
}

TestStatus SetTensorDescriptor(miopenTensorDescriptor_t tensorDesc,
                               const TensorParams& params,
                               bool check_skip)
{
    if(params.tensorLayout)
        return TestStatus::Skipped;

    if(check_skip)
        return TestStatus::Passed;

    miopenStatus_t status =
        miopenSetTensorDescriptor(tensorDesc,
                                  static_cast<miopenDataType_t>(params.dataType),
                                  params.nbDims,
                                  params.dimsA,
                                  params.stridesA);
    if(status == miopenStatusSuccess)
        return TestStatus::Passed;

    return TestStatus::Failed;
}

TestStatus SetTensorDescriptorV2(miopenTensorDescriptor_t tensorDesc,
                                 const TensorParams& params,
                                 bool check_skip)
{
    if(params.tensorLayout)
        return TestStatus::Skipped;

    if(check_skip)
        return TestStatus::Passed;

    size_t* dimsA    = nullptr;
    size_t* stridesA = nullptr;
    std::vector<std::size_t> dims;
    std::vector<std::size_t> strides;

    if(params.nbDims > 0)
    {
        if(params.dimsA != nullptr)
        {
            dims  = std::vector<std::size_t>(params.dimsA, params.dimsA + params.nbDims);
            dimsA = dims.data();
        }
        if(params.stridesA != nullptr)
        {
            strides  = std::vector<std::size_t>(params.stridesA, params.stridesA + params.nbDims);
            stridesA = strides.data();
        }
    }

    miopenStatus_t status = miopenSetTensorDescriptorV2(
        tensorDesc, static_cast<miopenDataType_t>(params.dataType), params.nbDims, dimsA, stridesA);
    if(status == miopenStatusSuccess)
        return TestStatus::Passed;

    return TestStatus::Failed;
}

const auto set_tensor_descr_funcs = {Set4dTensorDescriptor,
                                     SetNdTensorDescriptorWithLayout,
                                     Set4dTensorDescriptorEx,
                                     SetTensorDescriptor,
                                     SetTensorDescriptorV2};

// Get tensor descriptor
TestStatus Get4dTensorDescriptor(miopenTensorDescriptor_t tensorDesc, const TensorParams& params)
{
    if(params.tensorLayout || params.nbDims != 4)
        return TestStatus::Skipped;

    if(params.dimsA == nullptr)
        return TestStatus::Failed; // internal error

    miopenStatus_t status;
    miopenDataType_t dataType;
    int dims[4], strides[4];

    status = miopenGet4dTensorDescriptor(tensorDesc,
                                         &dataType,
                                         dims,
                                         dims + 1,
                                         dims + 2,
                                         dims + 3,
                                         strides,
                                         strides + 1,
                                         strides + 2,
                                         strides + 3);
    if(status != miopenStatusSuccess)
        return TestStatus::Failed;

    if(params.dataType != dataType || !CompareLengths(params.dimsA, dims, 4) ||
       (params.stridesA != nullptr && !CompareLengths(params.stridesA, strides, 4)))
    {
        return TestStatus::Failed;
    }

    return TestStatus::Passed;
}

TestStatus GetTensorDescriptor(miopenTensorDescriptor_t tensorDesc, const TensorParams& params)
{
    if(params.tensorLayout)
    {
        if((params.tensorLayout != miopenTensorNCHW && params.tensorLayout != miopenTensorNCDHW) ||
           params.stridesA != nullptr)
        {
            return TestStatus::Skipped;
        }
    }

    if(params.dimsA == nullptr)
        return TestStatus::Failed; // internal error

    miopenStatus_t status;
    int size;

    status = miopenGetTensorDescriptorSize(tensorDesc, &size);
    if(status != miopenStatusSuccess || size < 0 || size != params.nbDims)
        return TestStatus::Failed;

    miopenDataType_t dataType;
    std::vector<int> dims(size);
    std::vector<int> strides(size);

    status = miopenGetTensorDescriptor(tensorDesc, &dataType, dims.data(), strides.data());
    if(status != miopenStatusSuccess)
        return TestStatus::Failed;

    if(params.dataType != dataType || !CompareLengths(params.dimsA, dims, size) ||
       (params.stridesA != nullptr && !CompareLengths(params.stridesA, strides, size)))
        return TestStatus::Failed;

    return TestStatus::Passed;
}

const auto get_tensor_descr_funcs = {Get4dTensorDescriptor, GetTensorDescriptor};

class TestSetTensor : public ::testing::TestWithParam<TestConfig>
{
protected:
    static int GetNumDimsForLayout(testTensorLayout_t layout)
    {
        int ndims = 0;
        // clang-format off
        switch(layout)
        {
        case miopenTensorNCHW:
        case miopenTensorNHWC:
        case miopenTensorCHWN:
        case miopenTensorNCHWc4:
        case miopenTensorNCHWc8:
        case miopenTensorCHWNc4:
        case miopenTensorCHWNc8:
            ndims = 4;
            break;
        case miopenTensorNCDHW:
        case miopenTensorNDHWC:
            ndims = 5;
            break;
        }
        // clang-format on
        return ndims;
    }

    static void GenerateValidConfigs(std::vector<TestConfig>& configs)
    {
        static int dims[]    = {4, 4, 16, 9, 16};
        static int strides[] = {9216, 2304, 144, 16, 1};
        static_assert(sizeof(dims) == sizeof(strides));
        const auto max_ndims = sizeof(dims) / sizeof(dims[0]);

        // clang-format off
        for(testDataType_t datatype = miopenFirstDataType; datatype <= miopenLastDataType; datatype++)
        // clang-format on
        {
            if(datatype == 4)
                continue; // miopenInt8x4

            // clang-format off
            for(testTensorLayout_t layout = miopenFirstTensorLayout; layout <= miopenLastTensorLayout; layout++)
            // clang-format on
            {
                int ndims               = GetNumDimsForLayout(layout);
                const TestConfig config = {
                    false,
                    {datatype, layout, ndims, dims + (max_ndims - ndims), nullptr},
                    true,
                    false};
                configs.push_back(config);
            }

            for(int i = 0; i < 2; i++)
            {
                const bool use_strides = (i == 1);
                for(int ndims = 1; ndims <= max_ndims; ndims++)
                {
                    const TestConfig config = {
                        false,
                        {datatype,
                         std::nullopt,
                         ndims,
                         dims + (max_ndims - ndims),
                         use_strides ? (strides + (max_ndims - ndims)) : nullptr},
                        true,
                        false};
                    configs.push_back(config);
                }
            }
        }
    }

    static std::vector<TestConfig> GenerateValidConfigs()
    {
        std::vector<TestConfig> configs;
        GenerateValidConfigs(configs);
        return configs;
    }

    static std::vector<testDataType_t> GetWrongDataTypes()
    {
        std::vector<testDataType_t> wrong_datatypes = {
            static_cast<testDataType_t>(miopenFirstDataType) - 1,
            static_cast<testDataType_t>(miopenLastDataType) + 1,
            /*miopenInt8x4*/ 4};
        return wrong_datatypes;
    }

    static std::vector<testTensorLayout_t> GetWrongLayouts(int num_dims, bool use_strides)
    {
        std::vector<testTensorLayout_t> wrong_layouts = {
            static_cast<testTensorLayout_t>(miopenFirstTensorLayout) - 1,
            static_cast<testTensorLayout_t>(miopenLastTensorLayout) + 1};
        std::vector<testTensorLayout_t> layouts_4d = {miopenTensorNCHW,
                                                      miopenTensorNHWC,
                                                      miopenTensorCHWN,
                                                      miopenTensorNCHWc4,
                                                      miopenTensorNCHWc8,
                                                      miopenTensorCHWNc4,
                                                      miopenTensorCHWNc8};
        std::vector<testTensorLayout_t> layouts_5d = {miopenTensorNCDHW, miopenTensorNDHWC};
        if(use_strides)
        {
            wrong_layouts.insert(wrong_layouts.end(), layouts_4d.cbegin(), layouts_4d.cend());
            wrong_layouts.insert(wrong_layouts.end(), layouts_5d.cbegin(), layouts_5d.cend());
        }
        else
        {
            if(num_dims == 4)
                wrong_layouts.insert(wrong_layouts.end(), layouts_5d.cbegin(), layouts_5d.cend());
            else if(num_dims == 5)
                wrong_layouts.insert(wrong_layouts.end(), layouts_4d.cbegin(), layouts_4d.cend());
        }
        return wrong_layouts;
    }

    static std::vector<int> GetWrongNumDims(std::optional<testTensorLayout_t> layout)
    {
        std::vector<int> wrong_ndims = {-1, 0};
        if(!layout)
            return wrong_ndims;

        auto num_dims = GetNumDimsForLayout(layout.value());
        wrong_ndims.push_back(num_dims - 1);
        wrong_ndims.push_back(num_dims + 1);

        return wrong_ndims;
    }

    static void GenerateWrongConfigs(const TestConfig& valid_config,
                                     std::vector<TestConfig>& wrong_configs)
    {
        const auto wrong_datatypes = GetWrongDataTypes();
        const auto wrong_layouts =
            GetWrongLayouts(valid_config.params.nbDims, valid_config.params.stridesA != nullptr);
        const auto wrong_ndims     = GetWrongNumDims(valid_config.params.tensorLayout);
        static int wrong_dims[][8] = {{0, 0, 0, 0, 0, 0, 0, 0}, {-1, -1, -1, -1, -1, -1, -1, -1}};

        // tensorDesc = nullptr
        {
            auto config                   = valid_config;
            config.null_tensor_descriptor = true;
            config.valid                  = false;
            wrong_configs.push_back(config);
        }

        // wrong data type
        for(auto datatype : wrong_datatypes)
        {
            auto config            = valid_config;
            config.params.dataType = datatype;
            config.valid           = false;
#if !USE_OUT_OF_RANGE_ENUM
            if(config.params.dataType < miopenFirstDataType ||
               config.params.dataType > miopenLastDataType)
            {
                config.skip = true;
            }
#endif
            wrong_configs.push_back(config);
        }

        // wrong layout
        for(auto layout : wrong_layouts)
        {
            auto config                = valid_config;
            config.params.tensorLayout = layout;
            config.valid               = false;
#if !USE_OUT_OF_RANGE_ENUM
            if(config.params.tensorLayout < miopenFirstTensorLayout ||
               config.params.tensorLayout > miopenLastTensorLayout)
            {
                config.skip = true;
            }
#endif
            wrong_configs.push_back(config);
        }

        // wrong number of dimensions
        for(auto ndims : wrong_ndims)
        {
            auto config          = valid_config;
            config.params.nbDims = ndims;
            config.valid         = false;
            wrong_configs.push_back(config);
        }

        // dimsA = nullptr
        {
            auto config         = valid_config;
            config.params.dimsA = nullptr;
            config.valid        = false;
            wrong_configs.push_back(config);
        }

        // wrong dimensions
        for(auto dims : wrong_dims)
        {
            auto config         = valid_config;
            config.params.dimsA = dims;
            config.valid        = false;
            wrong_configs.push_back(config);
        }

        if(valid_config.params.stridesA != nullptr)
        {
            // wrong strides
            for(auto strides : wrong_dims)
            {
                auto config            = valid_config;
                config.params.stridesA = strides;
                config.valid           = false;
                wrong_configs.push_back(config);
            }
        }
    }

    static std::vector<TestConfig> GenerateWrongConfigs()
    {
        const auto& valid_configs = GetValidConfigs();
        std::vector<TestConfig> wrong_configs;
        for(const auto& valid_config : valid_configs)
            GenerateWrongConfigs(valid_config, wrong_configs);
        return wrong_configs;
    }

    static void RunTest(const TestConfig& config)
    {
        for(const auto set_tensor_descr_func : set_tensor_descr_funcs)
        {
            TestStatus test_status;
            miopenStatus_t status;
            miopenTensorDescriptor_t desc = nullptr;

            test_status = set_tensor_descr_func(nullptr, config.params, true);
            if(test_status == TestStatus::Skipped)
                continue;

            if(!config.null_tensor_descriptor)
            {
                status = miopenCreateTensorDescriptor(&desc);
                ASSERT_EQ(status, miopenStatusSuccess);
            }

            test_status = set_tensor_descr_func(desc, config.params, false);
            if(config.valid)
                ASSERT_NE(test_status, TestStatus::Failed);
            else
                ASSERT_NE(test_status, TestStatus::Passed);

            if(config.valid)
            {
                for(const auto get_tensor_descr_func : get_tensor_descr_funcs)
                {
                    test_status = get_tensor_descr_func(desc, config.params);
                    ASSERT_NE(test_status, TestStatus::Failed);
                }
            }

            if(!config.null_tensor_descriptor)
            {
                status = miopenDestroyTensorDescriptor(desc);
                ASSERT_EQ(status, miopenStatusSuccess);
            }
        }
    }

    static void RunTest()
    {
        const auto& config = GetParam();
        if(config.skip)
            GTEST_SKIP();
        RunTest(config);
    }

public:
    static const std::vector<TestConfig>& GetValidConfigs()
    {
        static const auto configs = GenerateValidConfigs();
        return configs;
    }

    static const std::vector<TestConfig>& GetWrongConfigs()
    {
        static const auto configs = GenerateWrongConfigs();
        return configs;
    }
};

} // namespace

using CPU_ApiTestSetTensorDescriptor_NONE      = TestSetTensor;
using CPU_ApiTestSetWrongTensorDescriptor_NONE = TestSetTensor;

TEST_P(CPU_ApiTestSetTensorDescriptor_NONE, TD) { RunTest(); }

TEST_P(CPU_ApiTestSetWrongTensorDescriptor_NONE, TD) { RunTest(); }

INSTANTIATE_TEST_SUITE_P(Full,
                         CPU_ApiTestSetTensorDescriptor_NONE,
                         testing::ValuesIn(TestSetTensor::GetValidConfigs()));

INSTANTIATE_TEST_SUITE_P(Full,
                         CPU_ApiTestSetWrongTensorDescriptor_NONE,
                         testing::ValuesIn(TestSetTensor::GetWrongConfigs()));