Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
df7f8a35
Commit
df7f8a35
authored
Nov 09, 2023
by
Umang Yadav
Browse files
changes for the FP8 ref implementation
parent
35e5298e
Changes
13
Hide whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
1082 additions
and
31 deletions
+1082
-31
src/api/include/migraphx/migraphx.h
src/api/include/migraphx/migraphx.h
+2
-1
src/include/migraphx/half.hpp
src/include/migraphx/half.hpp
+13
-0
src/include/migraphx/migraphx_f8_impl.hpp
src/include/migraphx/migraphx_f8_impl.hpp
+320
-0
src/include/migraphx/migraphx_float8.hpp
src/include/migraphx/migraphx_float8.hpp
+431
-0
src/include/migraphx/shape.hpp
src/include/migraphx/shape.hpp
+3
-1
src/include/migraphx/type_traits.hpp
src/include/migraphx/type_traits.hpp
+25
-5
src/py/migraphx_py.cpp
src/py/migraphx_py.cpp
+12
-1
src/targets/gpu/device/include/migraphx/gpu/device/types.hpp
src/targets/gpu/device/include/migraphx/gpu/device/types.hpp
+12
-12
test/CMakeLists.txt
test/CMakeLists.txt
+1
-0
test/float_equal.cpp
test/float_equal.cpp
+11
-1
test/fp8e4m3fnuz.cpp
test/fp8e4m3fnuz.cpp
+233
-0
test/gpu/jit.cpp
test/gpu/jit.cpp
+14
-8
tools/api/migraphx.h
tools/api/migraphx.h
+5
-2
No files found.
src/api/include/migraphx/migraphx.h
View file @
df7f8a35
...
@@ -44,7 +44,8 @@
...
@@ -44,7 +44,8 @@
m(int32_type, int32_t) \
m(int32_type, int32_t) \
m(int64_type, int64_t) \
m(int64_type, int64_t) \
m(uint32_type, uint32_t) \
m(uint32_type, uint32_t) \
m(uint64_type, uint64_t)
m(uint64_type, uint64_t) \
m(fp8e4m3fnuz_type, migraphx_fp8::fp8e4m3fnuz)
// clang-format on
// clang-format on
#ifdef __cplusplus
#ifdef __cplusplus
...
...
src/include/migraphx/half.hpp
View file @
df7f8a35
...
@@ -27,6 +27,7 @@
...
@@ -27,6 +27,7 @@
#include <half/half.hpp>
#include <half/half.hpp>
#include <migraphx/config.hpp>
#include <migraphx/config.hpp>
#include <migraphx/migraphx_float8.hpp>
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
...
@@ -67,6 +68,18 @@ struct common_type<T, migraphx::half> : std::common_type<float, T> // NOLINT
...
@@ -67,6 +68,18 @@ struct common_type<T, migraphx::half> : std::common_type<float, T> // NOLINT
{
{
};
};
template
<
>
struct
common_type
<
migraphx_fp8
::
fp8e4m3fnuz
,
migraphx
::
half
>
{
using
type
=
float
;
};
template
<
>
struct
common_type
<
migraphx
::
half
,
migraphx_fp8
::
fp8e4m3fnuz
>
{
using
type
=
float
;
};
template
<
>
template
<
>
struct
common_type
<
migraphx
::
half
,
migraphx
::
half
>
struct
common_type
<
migraphx
::
half
,
migraphx
::
half
>
{
{
...
...
src/include/migraphx/migraphx_f8_impl.hpp
0 → 100644
View file @
df7f8a35
/* ************************************************************************
* Copyright (C) 2016-2023 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 cop-
* ies 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 IM-
* PLIED, 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 CONNE-
* CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* ************************************************************************ */
#ifndef MIGRAPHX_FP8_IMPL_HPP
#define MIGRAPHX_FP8_IMPL_HPP
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-identifier"
#endif
#define CONST_FOLD(x) (__builtin_constant_p(x) ? (x) : (x))
namespace
migraphx_f8_impl
{
namespace
detail
{
template
<
bool
B
,
class
T
,
class
F
>
struct
conditional
{
using
type
=
T
;
};
template
<
class
T
,
class
F
>
struct
conditional
<
false
,
T
,
F
>
{
using
type
=
F
;
};
template
<
typename
To
,
typename
From
>
inline
constexpr
To
bit_cast
(
From
fr
)
noexcept
{
static_assert
(
sizeof
(
To
)
==
sizeof
(
From
));
#if defined(__GNUC__) and !defined(__clang__)
To
x
=
CONST_FOLD
(
*
reinterpret_cast
<
To
*>
(
&
fr
));
#else
To
x
=
__builtin_bit_cast
(
To
,
fr
);
#endif
return
x
;
}
}
// namespace detail
template
<
int
wm
,
int
we
,
typename
T
,
bool
negative_zero_nan
,
bool
clip
>
constexpr
uint8_t
cast_to_f8
(
T
_x
,
bool
stoch
,
uint32_t
rng
)
{
static_assert
(
wm
+
we
==
7
,
"wm+we==7"
);
const
int
mfmt
=
(
sizeof
(
T
)
==
4
)
?
23
:
10
;
typename
detail
::
conditional
<
sizeof
(
T
)
==
2
,
uint16_t
,
uint32_t
>::
type
x
;
if
constexpr
(
sizeof
(
T
)
==
4
)
x
=
detail
::
bit_cast
<
uint32_t
>
(
_x
);
else
x
=
detail
::
bit_cast
<
uint16_t
>
(
_x
);
uint32_t
head
,
mantissa
;
int
exponent
,
bias
;
uint32_t
sign
;
if
constexpr
(
sizeof
(
T
)
==
4
)
{
head
=
x
&
0xFF800000
;
mantissa
=
x
&
0x7FFFFF
;
exponent
=
(
head
>>
23
)
&
0xFF
;
sign
=
head
>>
31
;
bias
=
127
;
}
else
{
head
=
x
&
0xFC00
;
mantissa
=
x
&
0x3FF
;
exponent
=
(
head
>>
10
)
&
0x1F
;
sign
=
head
>>
15
;
bias
=
15
;
}
uint32_t
signed_inf
=
(
sign
<<
7
)
+
(((
1
<<
we
)
-
1
)
<<
wm
);
// Deal with inf and NaNs
if
(
negative_zero_nan
)
{
if
(
sizeof
(
T
)
==
4
)
{
if
((
x
&
0x7F800000
)
==
0x7F800000
)
return
0x80
;
}
else
{
// if(__hisinf(x) || __hisnan(x))
if
((
x
&
0x7C00
)
==
0x7C00
)
return
0x80
;
}
}
else
{
if
(
sizeof
(
T
)
==
4
)
{
if
((
x
&
0x7F800000
)
==
0x7F800000
)
return
signed_inf
+
(
mantissa
!=
0
?
1
:
0
);
}
else
{
if
((
x
&
0x7C00
)
==
0x7C00
)
return
signed_inf
+
(
mantissa
!=
0
?
1
:
0
);
}
}
// handle positive zero
if
(
x
==
0
)
return
0
;
// handle negative zero
if
((
sizeof
(
T
)
==
4
and
x
==
0x80000000
)
or
(
sizeof
(
T
)
==
2
and
x
==
0x8000
))
{
if
(
negative_zero_nan
)
{
return
0
;
}
else
{
return
0x80
;
}
}
// First need to check if it is normal or denorm as there is a difference of implict 1
// Then need to adjust the exponent to align with the F8 exponent, in the meanwhile, shift
// The mantissa. Then for stochastic rounding, add rng to mantissa and truncate. And for
// RNE, no need to add rng. Then probably need to check whether there is carry and adjust
// exponent and mantissa again
// For IEEE bias mode, the bias is 2^(k-1) -1 where k is the width of exponent bits
const
int
f8_bias
=
(
1
<<
(
we
-
1
))
-
1
+
(
negative_zero_nan
?
1
:
0
);
const
int
f8_denormal_act_exponent
=
1
-
f8_bias
;
// actual exponent of f8 denormal
// act_exponent is the actual exponent of fp32/fp16 (after subtracting bias)
// f8_exponent is the converted f8 exponent with bias encoding
// exponent_diff is the diff between fp32/fp16 exponent and f8 exponent,
// the difference needs to be adjusted and mantissa shifted
int
act_exponent
,
f8_exponent
,
exponent_diff
;
if
(
exponent
==
0
)
{
// fp32/fp16 is in denormal.
/* fp32 denormal is below 2^-127 so it is usually not a concern here, we mostly concern fp16
here. In this case, f8 is usually in denormal. But there could be exceptions. fp16 denormal has
exponent bias 15 while bf8 with NANOO has exponent bias 16. It means that there are some numbers in
fp16 denormal but they are bf8 (NANOO) normals - smallest bf8 (NANOO) normal is 2^-15. fp16 numbers
where exponent==0 (actual exponent -14) and highest bit of mantissa is 1 are bf8 (NANOO) normal. In
this case, the fp16 mantissa should be shift left by 1 */
act_exponent
=
exponent
-
bias
+
1
;
exponent_diff
=
f8_denormal_act_exponent
-
act_exponent
;
// actual exponent is exponent-bias+1 as it is denormal
}
else
{
// fp32/fp16 is normal with implicit 1
act_exponent
=
exponent
-
bias
;
if
(
act_exponent
<=
f8_denormal_act_exponent
)
{
/* This is the case where fp32/fp16 is normal but it is in f8 denormal range.
For example fp8 nanoo mode, denormal exponent is -7, but if the fp32/fp16
actual exponent is -7, it is actually larger due to the implict 1,
Therefore it needs to be adjust to -6 and mantissa shift right by 1.
So for fp32/fp16, exponent -8 is the cut point to convert to fp8 nanoo */
exponent_diff
=
f8_denormal_act_exponent
-
act_exponent
;
}
else
{
// both fp32/fp16 and f8 are in normal range
exponent_diff
=
0
;
// exponent_diff=0 does not mean there is no difference for this case,
// act_exponent could be larger. Just that it does not need shift mantissa
}
mantissa
+=
(
1
<<
mfmt
);
// Add the implicit 1 into mantissa
}
bool
midpoint
=
(
mantissa
&
((
1
<<
(
mfmt
-
wm
+
exponent_diff
))
-
1
))
==
(
1
<<
(
mfmt
-
wm
+
exponent_diff
-
1
));
/* This part is a bit tricky. The judgment of whether it is a tie needs to be done before we
shift right as shift right could rip off some residual part and make something not midpoint look
like midpoint. For example, the fp16 number 0x1002 (0 00100 0000000010), it is larger than
midpoint, but after shift right by 4 bits, it would look like midpoint.
*/
if
(
exponent_diff
>
0
)
mantissa
>>=
exponent_diff
;
else
if
(
exponent_diff
==
-
1
)
mantissa
<<=
-
exponent_diff
;
bool
implicit_one
=
mantissa
&
(
1
<<
mfmt
);
// if there is no implict 1, it means the f8 is denormal and need to adjust to denorm exponent
f8_exponent
=
(
act_exponent
+
exponent_diff
)
/*actual f8 exponent*/
+
f8_bias
-
(
implicit_one
?
0
:
1
);
// Now we have the exponent and mantissa adjusted
uint32_t
drop_mask
=
(
1
<<
(
mfmt
-
wm
))
-
1
;
bool
odd
=
mantissa
&
(
1
<<
(
mfmt
-
wm
));
// if the least significant bit that is not truncated is 1
mantissa
+=
(
stoch
?
rng
:
(
midpoint
?
(
odd
?
mantissa
:
mantissa
-
1
)
:
mantissa
))
&
drop_mask
;
// Now we deal with overflow
if
(
f8_exponent
==
0
)
{
if
((
1
<<
mfmt
)
&
mantissa
)
{
f8_exponent
=
1
;
// denormal overflow to become normal, promote exponent
}
}
else
{
if
((
1
<<
(
mfmt
+
1
))
&
mantissa
)
{
mantissa
>>=
1
;
f8_exponent
++
;
}
}
mantissa
>>=
(
mfmt
-
wm
);
// above range: quantize to maximum possible float of the same sign
const
int
max_exp
=
(
1
<<
we
)
-
(
negative_zero_nan
?
1
:
2
);
if
(
f8_exponent
>
max_exp
)
{
if
(
clip
)
{
mantissa
=
(
1
<<
wm
)
-
1
;
f8_exponent
=
max_exp
;
}
else
{
return
signed_inf
;
}
}
if
(
f8_exponent
==
0
&&
mantissa
==
0
)
return
negative_zero_nan
?
0
:
(
sign
<<
7
);
mantissa
&=
(
1
<<
wm
)
-
1
;
return
(
sign
<<
7
)
|
(
f8_exponent
<<
wm
)
|
mantissa
;
}
template
<
int
wm
,
int
we
,
typename
T
,
bool
negative_zero_nan
>
constexpr
T
cast_from_f8
(
uint8_t
x
)
{
constexpr
int
weo
=
8
;
constexpr
int
wmo
=
23
;
T
fInf
,
fNegInf
,
fNaN
,
fNeg0
;
uint32_t
ifInf
=
0x7F800000
;
uint32_t
ifNegInf
=
0xFF800000
;
uint32_t
ifNaN
=
0x7F800001
;
uint32_t
ifNeg0
=
0x80000000
;
// TODO: need to change T for half but right now it would never called with half
fInf
=
detail
::
bit_cast
<
float
>
(
ifInf
);
fNegInf
=
detail
::
bit_cast
<
float
>
(
ifNegInf
);
fNaN
=
detail
::
bit_cast
<
float
>
(
ifNaN
);
fNeg0
=
detail
::
bit_cast
<
float
>
(
ifNeg0
);
if
(
x
==
0
)
return
0
;
uint32_t
sign
=
x
>>
7
;
uint32_t
mantissa
=
x
&
((
1
<<
wm
)
-
1
);
int
exponent
=
(
x
&
0x7F
)
>>
wm
;
if
(
negative_zero_nan
)
{
if
(
x
==
0x80
)
return
fNaN
;
}
else
{
if
(
x
==
0x80
)
return
fNeg0
;
if
(
exponent
==
((
1
<<
we
)
-
1
))
return
(
mantissa
==
0
)
?
(
sign
?
fNegInf
:
fInf
)
:
fNaN
;
}
typename
detail
::
conditional
<
sizeof
(
T
)
==
2
,
uint16_t
,
uint32_t
>::
type
retval
;
const
int
exp_low_cutoff
=
(
1
<<
(
weo
-
1
))
-
(
1
<<
(
we
-
1
))
+
1
-
(
negative_zero_nan
?
1
:
0
);
// subnormal input
if
(
exponent
==
0
)
{
// guaranteed mantissa!=0 since cases 0x0 and 0x80 are handled above
int
sh
=
1
+
__builtin_clz
(
mantissa
)
-
(
32
-
wm
);
mantissa
<<=
sh
;
exponent
+=
1
-
sh
;
mantissa
&=
((
1
<<
wm
)
-
1
);
}
exponent
+=
exp_low_cutoff
-
1
;
mantissa
<<=
wmo
-
wm
;
// subnormal output (occurs when T=half, we=5, negative_zero_nan=true)
if
(
exponent
<=
0
)
{
mantissa
|=
1
<<
wmo
;
mantissa
>>=
1
-
exponent
;
exponent
=
0
;
}
if
(
sizeof
(
T
)
==
2
)
retval
=
(
sign
<<
15
)
|
(
exponent
<<
10
)
|
mantissa
;
else
retval
=
(
sign
<<
31
)
|
(
exponent
<<
23
)
|
mantissa
;
return
detail
::
bit_cast
<
T
>
(
retval
);
}
}
// namespace migraphx_f8_impl
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif // MIGRAPHX_FP8_IMPL_HPP
src/include/migraphx/migraphx_float8.hpp
0 → 100644
View file @
df7f8a35
/* ************************************************************************
* Copyright (C) 2016-2023 Advanced Micro Devices, Inc. All rights reserved.
*
* 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 cop-
* ies 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 IM-
* PLIED, 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 CONNE-
* CTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* ************************************************************************ */
#ifndef MIGRAPHX_GUARD_RTGLIB_FLOAT8_HPP
#define MIGRAPHX_GUARD_RTGLIB_FLOAT8_HPP
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wfloat-equal"
#pragma clang diagnostic ignored "-Wmacro-redefined"
#pragma clang diagnostic ignored "-Wc++20-extensions"
#endif // __clang__
#ifndef MIGRAPHX_FP8_FNUZ
#define MIGRAPHX_FP8_FNUZ true
#endif // MIGRAPHX_FP8_FNUZ
// We are clipping in down conversion by default
#define MIGRAPHX_F8_DOWNCAST_CLIPPING 1
#include <cmath>
#include <cstdint>
#include <climits>
#include <cstring>
#include <iosfwd>
#include <limits>
#include <sstream>
#include <iostream>
#include <string>
#include <utility>
namespace
migraphx_f8_impl
{
template
<
int
wm
,
int
we
,
typename
T
,
bool
negative_zero_nan
,
bool
clip
>
constexpr
uint8_t
cast_to_f8
(
T
_x
,
bool
stoch
=
false
,
uint32_t
rng
=
0
);
template
<
int
wm
,
int
we
,
typename
T
,
bool
negative_zero_nan
>
constexpr
T
cast_from_f8
(
uint8_t
x
);
}
// namespace migraphx_f8_impl
#include <migraphx/migraphx_f8_impl.hpp>
namespace
migraphx_fp8
{
enum
class
migraphx_f8_rounding_mode
{
standard
,
// standard rounding is doing RNE -- round to nearest even
stochastic
};
enum
class
f8_type
{
bf8
=
0
,
// s1e5m2
fp8
=
1
// s1e4m3
};
template
<
typename
T
>
class
numeric_limits
;
template
<
migraphx_fp8
::
f8_type
T
=
migraphx_fp8
::
f8_type
::
fp8
>
struct
float8
{
uint8_t
data
;
// default constructor
constexpr
float8
()
=
default
;
// default copy constructor
constexpr
float8
(
const
float8
<
T
>&
y
)
=
default
;
struct
from_bits_t
{
};
static
constexpr
from_bits_t
from_bits
()
{
return
from_bits_t
();
}
explicit
constexpr
float8
(
uint8_t
bits
,
from_bits_t
)
:
data
(
bits
)
{}
explicit
constexpr
float8
(
float
v
,
migraphx_fp8
::
migraphx_f8_rounding_mode
rm
=
migraphx_fp8
::
migraphx_f8_rounding_mode
::
standard
,
uint32_t
rng
=
0
)
{
if
constexpr
(
T
==
migraphx_fp8
::
f8_type
::
fp8
)
{
#ifdef MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx_f8_impl
::
cast_to_f8
<
3
,
4
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
,
true
/*clip*/
>
(
v
,
(
rm
==
migraphx_fp8
::
migraphx_f8_rounding_mode
::
stochastic
),
rng
);
#else // MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx_f8_impl
::
cast_to_f8
<
3
,
4
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
,
false
/*clip*/
>
(
v
,
(
rm
==
migraphx_fp8
::
migraphx_f8_rounding_mode
::
stochastic
),
rng
);
#endif // MIGRAPHX_F8_DOWNCAST_CLIPPING
}
else
{
#ifdef MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx_f8_impl
::
cast_to_f8
<
2
,
5
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
,
true
/*clip*/
>
(
v
,
(
rm
==
migraphx_fp8
::
migraphx_f8_rounding_mode
::
stochastic
),
rng
);
#else // MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx_f8_impl
::
cast_to_f8
<
2
,
5
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
,
false
/*clip*/
>
(
v
,
(
rm
==
migraphx_fp8
::
migraphx_f8_rounding_mode
::
stochastic
),
rng
);
#endif // rocblas_F8_downcast_clipping}
}
}
inline
constexpr
operator
float
()
const
{
if
constexpr
(
T
==
migraphx_fp8
::
f8_type
::
fp8
)
{
return
migraphx_f8_impl
::
cast_from_f8
<
3
,
4
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
>
(
data
);
}
// else
return
migraphx_f8_impl
::
cast_from_f8
<
2
,
5
,
float
,
MIGRAPHX_FP8_FNUZ
/*negative_zero_nan*/
>
(
data
);
}
inline
constexpr
bool
is_zero
()
const
{
if
constexpr
(
MIGRAPHX_FP8_FNUZ
)
{
return
data
==
0x00
;
}
else
{
return
(
data
==
0x00
)
||
(
data
==
0x80
);
}
}
inline
constexpr
bool
is_nan
()
const
{
if
constexpr
(
MIGRAPHX_FP8_FNUZ
)
{
return
data
==
0x80
;
}
else
{
if
(
T
==
migraphx_fp8
::
f8_type
::
bf8
)
{
return
(
data
==
0x7d
)
||
(
data
==
0x7e
)
||
(
data
==
0x7f
)
||
(
data
==
0xfd
)
||
(
data
==
0xfe
)
||
(
data
==
0xff
);
}
else
{
return
(
data
==
0x79
)
||
(
data
==
0x7a
)
||
(
data
==
0x7b
)
||
(
data
==
0x7c
)
||
(
data
==
0x7d
)
||
(
data
==
0x7e
)
||
(
data
==
0x7f
)
||
(
data
==
0xf9
)
||
(
data
==
0xfa
)
||
(
data
==
0xfb
)
||
(
data
==
0xfc
)
||
(
data
==
0xfd
)
||
(
data
==
0xfe
)
||
(
data
==
0xff
);
}
}
}
inline
constexpr
bool
is_inf
()
const
{
if
constexpr
(
MIGRAPHX_FP8_FNUZ
)
{
return
data
==
0x80
;
}
else
{
if
(
T
==
migraphx_fp8
::
f8_type
::
bf8
)
{
return
(
data
==
0x7c
)
||
(
data
==
0xfc
);
}
else
{
return
(
data
==
0x78
)
||
(
data
==
0xf8
);
}
}
}
#define MIGRAPHX_FP8_UNARY_OP(unary_op, binary_op) \
constexpr float8& operator unary_op(const float8& rhs) \
{ \
const auto tmp = static_cast<float>(*this) binary_op static_cast<float>(rhs); \
*this = static_cast<float8>(tmp); \
return *this; \
} \
constexpr float8& operator unary_op(const float& rhs) \
{ \
const auto tmp = static_cast<float>(*this) binary_op static_cast<float>(rhs); \
*this = static_cast<float8>(tmp); \
return *this; \
}
MIGRAPHX_FP8_UNARY_OP
(
*=
,
*
)
MIGRAPHX_FP8_UNARY_OP
(
-=
,
-
)
MIGRAPHX_FP8_UNARY_OP
(
+=
,
+
)
MIGRAPHX_FP8_UNARY_OP
(
/=
,
/
)
inline
constexpr
float8
&
operator
=
(
const
float8
&
rhs
)
=
default
;
inline
constexpr
float8
&
operator
=
(
float8
&&
rhs
)
=
default
;
inline
constexpr
float8
&
operator
=
(
float
rhs
)
{
*
this
=
static_cast
<
float8
>
(
rhs
);
return
*
this
;
}
inline
constexpr
bool
operator
==
(
const
float8
&
rhs
)
const
{
if
((
rhs
.
is_zero
()
&&
this
->
is_zero
())
||
(
fabs
(
rhs
-
*
this
)
<
migraphx_fp8
::
numeric_limits
<
float8
<
T
>>::
epsilon
()))
return
true
;
else
if
(
rhs
.
is_nan
()
||
rhs
.
is_inf
()
||
this
->
is_nan
()
||
this
->
is_inf
())
return
false
;
return
false
;
}
inline
constexpr
bool
operator
<
(
const
float8
&
rhs
)
const
{
const
auto
we
=
static_cast
<
float
>
(
*
this
);
const
auto
them
=
static_cast
<
float
>
(
rhs
);
return
we
<
them
;
}
inline
constexpr
bool
operator
>
(
const
float8
&
rhs
)
const
{
const
auto
we
=
static_cast
<
float
>
(
*
this
);
const
auto
them
=
static_cast
<
float
>
(
rhs
);
return
we
>
them
;
}
};
// Special operator overloading
template
<
migraphx_fp8
::
f8_type
T
>
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
migraphx_fp8
::
float8
<
T
>&
rhs
)
{
return
os
<<
static_cast
<
float
>
(
rhs
);
}
// NOLINTNEXTLINE
#define MIGRAPHX_FP8_BINARY_OP(binary_op, U) \
template <migraphx_fp8::f8_type T> \
inline constexpr U operator binary_op(const migraphx_fp8::float8<T>& lhs, \
const migraphx_fp8::float8<T>& rhs) \
{ \
return U(static_cast<float>(lhs) binary_op static_cast<float>(rhs)); \
}
// TODO: these should return floats
MIGRAPHX_FP8_BINARY_OP
(
*
,
migraphx_fp8
::
float8
<
T
>
)
MIGRAPHX_FP8_BINARY_OP
(
-
,
migraphx_fp8
::
float8
<
T
>
)
MIGRAPHX_FP8_BINARY_OP
(
/
,
migraphx_fp8
::
float8
<
T
>
)
MIGRAPHX_FP8_BINARY_OP
(
+
,
migraphx_fp8
::
float8
<
T
>
)
// TODO: Comparison ops shouldn't convert to float, maybe need to take care of rounding effects.
MIGRAPHX_FP8_BINARY_OP
(
==
,
bool
)
MIGRAPHX_FP8_BINARY_OP
(
>=
,
bool
)
MIGRAPHX_FP8_BINARY_OP
(
<=
,
bool
)
MIGRAPHX_FP8_BINARY_OP
(
>
,
bool
)
MIGRAPHX_FP8_BINARY_OP
(
<
,
bool
)
MIGRAPHX_FP8_BINARY_OP
(
!=
,
bool
)
template
<
migraphx_fp8
::
f8_type
T
>
inline
migraphx_fp8
::
float8
<
T
>
fabs
(
migraphx_fp8
::
float8
<
T
>
v
)
{
v
.
data
=
v
.
data
&
0x7f
;
return
v
;
}
template
<
class
T
>
constexpr
T
F8_Max
()
{
return
T
{
0x7F
,
T
::
from_bits
()};
}
template
<
class
T
>
constexpr
T
F8_Lowest
()
{
return
T
{
0xFF
,
T
::
from_bits
()};
}
using
fp8e4m3fnuz
=
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
;
template
<
>
class
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
{
public:
// TODO :figure out epsilon in Hex to make it constexpr
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
epsilon
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
(
0x28
,
migraphx_fp8
::
float8
<>::
from_bits
());
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
quiet_NaN
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
(
MIGRAPHX_FP8_FNUZ
?
0x80
:
0x7F
,
migraphx_fp8
::
float8
<>::
from_bits
());
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
max
()
{
return
migraphx_fp8
::
F8_Max
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
();
}
// TODO figure out Hex value
static
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
min
()
{
return
static_cast
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
(
-
1.0
f
)
*
migraphx_fp8
::
F8_Max
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
();
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
lowest
()
{
return
migraphx_fp8
::
F8_Lowest
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
();
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
infinity
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
(
MIGRAPHX_FP8_FNUZ
?
0x80
:
0x7F
,
migraphx_fp8
::
float8
<>::
from_bits
());
}
};
template
<
>
class
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
{
public:
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
epsilon
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
(
0x34
,
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>::
from_bits
());
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
quiet_NaN
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
(
MIGRAPHX_FP8_FNUZ
?
0x80
:
0x7d
,
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>::
from_bits
());
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
max
()
{
return
static_cast
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
(
migraphx_fp8
::
F8_Max
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
());
}
// TODO figure out constexpr value
static
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
min
()
{
return
static_cast
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
(
float
(
-
1.0
f
))
*
migraphx_fp8
::
F8_Max
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
();
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
lowest
()
{
return
migraphx_fp8
::
F8_Lowest
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
();
}
static
constexpr
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
infinity
()
{
return
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
(
MIGRAPHX_FP8_FNUZ
?
0x80
:
0x7c
,
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>::
from_bits
());
}
};
}
// namespace migraphx_fp8
// define numeric limits for the new data type
namespace
std
{
inline
bool
isfinite
(
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
x
)
// NOLINT
{
return
x
.
is_inf
();
}
inline
bool
isfinite
(
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
x
)
// NOLINT
{
return
x
.
is_inf
();
}
inline
bool
isnan
(
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>
x
)
// NOLINT
{
return
x
.
is_nan
();
}
inline
bool
isnan
(
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>
x
)
// NOLINT
{
return
x
.
is_nan
();
}
template
<
>
class
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
:
public
migraphx_fp8
::
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
fp8
>>
{
};
template
<
>
class
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
:
public
migraphx_fp8
::
numeric_limits
<
migraphx_fp8
::
float8
<
migraphx_fp8
::
f8_type
::
bf8
>>
{
};
template
<
class
T
>
struct
common_type
<
migraphx_fp8
::
fp8e4m3fnuz
,
T
>
:
std
::
common_type
<
float
,
T
>
// NOLINT
{
};
template
<
class
T
>
struct
common_type
<
T
,
migraphx_fp8
::
fp8e4m3fnuz
>
:
std
::
common_type
<
float
,
T
>
// NOLINT
{
};
template
<
>
struct
common_type
<
migraphx_fp8
::
fp8e4m3fnuz
,
migraphx_fp8
::
fp8e4m3fnuz
>
{
using
type
=
float
;
};
}
// namespace std
// =================================================================================================
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#endif // MIGRAPHX_GUARD_RTGLIB_FLOAT8_HPP
src/include/migraphx/shape.hpp
View file @
df7f8a35
...
@@ -34,6 +34,7 @@
...
@@ -34,6 +34,7 @@
#include <migraphx/functional.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/errors.hpp>
#include <migraphx/half.hpp>
#include <migraphx/half.hpp>
#include <migraphx/migraphx_float8.hpp>
#include <migraphx/serialize.hpp>
#include <migraphx/serialize.hpp>
#include <migraphx/config.hpp>
#include <migraphx/config.hpp>
...
@@ -60,7 +61,8 @@ struct MIGRAPHX_EXPORT shape
...
@@ -60,7 +61,8 @@ struct MIGRAPHX_EXPORT shape
m(int32_type, int32_t) \
m(int32_type, int32_t) \
m(int64_type, int64_t) \
m(int64_type, int64_t) \
m(uint32_type, uint32_t) \
m(uint32_type, uint32_t) \
m(uint64_type, uint64_t)
m(uint64_type, uint64_t) \
m(fp8e4m3fnuz_type, migraphx_fp8::fp8e4m3fnuz)
// clang-format on
// clang-format on
#define MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES(x, t) x,
#define MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES(x, t) x,
...
...
src/include/migraphx/type_traits.hpp
View file @
df7f8a35
...
@@ -28,25 +28,45 @@
...
@@ -28,25 +28,45 @@
#include <type_traits>
#include <type_traits>
#include <migraphx/half.hpp>
#include <migraphx/half.hpp>
#include <migraphx/config.hpp>
#include <migraphx/config.hpp>
#include <migraphx/migraphx_float8.hpp>
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
#define MIGRAPHX_DETAIL_DEFINE_TRAIT(trait) \
template <class X> \
struct trait : std::trait<X> \
{ \
};
#define MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR(trait, T) \
#define MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR(trait, T) \
template <class X> \
struct trait : std::trait<X> \
{ \
}; \
\
template <> \
template <> \
struct trait<T> : std::true_type \
struct trait<T> : std::true_type \
{ \
{ \
};
};
MIGRAPHX_DETAIL_DEFINE_TRAIT
(
is_floating_point
);
MIGRAPHX_DETAIL_DEFINE_TRAIT
(
is_arithmetic
);
MIGRAPHX_DETAIL_DEFINE_TRAIT
(
is_signed
);
template
<
class
T
,
class
U
>
struct
is_same
:
std
::
is_same
<
T
,
U
>
{
};
template
<
bool
B
,
class
T
,
class
U
>
struct
conditional
:
std
::
conditional
<
B
,
T
,
U
>
{
};
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_floating_point
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_floating_point
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_signed
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_signed
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_arithmetic
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_arithmetic
,
half
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_floating_point
,
migraphx_fp8
::
fp8e4m3fnuz
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_signed
,
migraphx_fp8
::
fp8e4m3fnuz
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_arithmetic
,
migraphx_fp8
::
fp8e4m3fnuz
)
template
<
class
T
>
template
<
class
T
>
using
accumulator_type
=
using
accumulator_type
=
std
::
conditional_t
<
is_floating_point
<
T
>
{},
std
::
conditional_t
<
is_floating_point
<
T
>
{},
...
...
src/py/migraphx_py.cpp
View file @
df7f8a35
...
@@ -40,7 +40,7 @@
...
@@ -40,7 +40,7 @@
#include <migraphx/json.hpp>
#include <migraphx/json.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/op/common.hpp>
#include <migraphx/op/common.hpp>
#include <migraphx/migraphx_float8.hpp>
#ifdef HAVE_GPU
#ifdef HAVE_GPU
#include <migraphx/gpu/hip.hpp>
#include <migraphx/gpu/hip.hpp>
#endif
#endif
...
@@ -144,6 +144,17 @@ struct npy_format_descriptor<half>
...
@@ -144,6 +144,17 @@ struct npy_format_descriptor<half>
static
constexpr
auto
name
()
{
return
_
(
"half"
);
}
static
constexpr
auto
name
()
{
return
_
(
"half"
);
}
};
};
template
<
>
struct
npy_format_descriptor
<
migraphx_fp8
::
fp8e4m3fnuz
>
{
static
std
::
string
format
()
{
// following: https://docs.python.org/3/library/struct.html#format-characters
return
"z"
;
}
static
constexpr
auto
name
()
{
return
_
(
"fp8e4m3fnuz"
);
}
};
}
// namespace detail
}
// namespace detail
}
// namespace pybind11
}
// namespace pybind11
...
...
src/targets/gpu/device/include/migraphx/gpu/device/types.hpp
View file @
df7f8a35
...
@@ -146,20 +146,20 @@ __device__ __host__ T to_hip_type(T x)
...
@@ -146,20 +146,20 @@ __device__ __host__ T to_hip_type(T x)
// Hip doens't support __fp16
// Hip doens't support __fp16
inline
__device__
__host__
float
to_hip_type
(
gpu_half
x
)
{
return
x
;
}
inline
__device__
__host__
float
to_hip_type
(
gpu_half
x
)
{
return
x
;
}
#define MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR(trait, T) \
#define MIGRAPHX_
DEVICE_
DETAIL_EXTEND_TRAIT_FOR(trait, T) \
template <class X> \
template <class X>
\
struct trait : std::trait<X> \
struct trait : std::trait<X>
\
{ \
{
\
}; \
};
\
\
\
template <> \
template <>
\
struct trait<T> : std::true_type \
struct trait<T> : std::true_type
\
{ \
{
\
};
};
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_floating_point
,
__fp16
)
MIGRAPHX_
DEVICE_
DETAIL_EXTEND_TRAIT_FOR
(
is_floating_point
,
__fp16
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_signed
,
__fp16
)
MIGRAPHX_
DEVICE_
DETAIL_EXTEND_TRAIT_FOR
(
is_signed
,
__fp16
)
MIGRAPHX_DETAIL_EXTEND_TRAIT_FOR
(
is_arithmetic
,
__fp16
)
MIGRAPHX_
DEVICE_
DETAIL_EXTEND_TRAIT_FOR
(
is_arithmetic
,
__fp16
)
}
// namespace device
}
// namespace device
}
// namespace gpu
}
// namespace gpu
...
...
test/CMakeLists.txt
View file @
df7f8a35
...
@@ -150,6 +150,7 @@ function(test_headers PREFIX)
...
@@ -150,6 +150,7 @@ function(test_headers PREFIX)
list
(
REMOVE_ITEM HEADERS
list
(
REMOVE_ITEM HEADERS
${
CMAKE_SOURCE_DIR
}
/src/targets/gpu/include/migraphx/gpu/ck.hpp
)
${
CMAKE_SOURCE_DIR
}
/src/targets/gpu/include/migraphx/gpu/ck.hpp
)
endif
()
endif
()
list
(
REMOVE_ITEM HEADERS
${
CMAKE_SOURCE_DIR
}
/src/include/migraphx/migraphx_f8_impl.hpp
)
foreach
(
HEADER
${
HEADERS
}
)
foreach
(
HEADER
${
HEADERS
}
)
file
(
RELATIVE_PATH HEADER_REL
${
CMAKE_SOURCE_DIR
}
${
HEADER
}
)
file
(
RELATIVE_PATH HEADER_REL
${
CMAKE_SOURCE_DIR
}
${
HEADER
}
)
string
(
MAKE_C_IDENTIFIER
${
HEADER_REL
}
TEST_NAME
)
string
(
MAKE_C_IDENTIFIER
${
HEADER_REL
}
TEST_NAME
)
...
...
test/float_equal.cpp
View file @
df7f8a35
...
@@ -22,6 +22,7 @@
...
@@ -22,6 +22,7 @@
* THE SOFTWARE.
* THE SOFTWARE.
*/
*/
#include <migraphx/float_equal.hpp>
#include <migraphx/float_equal.hpp>
#include <migraphx/migraphx_float8.hpp>
#include <migraphx/half.hpp>
#include <migraphx/half.hpp>
#include "test.hpp"
#include "test.hpp"
...
@@ -53,7 +54,7 @@ auto test_float_equal(T x, U y)
...
@@ -53,7 +54,7 @@ auto test_float_equal(T x, U y)
template
<
class
T
,
class
U
>
template
<
class
T
,
class
U
>
void
test_equality
()
void
test_equality
()
{
{
auto
x1
=
T
(
0.1
);
auto
x1
=
T
(
0.1
25
);
auto
x2
=
U
(
0.0
);
auto
x2
=
U
(
0.0
);
auto
x3
=
U
(
1.0
);
auto
x3
=
U
(
1.0
);
EXPECT
(
test_float_equal
(
x1
,
x1
));
EXPECT
(
test_float_equal
(
x1
,
x1
));
...
@@ -71,8 +72,12 @@ void test_equality()
...
@@ -71,8 +72,12 @@ void test_equality()
TEST_CASE_REGISTER
(
test_equality
<
double
,
float
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
float
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_equality
<
double
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_equality
<
float
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
float
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
float
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_equality
<
migraphx
::
half
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
migraphx
::
half
,
int
>
);
TEST_CASE_REGISTER
(
test_equality
<
migraphx
::
half
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_equality
<
migraphx_fp8
::
fp8e4m3fnuz
,
int
>
);
template
<
class
T
,
class
U
>
template
<
class
T
,
class
U
>
void
test_limits
()
void
test_limits
()
...
@@ -110,8 +115,13 @@ void test_limits()
...
@@ -110,8 +115,13 @@ void test_limits()
TEST_CASE_REGISTER
(
test_limits
<
double
,
float
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
float
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
int
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
int
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_limits
<
double
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_limits
<
float
,
int
>
);
TEST_CASE_REGISTER
(
test_limits
<
float
,
int
>
);
TEST_CASE_REGISTER
(
test_limits
<
float
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_limits
<
int
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_limits
<
int
,
migraphx
::
half
>
);
TEST_CASE_REGISTER
(
test_limits
<
int
,
migraphx_fp8
::
fp8e4m3fnuz
>
);
TEST_CASE_REGISTER
(
test_limits
<
migraphx_fp8
::
fp8e4m3fnuz
,
migraphx
::
half
>
);
#ifndef _WIN32
#ifndef _WIN32
// On Windows, types int and long have the same min and max values.
// On Windows, types int and long have the same min and max values.
TEST_CASE_REGISTER
(
test_limits
<
long
,
int
>
);
TEST_CASE_REGISTER
(
test_limits
<
long
,
int
>
);
...
...
test/fp8e4m3fnuz.cpp
0 → 100644
View file @
df7f8a35
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
*
* 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.
*/
#include <cmath>
#include <migraphx/float_equal.hpp>
#include <migraphx/migraphx_float8.hpp>
#include <migraphx/half.hpp>
#include <migraphx/ranges.hpp>
#include "test.hpp"
#include <limits>
float
fp8e4m3fnuz_to_fp32_value
(
uint8_t
input
)
{
constexpr
std
::
array
<
float
,
256
>
e4m3fnuz_lut
=
{
0.0
f
,
0.0009765625
f
,
0.001953125
f
,
0.0029296875
f
,
0.00390625
f
,
0.0048828125
f
,
0.005859375
f
,
0.0068359375
f
,
0.0078125
f
,
0.0087890625
f
,
0.009765625
f
,
0.0107421875
f
,
0.01171875
f
,
0.0126953125
f
,
0.013671875
f
,
0.0146484375
f
,
0.015625
f
,
0.017578125
f
,
0.01953125
f
,
0.021484375
f
,
0.0234375
f
,
0.025390625
f
,
0.02734375
f
,
0.029296875
f
,
0.03125
f
,
0.03515625
f
,
0.0390625
f
,
0.04296875
f
,
0.046875
f
,
0.05078125
f
,
0.0546875
f
,
0.05859375
f
,
0.0625
f
,
0.0703125
f
,
0.078125
f
,
0.0859375
f
,
0.09375
f
,
0.1015625
f
,
0.109375
f
,
0.1171875
f
,
0.125
f
,
0.140625
f
,
0.15625
f
,
0.171875
f
,
0.1875
f
,
0.203125
f
,
0.21875
f
,
0.234375
f
,
0.25
f
,
0.28125
f
,
0.3125
f
,
0.34375
f
,
0.375
f
,
0.40625
f
,
0.4375
f
,
0.46875
f
,
0.5
f
,
0.5625
f
,
0.625
f
,
0.6875
f
,
0.75
f
,
0.8125
f
,
0.875
f
,
0.9375
f
,
1.0
f
,
1.125
f
,
1.25
f
,
1.375
f
,
1.5
f
,
1.625
f
,
1.75
f
,
1.875
f
,
2.0
f
,
2.25
f
,
2.5
f
,
2.75
f
,
3.0
f
,
3.25
f
,
3.5
f
,
3.75
f
,
4.0
f
,
4.5
f
,
5.0
f
,
5.5
f
,
6.0
f
,
6.5
f
,
7.0
f
,
7.5
f
,
8.0
f
,
9.0
f
,
10.0
f
,
11.0
f
,
12.0
f
,
13.0
f
,
14.0
f
,
15.0
f
,
16.0
f
,
18.0
f
,
20.0
f
,
22.0
f
,
24.0
f
,
26.0
f
,
28.0
f
,
30.0
f
,
32.0
f
,
36.0
f
,
40.0
f
,
44.0
f
,
48.0
f
,
52.0
f
,
56.0
f
,
60.0
f
,
64.0
f
,
72.0
f
,
80.0
f
,
88.0
f
,
96.0
f
,
104.0
f
,
112.0
f
,
120.0
f
,
128.0
f
,
144.0
f
,
160.0
f
,
176.0
f
,
192.0
f
,
208.0
f
,
224.0
f
,
240.0
f
,
std
::
numeric_limits
<
float
>::
quiet_NaN
(),
-
0.0009765625
f
,
-
0.001953125
f
,
-
0.0029296875
f
,
-
0.00390625
f
,
-
0.0048828125
f
,
-
0.005859375
f
,
-
0.0068359375
f
,
-
0.0078125
f
,
-
0.0087890625
f
,
-
0.009765625
f
,
-
0.0107421875
f
,
-
0.01171875
f
,
-
0.0126953125
f
,
-
0.013671875
f
,
-
0.0146484375
f
,
-
0.015625
f
,
-
0.017578125
f
,
-
0.01953125
f
,
-
0.021484375
f
,
-
0.0234375
f
,
-
0.025390625
f
,
-
0.02734375
f
,
-
0.029296875
f
,
-
0.03125
f
,
-
0.03515625
f
,
-
0.0390625
f
,
-
0.04296875
f
,
-
0.046875
f
,
-
0.05078125
f
,
-
0.0546875
f
,
-
0.05859375
f
,
-
0.0625
f
,
-
0.0703125
f
,
-
0.078125
f
,
-
0.0859375
f
,
-
0.09375
f
,
-
0.1015625
f
,
-
0.109375
f
,
-
0.1171875
f
,
-
0.125
f
,
-
0.140625
f
,
-
0.15625
f
,
-
0.171875
f
,
-
0.1875
f
,
-
0.203125
f
,
-
0.21875
f
,
-
0.234375
f
,
-
0.25
f
,
-
0.28125
f
,
-
0.3125
f
,
-
0.34375
f
,
-
0.375
f
,
-
0.40625
f
,
-
0.4375
f
,
-
0.46875
f
,
-
0.5
f
,
-
0.5625
f
,
-
0.625
f
,
-
0.6875
f
,
-
0.75
f
,
-
0.8125
f
,
-
0.875
f
,
-
0.9375
f
,
-
1.0
f
,
-
1.125
f
,
-
1.25
f
,
-
1.375
f
,
-
1.5
f
,
-
1.625
f
,
-
1.75
f
,
-
1.875
f
,
-
2.0
f
,
-
2.25
f
,
-
2.5
f
,
-
2.75
f
,
-
3.0
f
,
-
3.25
f
,
-
3.5
f
,
-
3.75
f
,
-
4.0
f
,
-
4.5
f
,
-
5.0
f
,
-
5.5
f
,
-
6.0
f
,
-
6.5
f
,
-
7.0
f
,
-
7.5
f
,
-
8.0
f
,
-
9.0
f
,
-
10.0
f
,
-
11.0
f
,
-
12.0
f
,
-
13.0
f
,
-
14.0
f
,
-
15.0
f
,
-
16.0
f
,
-
18.0
f
,
-
20.0
f
,
-
22.0
f
,
-
24.0
f
,
-
26.0
f
,
-
28.0
f
,
-
30.0
f
,
-
32.0
f
,
-
36.0
f
,
-
40.0
f
,
-
44.0
f
,
-
48.0
f
,
-
52.0
f
,
-
56.0
f
,
-
60.0
f
,
-
64.0
f
,
-
72.0
f
,
-
80.0
f
,
-
88.0
f
,
-
96.0
f
,
-
104.0
f
,
-
112.0
f
,
-
120.0
f
,
-
128.0
f
,
-
144.0
f
,
-
160.0
f
,
-
176.0
f
,
-
192.0
f
,
-
208.0
f
,
-
224.0
f
,
-
240.0
f
,
};
return
e4m3fnuz_lut
[
input
];
}
TEST_CASE
(
test_fp8_cast_to_float
)
{
std
::
vector
<
uint8_t
>
bit_vals
(
256
);
std
::
iota
(
bit_vals
.
begin
(),
bit_vals
.
end
(),
0
);
EXPECT
(
bool
{
std
::
all_of
(
bit_vals
.
begin
(),
bit_vals
.
end
(),
[](
uint8_t
bit_val
)
{
migraphx_fp8
::
fp8e4m3fnuz
fp8_val
(
bit_val
,
migraphx_fp8
::
fp8e4m3fnuz
::
from_bits
());
if
(
std
::
isnan
(
float
(
fp8_val
))
and
std
::
isnan
(
fp8e4m3fnuz_to_fp32_value
(
bit_val
)))
{
return
true
;
}
return
migraphx
::
float_equal
(
float
(
fp8_val
),
fp8e4m3fnuz_to_fp32_value
(
bit_val
));
})});
}
TEST_CASE
(
test_positive_zero
)
{
float
zero
=
0.0
;
migraphx_fp8
::
fp8e4m3fnuz
fp8_zero
(
zero
);
EXPECT
(
fp8_zero
.
is_zero
());
EXPECT
(
migraphx
::
float_equal
(
zero
,
float
(
fp8_zero
)));
}
TEST_CASE
(
test_negative_zero
)
{
float
nzero
=
-
0.0
;
float
pzero
=
0.0
;
migraphx_fp8
::
fp8e4m3fnuz
fp8_nzero
(
nzero
);
EXPECT
(
fp8_nzero
.
is_zero
());
// negative zero gets converted to positive zero
EXPECT
(
migraphx
::
float_equal
(
pzero
,
float
(
fp8_nzero
)));
}
TEST_CASE
(
test_nan_1
)
{
float
fnan
=
std
::
numeric_limits
<
float
>::
quiet_NaN
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_nan
(
fnan
);
EXPECT
(
fp8_nan
.
is_nan
());
EXPECT
(
std
::
isnan
(
fp8_nan
));
}
TEST_CASE
(
test_nan_2
)
{
auto
fnan
=
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
quiet_NaN
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_nan
(
fnan
.
data
,
migraphx_fp8
::
fp8e4m3fnuz
::
from_bits
());
EXPECT
(
fp8_nan
.
is_nan
());
EXPECT
(
std
::
isnan
(
fp8_nan
));
EXPECT
(
std
::
isnan
(
float
(
fp8_nan
)));
}
TEST_CASE
(
test_infinity_1
)
{
float
finf
=
std
::
numeric_limits
<
float
>::
infinity
();
// no inf in fp8e4m3fnuz
migraphx_fp8
::
fp8e4m3fnuz
fp8_nan
(
finf
);
EXPECT
(
fp8_nan
.
is_nan
());
EXPECT
(
std
::
isnan
(
float
(
fp8_nan
)));
}
TEST_CASE
(
test_infinity_2
)
{
// no inf in fp8e4m3fnuz, it gets converted to NaNs
migraphx_fp8
::
fp8e4m3fnuz
fp8_nan
(
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
infinity
());
EXPECT
(
fp8_nan
.
is_nan
());
EXPECT
(
std
::
isnan
(
float
(
fp8_nan
)));
}
TEST_CASE
(
test_infinity_3
)
{
// neg inf
float
finf
=
-
1.0
*
std
::
numeric_limits
<
float
>::
infinity
();
// no inf in fp8e4m3fnuz
migraphx_fp8
::
fp8e4m3fnuz
fp8_nan
(
finf
);
EXPECT
(
fp8_nan
.
is_nan
());
EXPECT
(
std
::
isnan
(
float
(
fp8_nan
)));
}
TEST_CASE
(
test_numeric_max_1
)
{
float
fmax
=
std
::
numeric_limits
<
float
>::
max
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_max
(
fmax
);
EXPECT
(
fp8_max
==
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
max
());
}
TEST_CASE
(
test_numeric_max_2
)
{
// gets clipped to max
float
fmax
=
2
*
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
max
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_max
(
fmax
);
EXPECT
(
fp8_max
==
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
max
());
}
TEST_CASE
(
test_numeric_lowest_1
)
{
float
flowest
=
std
::
numeric_limits
<
float
>::
lowest
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_lowest
(
flowest
);
EXPECT
(
fp8_lowest
==
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
lowest
());
}
TEST_CASE
(
test_numeric_lowest_2
)
{
// gets clipped to lowest
float
fmin
=
2.0
*
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
lowest
();
migraphx_fp8
::
fp8e4m3fnuz
fp8_lowest
(
fmin
);
EXPECT
(
fp8_lowest
==
std
::
numeric_limits
<
migraphx_fp8
::
fp8e4m3fnuz
>::
lowest
());
}
int
main
(
int
argc
,
const
char
*
argv
[])
{
test
::
run
(
argc
,
argv
);
}
test/gpu/jit.cpp
View file @
df7f8a35
...
@@ -237,12 +237,12 @@ TEST_CASE(code_object_hip)
...
@@ -237,12 +237,12 @@ TEST_CASE(code_object_hip)
std
::
vector
<
migraphx
::
shape
>
expected_inputs
=
{
input
,
input
};
std
::
vector
<
migraphx
::
shape
>
expected_inputs
=
{
input
,
input
};
auto
co
=
migraphx
::
make_op
(
"gpu::code_object"
,
auto
co
=
migraphx
::
make_op
(
"gpu::code_object"
,
{{
"code_object"
,
migraphx
::
value
::
binary
{
binaries
.
front
()}},
{{
"code_object"
,
migraphx
::
value
::
binary
{
binaries
.
front
()}},
{
"symbol_name"
,
"add_2"
},
{
"symbol_name"
,
"add_2"
},
{
"global"
,
input
.
elements
()},
{
"global"
,
input
.
elements
()},
{
"local"
,
1024
},
{
"local"
,
1024
},
{
"expected_inputs"
,
migraphx
::
to_value
(
expected_inputs
)},
{
"expected_inputs"
,
migraphx
::
to_value
(
expected_inputs
)},
{
"output"
,
migraphx
::
to_value
(
input
)}});
{
"output"
,
migraphx
::
to_value
(
input
)}});
migraphx
::
program
p
;
migraphx
::
program
p
;
auto
*
mm
=
p
.
get_main_module
();
auto
*
mm
=
p
.
get_main_module
();
...
@@ -348,7 +348,10 @@ TEST_CASE(compile_math)
...
@@ -348,7 +348,10 @@ TEST_CASE(compile_math)
auto
vec_sizes
=
{
2
,
4
,
6
};
auto
vec_sizes
=
{
2
,
4
,
6
};
for
(
auto
&&
t
:
migraphx
::
shape
::
types
())
for
(
auto
&&
t
:
migraphx
::
shape
::
types
())
{
{
if
(
contains
({
migraphx
::
shape
::
bool_type
,
migraphx
::
shape
::
tuple_type
},
t
))
if
(
contains
({
migraphx
::
shape
::
bool_type
,
migraphx
::
shape
::
fp8e4m3fnuz_type
,
migraphx
::
shape
::
tuple_type
},
t
))
continue
;
continue
;
auto
name
=
migraphx
::
shape
::
cpp_type
(
t
);
auto
name
=
migraphx
::
shape
::
cpp_type
(
t
);
if
(
t
==
migraphx
::
shape
::
half_type
)
if
(
t
==
migraphx
::
shape
::
half_type
)
...
@@ -396,7 +399,10 @@ TEST_CASE(assert_type_min_max)
...
@@ -396,7 +399,10 @@ TEST_CASE(assert_type_min_max)
migraphx
::
gpu
::
hip_compile_options
options
;
migraphx
::
gpu
::
hip_compile_options
options
;
for
(
auto
&&
t
:
migraphx
::
shape
::
types
())
for
(
auto
&&
t
:
migraphx
::
shape
::
types
())
{
{
if
(
contains
({
migraphx
::
shape
::
bool_type
,
migraphx
::
shape
::
tuple_type
},
t
))
if
(
contains
({
migraphx
::
shape
::
bool_type
,
migraphx
::
shape
::
fp8e4m3fnuz_type
,
migraphx
::
shape
::
tuple_type
},
t
))
continue
;
continue
;
auto
name
=
migraphx
::
shape
::
cpp_type
(
t
);
auto
name
=
migraphx
::
shape
::
cpp_type
(
t
);
if
(
t
==
migraphx
::
shape
::
half_type
)
if
(
t
==
migraphx
::
shape
::
half_type
)
...
...
tools/api/migraphx.h
View file @
df7f8a35
...
@@ -44,7 +44,8 @@
...
@@ -44,7 +44,8 @@
m(int32_type, int32_t) \
m(int32_type, int32_t) \
m(int64_type, int64_t) \
m(int64_type, int64_t) \
m(uint32_type, uint32_t) \
m(uint32_type, uint32_t) \
m(uint64_type, uint64_t)
m(uint64_type, uint64_t) \
m(fp8e4m3fnuz_type, migraphx_fp8::fp8e4m3fnuz)
// clang-format on
// clang-format on
#ifdef __cplusplus
#ifdef __cplusplus
...
@@ -70,7 +71,9 @@ typedef enum
...
@@ -70,7 +71,9 @@ typedef enum
}
migraphx_shape_datatype_t
;
}
migraphx_shape_datatype_t
;
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
#undef MIGRAPHX_SHAPE_GENERATE_ENUM_TYPES
<%
generate_c_header
()
%>
<%
generate_c_header
()
%>
#ifdef __cplusplus
#ifdef __cplusplus
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment