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
187a4769
Commit
187a4769
authored
Jun 04, 2022
by
Paul
Browse files
Fix div by zero issue
parent
a62ef598
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
13 additions
and
11 deletions
+13
-11
src/targets/gpu/jit/layernorm.cpp
src/targets/gpu/jit/layernorm.cpp
+2
-2
src/targets/gpu/kernels/include/migraphx/kernels/layernorm.hpp
...argets/gpu/kernels/include/migraphx/kernels/layernorm.hpp
+11
-9
No files found.
src/targets/gpu/jit/layernorm.cpp
View file @
187a4769
...
@@ -26,8 +26,8 @@ namespace migraphx {
...
@@ -26,8 +26,8 @@ namespace migraphx {
extern "C" {
extern "C" {
__global__ void layernorm_kernel(void* input_p, void* output_p)
__global__ void layernorm_kernel(void* input_p, void* output_p)
{
{
transform_args(make_tensors(), ${transformers})(input_p, output_p)([](auto
input, auto output
) {
transform_args(make_tensors(),
rotate_last(),
${transformers})(input_p, output_p)([](auto
... xs
) {
layernorm<${axis}>(
input, output
);
layernorm<${axis}>(
op::id{}, xs...
);
});
});
}
}
...
...
src/targets/gpu/kernels/include/migraphx/kernels/layernorm.hpp
View file @
187a4769
...
@@ -2,18 +2,20 @@
...
@@ -2,18 +2,20 @@
#define MIGRAPHX_GUARD_KERNELS_LAYERNORM_HPP
#define MIGRAPHX_GUARD_KERNELS_LAYERNORM_HPP
#include <migraphx/kernels/reduce.hpp>
#include <migraphx/kernels/reduce.hpp>
#include <migraphx/kernels/ops.hpp>
#include <migraphx/kernels/ops.hpp>
#include <migraphx/kernels/print.hpp>
namespace
migraphx
{
namespace
migraphx
{
template
<
index_int
Axis
,
class
Input
,
class
Out
put
>
template
<
index_int
Axis
,
class
F
,
class
Output
,
class
Input
,
class
...
In
put
s
>
__device__
void
layernorm
(
Input
input
,
Output
out
put
)
__device__
void
layernorm
(
F
compute
,
Output
output
,
Input
input
,
Inputs
...
in
put
s
)
{
{
constexpr
auto
relements
=
using
reduce_output
=
reduce
::
with_axis
<
Input
,
Axis
>
;
get_shape_c
<
reduce
::
with_axis
<
Input
,
Axis
>>
{}.
elements
()
/
get_shape_c
<
Input
>
{}.
elements
();
constexpr
auto
relements
=
get_shape_c
<
Input
>
{}.
elements
()
/
get_shape_c
<
reduce_output
>
{}.
elements
();
reduce
::
block
::
run
<
reduce
::
with_axis
<
Input
,
Axis
>>
([
&
](
auto
,
auto
r
)
{
MIGRAPHX_ASSERT
(
relements
>
0
);
reduce
::
block
::
run
<
reduce_output
>
([
&
](
auto
,
auto
r
)
{
using
value_type
=
typename
Input
::
type
;
using
value_type
=
typename
Input
::
type
;
auto
mean
=
[
&
](
auto
f
)
{
auto
mean
=
[
&
](
auto
f
)
{
return
r
.
reduce
(
op
::
sum
{},
0
,
f
)(
input
)
/
value_type
{
relements
};
return
r
.
reduce
(
op
::
sum
{},
0
,
[
&
](
auto
x
)
{
return
f
(
x
)
/
value_type
{
relements
};
})(
input
);
};
};
// mean(x)
// mean(x)
auto
mean_x
=
mean
(
op
::
id
{});
auto
mean_x
=
mean
(
op
::
id
{});
...
@@ -23,11 +25,11 @@ __device__ void layernorm(Input input, Output output)
...
@@ -23,11 +25,11 @@ __device__ void layernorm(Input input, Output output)
return
m
*
m
;
return
m
*
m
;
});
});
r
.
inner
([
&
](
auto
&
y
,
auto
x
)
{
r
.
inner
([
&
](
auto
&
y
,
auto
x
,
auto
...
xs
)
{
auto
m
=
x
-
mean_x
;
auto
m
=
x
-
mean_x
;
// m * rsqrt(mean(m ^ 2) + 1e-12)
// m * rsqrt(mean(m ^ 2) + 1e-12)
y
=
m
*
rsqrt
(
mean_m2
+
value_type
{
1e-12
});
y
=
compute
(
m
*
rsqrt
(
mean_m2
+
value_type
{
1e-12
})
,
xs
...)
;
})(
output
,
input
);
})(
output
,
input
,
inputs
...
);
});
});
}
}
...
...
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