Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Xiaomi
Mace
提交
4ebaa508
Mace
项目概览
Xiaomi
/
Mace
通知
106
Star
40
Fork
27
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Mace
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
4ebaa508
编写于
4月 08, 2018
作者:
Y
yejianwu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add unit test for out_of_range_check
上级
857e6e60
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
152 addition
and
0 deletion
+152
-0
mace/kernels/BUILD
mace/kernels/BUILD
+2
-0
mace/kernels/opencl/out_of_range_check_test.cc
mace/kernels/opencl/out_of_range_check_test.cc
+150
-0
未找到文件。
mace/kernels/BUILD
浏览文件 @
4ebaa508
...
...
@@ -20,6 +20,7 @@ cc_library(
exclude
=
[
"*_test.cc"
,
"arm/*_test.cc"
,
"opencl/*_test.cc"
,
],
),
hdrs
=
glob
([
...
...
@@ -42,6 +43,7 @@ cc_test(
[
"*_test.cc"
,
"arm/*_test.cc"
,
"opencl/*_test.cc"
,
],
),
copts
=
if_openmp_enabled
([
"-fopenmp"
])
+
if_neon_enabled
([
"-DMACE_ENABLE_NEON"
]),
...
...
mace/kernels/opencl/out_of_range_check_test.cc
0 → 100644
浏览文件 @
4ebaa508
//
// Copyright (c) 2017 XiaoMi All rights reserved.
//
#include <vector>
#include "gtest/gtest.h"
#include "mace/core/runtime/opencl/opencl_runtime.h"
#include "mace/core/tensor.h"
#include "mace/core/workspace.h"
#include "mace/kernels/opencl/helper.h"
#include "mace/utils/tuner.h"
#include "mace/utils/utils.h"
namespace
mace
{
namespace
kernels
{
namespace
{
const
bool
BufferToImageOpImpl
(
Tensor
*
buffer
,
Tensor
*
image
,
std
::
vector
<
size_t
>
&
image_shape
)
{
std
::
unique_ptr
<
BufferBase
>
kernel_error
;
uint32_t
gws
[
2
]
=
{
static_cast
<
uint32_t
>
(
image_shape
[
0
]),
static_cast
<
uint32_t
>
(
image_shape
[
1
])};
auto
runtime
=
OpenCLRuntime
::
Global
();
std
::
string
kernel_name
=
"in_out_buffer_to_image"
;
std
::
string
obfuscated_kernel_name
=
MACE_OBFUSCATE_SYMBOL
(
kernel_name
);
std
::
set
<
std
::
string
>
built_options
;
std
::
stringstream
kernel_name_ss
;
kernel_name_ss
<<
"-D"
<<
kernel_name
<<
"="
<<
obfuscated_kernel_name
;
built_options
.
emplace
(
kernel_name_ss
.
str
());
if
(
runtime
->
IsNonUniformWorkgroupsSupported
())
{
built_options
.
emplace
(
"-DNON_UNIFORM_WORK_GROUP"
);
}
if
(
buffer
->
dtype
()
==
image
->
dtype
())
{
built_options
.
emplace
(
"-DDATA_TYPE="
+
DtToCLDt
(
DataTypeToEnum
<
float
>::
value
));
built_options
.
emplace
(
"-DCMD_DATA_TYPE="
+
DtToCLCMDDt
(
DataTypeToEnum
<
float
>::
value
));
}
else
{
built_options
.
emplace
(
"-DDATA_TYPE="
+
DtToUpstreamCLDt
(
DataTypeToEnum
<
float
>::
value
));
built_options
.
emplace
(
"-DCMD_DATA_TYPE="
+
DtToUpstreamCLCMDDt
(
DataTypeToEnum
<
float
>::
value
));
}
if
(
runtime
->
IsOutOfRangeCheckEnabled
())
{
built_options
.
emplace
(
"-DOUT_OF_RANGE_CHECK"
);
kernel_error
=
std
::
move
(
std
::
unique_ptr
<
Buffer
>
(
new
Buffer
(
GetDeviceAllocator
(
DeviceType
::
OPENCL
),
1
)));
kernel_error
->
Map
(
nullptr
);
*
(
kernel_error
->
mutable_data
<
char
>
())
=
'0'
;
kernel_error
->
UnMap
();
}
auto
b2f_kernel
=
runtime
->
BuildKernel
(
"buffer_to_image"
,
obfuscated_kernel_name
,
built_options
);
uint32_t
idx
=
0
;
if
(
runtime
->
IsOutOfRangeCheckEnabled
())
{
b2f_kernel
.
setArg
(
idx
++
,
*
(
static_cast
<
cl
::
Buffer
*>
(
kernel_error
->
buffer
())));
}
if
(
!
runtime
->
IsNonUniformWorkgroupsSupported
())
{
b2f_kernel
.
setArg
(
idx
++
,
gws
[
0
]);
b2f_kernel
.
setArg
(
idx
++
,
gws
[
1
]);
}
b2f_kernel
.
setArg
(
idx
++
,
*
(
buffer
->
opencl_buffer
()));
MACE_CHECK
(
buffer
->
buffer_offset
()
%
GetEnumTypeSize
(
buffer
->
dtype
())
==
0
,
"buffer offset not aligned"
);
b2f_kernel
.
setArg
(
idx
++
,
static_cast
<
uint32_t
>
(
buffer
->
buffer_offset
()
/
GetEnumTypeSize
(
buffer
->
dtype
())));
b2f_kernel
.
setArg
(
idx
++
,
static_cast
<
uint32_t
>
(
buffer
->
dim
(
1
)));
b2f_kernel
.
setArg
(
idx
++
,
static_cast
<
uint32_t
>
(
buffer
->
dim
(
2
)));
b2f_kernel
.
setArg
(
idx
++
,
static_cast
<
uint32_t
>
(
buffer
->
dim
(
3
)));
b2f_kernel
.
setArg
(
idx
++
,
*
(
image
->
opencl_image
()));
const
uint32_t
kwg_size
=
static_cast
<
uint32_t
>
(
runtime
->
GetKernelMaxWorkGroupSize
(
b2f_kernel
));
const
std
::
vector
<
uint32_t
>
lws
=
{
16
,
kwg_size
/
16
};
cl
::
Event
event
;
cl_int
error
;
if
(
runtime
->
IsNonUniformWorkgroupsSupported
())
{
error
=
runtime
->
command_queue
().
enqueueNDRangeKernel
(
b2f_kernel
,
cl
::
NullRange
,
cl
::
NDRange
(
gws
[
0
],
gws
[
1
]),
cl
::
NDRange
(
lws
[
0
],
lws
[
1
]),
nullptr
,
&
event
);
}
else
{
std
::
vector
<
uint32_t
>
roundup_gws
(
lws
.
size
());
for
(
size_t
i
=
0
;
i
<
lws
.
size
();
++
i
)
{
roundup_gws
[
i
]
=
RoundUp
(
gws
[
i
],
lws
[
i
]);
}
error
=
runtime
->
command_queue
().
enqueueNDRangeKernel
(
b2f_kernel
,
cl
::
NullRange
,
cl
::
NDRange
(
roundup_gws
[
0
],
roundup_gws
[
1
]),
cl
::
NDRange
(
lws
[
0
],
lws
[
1
]),
nullptr
,
&
event
);
}
MACE_CHECK_CL_SUCCESS
(
error
);
runtime
->
command_queue
().
finish
();
bool
is_out_of_range
=
false
;
if
(
runtime
->
IsOutOfRangeCheckEnabled
())
{
kernel_error
->
Map
(
nullptr
);
is_out_of_range
=
*
(
kernel_error
->
mutable_data
<
char
>
())
==
'1'
?
true
:
false
;
kernel_error
->
UnMap
();
}
return
is_out_of_range
;
}
}
// namespace
class
OutOfRangeCheckTest
:
public
::
testing
::
Test
{
protected:
virtual
void
SetUp
()
{
setenv
(
"MACE_OUT_OF_RANGE_CHECK"
,
"1"
,
1
);
}
};
TEST
(
OutOfRangeCheckTest
,
RandomTest
)
{
static
unsigned
int
seed
=
time
(
NULL
);
index_t
batch
=
11
+
rand_r
(
&
seed
)
%
10
;
index_t
height
=
12
+
rand_r
(
&
seed
)
%
100
;
index_t
width
=
13
+
rand_r
(
&
seed
)
%
100
;
index_t
channels
=
14
+
rand_r
(
&
seed
)
%
50
;
std
::
vector
<
index_t
>
buffer_shape
=
{
batch
,
height
,
width
,
channels
};
Workspace
ws
;
Tensor
*
buffer
=
ws
.
CreateTensor
(
"Buffer"
,
GetDeviceAllocator
(
DeviceType
::
OPENCL
),
DataTypeToEnum
<
float
>::
v
());
buffer
->
Resize
(
buffer_shape
);
std
::
vector
<
size_t
>
image_shape
;
Tensor
*
image
=
ws
.
CreateTensor
(
"Image"
,
GetDeviceAllocator
(
DeviceType
::
OPENCL
),
DataTypeToEnum
<
float
>::
v
());
CalImage2DShape
(
buffer
->
shape
(),
IN_OUT_CHANNEL
,
&
image_shape
);
image
->
ResizeImage
(
buffer
->
shape
(),
image_shape
);
ASSERT_FALSE
(
BufferToImageOpImpl
(
buffer
,
image
,
image_shape
));
std
::
vector
<
size_t
>
overflow_image_shape
=
image_shape
;
for
(
int
i
=
0
;
i
<
overflow_image_shape
.
size
();
++
i
)
{
overflow_image_shape
[
i
]
+=
1
;
}
ASSERT_TRUE
(
BufferToImageOpImpl
(
buffer
,
image
,
overflow_image_shape
));
}
}
// namespace kernels
}
// namespace mace
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录