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
composable_kernel
Commits
d305c079
Commit
d305c079
authored
Nov 13, 2023
by
Artur Wojcik
Browse files
Merge branch 'uif2-initial' into uif2-migraphx
parents
57e82227
7c284291
Changes
162
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
818 additions
and
128 deletions
+818
-128
example/27_layernorm2d_fwd/layernorm2d_fwd_splitk_fp16.cpp
example/27_layernorm2d_fwd/layernorm2d_fwd_splitk_fp16.cpp
+45
-0
example/27_layernorm2d_fwd/run_layernorm_example.inc
example/27_layernorm2d_fwd/run_layernorm_example.inc
+3
-3
example/42_groupnorm/CMakeLists.txt
example/42_groupnorm/CMakeLists.txt
+0
-3
example/42_groupnorm/groupnorm_splitk_fp16.cpp
example/42_groupnorm/groupnorm_splitk_fp16.cpp
+0
-45
example/42_groupnorm/groupnorm_swish_fp16.cpp
example/42_groupnorm/groupnorm_swish_fp16.cpp
+0
-45
example/42_groupnorm_fwd/CMakeLists.txt
example/42_groupnorm_fwd/CMakeLists.txt
+3
-0
example/42_groupnorm_fwd/common.hpp
example/42_groupnorm_fwd/common.hpp
+2
-2
example/42_groupnorm_fwd/groupnorm_fwd_sigmoid_mul_fp16.cpp
example/42_groupnorm_fwd/groupnorm_fwd_sigmoid_mul_fp16.cpp
+65
-0
example/42_groupnorm_fwd/groupnorm_fwd_splitk_fp16.cpp
example/42_groupnorm_fwd/groupnorm_fwd_splitk_fp16.cpp
+45
-0
example/42_groupnorm_fwd/groupnorm_fwd_swish_fp16.cpp
example/42_groupnorm_fwd/groupnorm_fwd_swish_fp16.cpp
+45
-0
example/42_groupnorm_fwd/run_groupnorm_fwd_example.inc
example/42_groupnorm_fwd/run_groupnorm_fwd_example.inc
+3
-3
example/44_elementwise_permute/CMakeLists.txt
example/44_elementwise_permute/CMakeLists.txt
+2
-0
example/44_elementwise_permute/elementwise_permute.cpp
example/44_elementwise_permute/elementwise_permute.cpp
+135
-0
example/44_elementwise_permute/elementwise_permute_3d.cpp
example/44_elementwise_permute/elementwise_permute_3d.cpp
+120
-0
example/44_elementwise_permute/elementwise_permute_4D_fp16.cpp
...le/44_elementwise_permute/elementwise_permute_4D_fp16.cpp
+7
-8
example/44_elementwise_permute/elementwise_permute_4D_fp16_2d.cpp
...44_elementwise_permute/elementwise_permute_4D_fp16_2d.cpp
+9
-19
example/53_layernorm_bwd/CMakeLists.txt
example/53_layernorm_bwd/CMakeLists.txt
+1
-0
example/53_layernorm_bwd/layernorm2d_bwd_fp16.cpp
example/53_layernorm_bwd/layernorm2d_bwd_fp16.cpp
+165
-0
example/54_groupnorm_bwd/CMakeLists.txt
example/54_groupnorm_bwd/CMakeLists.txt
+1
-0
example/54_groupnorm_bwd/groupnorm_bwd_fp16.cpp
example/54_groupnorm_bwd/groupnorm_bwd_fp16.cpp
+167
-0
No files found.
example/27_layernorm2d_fwd/layernorm2d_fwd_splitk_fp16.cpp
0 → 100644
View file @
d305c079
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include "common.hpp"
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
BetaDataType
=
ck
::
half_t
;
using
YDataType
=
ck
::
half_t
;
using
SaveMeanInvStdDataType
=
float
;
using
ComputeDataType
=
float
;
using
PassThrough
=
ck
::
tensor_operation
::
element_wise
::
PassThrough
;
#define SAVE_MEAN_INV_STD
constexpr
int
Rank
=
2
;
constexpr
int
NumReduceDim
=
1
;
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationFwdSplitKImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
PassThrough
,
Rank
,
NumReduceDim
,
256
,
// BlockSize
8
,
// ClusterM
32
,
// ClusterK
1
,
// SliceM
8
,
// SliceK
1
,
// XYVectorDim (0=M, 1=K)
8
,
// XScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
8
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
8
,
// BetaScalarPerVector
8
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_layernorm_example.inc"
int
main
()
{
return
run_layernorm2d_fwd_example
<
DeviceInstance
>
();
}
example/27_layernorm/run_layernorm_example.inc
→
example/27_layernorm
2d_fwd
/run_layernorm_example.inc
View file @
d305c079
...
...
@@ -4,7 +4,7 @@
#pragma once
template
<
typename
DeviceInstance
>
int
run_
groupnorm
_example
()
int
run_
layernorm2d_fwd
_example
()
{
bool
time_kernel
=
false
;
...
...
@@ -44,9 +44,9 @@ int run_groupnorm_example()
{
0
,
1
},
std
::
vector
<
ck
::
index_t
>
{
y
.
mDesc
.
GetStrides
()
.
begin
(),
y
.
mDesc
.
GetStrides
()
.
end
()},
std
::
vector
<
ck
::
index_t
>
{
save_mean
.
mDesc
.
GetStrides
()
.
begin
(),
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
std
::
vector
<
ck
::
index_t
>
{
save_mean
.
mDesc
.
GetStrides
()
.
begin
(),
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
{
1
},
1
e
-
4
,
x_dev
.
GetDeviceBuffer
(),
...
...
example/42_groupnorm/CMakeLists.txt
deleted
100644 → 0
View file @
57e82227
add_example_executable
(
example_groupnorm_sigmoid_mul_fp16 groupnorm_sigmoid_mul_fp16.cpp
)
add_example_executable
(
example_groupnorm_splitk_fp16 groupnorm_splitk_fp16.cpp
)
add_example_executable
(
example_groupnorm_swish_fp16 groupnorm_swish_fp16.cpp
)
example/42_groupnorm/groupnorm_splitk_fp16.cpp
deleted
100644 → 0
View file @
57e82227
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include "common.hpp"
constexpr
int
Rank
=
5
;
constexpr
int
NumReduceDim
=
3
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
BetaDataType
=
ck
::
half_t
;
using
YDataType
=
ck
::
half_t
;
using
SaveMeanInvStdDataType
=
float
;
using
ComputeDataType
=
float
;
using
YElementOp
=
ck
::
tensor_operation
::
element_wise
::
Swish
;
#define SAVE_MEAN_INV_STD
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationSplitKImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
256
,
// BlockSize
1
,
// ClusterM
256
,
// ClusterK
1
,
// SliceM
16
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_groupnorm_example.inc"
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_example
(
argc
,
argv
);
}
example/42_groupnorm/groupnorm_swish_fp16.cpp
deleted
100644 → 0
View file @
57e82227
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include "common.hpp"
constexpr
int
Rank
=
5
;
constexpr
int
NumReduceDim
=
3
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
BetaDataType
=
ck
::
half_t
;
using
YDataType
=
ck
::
half_t
;
using
SaveMeanInvStdDataType
=
float
;
using
ComputeDataType
=
float
;
using
YElementOp
=
ck
::
tensor_operation
::
element_wise
::
Swish
;
#define SAVE_MEAN_INV_STD
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
1024
,
// BlockSize
1
,
// ClusterM
1024
,
// ClusterK
1
,
// SliceM
32
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_groupnorm_example.inc"
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_example
(
argc
,
argv
);
}
example/42_groupnorm_fwd/CMakeLists.txt
0 → 100644
View file @
d305c079
add_example_executable
(
example_groupnorm_fwd_sigmoid_mul_fp16 groupnorm_fwd_sigmoid_mul_fp16.cpp
)
add_example_executable
(
example_groupnorm_fwd_splitk_fp16 groupnorm_fwd_splitk_fp16.cpp
)
add_example_executable
(
example_groupnorm_fwd_swish_fp16 groupnorm_fwd_swish_fp16.cpp
)
example/42_groupnorm/common.hpp
→
example/42_groupnorm
_fwd
/common.hpp
View file @
d305c079
...
...
@@ -11,8 +11,8 @@
#include "ck/ck.hpp"
#include "ck/utility/reduction_enums.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_impl.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_splitk_impl.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_
fwd_
impl.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_
fwd_
splitk_impl.hpp"
#include "ck/tensor_operation/gpu/device/reduction_operator_mapping.hpp"
#include "ck/library/utility/fill.hpp"
...
...
example/42_groupnorm/groupnorm_sigmoid_mul_fp16.cpp
→
example/42_groupnorm
_fwd
/groupnorm_
fwd_
sigmoid_mul_fp16.cpp
View file @
d305c079
...
...
@@ -37,29 +37,29 @@ struct YElementOp
};
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
1024
,
// BlockSize
1
,
// ClusterM
1024
,
// ClusterK
1
,
// SliceM
32
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
ck
::
tensor_operation
::
device
::
DeviceNormalization
Fwd
Impl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
1024
,
// BlockSize
1
,
// ClusterM
1024
,
// ClusterK
1
,
// SliceM
32
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_groupnorm_example.inc"
#include "run_groupnorm_
fwd_
example.inc"
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_example
(
argc
,
argv
);
}
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_
fwd_
example
(
argc
,
argv
);
}
example/42_groupnorm_fwd/groupnorm_fwd_splitk_fp16.cpp
0 → 100644
View file @
d305c079
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include "common.hpp"
constexpr
int
Rank
=
5
;
constexpr
int
NumReduceDim
=
3
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
BetaDataType
=
ck
::
half_t
;
using
YDataType
=
ck
::
half_t
;
using
SaveMeanInvStdDataType
=
float
;
using
ComputeDataType
=
float
;
using
YElementOp
=
ck
::
tensor_operation
::
element_wise
::
Swish
;
#define SAVE_MEAN_INV_STD
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationFwdSplitKImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
256
,
// BlockSize
1
,
// ClusterM
256
,
// ClusterK
1
,
// SliceM
16
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_groupnorm_fwd_example.inc"
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_fwd_example
(
argc
,
argv
);
}
example/42_groupnorm_fwd/groupnorm_fwd_swish_fp16.cpp
0 → 100644
View file @
d305c079
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include "common.hpp"
constexpr
int
Rank
=
5
;
constexpr
int
NumReduceDim
=
3
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
BetaDataType
=
ck
::
half_t
;
using
YDataType
=
ck
::
half_t
;
using
SaveMeanInvStdDataType
=
float
;
using
ComputeDataType
=
float
;
using
YElementOp
=
ck
::
tensor_operation
::
element_wise
::
Swish
;
#define SAVE_MEAN_INV_STD
using
DeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationFwdImpl
<
XDataType
,
GammaDataType
,
BetaDataType
,
ComputeDataType
,
YDataType
,
SaveMeanInvStdDataType
,
YElementOp
,
Rank
,
NumReduceDim
,
1024
,
// BlockSize
1
,
// ClusterM
1024
,
// ClusterK
1
,
// SliceM
32
,
// SliceK
1
,
// SrcVecDim (0=M, 1=K)
2
,
// SrcScalarPerVector
1
,
// GammaVecDim (0=M, 1=K)
2
,
// GammaScalarPerVector
1
,
// BetaVecDim (0=M, 1=K)
2
,
// BetaScalarPerVector
2
,
// YScalarPerVector
1
>
;
// SaveMeanInvStdScalarPerVector
#include "run_groupnorm_fwd_example.inc"
int
main
(
int
argc
,
char
*
argv
[])
{
run_groupnorm_fwd_example
(
argc
,
argv
);
}
example/42_groupnorm/run_groupnorm_example.inc
→
example/42_groupnorm
_fwd
/run_groupnorm_
fwd_
example.inc
View file @
d305c079
...
...
@@ -3,7 +3,7 @@
#pragma once
int
run_groupnorm_example
(
int
argc
,
char
*
argv
[])
int
run_groupnorm_
fwd_
example
(
int
argc
,
char
*
argv
[])
{
ck
::
index_t
N
=
32
;
ck
::
index_t
H
=
16
;
...
...
@@ -65,9 +65,9 @@ int run_groupnorm_example(int argc, char* argv[])
{
0
,
0
,
0
,
C
,
1
},
std
::
vector
<
ck
::
index_t
>
{
y
.
mDesc
.
GetStrides
()
.
begin
(),
y
.
mDesc
.
GetStrides
()
.
end
()},
std
::
vector
<
ck
::
index_t
>
{
save_mean
.
mDesc
.
GetStrides
()
.
begin
(),
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
std
::
vector
<
ck
::
index_t
>
{
save_mean
.
mDesc
.
GetStrides
()
.
begin
(),
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
save_mean
.
mDesc
.
GetStrides
()
.
end
()},
{
1
,
2
,
4
},
// reduction dimension: [H, W, C]
1
e
-
6
,
x_dev
.
GetDeviceBuffer
(),
...
...
example/44_elementwise_permute/CMakeLists.txt
View file @
d305c079
add_example_executable
(
example_elementwise_permute_4D_fp16 elementwise_permute_4D_fp16.cpp
)
add_example_executable
(
example_elementwise_permute_4D_fp16_2d elementwise_permute_4D_fp16_2d.cpp
)
add_example_executable
(
example_elementwise_permute elementwise_permute.cpp
)
add_example_executable
(
example_elementwise_permute_3d elementwise_permute_3d.cpp
)
example/44_elementwise_permute/elementwise_permute.cpp
0 → 100644
View file @
d305c079
#include <iostream>
#include <cstdlib>
#include "ck/ck.hpp"
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_elementwise_impl.hpp"
#include "ck/library/utility/algorithm.hpp"
#include "ck/library/utility/check_err.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
using
F16
=
ck
::
half_t
;
using
F32
=
float
;
using
ADataType
=
F16
;
using
BDataType
=
F16
;
using
PassThrough
=
ck
::
tensor_operation
::
element_wise
::
PassThrough
;
using
DeviceElementwisePermuteInstance
=
ck
::
tensor_operation
::
device
::
DeviceElementwiseImpl
<
ck
::
Tuple
<
ADataType
>
,
// InDataTypeTuple
ck
::
Tuple
<
BDataType
>
,
// OutDataTypeTuple
PassThrough
,
// ElementwiseOp
5
,
// NumDim
8
,
// MPerThread
ck
::
Sequence
<
1
>
,
// InScalarPerVectorSeq
ck
::
Sequence
<
1
>>
;
// OutScalarPerVectorSeq
template
<
typename
HostTensorA
,
typename
HostTensorB
,
typename
Functor
>
void
host_elementwise4D
(
HostTensorB
&
B_ndhwc
,
const
HostTensorA
&
A_ncdhw
,
Functor
functor
)
{
for
(
std
::
size_t
n
=
0
;
n
<
A_ncdhw
.
mDesc
.
GetLengths
()[
0
];
++
n
)
for
(
std
::
size_t
c
=
0
;
c
<
A_ncdhw
.
mDesc
.
GetLengths
()[
1
];
++
c
)
for
(
std
::
size_t
d
=
0
;
d
<
A_ncdhw
.
mDesc
.
GetLengths
()[
2
];
++
d
)
for
(
std
::
size_t
h
=
0
;
h
<
A_ncdhw
.
mDesc
.
GetLengths
()[
3
];
++
h
)
for
(
std
::
size_t
w
=
0
;
w
<
A_ncdhw
.
mDesc
.
GetLengths
()[
4
];
++
w
)
{
auto
a_val
=
A_ncdhw
(
n
,
c
,
d
,
h
,
w
);
functor
(
B_ndhwc
(
n
,
d
,
h
,
w
,
c
),
a_val
);
}
}
int
main
()
{
bool
do_verification
=
true
;
bool
time_kernel
=
true
;
std
::
vector
<
std
::
size_t
>
ncdhw
=
{
16
,
8
,
8
,
8
,
8
};
std
::
vector
<
std
::
size_t
>
ndhwc
=
{
16
,
8
,
8
,
8
,
8
};
Tensor
<
ADataType
>
a
(
ncdhw
);
Tensor
<
BDataType
>
b
(
ndhwc
);
a
.
GenerateTensorValue
(
GeneratorTensor_3
<
ADataType
>
{
0.0
,
1.0
});
DeviceMem
a_device_buf
(
sizeof
(
ADataType
)
*
a
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
b_device_buf
(
sizeof
(
BDataType
)
*
b
.
mDesc
.
GetElementSpaceSize
());
a_device_buf
.
ToDevice
(
a
.
mData
.
data
());
std
::
array
<
const
void
*
,
1
>
input
=
{
a_device_buf
.
GetDeviceBuffer
()};
std
::
array
<
void
*
,
1
>
output
=
{
b_device_buf
.
GetDeviceBuffer
()};
std
::
array
<
ck
::
index_t
,
5
>
ab_lengths
;
/**std::array<ck::index_t, 5> a_strides = {
static_cast<int>(ncdhw[1] * ncdhw[2] * ncdhw[3] * ncdhw[4]),
static_cast<int>(ncdhw[2] * ncdhw[3] * ncdhw[4]),
static_cast<int>(ncdhw[3] * ncdhw[4]),
static_cast<int>(ncdhw[4]),
1};
std::array<ck::index_t, 5> b_strides = {
static_cast<int>(ndhwc[1] * ndhwc[2] * ndhwc[3] * ndhwc[4]),
static_cast<int>(ndhwc[2] * ndhwc[3] * ndhwc[4]),
1,
static_cast<int>(ndhwc[3] * ndhwc[4]),
static_cast<int>(ndhwc[4])};**/
std
::
array
<
ck
::
index_t
,
5
>
a_strides
=
{
static_cast
<
int
>
(
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
]),
static_cast
<
int
>
(
ncdhw
[
3
]
*
ncdhw
[
4
]),
static_cast
<
int
>
(
ncdhw
[
4
]),
1
,
static_cast
<
int
>
(
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
])};
std
::
array
<
ck
::
index_t
,
5
>
b_strides
=
{
static_cast
<
int
>
(
ndhwc
[
1
]
*
ndhwc
[
2
]
*
ndhwc
[
3
]
*
ndhwc
[
4
]),
static_cast
<
int
>
(
ndhwc
[
2
]
*
ndhwc
[
3
]
*
ndhwc
[
4
]),
static_cast
<
int
>
(
ndhwc
[
3
]
*
ndhwc
[
4
]),
static_cast
<
int
>
(
ndhwc
[
4
]),
1
};
ck
::
ranges
::
copy
(
ncdhw
,
ab_lengths
.
begin
());
auto
broadcastPermute
=
DeviceElementwisePermuteInstance
{};
auto
argument
=
broadcastPermute
.
MakeArgumentPointer
(
ab_lengths
,
{
a_strides
},
{
b_strides
},
input
,
output
,
PassThrough
{});
if
(
!
broadcastPermute
.
IsSupportedArgument
(
argument
.
get
()))
{
throw
std
::
runtime_error
(
"The runtime parameters seems not supported by the device instance, exiting!"
);
};
std
::
cout
<<
"A (ncdhw): "
<<
a
.
mDesc
<<
std
::
endl
;
std
::
cout
<<
"B (ndhwc): "
<<
b
.
mDesc
<<
std
::
endl
;
auto
broadcastPermute_invoker_ptr
=
broadcastPermute
.
MakeInvokerPointer
();
float
ave_time
=
broadcastPermute_invoker_ptr
->
Run
(
argument
.
get
(),
StreamConfig
{
nullptr
,
time_kernel
});
std
::
size_t
flop
=
std
::
size_t
(
2
)
*
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
];
std
::
size_t
num_btype
=
sizeof
(
ADataType
)
*
(
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
])
+
sizeof
(
BDataType
)
*
(
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
]);
float
tflops
=
static_cast
<
float
>
(
flop
)
/
1.E9
/
ave_time
;
float
gb_per_sec
=
num_btype
/
1.E6
/
ave_time
;
std
::
cout
<<
"Perf: "
<<
ave_time
<<
" ms, "
<<
tflops
<<
" TFlops, "
<<
gb_per_sec
<<
" GB/s"
<<
std
::
endl
;
bool
pass
=
true
;
if
(
do_verification
)
{
b_device_buf
.
FromDevice
(
b
.
mData
.
data
());
Tensor
<
BDataType
>
host_b
(
ndhwc
);
host_elementwise4D
(
host_b
,
a
,
PassThrough
{});
pass
&=
ck
::
utils
::
check_err
(
b
.
mData
,
host_b
.
mData
,
"Error: Incorrect results b"
,
1e-3
,
1e-3
);
}
return
pass
?
0
:
1
;
}
example/44_elementwise_permute/elementwise_permute_3d.cpp
0 → 100644
View file @
d305c079
#include <iostream>
#include <cstdlib>
#include "ck/ck.hpp"
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_elementwise_3d_impl.hpp"
#include "ck/library/utility/algorithm.hpp"
#include "ck/library/utility/check_err.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
using
F16
=
ck
::
half_t
;
using
F32
=
float
;
using
ADataType
=
F16
;
using
BDataType
=
F16
;
using
PassThrough
=
ck
::
tensor_operation
::
element_wise
::
PassThrough
;
using
DeviceElementwisePermuteInstance
=
ck
::
tensor_operation
::
device
::
DeviceElementwise3dImpl
<
ck
::
Tuple
<
ADataType
>
,
// InDataTypeTuple
ck
::
Tuple
<
BDataType
>
,
// OutDataTypeTuple
PassThrough
,
// ElementwiseOp
2
,
// NumDim_m, {N, C}
2
,
// NumDim_n, {H, W}
1
,
// NumDim_k, {D}
8
,
// MPerThread
8
,
// NPerThread
8
,
// KPerThread
ck
::
Sequence
<
8
>
,
// InScalarPerVectorSeq
ck
::
Sequence
<
4
>>
;
// OutScalarPerVectorSeq
template
<
typename
HostTensorA
,
typename
HostTensorB
,
typename
Functor
>
void
host_elementwise4D
(
HostTensorB
&
B_ndhwc
,
const
HostTensorA
&
A_ncdhw
,
Functor
functor
)
{
for
(
std
::
size_t
n
=
0
;
n
<
A_ncdhw
.
mDesc
.
GetLengths
()[
0
];
++
n
)
for
(
std
::
size_t
c
=
0
;
c
<
A_ncdhw
.
mDesc
.
GetLengths
()[
1
];
++
c
)
for
(
std
::
size_t
d
=
0
;
d
<
A_ncdhw
.
mDesc
.
GetLengths
()[
2
];
++
d
)
for
(
std
::
size_t
h
=
0
;
h
<
A_ncdhw
.
mDesc
.
GetLengths
()[
3
];
++
h
)
for
(
std
::
size_t
w
=
0
;
w
<
A_ncdhw
.
mDesc
.
GetLengths
()[
4
];
++
w
)
{
auto
a_val
=
A_ncdhw
(
n
,
c
,
d
,
h
,
w
);
functor
(
B_ndhwc
(
n
,
d
,
h
,
w
,
c
),
a_val
);
}
}
int
main
()
{
bool
do_verification
=
true
;
bool
time_kernel
=
true
;
const
int
N
=
4
;
const
int
C
=
16
;
const
int
H
=
32
;
const
int
W
=
5
;
const
int
D
=
16
;
std
::
vector
<
std
::
size_t
>
ncdhw
=
{
N
,
C
,
D
,
H
,
W
};
std
::
vector
<
std
::
size_t
>
ndhwc
=
{
N
,
D
,
H
,
W
,
C
};
Tensor
<
ADataType
>
a
(
ncdhw
);
Tensor
<
BDataType
>
b
(
ndhwc
);
a
.
GenerateTensorValue
(
GeneratorTensor_3
<
ADataType
>
{
0.0
,
1.0
});
DeviceMem
a_device_buf
(
sizeof
(
ADataType
)
*
a
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
b_device_buf
(
sizeof
(
BDataType
)
*
b
.
mDesc
.
GetElementSpaceSize
());
a_device_buf
.
ToDevice
(
a
.
mData
.
data
());
std
::
array
<
const
void
*
,
1
>
input
=
{
a_device_buf
.
GetDeviceBuffer
()};
std
::
array
<
void
*
,
1
>
output
=
{
b_device_buf
.
GetDeviceBuffer
()};
std
::
array
<
ck
::
index_t
,
5
>
ab_lengths
{
N
,
C
,
H
,
W
,
D
};
std
::
array
<
ck
::
index_t
,
5
>
a_strides
=
{
C
*
D
*
H
*
W
,
H
*
W
,
W
,
1
,
D
*
H
*
W
};
// N, C, D, H, W
std
::
array
<
ck
::
index_t
,
5
>
b_strides
=
{
C
*
H
*
W
*
D
,
H
*
W
*
D
,
W
*
D
,
D
,
1
};
// N, D, H, W, C
auto
broadcastPermute
=
DeviceElementwisePermuteInstance
{};
auto
argument
=
broadcastPermute
.
MakeArgumentPointer
(
ab_lengths
,
{
a_strides
},
{
b_strides
},
input
,
output
,
PassThrough
{});
if
(
!
broadcastPermute
.
IsSupportedArgument
(
argument
.
get
()))
{
throw
std
::
runtime_error
(
"The runtime parameters seems not supported by the device instance, exiting!"
);
};
std
::
cout
<<
"A (ncdhw): "
<<
a
.
mDesc
<<
std
::
endl
;
std
::
cout
<<
"B (ndhwc): "
<<
b
.
mDesc
<<
std
::
endl
;
auto
broadcastPermute_invoker_ptr
=
broadcastPermute
.
MakeInvokerPointer
();
float
ave_time
=
broadcastPermute_invoker_ptr
->
Run
(
argument
.
get
(),
StreamConfig
{
nullptr
,
time_kernel
});
std
::
size_t
flop
=
std
::
size_t
(
2
)
*
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
];
std
::
size_t
num_btype
=
sizeof
(
ADataType
)
*
(
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
])
+
sizeof
(
BDataType
)
*
(
ncdhw
[
0
]
*
ncdhw
[
1
]
*
ncdhw
[
2
]
*
ncdhw
[
3
]
*
ncdhw
[
4
]);
float
tflops
=
static_cast
<
float
>
(
flop
)
/
1.E9
/
ave_time
;
float
gb_per_sec
=
num_btype
/
1.E6
/
ave_time
;
std
::
cout
<<
"Perf: "
<<
ave_time
<<
" ms, "
<<
tflops
<<
" TFlops, "
<<
gb_per_sec
<<
" GB/s"
<<
std
::
endl
;
bool
pass
=
true
;
if
(
do_verification
)
{
b_device_buf
.
FromDevice
(
b
.
mData
.
data
());
Tensor
<
BDataType
>
host_b
(
ndhwc
);
host_elementwise4D
(
host_b
,
a
,
PassThrough
{});
pass
&=
ck
::
utils
::
check_err
(
b
.
mData
,
host_b
.
mData
,
"Error: Incorrect results b"
,
1e-3
,
1e-3
);
}
return
pass
?
0
:
1
;
}
example/44_elementwise_permute/elementwise_permute_4D_fp16.cpp
View file @
d305c079
...
...
@@ -19,13 +19,13 @@ using BDataType = F16;
using
PassThrough
=
ck
::
tensor_operation
::
element_wise
::
PassThrough
;
using
DeviceElementwisePermuteInstance
=
ck
::
tensor_operation
::
device
::
DeviceElementwiseImpl
<
ck
::
Tuple
<
ADataType
>
,
ck
::
Tuple
<
BDataType
>
,
PassThrough
,
4
,
8
,
ck
::
Sequence
<
8
>
,
ck
::
Sequence
<
1
>>
;
ck
::
tensor_operation
::
device
::
DeviceElementwiseImpl
<
ck
::
Tuple
<
ADataType
>
,
// InDataTypeTuple
ck
::
Tuple
<
BDataType
>
,
// OutDataTypeTuple
PassThrough
,
// Elementwise op
4
,
// NumDim
8
,
// MPerThread
ck
::
Sequence
<
8
>
,
// InScalarPerVectorSeq
ck
::
Sequence
<
1
>>
;
// OutScalarPerVectorSeq
template
<
typename
HostTensorA
,
typename
HostTensorB
,
typename
Functor
>
void
host_elementwise4D
(
HostTensorB
&
B_nhwc
,
const
HostTensorA
&
A_nchw
,
Functor
functor
)
...
...
@@ -99,7 +99,6 @@ int main()
std
::
cout
<<
"Perf: "
<<
ave_time
<<
" ms, "
<<
tflops
<<
" TFlops, "
<<
gb_per_sec
<<
" GB/s"
<<
std
::
endl
;
bool
pass
=
true
;
if
(
do_verification
)
...
...
example/44_elementwise_permute/elementwise_permute_4D_fp16_2d.cpp
View file @
d305c079
...
...
@@ -17,15 +17,15 @@ using BDataType = F16;
using
PassThrough
=
ck
::
tensor_operation
::
element_wise
::
PassThrough
;
using
DeviceElementwisePermuteInstance
=
ck
::
tensor_operation
::
device
::
DeviceElementwise2dImpl
<
ck
::
Tuple
<
ADataType
>
,
ck
::
Tuple
<
BDataType
>
,
PassThrough
,
3
,
// NumDim_M
1
,
// NumDim_N
8
,
8
,
ck
::
Sequence
<
8
>
,
ck
::
Sequence
<
8
>>
;
ck
::
tensor_operation
::
device
::
DeviceElementwise2dImpl
<
ck
::
Tuple
<
ADataType
>
,
// InDataTypeTuple
ck
::
Tuple
<
BDataType
>
,
// OutDataTypeTuple
PassThrough
,
// Elementwise op
3
,
// NumDim_M
1
,
// NumDim_N
1
,
// MPerThread
1
,
// NPerThread
ck
::
Sequence
<
1
>
,
// InScalarPerVectorSeq
ck
::
Sequence
<
1
>>
;
// OutScalarPerVectorSeq
template
<
typename
HostTensorA
,
typename
HostTensorB
,
typename
Functor
>
void
host_elementwise4D
(
HostTensorB
&
B_nhwc
,
...
...
@@ -53,12 +53,6 @@ int main()
const
int
H
=
32
;
const
int
W
=
1024
;
/**const int N = 120;
const int H = 32;
const int W = 64;
const int C = 128;**/
std
::
vector
<
std
::
size_t
>
nchw
=
{
N
,
C
,
H
,
W
};
std
::
vector
<
std
::
size_t
>
nhwc
=
{
N
,
H
,
W
,
C
};
...
...
@@ -71,7 +65,6 @@ int main()
DeviceMem
b_device_buf
(
sizeof
(
BDataType
)
*
b
.
mDesc
.
GetElementSpaceSize
());
a_device_buf
.
ToDevice
(
a
.
mData
.
data
());
// LogRangeAsType<float>(std::cout << "Tensor a : ", a.mData, ",") << std::endl;
std
::
array
<
const
void
*
,
1
>
input
=
{
a_device_buf
.
GetDeviceBuffer
()};
std
::
array
<
void
*
,
1
>
output
=
{
b_device_buf
.
GetDeviceBuffer
()};
...
...
@@ -115,13 +108,10 @@ int main()
if
(
do_verification
)
{
b_device_buf
.
FromDevice
(
b
.
mData
.
data
());
// LogRangeAsType<float>(std::cout << "Tensor b : ", b.mData, ",") << std::endl;
Tensor
<
BDataType
>
host_b
(
nhwc
);
host_elementwise4D
<
Tensor
<
ADataType
>
,
Tensor
<
BDataType
>
,
PassThrough
>
(
host_b
,
a
,
nchw
,
PassThrough
{});
// LogRangeAsType<float>(std::cout << "Host b : ", host_b.mData, ",") << std::endl;
pass
&=
ck
::
utils
::
check_err
(
b
.
mData
,
host_b
.
mData
,
"Error: Incorrect results b"
,
1e-3
,
1e-3
);
}
...
...
example/53_layernorm_bwd/CMakeLists.txt
0 → 100644
View file @
d305c079
add_example_executable
(
example_layernorm2d_bwd_fp16 layernorm2d_bwd_fp16.cpp
)
example/53_layernorm_bwd/layernorm2d_bwd_fp16.cpp
0 → 100644
View file @
d305c079
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include <iostream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <getopt.h>
#include "ck/library/utility/check_err.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_common_util.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
#include "ck/library/utility/literals.hpp"
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_bwd_gamma_beta_impl.hpp"
#include "ck/library/reference_tensor_operation/cpu/reference_layernorm_bwd.hpp"
using
DYDataType
=
ck
::
half_t
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
MeanInvStdDataType
=
float
;
using
DGammaDataType
=
ck
::
half_t
;
using
DBetaDataType
=
ck
::
half_t
;
using
DXDataType
=
ck
::
half_t
;
using
ComputeDataType
=
float
;
constexpr
int
Rank
=
2
;
constexpr
int
NumReduceDim
=
1
;
// Layernorm:
// Input shape
// dy: [M, N]
// x: [M, N]
// mean: [M, 1]
// inv_std: [M, 1]
// Output shape
// dgamma: [1, N]
// dbeta: [1, N]
// dgamma = reduce_sum(dy * (x - mean) * inv_std, axis=0)
// dbeta = reduce_sum(dy, axis=0)
// [CAUSION]
// In DeviceNormalizationBwdGammaBetaImpl, M is invarient dimension, K is reduced dimension
// Hence, M in this example and DeviceNormalizationBwdGammaBetaImpl is different
using
GammaBetaDeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationBwdGammaBetaImpl
<
DYDataType
,
XDataType
,
MeanInvStdDataType
,
ComputeDataType
,
DGammaDataType
,
DBetaDataType
,
Rank
,
NumReduceDim
,
256
,
// BlockSize
8
,
// ClusterInvarient
32
,
// ClusterReduce
8
,
// SliceInvarient
1
,
// SliceReduce
false
,
// IsDYFastestDimReduced
8
,
// DYSrcVectorSize
false
,
// IsXFastestDimReduced
8
,
// XSrcVectorSize
true
,
// IsMeanInvStdFastestDimReduced
1
,
// MeanInvStdSrcVectorSize
1
,
// DGammaDstVectorSize
1
>
;
// DBetaDstVectorSize
int
main
()
{
bool
time_kernel
=
false
;
ck
::
index_t
M
=
1024
;
ck
::
index_t
N
=
512
;
Tensor
<
DYDataType
>
dy
({
M
,
N
});
Tensor
<
XDataType
>
x
({
M
,
N
});
Tensor
<
GammaDataType
>
gamma
({
N
});
Tensor
<
MeanInvStdDataType
>
mean
({
M
});
Tensor
<
MeanInvStdDataType
>
inv_std
({
M
});
Tensor
<
DGammaDataType
>
dgamma
({
N
});
Tensor
<
DBetaDataType
>
dbeta
({
N
});
Tensor
<
DXDataType
>
dx
({
M
,
N
});
dy
.
GenerateTensorValue
(
GeneratorTensor_3
<
DYDataType
>
{
0.0
,
1.0
});
x
.
GenerateTensorValue
(
GeneratorTensor_3
<
XDataType
>
{
0.0
,
1.0
});
gamma
.
GenerateTensorValue
(
GeneratorTensor_3
<
GammaDataType
>
{
0.0
,
1.0
});
mean
.
GenerateTensorValue
(
GeneratorTensor_3
<
MeanInvStdDataType
>
{
0.0
,
1.0
});
inv_std
.
GenerateTensorValue
(
GeneratorTensor_3
<
MeanInvStdDataType
>
{
0.0
,
1.0
});
DeviceMem
dy_dev
(
sizeof
(
DYDataType
)
*
dy
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
x_dev
(
sizeof
(
XDataType
)
*
x
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
mean_dev
(
sizeof
(
MeanInvStdDataType
)
*
mean
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
inv_std_dev
(
sizeof
(
MeanInvStdDataType
)
*
inv_std
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
dgamma_dev
(
sizeof
(
DGammaDataType
)
*
dgamma
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
dbeta_dev
(
sizeof
(
DBetaDataType
)
*
dbeta
.
mDesc
.
GetElementSpaceSize
());
dy_dev
.
ToDevice
(
dy
.
mData
.
data
());
x_dev
.
ToDevice
(
x
.
mData
.
data
());
mean_dev
.
ToDevice
(
mean
.
mData
.
data
());
inv_std_dev
.
ToDevice
(
inv_std
.
mData
.
data
());
auto
gamma_beta_device_instance
=
GammaBetaDeviceInstance
{};
auto
gamma_beta_argument_ptr
=
gamma_beta_device_instance
.
MakeArgumentPointer
({
M
,
N
},
// inLengths
{
N
,
1
},
// dyStrides
{
N
,
1
},
// xStrides
{
1
,
0
},
// meanStrides
{
1
,
0
},
// invStdStrides
{
N
},
// outLengths
{
1
},
// dgammaStrides
{
1
},
// dbetaStrides
{
0
},
// reduceDims
dy_dev
.
GetDeviceBuffer
(),
x_dev
.
GetDeviceBuffer
(),
mean_dev
.
GetDeviceBuffer
(),
inv_std_dev
.
GetDeviceBuffer
(),
dgamma_dev
.
GetDeviceBuffer
(),
dbeta_dev
.
GetDeviceBuffer
());
if
(
!
gamma_beta_device_instance
.
IsSupportedArgument
(
gamma_beta_argument_ptr
.
get
()))
{
std
::
cout
<<
"The runtime parameters are not supported"
<<
std
::
endl
;
return
1
;
};
auto
gamma_beta_invoker_ptr
=
gamma_beta_device_instance
.
MakeInvokerPointer
();
gamma_beta_invoker_ptr
->
Run
(
gamma_beta_argument_ptr
.
get
(),
StreamConfig
{
nullptr
,
time_kernel
});
bool
pass
=
true
;
{
Tensor
<
DGammaDataType
>
host_dgamma
({
N
});
Tensor
<
DBetaDataType
>
host_dbeta
({
N
});
Tensor
<
DXDataType
>
host_dx
({
M
,
N
});
using
ReferenceInstance
=
ck
::
tensor_operation
::
host
::
ReferenceLayernormBwd
<
DYDataType
,
XDataType
,
GammaDataType
,
MeanInvStdDataType
,
DGammaDataType
,
DBetaDataType
,
DXDataType
,
ComputeDataType
>
;
ReferenceInstance
ref
;
auto
ref_argument
=
ref
.
MakeArgument
(
dy
,
x
,
gamma
,
mean
,
inv_std
,
host_dgamma
,
host_dbeta
,
host_dx
,
{
M
,
N
});
auto
ref_invoker
=
ref
.
MakeInvoker
();
ref_invoker
.
Run
(
ref_argument
);
dgamma_dev
.
FromDevice
(
dgamma
.
mData
.
data
());
dbeta_dev
.
FromDevice
(
dbeta
.
mData
.
data
());
pass
&=
ck
::
utils
::
check_err
(
dgamma
,
host_dgamma
,
"Error: Incorrect dgamma"
,
1e-3
,
1e-3
);
pass
&=
ck
::
utils
::
check_err
(
dbeta
,
host_dbeta
,
"Error: Incorrect dbeta"
,
1e-3
,
1e-3
);
}
return
(
pass
?
0
:
1
);
}
example/54_groupnorm_bwd/CMakeLists.txt
0 → 100644
View file @
d305c079
add_example_executable
(
example_groupnorm_bwd_fp16 groupnorm_bwd_fp16.cpp
)
example/54_groupnorm_bwd/groupnorm_bwd_fp16.cpp
0 → 100644
View file @
d305c079
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
#include <iostream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <getopt.h>
#include "ck/library/utility/check_err.hpp"
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_common_util.hpp"
#include "ck/library/utility/host_tensor.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
#include "ck/library/utility/literals.hpp"
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"
#include "ck/tensor_operation/gpu/device/impl/device_normalization_bwd_gamma_beta_impl.hpp"
#include "ck/library/reference_tensor_operation/cpu/reference_groupnorm_bwd.hpp"
using
DYDataType
=
ck
::
half_t
;
using
XDataType
=
ck
::
half_t
;
using
GammaDataType
=
ck
::
half_t
;
using
MeanInvStdDataType
=
float
;
using
DGammaDataType
=
ck
::
half_t
;
using
DBetaDataType
=
ck
::
half_t
;
using
DXDataType
=
ck
::
half_t
;
using
ComputeDataType
=
float
;
constexpr
int
Rank
=
5
;
constexpr
int
NumReduceDim
=
3
;
// Grouprnorm
// kernel: M , K
// dy: N, H, W, G, C -> G * C, N * H * W
// x: N, H, W, G, C -> G * C, N * H * W
// mean: N, 1, 1, G, 1 -> G * 1, N * 1 * 1
// rstd: N, 1, 1, G, 1 -> G * 1, N * 1 * 1
// dgamma: 1, 1, 1, G, C -> G * C
// dbeta: 1, 1, 1, G, C -> G * C
// reduced axis: 0, 1, 2
using
GammaBetaDeviceInstance
=
ck
::
tensor_operation
::
device
::
DeviceNormalizationBwdGammaBetaImpl
<
DYDataType
,
XDataType
,
MeanInvStdDataType
,
ComputeDataType
,
DGammaDataType
,
DBetaDataType
,
Rank
,
NumReduceDim
,
256
,
// BlockSize
8
,
// ClusterInvarient
32
,
// ClusterReduce
8
,
// SliceInvarient
1
,
// SliceReduce
false
,
// IsDYFastestDimReduced
8
,
// DYSrcVectorSize
false
,
// IsXFastestDimReduced
8
,
// XSrcVectorSize
false
,
// IsMeanInvStdFastestDimReduced
1
,
// MeanInvStdSrcVectorSize
1
,
// DGammaDstVectorSize
1
>
;
// DBetaDstVectorSize
int
main
()
{
bool
time_kernel
=
false
;
ck
::
index_t
N
=
16
;
ck
::
index_t
H
=
16
;
ck
::
index_t
W
=
16
;
ck
::
index_t
G
=
32
;
ck
::
index_t
C
=
64
;
Tensor
<
DYDataType
>
dy
({
N
,
H
,
W
,
G
,
C
});
Tensor
<
XDataType
>
x
({
N
,
H
,
W
,
G
,
C
});
Tensor
<
GammaDataType
>
gamma
({
G
,
C
});
Tensor
<
MeanInvStdDataType
>
mean
({
N
,
G
});
Tensor
<
MeanInvStdDataType
>
inv_std
({
N
,
G
});
Tensor
<
DGammaDataType
>
dgamma
({
G
,
C
});
Tensor
<
DBetaDataType
>
dbeta
({
G
,
C
});
Tensor
<
DXDataType
>
dx
({
N
,
H
,
W
,
G
,
C
});
dy
.
GenerateTensorValue
(
GeneratorTensor_3
<
DYDataType
>
{
0.0
,
1.0
});
x
.
GenerateTensorValue
(
GeneratorTensor_3
<
XDataType
>
{
0.0
,
1.0
});
gamma
.
GenerateTensorValue
(
GeneratorTensor_3
<
GammaDataType
>
{
0.0
,
1.0
});
mean
.
GenerateTensorValue
(
GeneratorTensor_3
<
MeanInvStdDataType
>
{
0.0
,
1.0
});
inv_std
.
GenerateTensorValue
(
GeneratorTensor_3
<
MeanInvStdDataType
>
{
0.0
,
1.0
});
DeviceMem
dy_dev
(
sizeof
(
DYDataType
)
*
dy
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
x_dev
(
sizeof
(
XDataType
)
*
x
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
mean_dev
(
sizeof
(
MeanInvStdDataType
)
*
mean
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
inv_std_dev
(
sizeof
(
MeanInvStdDataType
)
*
inv_std
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
dgamma_dev
(
sizeof
(
DGammaDataType
)
*
dgamma
.
mDesc
.
GetElementSpaceSize
());
DeviceMem
dbeta_dev
(
sizeof
(
DBetaDataType
)
*
dbeta
.
mDesc
.
GetElementSpaceSize
());
dy_dev
.
ToDevice
(
dy
.
mData
.
data
());
x_dev
.
ToDevice
(
x
.
mData
.
data
());
mean_dev
.
ToDevice
(
mean
.
mData
.
data
());
inv_std_dev
.
ToDevice
(
inv_std
.
mData
.
data
());
std
::
vector
<
ck
::
index_t
>
dyStrides
{
dy
.
mDesc
.
GetStrides
().
begin
(),
dy
.
mDesc
.
GetStrides
().
end
()};
std
::
vector
<
ck
::
index_t
>
xStrides
{
x
.
mDesc
.
GetStrides
().
begin
(),
x
.
mDesc
.
GetStrides
().
end
()};
std
::
vector
<
ck
::
index_t
>
meanStrides
=
{
G
,
0
,
0
,
1
,
0
};
std
::
vector
<
ck
::
index_t
>
invStdStrides
=
{
G
,
0
,
0
,
1
,
0
};
auto
gamma_beta_device_instance
=
GammaBetaDeviceInstance
{};
auto
gamma_beta_argument_ptr
=
gamma_beta_device_instance
.
MakeArgumentPointer
({
N
,
H
,
W
,
G
,
C
},
// inLengths
dyStrides
,
// dyStrides
xStrides
,
// xStrides
meanStrides
,
// meanStrides
invStdStrides
,
// invStdStrides
{
G
,
C
},
// outLengths
{
C
,
1
},
// dgammaStrides
{
C
,
1
},
// dbetaStrides
{
0
,
1
,
2
},
// reduceDims
dy_dev
.
GetDeviceBuffer
(),
x_dev
.
GetDeviceBuffer
(),
mean_dev
.
GetDeviceBuffer
(),
inv_std_dev
.
GetDeviceBuffer
(),
dgamma_dev
.
GetDeviceBuffer
(),
dbeta_dev
.
GetDeviceBuffer
());
if
(
!
gamma_beta_device_instance
.
IsSupportedArgument
(
gamma_beta_argument_ptr
.
get
()))
{
std
::
cout
<<
"The runtime parameters are not supported"
<<
std
::
endl
;
return
1
;
};
auto
gamma_beta_invoker_ptr
=
gamma_beta_device_instance
.
MakeInvokerPointer
();
gamma_beta_invoker_ptr
->
Run
(
gamma_beta_argument_ptr
.
get
(),
StreamConfig
{
nullptr
,
time_kernel
});
bool
pass
=
true
;
{
Tensor
<
DGammaDataType
>
host_dgamma
({
G
,
C
});
Tensor
<
DBetaDataType
>
host_dbeta
({
G
,
C
});
Tensor
<
DXDataType
>
host_dx
({
N
,
H
,
W
,
G
,
C
});
using
ReferenceInstance
=
ck
::
tensor_operation
::
host
::
ReferenceGroupnormBwd
<
DYDataType
,
XDataType
,
GammaDataType
,
MeanInvStdDataType
,
DGammaDataType
,
DBetaDataType
,
DXDataType
,
ComputeDataType
>
;
ReferenceInstance
ref
;
auto
ref_argument
=
ref
.
MakeArgument
(
dy
,
x
,
gamma
,
mean
,
inv_std
,
host_dgamma
,
host_dbeta
,
host_dx
,
{
N
,
H
,
W
,
G
,
C
});
auto
ref_invoker
=
ref
.
MakeInvoker
();
ref_invoker
.
Run
(
ref_argument
);
dgamma_dev
.
FromDevice
(
dgamma
.
mData
.
data
());
dbeta_dev
.
FromDevice
(
dbeta
.
mData
.
data
());
pass
&=
ck
::
utils
::
check_err
(
dgamma
,
host_dgamma
,
"Error: Incorrect dgamma"
,
1e-3
,
1e-3
);
pass
&=
ck
::
utils
::
check_err
(
dbeta
,
host_dbeta
,
"Error: Incorrect dbeta"
,
1e-3
,
1e-3
);
}
return
(
pass
?
0
:
1
);
}
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