"ml/vscode:/vscode.git/clone" did not exist on "854a9195f351ffe7c8aaaad34d19022144963e51"
olc_driver_common.hpp 2.17 KB
Newer Older
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
#ifndef OLC_DRIVER_COMMON_HPP
#define OLC_DRIVER_COMMON_HPP

#include <half.hpp>
#include <vector>
#include <cassert>

// this enumerate should be synchronized with include/miopen.h
typedef enum {
    appHalf     = 0,
    appFloat    = 1,
    appInt32    = 2,
    appInt8     = 3,
    appInt8x4   = 4,
    appBFloat16 = 5,
    appDouble   = 6,
} appDataType_t;

namespace Driver {

template <appDataType_t typeNum>
struct get_type_from_type_enum
{
    using type = float;
};

template <>
struct get_type_from_type_enum<appHalf>
{
    using type = half_float::half;
};

template <>
struct get_type_from_type_enum<appFloat>
{
    using type = float;
};

template <>
struct get_type_from_type_enum<appDouble>
{
    using type = double;
};

template <>
struct get_type_from_type_enum<appInt32>
{
    using type = int;
};

static inline int get_typeid_from_type_enum(appDataType_t t)
{
    switch(t)
    {
    case appHalf: return (static_cast<int>('H'));
    case appFloat: return (static_cast<int>('F'));
    case appBFloat16: return (static_cast<int>('B'));
    case appDouble: return (static_cast<int>('D'));
    case appInt8:
    case appInt8x4:
    case appInt32: return (static_cast<int>('O'));
    default: throw std::runtime_error("Only float, half, bfloat16 data type is supported."); break;
    };
};

template <typename T>
static inline int get_typeid_from_type()
{
    throw std::runtime_error("Unsupported typeid conversion for this type!");
};

template <>
inline int get_typeid_from_type<float>()
{
    return (static_cast<int>('F'));
};

template <>
inline int get_typeid_from_type<half_float::half>()
{
    return (static_cast<int>('H'));
};

template <>
inline int get_typeid_from_type<double>()
{
    return (static_cast<int>('D'));
};

static inline float get_effective_average(std::vector<float>& values)
{
    assert(!values.empty());

    if(values.size() == 1)
        return (values[0]);
    else
    {
        float sum    = 0.0f;
        float maxVal = 0.0f;

        for(const auto val : values)
        {
            if(maxVal < val)
                maxVal = val;
            sum += val;
        };

        return ((sum - maxVal) / (values.size() - 1));
    };
};

} // namespace Driver

#endif