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
f69d828d
Commit
f69d828d
authored
Nov 22, 2023
by
Manupa Karunaratne
Browse files
Merge branch 'develop' of
https://github.com/ROCmSoftwarePlatform/AMDMIGraphX
into mlir-attention
parents
fe36d210
24148857
Changes
161
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
1028 additions
and
53 deletions
+1028
-53
src/driver/argument_parser.hpp
src/driver/argument_parser.hpp
+2
-0
src/driver/main.cpp
src/driver/main.cpp
+23
-18
src/driver/verify.cpp
src/driver/verify.cpp
+4
-1
src/dynamic_loader.cpp
src/dynamic_loader.cpp
+24
-0
src/fuse_pointwise.cpp
src/fuse_pointwise.cpp
+1
-2
src/include/migraphx/bit_cast.hpp
src/include/migraphx/bit_cast.hpp
+50
-0
src/include/migraphx/dynamic_loader.hpp
src/include/migraphx/dynamic_loader.hpp
+0
-3
src/include/migraphx/float8.hpp
src/include/migraphx/float8.hpp
+409
-0
src/include/migraphx/float8_impl.hpp
src/include/migraphx/float8_impl.hpp
+328
-0
src/include/migraphx/half.hpp
src/include/migraphx/half.hpp
+13
-0
src/include/migraphx/matcher.hpp
src/include/migraphx/matcher.hpp
+19
-0
src/include/migraphx/module.hpp
src/include/migraphx/module.hpp
+1
-0
src/include/migraphx/onnx.hpp
src/include/migraphx/onnx.hpp
+5
-1
src/include/migraphx/op/isinf.hpp
src/include/migraphx/op/isinf.hpp
+53
-0
src/include/migraphx/op/multinomial.hpp
src/include/migraphx/op/multinomial.hpp
+70
-13
src/include/migraphx/op/nearbyint.hpp
src/include/migraphx/op/nearbyint.hpp
+11
-7
src/include/migraphx/op/normalize_attribute.hpp
src/include/migraphx/op/normalize_attribute.hpp
+2
-0
src/include/migraphx/op/prefix_scan_op.hpp
src/include/migraphx/op/prefix_scan_op.hpp
+6
-0
src/include/migraphx/op/quantizelinear.hpp
src/include/migraphx/op/quantizelinear.hpp
+5
-5
src/include/migraphx/op/random_uniform.hpp
src/include/migraphx/op/random_uniform.hpp
+2
-3
No files found.
src/driver/argument_parser.hpp
View file @
f69d828d
...
@@ -105,6 +105,8 @@ inline std::ostream& operator<<(std::ostream& os, const color& c)
...
@@ -105,6 +105,8 @@ inline std::ostream& operator<<(std::ostream& os, const color& c)
static
const
bool
use_color
=
isatty
(
STDOUT_FILENO
)
!=
0
;
static
const
bool
use_color
=
isatty
(
STDOUT_FILENO
)
!=
0
;
if
(
use_color
)
if
(
use_color
)
return
os
<<
"
\033
["
<<
static_cast
<
std
::
size_t
>
(
c
)
<<
"m"
;
return
os
<<
"
\033
["
<<
static_cast
<
std
::
size_t
>
(
c
)
<<
"m"
;
#else
(
void
)
c
;
#endif
#endif
return
os
;
return
os
;
}
}
...
...
src/driver/main.cpp
View file @
f69d828d
...
@@ -59,6 +59,13 @@ namespace migraphx {
...
@@ -59,6 +59,13 @@ namespace migraphx {
namespace
driver
{
namespace
driver
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
std
::
string
get_version
()
{
return
"MIGraphX Version: "
+
std
::
to_string
(
MIGRAPHX_VERSION_MAJOR
)
+
"."
+
std
::
to_string
(
MIGRAPHX_VERSION_MINOR
)
+
"."
+
std
::
to_string
(
MIGRAPHX_VERSION_PATCH
)
+
"."
MIGRAPHX_VERSION_TWEAK
;
}
struct
loader
struct
loader
{
{
std
::
string
model
;
std
::
string
model
;
...
@@ -597,17 +604,6 @@ struct verify : command<verify>
...
@@ -597,17 +604,6 @@ struct verify : command<verify>
}
}
};
};
struct
version
:
command
<
version
>
{
void
parse
(
const
argument_parser
&
)
{}
void
run
()
const
{
std
::
cout
<<
"MIGraphX Version: "
<<
MIGRAPHX_VERSION_MAJOR
<<
"."
<<
MIGRAPHX_VERSION_MINOR
<<
"."
<<
MIGRAPHX_VERSION_PATCH
<<
"."
<<
MIGRAPHX_STRINGIZE
(
MIGRAPHX_VERSION_TWEAK
)
<<
std
::
endl
;
}
};
struct
compile
:
command
<
compile
>
struct
compile
:
command
<
compile
>
{
{
compiler
c
;
compiler
c
;
...
@@ -760,16 +756,14 @@ struct main_command
...
@@ -760,16 +756,14 @@ struct main_command
}
}
void
parse
(
argument_parser
&
ap
)
void
parse
(
argument_parser
&
ap
)
{
{
std
::
string
version_str
=
"MIGraphX Version: "
+
std
::
to_string
(
MIGRAPHX_VERSION_MAJOR
)
+
std
::
string
version_str
=
get_version
();
"."
+
std
::
to_string
(
MIGRAPHX_VERSION_MINOR
)
+
"."
+
std
::
to_string
(
MIGRAPHX_VERSION_PATCH
)
+
"."
+
MIGRAPHX_STRINGIZE
(
MIGRAPHX_VERSION_TWEAK
);
ap
(
wrong_commands
,
{},
ap
.
metavar
(
"<command>"
),
ap
.
append
());
ap
(
wrong_commands
,
{},
ap
.
metavar
(
"<command>"
),
ap
.
append
());
ap
(
nullptr
,
{
"-h"
,
"--help"
},
ap
.
help
(
"Show help"
),
ap
.
show_help
(
get_command_help
()));
ap
(
nullptr
,
{
"-h"
,
"--help"
},
ap
.
help
(
"Show help"
),
ap
.
show_help
(
get_command_help
()));
ap
(
nullptr
,
ap
(
nullptr
,
{
"-v"
,
"--version"
},
{
"-v"
,
"--version"
},
ap
.
help
(
"Show MIGraphX version"
),
ap
.
help
(
"Show MIGraphX version"
),
ap
.
show_help
(
version_str
));
ap
.
show_help
(
version_str
));
ap
(
nullptr
,
{
"--ort-sha"
},
ap
.
help
(
"Show MIGraphX onnx runtime SHA"
));
// Trim command off of exe name
// Trim command off of exe name
ap
.
set_exe_name
(
ap
.
get_exe_name
().
substr
(
0
,
ap
.
get_exe_name
().
size
()
-
5
));
ap
.
set_exe_name
(
ap
.
get_exe_name
().
substr
(
0
,
ap
.
get_exe_name
().
size
()
-
5
));
...
@@ -812,7 +806,6 @@ using namespace migraphx::driver; // NOLINT
...
@@ -812,7 +806,6 @@ using namespace migraphx::driver; // NOLINT
int
main
(
int
argc
,
const
char
*
argv
[])
int
main
(
int
argc
,
const
char
*
argv
[])
{
{
std
::
vector
<
std
::
string
>
args
(
argv
+
1
,
argv
+
argc
);
std
::
vector
<
std
::
string
>
args
(
argv
+
1
,
argv
+
argc
);
// no argument, print the help infomration by default
// no argument, print the help infomration by default
if
(
args
.
empty
())
if
(
args
.
empty
())
{
{
...
@@ -822,15 +815,27 @@ int main(int argc, const char* argv[])
...
@@ -822,15 +815,27 @@ int main(int argc, const char* argv[])
auto
&&
m
=
get_commands
();
auto
&&
m
=
get_commands
();
auto
cmd
=
args
.
front
();
auto
cmd
=
args
.
front
();
if
(
cmd
==
"ort-sha"
)
if
(
cmd
==
"
--
ort-sha"
)
{
{
std
::
cout
<<
MIGRAPHX_ORT_SHA1
<<
std
::
endl
;
std
::
cout
<<
MIGRAPHX_ORT_SHA1
<<
std
::
endl
;
return
0
;
return
0
;
}
}
if
(
cmd
==
"-v"
or
cmd
==
"--version"
)
{
std
::
cout
<<
get_version
()
<<
std
::
endl
;
return
0
;
}
if
(
m
.
count
(
cmd
)
>
0
)
if
(
m
.
count
(
cmd
)
>
0
)
{
{
m
.
at
(
cmd
)(
argv
[
0
],
{
args
.
begin
()
+
1
,
args
.
end
()});
std
::
string
driver_invocation
=
std
::
string
(
argv
[
0
])
+
" "
+
migraphx
::
to_string_range
(
args
,
" "
);
std
::
cout
<<
"Running [ "
<<
get_version
()
<<
" ]: "
<<
driver_invocation
<<
std
::
endl
;
m
.
at
(
cmd
)(
argv
[
0
],
{
args
.
begin
()
+
1
,
args
.
end
()});
// run driver command found in commands map
std
::
cout
<<
"[ "
<<
get_version
()
<<
" ] Complete: "
<<
driver_invocation
<<
std
::
endl
;
}
}
else
else
{
{
...
...
src/driver/verify.cpp
View file @
f69d828d
...
@@ -119,6 +119,7 @@ void verify_program(const std::string& name,
...
@@ -119,6 +119,7 @@ void verify_program(const std::string& name,
auto
target_outs
=
run_target
(
p
,
t
,
options
,
quantize
,
inputs
);
auto
target_outs
=
run_target
(
p
,
t
,
options
,
quantize
,
inputs
);
std
::
size_t
output_num
=
ref_outs
.
size
();
std
::
size_t
output_num
=
ref_outs
.
size
();
bool
passed
=
true
;
for
(
std
::
size_t
i
=
0
;
i
<
output_num
;
++
i
)
for
(
std
::
size_t
i
=
0
;
i
<
output_num
;
++
i
)
{
{
if
(
ref_outs
[
i
].
get_shape
().
type
()
!=
target_outs
[
i
].
get_shape
().
type
()
or
if
(
ref_outs
[
i
].
get_shape
().
type
()
!=
target_outs
[
i
].
get_shape
().
type
()
or
...
@@ -130,9 +131,11 @@ void verify_program(const std::string& name,
...
@@ -130,9 +131,11 @@ void verify_program(const std::string& name,
}
}
else
else
{
{
verify_args
(
name
,
target_outs
[
i
],
verify
::
expected
{
ref_outs
[
i
]},
tols
);
passed
&=
verify_args
(
name
,
target_outs
[
i
],
verify
::
expected
{
ref_outs
[
i
]},
tols
);
}
}
}
}
if
(
passed
)
std
::
cout
<<
"MIGraphX verification passed successfully."
<<
std
::
endl
;
}
}
void
verify_instructions
(
const
program
&
prog
,
void
verify_instructions
(
const
program
&
prog
,
...
...
src/dynamic_loader.cpp
View file @
f69d828d
...
@@ -130,6 +130,30 @@ struct dynamic_loader_impl
...
@@ -130,6 +130,30 @@ struct dynamic_loader_impl
tmp_dir
temp
;
tmp_dir
temp
;
};
};
fs
::
path
dynamic_loader
::
path
(
void
*
address
)
{
HMODULE
module
=
nullptr
;
if
(
GetModuleHandleEx
(
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
|
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
static_cast
<
LPCSTR
>
(
address
),
&
module
)
==
0
)
{
auto
err
=
GetLastError
();
MIGRAPHX_THROW
(
"Unable to obtain module handle, error = "
+
std
::
to_string
(
err
));
}
TCHAR
buffer
[
MAX_PATH
];
if
(
GetModuleFileName
(
module
,
buffer
,
sizeof
(
buffer
))
==
0
)
{
auto
err
=
GetLastError
();
MIGRAPHX_THROW
(
"Unable to read module file path, error = "
+
std
::
to_string
(
err
));
}
if
(
GetLastError
()
==
ERROR_INSUFFICIENT_BUFFER
)
{
MIGRAPHX_THROW
(
"Buffer too small ("
+
std
::
to_string
(
MAX_PATH
)
+
") to hold the path"
);
}
return
{
buffer
};
}
#endif
#endif
optional
<
dynamic_loader
>
dynamic_loader
::
try_load
(
const
fs
::
path
&
p
)
optional
<
dynamic_loader
>
dynamic_loader
::
try_load
(
const
fs
::
path
&
p
)
...
...
src/fuse_pointwise.cpp
View file @
f69d828d
...
@@ -219,9 +219,8 @@ struct find_pointwise_reshape_pointwise
...
@@ -219,9 +219,8 @@ struct find_pointwise_reshape_pointwise
auto
reshape_input
=
[
&
](
const
auto
&
ins_to_insert
)
{
auto
reshape_input
=
[
&
](
const
auto
&
ins_to_insert
)
{
return
[
&
](
auto
input
)
{
return
[
&
](
auto
input
)
{
auto
c
=
m
.
insert_instruction
(
ins_to_insert
,
make_op
(
"contiguous"
),
input
);
return
m
.
insert_instruction
(
return
m
.
insert_instruction
(
ins_to_insert
,
make_op
(
"reshape"
,
{{
"dims"
,
cd
.
dims
}}),
c
);
ins_to_insert
,
make_op
(
"reshape"
,
{{
"dims"
,
cd
.
dims
}}),
input
);
};
};
};
};
auto
x_inputs
=
x_ins
->
inputs
();
auto
x_inputs
=
x_ins
->
inputs
();
...
...
src/include/migraphx/bit_cast.hpp
0 → 100644
View file @
f69d828d
/* ************************************************************************
* 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_BITCAST_HPP
#define MIGRAPHX_GUARD_RTGLIB_BITCAST_HPP
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
#endif
#include <migraphx/config.hpp>
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define MIGRAPHX_CONST_FOLD(x) (__builtin_constant_p(x) ? (x) : (x))
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
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__)
return
MIGRAPHX_CONST_FOLD
(
*
reinterpret_cast
<
To
*>
(
&
fr
));
#else
return
__builtin_bit_cast
(
To
,
fr
);
#endif
}
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#endif // MIGRAPHX_GUARD_RTGLIB_BITCAST_HPP
src/include/migraphx/dynamic_loader.hpp
View file @
f69d828d
...
@@ -38,15 +38,12 @@ struct dynamic_loader_impl;
...
@@ -38,15 +38,12 @@ struct dynamic_loader_impl;
struct
MIGRAPHX_EXPORT
dynamic_loader
struct
MIGRAPHX_EXPORT
dynamic_loader
{
{
#ifndef _WIN32
template
<
class
T
>
template
<
class
T
>
static
fs
::
path
path
(
T
*
address
)
static
fs
::
path
path
(
T
*
address
)
{
{
return
path
(
reinterpret_cast
<
void
*>
(
address
));
return
path
(
reinterpret_cast
<
void
*>
(
address
));
}
}
static
fs
::
path
path
(
void
*
address
);
static
fs
::
path
path
(
void
*
address
);
#endif
static
optional
<
dynamic_loader
>
try_load
(
const
fs
::
path
&
p
);
static
optional
<
dynamic_loader
>
try_load
(
const
fs
::
path
&
p
);
dynamic_loader
()
=
default
;
dynamic_loader
()
=
default
;
...
...
src/include/migraphx/float8.hpp
0 → 100644
View file @
f69d828d
/* ************************************************************************
* 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
// We are clipping/saturation in down conversion by default. Unclipped version is not tested and
// shouldn't be used without having enough tests.
// logic is based on clipping table from here : https://onnx.ai/onnx/technical/float8.html#cast
// NOLINTNEXTLINE
#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>
#include <migraphx/config.hpp>
#include <migraphx/float8_impl.hpp>
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
namespace
fp8
{
enum
class
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
,
bool
FNUZ
=
true
>
class
numeric_limits
;
template
<
migraphx
::
fp8
::
f8_type
T
=
migraphx
::
fp8
::
f8_type
::
fp8
,
bool
FNUZ
=
true
>
struct
float8
{
uint8_t
data
=
0x00
;
// default constructor
constexpr
float8
()
=
default
;
// default copy constructor
constexpr
float8
(
const
float8
&
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
::
rounding_mode
rm
=
migraphx
::
fp8
::
rounding_mode
::
standard
,
uint32_t
rng
=
0
)
{
if
constexpr
(
T
==
migraphx
::
fp8
::
f8_type
::
fp8
)
{
#ifdef MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx
::
fp8
::
impl
::
cast_to_f8
<
3
,
4
,
float
,
FNUZ
/*negative_zero_nan*/
,
true
/*clip*/
>
(
v
,
(
rm
==
migraphx
::
fp8
::
rounding_mode
::
stochastic
),
rng
);
#else // MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx
::
fp8
::
impl
::
cast_to_f8
<
3
,
4
,
float
,
FNUZ
/*negative_zero_nan*/
,
false
/*clip*/
>
(
v
,
(
rm
==
migraphx
::
fp8
::
rounding_mode
::
stochastic
),
rng
);
#endif // MIGRAPHX_F8_DOWNCAST_CLIPPING
}
else
{
#ifdef MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx
::
fp8
::
impl
::
cast_to_f8
<
2
,
5
,
float
,
FNUZ
/*negative_zero_nan*/
,
true
/*clip*/
>
(
v
,
(
rm
==
migraphx
::
fp8
::
rounding_mode
::
stochastic
),
rng
);
#else // MIGRAPHX_F8_DOWNCAST_CLIPPING
data
=
migraphx
::
fp8
::
impl
::
cast_to_f8
<
2
,
5
,
float
,
FNUZ
/*negative_zero_nan*/
,
false
/*clip*/
>
(
v
,
(
rm
==
migraphx
::
fp8
::
rounding_mode
::
stochastic
),
rng
);
#endif // rocblas_F8_downcast_clipping}
}
}
inline
constexpr
operator
float
()
const
{
if
constexpr
(
T
==
migraphx
::
fp8
::
f8_type
::
fp8
)
{
return
migraphx
::
fp8
::
impl
::
cast_from_f8
<
3
,
4
,
float
,
FNUZ
/*negative_zero_nan*/
>
(
data
);
}
// else
return
migraphx
::
fp8
::
impl
::
cast_from_f8
<
2
,
5
,
float
,
FNUZ
/*negative_zero_nan*/
>
(
data
);
}
inline
constexpr
bool
is_zero
()
const
{
if
constexpr
(
FNUZ
)
{
return
data
==
0x00
;
}
else
{
return
(
data
==
0x00
)
or
(
data
==
0x80
);
}
}
inline
constexpr
bool
is_nan
()
const
{
if
constexpr
(
FNUZ
)
{
return
data
==
0x80
;
}
else
{
if
(
T
==
migraphx
::
fp8
::
f8_type
::
bf8
)
{
return
(
data
==
0x7D
)
or
(
data
==
0x7E
)
or
(
data
==
0x7F
)
or
(
data
==
0xFD
)
or
(
data
==
0xFE
)
or
(
data
==
0xFF
);
}
else
{
return
(
data
==
0x7F
)
or
(
data
==
0xFF
);
}
}
}
inline
constexpr
bool
is_inf
()
const
{
if
constexpr
(
FNUZ
)
{
return
data
==
0x80
;
}
else
{
if
(
T
==
migraphx
::
fp8
::
f8_type
::
bf8
)
{
return
(
data
==
0x7C
)
or
(
data
==
0xFC
);
}
else
{
// no infinities in e4m3fn, represent them as NaNs
return
(
data
==
0x7F
)
or
(
data
==
0xFF
);
}
}
}
// NOLINTNEXTLINE
#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
)
noexcept
=
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_nan
()
or
rhs
.
is_inf
()
or
this
->
is_nan
()
or
this
->
is_inf
())
return
false
;
else
if
((
rhs
.
is_zero
()
and
this
->
is_zero
())
or
(
this
->
data
==
rhs
.
data
))
return
true
;
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
;
}
};
// https://onnx.ai/onnx/technical/float8.html
using
fp8e4m3fn
=
float8
<
migraphx
::
fp8
::
f8_type
::
fp8
,
false
>
;
using
fp8e5m2
=
float8
<
migraphx
::
fp8
::
f8_type
::
bf8
,
false
>
;
using
fp8e4m3fnuz
=
float8
<
migraphx
::
fp8
::
f8_type
::
fp8
,
true
>
;
using
fp8e5m2fnuz
=
float8
<
migraphx
::
fp8
::
f8_type
::
bf8
,
true
>
;
/*
// NOLINTNEXTLINE
#define MIGRAPHX_FP8_BINARY_OP(binary_op, T, U) \
inline constexpr U operator binary_op(const T& lhs, const T& rhs) \
{ \
return U(static_cast<float>(lhs) binary_op static_cast<float>(rhs)); \
}
// TODO: these should return floats for binary ops
// NOLINTNEXTLINE
#define MIGRAPHX_FP8_BINARY_OP_GEN_FOR(T) \
MIGRAPHX_FP8_BINARY_OP(*, T, T) \
MIGRAPHX_FP8_BINARY_OP(-, T, T) \
MIGRAPHX_FP8_BINARY_OP(/, T, T) \
MIGRAPHX_FP8_BINARY_OP(+, T, T) \
MIGRAPHX_FP8_BINARY_OP(==, T, bool) \
MIGRAPHX_FP8_BINARY_OP(>=, T, bool) \
MIGRAPHX_FP8_BINARY_OP(<=, T, bool) \
MIGRAPHX_FP8_BINARY_OP(>, T, bool) \
MIGRAPHX_FP8_BINARY_OP(<, T, bool) \
MIGRAPHX_FP8_BINARY_OP(!=, T, bool)
MIGRAPHX_FP8_BINARY_OP_GEN_FOR(fp8e5m2)
MIGRAPHX_FP8_BINARY_OP_GEN_FOR(fp8e4m3fn)
MIGRAPHX_FP8_BINARY_OP_GEN_FOR(fp8e5m2fnuz)
MIGRAPHX_FP8_BINARY_OP_GEN_FOR(fp8e4m3fnuz)
*/
// Special operator overloading
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
fp8e4m3fnuz
&
rhs
)
{
return
os
<<
static_cast
<
float
>
(
rhs
);
}
inline
fp8e4m3fnuz
fabs
(
fp8e4m3fnuz
v
)
{
v
.
data
=
v
.
data
&
0x7F
;
// NOLINT
return
v
;
}
// Special operator overloading
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
fp8e4m3fn
&
rhs
)
{
return
os
<<
static_cast
<
float
>
(
rhs
);
}
inline
fp8e4m3fn
fabs
(
fp8e4m3fn
v
)
{
v
.
data
=
v
.
data
&
0x7F
;
// NOLINT
return
v
;
}
// Special operator overloading
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
fp8e5m2fnuz
&
rhs
)
{
return
os
<<
static_cast
<
float
>
(
rhs
);
}
inline
fp8e5m2fnuz
fabs
(
fp8e5m2fnuz
v
)
{
v
.
data
=
v
.
data
&
0x7F
;
// NOLINT
return
v
;
}
// Special operator overloading
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
fp8e5m2
&
rhs
)
{
return
os
<<
static_cast
<
float
>
(
rhs
);
}
inline
fp8e5m2
fabs
(
fp8e5m2
v
)
{
v
.
data
=
v
.
data
&
0x7F
;
// NOLINT
return
v
;
}
template
<
>
class
numeric_limits
<
fp8e4m3fnuz
>
{
public:
static
constexpr
bool
has_infinity
=
false
;
static
constexpr
fp8e4m3fnuz
epsilon
()
{
return
fp8e4m3fnuz
(
0x28
,
fp8e4m3fnuz
::
from_bits
());
}
// NOLINTNEXTLINE
static
constexpr
fp8e4m3fnuz
quiet_NaN
()
{
return
fp8e4m3fnuz
(
0x80
,
fp8e4m3fnuz
::
from_bits
());
}
static
constexpr
fp8e4m3fnuz
max
()
{
return
fp8e4m3fnuz
(
0x7F
,
fp8e4m3fnuz
::
from_bits
());
}
// this is min value that is not DeNorm. DeNorm min is 0x01
static
constexpr
fp8e4m3fnuz
min
()
{
return
fp8e4m3fnuz
(
0x08
,
fp8e4m3fnuz
::
from_bits
());
}
static
constexpr
fp8e4m3fnuz
lowest
()
{
return
fp8e4m3fnuz
(
0xFF
,
fp8e4m3fnuz
::
from_bits
());
}
};
template
<
>
class
numeric_limits
<
fp8e4m3fn
>
{
public:
static
constexpr
bool
has_infinity
=
false
;
static
constexpr
fp8e4m3fn
epsilon
()
{
return
fp8e4m3fn
(
0x20
,
fp8e4m3fn
::
from_bits
());
}
// NOLINTNEXTLINE
static
constexpr
fp8e4m3fn
quiet_NaN
()
{
return
fp8e4m3fn
(
0x7F
,
fp8e4m3fn
::
from_bits
());
}
static
constexpr
fp8e4m3fn
max
()
{
return
fp8e4m3fn
(
0x7E
,
fp8e4m3fn
::
from_bits
());
}
// this is min value that is not DeNorm. DeNorm min is 0x01
static
constexpr
fp8e4m3fn
min
()
{
return
fp8e4m3fn
(
0x08
,
fp8e4m3fn
::
from_bits
());
}
static
constexpr
fp8e4m3fn
lowest
()
{
return
fp8e4m3fn
(
0xFE
,
fp8e4m3fn
::
from_bits
());
}
};
template
<
>
class
numeric_limits
<
fp8e5m2fnuz
>
{
public:
static
constexpr
bool
has_infinity
=
false
;
static
constexpr
fp8e5m2fnuz
epsilon
()
{
return
fp8e5m2fnuz
(
0x34
,
fp8e5m2fnuz
::
from_bits
());
}
static
constexpr
fp8e5m2fnuz
quiet_NaN
()
// NOLINT
{
return
fp8e5m2fnuz
(
0x80
,
fp8e5m2fnuz
::
from_bits
());
}
static
constexpr
fp8e5m2fnuz
max
()
{
return
fp8e5m2fnuz
(
0x7F
,
fp8e5m2fnuz
::
from_bits
());
}
// this is min value that is not DeNorm. DeNorm min is 0x01. I am not sure if we want to make
// this distinction. For the floating points we would end up using lowest most of the times.
static
constexpr
fp8e5m2fnuz
min
()
{
return
fp8e5m2fnuz
(
0x4
,
fp8e5m2fnuz
::
from_bits
());
}
static
constexpr
fp8e5m2fnuz
lowest
()
{
return
fp8e5m2fnuz
(
0xFF
,
fp8e5m2fnuz
::
from_bits
());
}
};
template
<
>
class
numeric_limits
<
fp8e5m2
>
{
public:
static
constexpr
bool
has_infinity
=
true
;
static
constexpr
fp8e5m2
epsilon
()
{
return
fp8e5m2
(
0x34
,
fp8e5m2
::
from_bits
());
}
// 7D, 7E, 7F are positive NaNs and FD, FE, FF are negative NaNs
static
constexpr
fp8e5m2
quiet_NaN
()
{
return
fp8e5m2
(
0xFF
,
fp8e5m2
::
from_bits
());
}
// NOLINT
static
constexpr
fp8e5m2
max
()
{
return
fp8e5m2
(
0x7B
,
fp8e5m2
::
from_bits
());
}
// this is min value that is not DeNorm. DeNorm min is 0x01. I am not sure if we want to make
// this distinction. For the floating points we would end up using lowest most of the times.
static
constexpr
fp8e5m2
min
()
{
return
fp8e5m2
(
0x4
,
fp8e5m2
::
from_bits
());
}
static
constexpr
fp8e5m2
lowest
()
{
return
fp8e5m2
(
0xFB
,
fp8e5m2
::
from_bits
());
}
// 7C and FC both are infinity
static
constexpr
fp8e5m2
infinity
()
{
return
fp8e5m2
(
0x7C
,
fp8e5m2
::
from_bits
());
}
};
}
// namespace fp8
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
// =================================================================================================
// define numeric limits for the new data type
// NOLINTBEGIN
namespace
std
{
#define MIGRAPHX_FP8_STD_OVERLOADS(T) \
inline bool isfinite(T x) { return not x.is_inf() and not x.is_nan(); } \
inline bool isnan(T x) { return x.is_nan(); } \
template <> \
class numeric_limits<T> : public migraphx::fp8::numeric_limits<T> \
{ \
}; \
template <class U> \
struct common_type<T, U> : std::common_type<float, U> \
{ \
}; \
template <class U> \
struct common_type<U, T> : std::common_type<float, U> \
{ \
}; \
template <> \
struct common_type<T, T> \
{ \
using type = T; \
};
MIGRAPHX_FP8_STD_OVERLOADS
(
migraphx
::
fp8
::
fp8e4m3fn
)
MIGRAPHX_FP8_STD_OVERLOADS
(
migraphx
::
fp8
::
fp8e5m2
)
MIGRAPHX_FP8_STD_OVERLOADS
(
migraphx
::
fp8
::
fp8e4m3fnuz
)
MIGRAPHX_FP8_STD_OVERLOADS
(
migraphx
::
fp8
::
fp8e5m2fnuz
)
}
// namespace std
// NOLINTEND
// =================================================================================================
#endif // MIGRAPHX_GUARD_RTGLIB_FLOAT8_HPP
src/include/migraphx/float8_impl.hpp
0 → 100644
View file @
f69d828d
/* ************************************************************************
* 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_IMPL_HPP
#define MIGRAPHX_GUARD_RTGLIB_FLOAT8_IMPL_HPP
#include <algorithm>
#include <cstdint>
#include <type_traits>
#include <migraphx/config.hpp>
#include <migraphx/bit_cast.hpp>
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
namespace
fp8
{
namespace
impl
{
// NOLINTBEGIN
template
<
uint32_t
Wm
,
uint32_t
We
,
typename
T
,
bool
NegativeZeroNan
,
bool
Clip
>
constexpr
uint8_t
cast_to_f8
(
T
f_x
,
bool
stoch
=
false
,
uint32_t
rng
=
0
)
{
constexpr
bool
is_float
=
std
::
is_same
<
T
,
float
>::
value
;
// half is not supported for now
constexpr
bool
is_half
=
false
;
static_assert
(
Wm
+
We
==
7
,
"Wm+We==7"
);
static_assert
(
is_float
or
is_half
,
"Only float can be cast to f8"
);
const
uint32_t
mfmt
=
(
sizeof
(
T
)
==
4
)
?
23
:
10
;
typename
std
::
conditional
<
sizeof
(
T
)
==
2
,
uint16_t
,
uint32_t
>::
type
x
;
if
constexpr
(
sizeof
(
T
)
==
4
)
x
=
migraphx
::
bit_cast
<
uint32_t
>
(
f_x
);
else
x
=
migraphx
::
bit_cast
<
uint16_t
>
(
f_x
);
uint32_t
head
=
0
;
uint32_t
mantissa
=
0
;
int
exponent
=
0
;
uint32_t
bias
=
0
;
uint32_t
sign
=
0
;
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
);
uint32_t
signed_all_ones
=
(
sign
<<
7
)
+
((((
1
<<
We
)
-
1
)
<<
Wm
)
+
((
1
<<
Wm
)
-
1
));
// Calcualte maximum singed value FLT_MAX, FLT_MIN
uint32_t
signed_max
=
signed_all_ones
;
if
(
not
NegativeZeroNan
)
signed_max
=
(
Wm
==
2
)
?
(
signed_max
-
4
)
:
(
signed_max
-
1
);
// Deal with inf and NaNs
if
(
NegativeZeroNan
)
// For the FNUZ cases, it is simple just return NaNs
{
if
((
sizeof
(
T
)
==
4
and
((
x
&
0x7F800000
)
==
0x7F800000
))
or
(
sizeof
(
T
)
==
2
and
((
x
&
0x7C00
)
==
0x7C00
)))
return
0x80
;
}
else
{
// calculate most common NaN mantissa for FP8, which is all Ones in binary
uint32_t
nan_mantissa
=
1
;
for
(
auto
i
=
1
;
i
<
Wm
;
++
i
)
{
nan_mantissa
|=
(
nan_mantissa
<<
1
);
}
if
((
sizeof
(
T
)
==
4
and
((
x
&
0x7F800000
)
==
0x7F800000
))
or
(
sizeof
(
T
)
==
2
and
((
x
&
0x7C00
)
==
0x7C00
)))
{
// infinity
if
(
mantissa
==
0
)
{
if
(
sign
==
0
)
return
(
Wm
==
2
)
?
0x7B
:
0x7E
;
else
return
(
Wm
==
2
)
?
0xFB
:
0xFE
;
}
else
// NaNs
return
signed_inf
+
nan_mantissa
;
}
}
// handle positive zero
if
(
x
==
0
)
return
0
;
// handle negative zero
else
if
((
sizeof
(
T
)
==
4
and
x
==
0x80000000
)
or
(
sizeof
(
T
)
==
2
and
x
==
0x8000
))
{
return
NegativeZeroNan
?
0
:
0x80
;
// For FNUZ types neg zero is just positive zero
}
/* 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
-
1u
))
-
1
+
(
NegativeZeroNan
?
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
=
0
;
int
f8_exponent
=
0
;
int
exponent_diff
=
0
;
if
(
exponent
==
0
and
mantissa
!=
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 FNUZ has exponent bias 16. It means that there are some
numbers in fp16 denormal but they are bf8 (FNUZ) normals - smallest bf8 (FNUZ) normal is
2^-15. fp16 numbers where exponent==0 (actual exponent -14) and highest bit of mantissa is 1
are bf8 (FNUZ) normal. In this case, the fp16 mantissa should be shift left by 1 */
act_exponent
=
1
-
bias
;
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 FNUZ 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 FNUZ */
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
+=
(
1u
<<
mfmt
);
// Add the implicit 1 into mantissa
}
// need to know whether the number is right in the middle of two adjacent fp8 numbers. use max
// value of 31 to avoid undefined behaviour
bool
midpoint
=
(
mantissa
&
((
1u
<<
std
::
min
(
31u
,
mfmt
-
Wm
+
exponent_diff
))
-
1
))
==
(
1u
<<
std
::
min
(
31u
,
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
>>=
std
::
min
(
31u
,
uint32_t
(
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
=
(
1u
<<
(
mfmt
-
Wm
))
-
1
;
bool
odd
=
mantissa
&
(
1u
<<
(
mfmt
-
Wm
));
// if the least significant bit that is not truncated is 1
/*
This part is doing rounding by adding mantissa part that is going to get dropped.
e.g. if the dropped part for less than 0.5 than it would round down.
if the dropped part is more than 0.5 then it would round up by rolling carry to LSB of retained
mantissa.
For the mid point when bit pattern is like this for Odd: `xy1:10000000` for Odd and
`xy0:10000000` for the Even. where `:` is delimiter for dropped v/s retained part.
For the odd case :
this will add xy1:10000000 + 000:10000000 which would roll over carry to LSB of retained
part making it RNE.
For the even case : this will add xy0:10000000 + 000:01111111 which would
round down and keep number Even
*/
mantissa
+=
(
stoch
?
rng
:
(
midpoint
?
(
odd
?
mantissa
:
mantissa
-
1
)
:
mantissa
))
&
drop_mask
;
// Now we deal with overflow
if
(
f8_exponent
==
0
and
((
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
// for e5m2 case, max_exp is 14, since exp = 15 is reserved for Infs and Nans
const
int
max_exp
=
(
1
<<
We
)
-
((
NegativeZeroNan
or
Wm
==
3
)
?
1
:
2
);
if
(
f8_exponent
>
max_exp
)
{
if
(
Clip
)
return
signed_max
;
else
{
// https://onnx.ai/onnx/technical/float8.html#cast
if
(
NegativeZeroNan
)
return
0x80
;
else
return
(
Wm
==
2
)
?
signed_inf
:
signed_all_ones
;
}
}
if
(
f8_exponent
==
0
and
mantissa
==
0
)
return
NegativeZeroNan
?
0
:
(
sign
<<
7
);
mantissa
&=
(
1
<<
Wm
)
-
1
;
return
(
sign
<<
7
)
|
(
f8_exponent
<<
Wm
)
|
mantissa
;
}
// NOLINTEND
template
<
uint32_t
Wm
,
uint32_t
We
,
typename
T
,
bool
NegativeZeroNan
>
constexpr
T
cast_from_f8
(
uint8_t
x
)
{
// half is not supported for now
constexpr
bool
is_half
=
false
;
constexpr
bool
is_float
=
std
::
is_same
<
T
,
float
>::
value
;
static_assert
(
is_float
or
is_half
,
"Only float are supported"
);
constexpr
int
weo
=
is_half
?
5
:
8
;
constexpr
int
wmo
=
is_half
?
10
:
(
is_float
?
23
:
7
);
// NOLINTNEXTLINE
T
f_inf
,
f_neg_inf
,
f_nan
,
f_neg0
;
if
constexpr
(
is_float
)
{
const
uint32_t
if_inf
=
0x7F800000
;
const
uint32_t
if_neg_inf
=
0xFF800000
;
const
uint32_t
if_nan
=
0x7F800001
;
const
uint32_t
if_neg0
=
0x80000000
;
f_inf
=
migraphx
::
bit_cast
<
float
>
(
if_inf
);
f_neg_inf
=
migraphx
::
bit_cast
<
float
>
(
if_neg_inf
);
f_nan
=
migraphx
::
bit_cast
<
float
>
(
if_nan
);
f_neg0
=
migraphx
::
bit_cast
<
float
>
(
if_neg0
);
}
if
(
x
==
0
)
return
0
;
uint32_t
sign
=
x
>>
7
;
// NOLINT
uint32_t
mantissa
=
x
&
((
1
<<
Wm
)
-
1
);
// NOLINT
int
exponent
=
(
x
&
0x7F
)
>>
Wm
;
// NOLINT
if
(
NegativeZeroNan
)
{
if
(
x
==
0x80
)
return
f_nan
;
}
else
{
if
(
x
==
0x80
)
return
f_neg0
;
if
(
exponent
==
((
1
<<
We
)
-
1
)
and
Wm
==
2
)
// NOLINT
return
(
mantissa
==
0
)
?
(
sign
?
f_neg_inf
:
f_inf
)
:
f_nan
;
else
if
(
Wm
==
3
and
(
x
==
0x7F
or
x
==
0xFF
))
return
f_nan
;
}
typename
std
::
conditional
<
sizeof
(
T
)
==
2
,
uint16_t
,
uint32_t
>::
type
retval
;
const
int
exp_low_cutoff
=
(
1
<<
(
weo
-
1
))
-
(
1
<<
(
We
-
1
))
+
1
-
(
NegativeZeroNan
?
1
:
0
);
// NOLINT
// 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
;
// NOLINT
exponent
+=
1
-
sh
;
mantissa
&=
((
1
<<
Wm
)
-
1
);
// NOLINT
}
exponent
+=
exp_low_cutoff
-
1
;
mantissa
<<=
wmo
-
Wm
;
// NOLINT
// subnormal output (occurs when T=half, We=5, negative_zero_nan=true)
if
(
exponent
<=
0
)
{
mantissa
|=
1
<<
wmo
;
// NOLINT
mantissa
>>=
1
-
exponent
;
// NOLINT
exponent
=
0
;
}
if
(
sizeof
(
T
)
==
2
)
retval
=
(
sign
<<
15
)
|
(
exponent
<<
10
)
|
mantissa
;
// NOLINT
else
retval
=
(
sign
<<
31
)
|
(
exponent
<<
23
)
|
mantissa
;
// NOLINT
return
migraphx
::
bit_cast
<
T
>
(
retval
);
}
}
// namespace impl
}
// namespace fp8
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
#endif // MIGRAPHX_GUARD_RTGLIB_FLOAT8_IMPL
src/include/migraphx/half.hpp
View file @
f69d828d
...
@@ -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/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/matcher.hpp
View file @
f69d828d
...
@@ -591,6 +591,19 @@ MIGRAPHX_PRED_MATCHER(same_input_shapes, instruction_ref ins)
...
@@ -591,6 +591,19 @@ MIGRAPHX_PRED_MATCHER(same_input_shapes, instruction_ref ins)
ins
->
inputs
().
begin
(),
ins
->
inputs
().
end
(),
[
&
](
auto
x
)
{
return
x
->
get_shape
()
==
s
;
});
ins
->
inputs
().
begin
(),
ins
->
inputs
().
end
(),
[
&
](
auto
x
)
{
return
x
->
get_shape
()
==
s
;
});
}
}
MIGRAPHX_PRED_MATCHER
(
has_same_value
,
instruction_ref
ins
)
{
if
(
ins
->
name
()
!=
"@literal"
)
return
false
;
bool
all_same
=
false
;
ins
->
get_literal
().
visit
([
&
](
auto
s
)
{
all_same
=
std
::
all_of
(
s
.
begin
()
+
1
,
s
.
end
(),
[
&
](
const
auto
&
scale
)
{
return
float_equal
(
scale
,
s
.
front
());
});
});
return
all_same
;
}
MIGRAPHX_BASIC_MATCHER
(
output
,
const
matcher_context
&
,
instruction_ref
ins
)
MIGRAPHX_BASIC_MATCHER
(
output
,
const
matcher_context
&
,
instruction_ref
ins
)
{
{
if
(
ins
->
outputs
().
size
()
==
1
)
if
(
ins
->
outputs
().
size
()
==
1
)
...
@@ -844,6 +857,12 @@ auto skip_broadcasts_converts(Ms... ms)
...
@@ -844,6 +857,12 @@ auto skip_broadcasts_converts(Ms... ms)
return
skip
(
name
(
"broadcast"
,
"multibroadcast"
,
"contiguous"
,
"convert"
))(
ms
...);
return
skip
(
name
(
"broadcast"
,
"multibroadcast"
,
"contiguous"
,
"convert"
))(
ms
...);
}
}
template
<
class
...
Ms
>
auto
skip_broadcasts_transposes_contiguous
(
Ms
...
ms
)
{
return
skip
(
name
(
"broadcast"
,
"multibroadcast"
,
"contiguous"
,
"transpose"
))(
ms
...);
}
template
<
class
T
>
template
<
class
T
>
inline
auto
has_value
(
T
x
,
float
tolerance
=
1e-6
)
inline
auto
has_value
(
T
x
,
float
tolerance
=
1e-6
)
{
{
...
...
src/include/migraphx/module.hpp
View file @
f69d828d
...
@@ -42,6 +42,7 @@
...
@@ -42,6 +42,7 @@
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
MIGRAPHX_EXPORT
const
operation
&
get_operation
(
instruction_ref
ins
);
const
operation
&
get_operation
(
instruction_ref
ins
);
struct
module_impl
;
struct
module_impl
;
...
...
src/include/migraphx/onnx.hpp
View file @
f69d828d
...
@@ -48,8 +48,12 @@ struct onnx_options
...
@@ -48,8 +48,12 @@ struct onnx_options
bool
skip_unknown_operators
=
false
;
bool
skip_unknown_operators
=
false
;
/// Print program if an error occurs
/// Print program if an error occurs
bool
print_program_on_error
=
false
;
bool
print_program_on_error
=
false
;
/// Max iter num for the loop operator
/// Max iter num for the loop operator
if trip count is not set
int64_t
max_loop_iterations
=
10
;
int64_t
max_loop_iterations
=
10
;
/// Max iter limit for the loop operator.
/// Since loop will become a tensor of max iter size a huge number can cause overflow during
/// shape computations.
int64_t
limit_max_iterations
=
std
::
numeric_limits
<
uint16_t
>::
max
();
/// Use dynamic output for operators when available
/// Use dynamic output for operators when available
bool
use_dyn_output
=
false
;
bool
use_dyn_output
=
false
;
};
};
...
...
src/include/migraphx/op/isinf.hpp
0 → 100644
View file @
f69d828d
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-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
* 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.
*/
#ifndef MIGRAPHX_GUARD_OPERATORS_ISINF_HPP
#define MIGRAPHX_GUARD_OPERATORS_ISINF_HPP
#include <migraphx/op/unary.hpp>
#include <migraphx/config.hpp>
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
namespace
op
{
struct
isinf
:
unary
<
isinf
>
{
auto
apply
()
const
{
return
[
&
](
auto
x
)
{
return
std
::
isinf
(
static_cast
<
double
>
(
x
));
};
}
std
::
string
name
()
const
{
return
"isinf"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
return
unary
<
isinf
>::
compute_shape
(
std
::
move
(
inputs
)).
with_type
(
shape
::
bool_type
);
}
};
}
// namespace op
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
#endif
src/include/migraphx/op/multinomial.hpp
View file @
f69d828d
/*
/*
* The MIT License (MIT)
* The MIT License (MIT)
*
*
* Copyright (c) 2015-202
2
Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-202
3
Advanced Micro Devices, Inc. All rights reserved.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* of this software and associated documentation files (the "Software"), to deal
...
@@ -21,11 +21,52 @@
...
@@ -21,11 +21,52 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* THE SOFTWARE.
*/
*/
/**
* * Multinomial or categorical distribution. Performs a sampling of random input
* and returns a count of
* each category, or bucket. This does not require the standard multinomial
* distribution but instead takes a probability distribution, i.e. cumulative
* distribution function (CDF) as its first input.
*
* Inputs: args[0] - a tensor of probabilities for each category. Values are
* cumulative density function
* totals as provided by operation prefix_scan_sum. Values are
* cumulative probabilities (i.e. start with any set of numbers > 0
* and then apply prefix_scan_sum). Values do not need to be
* normalized to sum to 1; this is done in runtime computation.
*
* This input has Rank 2. Dimension 0 is batch #, so that there can be
* a different CDF for each iteration in the batch. The size of dimension
* 1 is the number of categories.
*
* args[1] - a tensor of random numbers. The last dimension is the sample
* size, i.e. the number of
* random samples in each iteration of the batch. Nominally
* has two dimensions where the first dimension is batch size, but
* any reshaping such that the total
* number of elements is (batch_size * sample_size) is legal.
*
* Values as created by a std::mt19937 like this:
*
* size_t sample_size = 100000;
* float seed = 0.0f;
* std::mt19937 gen(seed);
* std::uniform_real_distribution<> dis(0.0, 1.0);
* std::vector<float> rand_samples(sample_size);
* std::generate(rand_samples.begin(), rand_samples.end(), [&]() { return
* dis(gen); });
*
* Output: A 2D vector of category each input. Dimensions are (Input 1[first], Input
2[last]).
*
*/
#ifndef MIGRAPHX_GUARD_OPERATORS_MULTINOMIAL_HPP
#ifndef MIGRAPHX_GUARD_OPERATORS_MULTINOMIAL_HPP
#define MIGRAPHX_GUARD_OPERATORS_MULTINOMIAL_HPP
#define MIGRAPHX_GUARD_OPERATORS_MULTINOMIAL_HPP
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/check_shapes.hpp>
#include <migraphx/dyn_output.hpp>
#include <migraphx/par_for.hpp>
#include <migraphx/par_for.hpp>
#include <migraphx/reflect.hpp>
#include <migraphx/reflect.hpp>
#include <random>
#include <random>
...
@@ -47,22 +88,35 @@ struct multinomial
...
@@ -47,22 +88,35 @@ struct multinomial
std
::
string
name
()
const
{
return
"multinomial"
;
}
std
::
string
name
()
const
{
return
"multinomial"
;
}
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
shape
compute_shape
(
std
::
vector
<
shape
>
inputs
)
const
{
{
check_shapes
{
inputs
,
*
this
}.
has
(
2
).
only_dims
(
2
);
check_shapes
{
inputs
,
*
this
,
true
}.
has
(
2
).
only_dims
(
2
);
size_t
sample_size
=
inputs
.
back
().
lens
().
back
();
if
(
not
contains
({
shape
::
int32_type
,
shape
::
int64_type
},
dtype
))
if
(
inputs
.
back
().
ndim
()
<
1
)
MIGRAPHX_THROW
(
MIGRAPHX_THROW
(
"Multinomial: Second input shape (sample) has no dimensions"
);
"Multinomial: Invalid output type. Valid types are int32_type and int64_type."
);
if
(
dtype
==
shape
::
bool_type
)
MIGRAPHX_THROW
(
"Multinomial: boolean output type invalid."
);
return
{
dtype
,
{
inputs
.
front
().
lens
().
front
(),
sample_size
}};
// Output takes one dimension from each of the two input shapes. If they are both fixed,
// return a static shape
if
((
not
inputs
.
front
().
dynamic
())
or
(
inputs
.
front
().
dyn_dims
().
front
().
is_fixed
()))
{
if
((
not
inputs
.
back
().
dynamic
())
or
(
inputs
.
back
().
dyn_dims
().
back
().
is_fixed
()))
{
size_t
batch
=
{
inputs
.
front
().
max_lens
().
front
()};
size_t
sample_size
{
inputs
.
back
().
max_lens
().
back
()};
return
{
dtype
,
{
batch
,
sample_size
}};
}
}
return
{
dtype
,
{
inputs
.
front
().
to_dynamic
().
dyn_dims
().
front
(),
inputs
.
back
().
to_dynamic
().
dyn_dims
().
back
()}};
}
}
argument
compute
(
const
shape
&
output_shape
,
std
::
vector
<
argument
>
args
)
const
argument
compute
(
const
dyn_output
&
dyn_out
,
std
::
vector
<
argument
>
args
)
const
{
{
argument
result
{
out
put_shape
};
argument
result
{
dyn_out
.
com
put
ed
_shape
};
size_t
batch_size
=
out
put_shape
.
lens
().
front
();
size_t
batch_size
=
dyn_out
.
com
put
ed
_shape
.
lens
().
front
();
size_t
class_size
=
args
[
0
].
get_shape
().
lens
().
back
();
size_t
class_size
=
args
[
0
].
get_shape
().
lens
().
back
();
size_t
sample_size
=
out
put_shape
.
lens
().
back
();
size_t
sample_size
=
dyn_out
.
com
put
ed
_shape
.
lens
().
back
();
visit_all
(
args
[
0
],
args
[
1
])([
&
](
auto
cdf
,
auto
dist
)
{
visit_all
(
args
[
0
],
args
[
1
])([
&
](
auto
cdf
,
auto
dist
)
{
result
.
visit
([
&
](
auto
output
)
{
result
.
visit
([
&
](
auto
output
)
{
...
@@ -70,13 +124,16 @@ struct multinomial
...
@@ -70,13 +124,16 @@ struct multinomial
auto
idx
=
args
[
1
].
get_shape
().
multi
(
i
);
auto
idx
=
args
[
1
].
get_shape
().
multi
(
i
);
auto
cdf_begin
=
cdf
.
begin
()
+
(
idx
[
0
]
*
class_size
);
auto
cdf_begin
=
cdf
.
begin
()
+
(
idx
[
0
]
*
class_size
);
auto
cdf_end
=
cdf_begin
+
class_size
;
auto
cdf_end
=
cdf_begin
+
class_size
;
// std::upper_bound returns an iterator to the bucket the value belongs in,
// when normalized by the probability distribution dist
auto
sample_iter
=
auto
sample_iter
=
std
::
upper_bound
(
cdf_begin
,
cdf_end
,
dist
[
i
]
*
*
(
std
::
prev
(
cdf_end
)));
std
::
upper_bound
(
cdf_begin
,
cdf_end
,
dist
[
i
]
*
*
(
std
::
prev
(
cdf_end
)));
// convert iterator to an integer index
output
[
i
]
=
std
::
distance
(
cdf_begin
,
sample_iter
);
output
[
i
]
=
std
::
distance
(
cdf_begin
,
sample_iter
);
});
});
});
});
});
});
return
result
;
return
result
;
}
}
};
};
...
...
src/include/migraphx/op/
round
.hpp
→
src/include/migraphx/op/
nearbyint
.hpp
View file @
f69d828d
/*
/*
* The MIT License (MIT)
* The MIT License (MIT)
*
*
* Copyright (c) 2015-202
2
Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-202
3
Advanced Micro Devices, Inc. All rights reserved.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* of this software and associated documentation files (the "Software"), to deal
...
@@ -21,24 +21,28 @@
...
@@ -21,24 +21,28 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* THE SOFTWARE.
*/
*/
#ifndef MIGRAPHX_GUARD_OPERATORS_
ROUND
_HPP
#ifndef MIGRAPHX_GUARD_OPERATORS_
NEARBYINT
_HPP
#define MIGRAPHX_GUARD_OPERATORS_
ROUND
_HPP
#define MIGRAPHX_GUARD_OPERATORS_
NEARBYINT
_HPP
#include <migraphx/op/unary.hpp>
#include <migraphx/op/unary.hpp>
#include <migraphx/config.hpp>
#include <migraphx/config.hpp>
#include <fenv.h>
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
namespace
op
{
namespace
op
{
struct
nearbyint
:
unary
<
nearbyint
>
struct
round
:
unary
<
round
>
{
{
auto
apply
()
const
auto
apply
()
const
{
{
return
[](
auto
x
)
{
return
std
::
round
(
x
);
};
return
[](
auto
x
)
{
auto
rounding_mode
=
fegetround
();
fesetround
(
FE_TONEAREST
);
return
std
::
nearbyint
(
x
);
fesetround
(
rounding_mode
);
};
}
}
};
};
}
// namespace op
}
// namespace op
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
}
// namespace migraphx
...
...
src/include/migraphx/op/normalize_attribute.hpp
View file @
f69d828d
...
@@ -40,6 +40,8 @@ namespace op {
...
@@ -40,6 +40,8 @@ namespace op {
* 2. use_rank (default) vs use_len:
* 2. use_rank (default) vs use_len:
* `use_rank` sets the max value/index of the attribute as the rank of lens.
* `use_rank` sets the max value/index of the attribute as the rank of lens.
* `use_lens` sets the max value/index as the corresponding value in lens at the axes index.
* `use_lens` sets the max value/index as the corresponding value in lens at the axes index.
* Uses the dynamic_dimension.max value for dynamic shapes. Returns the original vector
* (no normalization) if any of dynamic_dimension[axes] are not fixed.
* 3. `clip_min` vs. `not_clip_min` (default):
* 3. `clip_min` vs. `not_clip_min` (default):
* Clip values less than the minimum to the minimum or not.
* Clip values less than the minimum to the minimum or not.
* 4. `include_min` vs. `exclude_min` (default):
* 4. `include_min` vs. `exclude_min` (default):
...
...
src/include/migraphx/op/prefix_scan_op.hpp
View file @
f69d828d
...
@@ -22,6 +22,12 @@
...
@@ -22,6 +22,12 @@
* THE SOFTWARE.
* THE SOFTWARE.
*/
*/
/**
* Parent struct for prefix scan ops. A prefix scan is a mathematical entity useful
* in parallelizing various computations. Given a list of numbers, a prefix scan
* op returns an equal size list of running totals of the values. Other operations
* besides addition can be supported by child ops.
*/
#ifndef MIGRAPHX_GUARD_OPERATORS_SCAN_OP_HPP
#ifndef MIGRAPHX_GUARD_OPERATORS_SCAN_OP_HPP
#define MIGRAPHX_GUARD_OPERATORS_SCAN_OP_HPP
#define MIGRAPHX_GUARD_OPERATORS_SCAN_OP_HPP
...
...
src/include/migraphx/op/quantizelinear.hpp
View file @
f69d828d
...
@@ -30,11 +30,11 @@
...
@@ -30,11 +30,11 @@
#include <migraphx/par_for.hpp>
#include <migraphx/par_for.hpp>
#include <migraphx/value.hpp>
#include <migraphx/value.hpp>
#include <cmath>
#include <cmath>
#include <fenv.h>
namespace
migraphx
{
namespace
migraphx
{
inline
namespace
MIGRAPHX_INLINE_NS
{
inline
namespace
MIGRAPHX_INLINE_NS
{
namespace
op
{
namespace
op
{
struct
quantizelinear
struct
quantizelinear
{
{
std
::
string
name
()
const
{
return
"quantizelinear"
;
}
std
::
string
name
()
const
{
return
"quantizelinear"
;
}
...
@@ -71,26 +71,26 @@ struct quantizelinear
...
@@ -71,26 +71,26 @@ struct quantizelinear
{
{
y_zero_point
=
args
.
at
(
2
);
y_zero_point
=
args
.
at
(
2
);
}
}
argument
result
{
output_shape
};
argument
result
{
output_shape
};
auto
rounding_mode
=
fegetround
();
fesetround
(
FE_TONEAREST
);
visit_all
(
result
,
y_zero_point
)([
&
](
auto
output
,
auto
zero_pts
)
{
visit_all
(
result
,
y_zero_point
)([
&
](
auto
output
,
auto
zero_pts
)
{
visit_all
(
x
,
y_scale
)([
&
](
auto
input
,
auto
scales
)
{
visit_all
(
x
,
y_scale
)([
&
](
auto
input
,
auto
scales
)
{
using
quant_type
=
typename
decltype
(
output
)
::
value_type
;
using
quant_type
=
typename
decltype
(
output
)
::
value_type
;
auto
min_value
=
std
::
numeric_limits
<
quant_type
>::
min
();
auto
min_value
=
std
::
numeric_limits
<
quant_type
>::
min
();
auto
max_value
=
std
::
numeric_limits
<
quant_type
>::
max
();
auto
max_value
=
std
::
numeric_limits
<
quant_type
>::
max
();
par_for
(
output_shape
.
elements
(),
[
&
](
auto
i
)
{
par_for
(
output_shape
.
elements
(),
[
&
](
auto
i
)
{
int64_t
quantized
=
static_cast
<
int64_t
>
(
std
::
round
(
input
[
i
]
/
scales
[
i
]))
+
int64_t
quantized
=
static_cast
<
int64_t
>
(
std
::
nearbyint
(
input
[
i
]
/
scales
[
i
]))
+
static_cast
<
int64_t
>
(
zero_pts
[
i
]);
static_cast
<
int64_t
>
(
zero_pts
[
i
]);
output
[
i
]
=
std
::
max
(
static_cast
<
int64_t
>
(
min_value
),
output
[
i
]
=
std
::
max
(
static_cast
<
int64_t
>
(
min_value
),
std
::
min
(
static_cast
<
int64_t
>
(
max_value
),
quantized
));
std
::
min
(
static_cast
<
int64_t
>
(
max_value
),
quantized
));
});
});
});
});
});
});
fesetround
(
rounding_mode
);
return
result
;
return
result
;
}
}
};
};
}
// namespace op
}
// namespace op
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace MIGRAPHX_INLINE_NS
}
// namespace migraphx
}
// namespace migraphx
...
...
src/include/migraphx/op/random_uniform.hpp
View file @
f69d828d
...
@@ -65,11 +65,10 @@ struct random_uniform
...
@@ -65,11 +65,10 @@ struct random_uniform
return
inputs
.
at
(
1
);
return
inputs
.
at
(
1
);
}
}
argument
compute
(
const
shape
&
,
std
::
vector
<
argument
>
args
)
const
argument
compute
(
const
dyn_output
&
dyn_out
,
std
::
vector
<
argument
>
args
)
const
{
{
// Output goes into the passed buffer, not the shape output.
// Output goes into the passed buffer, not the shape output.
auto
result
=
args
[
1
];
argument
result
{
dyn_out
.
computed_shape
};
uint64_t
local_seed
=
args
[
0
].
at
<
uint64_t
>
(
0
);
uint64_t
local_seed
=
args
[
0
].
at
<
uint64_t
>
(
0
);
std
::
mt19937
gen
(
local_seed
);
std
::
mt19937
gen
(
local_seed
);
...
...
Prev
1
2
3
4
5
6
…
9
Next
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