Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
4dbbab5e
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
337
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
4dbbab5e
编写于
1月 22, 2019
作者:
Z
zhaojiaying01
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add logical_and、logical_or、logical_not、logical_xor op
上级
79039244
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
816 addition
and
1 deletion
+816
-1
src/common/types.cpp
src/common/types.cpp
+9
-1
src/operators/kernel/arm/logical_kernel.cpp
src/operators/kernel/arm/logical_kernel.cpp
+121
-0
src/operators/kernel/logical_kernel.h
src/operators/kernel/logical_kernel.h
+40
-0
src/operators/logical_op.cpp
src/operators/logical_op.cpp
+69
-0
src/operators/logical_op.h
src/operators/logical_op.h
+42
-0
src/operators/op_param.h
src/operators/op_param.h
+97
-0
test/CMakeLists.txt
test/CMakeLists.txt
+16
-0
test/operators/test_gru_unit_op.cpp
test/operators/test_gru_unit_op.cpp
+56
-0
test/operators/test_logical_and_op.cpp
test/operators/test_logical_and_op.cpp
+84
-0
test/operators/test_logical_not_op.cpp
test/operators/test_logical_not_op.cpp
+76
-0
test/operators/test_logical_or_op.cpp
test/operators/test_logical_or_op.cpp
+84
-0
test/operators/test_logical_xor_op.cpp
test/operators/test_logical_xor_op.cpp
+86
-0
test/test_helper.h
test/test_helper.h
+20
-0
tools/op.cmake
tools/op.cmake
+16
-0
未找到文件。
src/common/types.cpp
浏览文件 @
4dbbab5e
...
...
@@ -77,6 +77,10 @@ const char *G_OP_TYPE_CAST = "cast";
const
char
*
G_OP_TYPE_LOG
=
"log"
;
const
char
*
G_OP_TYPE_LOD_RESET
=
"lod_reset"
;
const
char
*
G_OP_TYPE_LESS_THAN
=
"less_than"
;
const
char
*
G_OP_TYPE_LOGICAL_AND
=
"logical_and"
;
const
char
*
G_OP_TYPE_LOGICAL_OR
=
"logical_or"
;
const
char
*
G_OP_TYPE_LOGICAL_NOT
=
"logical_not"
;
const
char
*
G_OP_TYPE_LOGICAL_XOR
=
"logical_xor"
;
const
char
*
G_OP_TYPE_QUANTIZE
=
"quantize"
;
const
char
*
G_OP_TYPE_DEQUANTIZE
=
"dequantize"
;
...
...
@@ -181,5 +185,9 @@ std::unordered_map<
{
G_OP_TYPE_NORM
,
{{
"X"
},
{
"Out"
,
"Norm"
}}},
{
G_OP_TYPE_LOG
,
{{
"X"
},
{
"Out"
}}},
{
G_OP_TYPE_LOD_RESET
,
{{
"X"
,
"Y"
},
{
"Out"
}}},
{
G_OP_TYPE_LESS_THAN
,
{{
"X"
,
"Y"
},
{
"Out"
}}}};
{
G_OP_TYPE_LESS_THAN
,
{{
"X"
,
"Y"
},
{
"Out"
}}},
{
G_OP_TYPE_LOGICAL_AND
,
{{
"X"
,
"Y"
},
{
"Out"
}}},
{
G_OP_TYPE_LOGICAL_OR
,
{{
"X"
,
"Y"
},
{
"Out"
}}},
{
G_OP_TYPE_LOGICAL_XOR
,
{{
"X"
,
"Y"
},
{
"Out"
}}},
{
G_OP_TYPE_LOGICAL_NOT
,
{{
"X"
},
{
"Out"
}}}};
}
// namespace paddle_mobile
src/operators/kernel/arm/logical_kernel.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "operators/kernel/logical_kernel.h"
namespace
paddle_mobile
{
namespace
operators
{
template
<
typename
T
>
struct
LogicalAndFunctor
{
bool
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
return
a
&&
b
;
}
};
template
<
typename
T
>
struct
LogicalOrFunctor
{
bool
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
return
a
||
b
;
}
};
template
<
typename
T
>
struct
LogicalNotFunctor
{
bool
operator
()(
const
T
&
a
)
const
{
return
!
a
;
}
};
template
<
typename
T
>
struct
LogicalXorFunctor
{
bool
operator
()(
const
T
&
a
,
const
T
&
b
)
const
{
return
(
a
||
b
)
&&
!
(
a
&&
b
);
}
};
template
<
typename
T
,
typename
Functor
>
void
UnaryLogicalCompute
(
const
Tensor
*
inputX
,
Tensor
*
output
)
{
Functor
func
;
std
::
transform
(
inputX
->
data
<
T
>
(),
inputX
->
data
<
T
>
()
+
inputX
->
numel
(),
output
->
data
<
T
>
(),
func
);
}
template
<
typename
T
,
typename
Functor
>
void
BinaryLogicalCompute
(
const
Tensor
*
inputX
,
const
Tensor
*
inputY
,
Tensor
*
output
)
{
Functor
func
;
std
::
transform
(
inputX
->
data
<
T
>
(),
inputX
->
data
<
T
>
()
+
inputX
->
numel
(),
inputY
->
data
<
T
>
(),
output
->
data
<
T
>
(),
func
);
}
#ifdef LOGICAL_AND_OP
template
<
>
bool
LogicalAndKernel
<
CPU
,
float
>::
Init
(
LogicalAndParam
<
CPU
>*
param
)
{
return
true
;
}
template
<
>
void
LogicalAndKernel
<
CPU
,
float
>::
Compute
(
const
LogicalAndParam
<
CPU
>&
param
)
{
auto
*
inputX
=
param
.
InputX
();
auto
*
inputY
=
param
.
InputY
();
auto
*
out
=
param
.
Out
();
out
->
mutable_data
<
bool
>
();
BinaryLogicalCompute
<
bool
,
LogicalAndFunctor
<
bool
>>
(
inputX
,
inputY
,
out
);
}
#endif
#ifdef LOGICAL_OR_OP
template
<
>
bool
LogicalOrKernel
<
CPU
,
float
>::
Init
(
LogicalOrParam
<
CPU
>*
param
)
{
return
true
;
}
template
<
>
void
LogicalOrKernel
<
CPU
,
float
>::
Compute
(
const
LogicalOrParam
<
CPU
>&
param
)
{
auto
*
inputX
=
param
.
InputX
();
auto
*
inputY
=
param
.
InputY
();
auto
*
out
=
param
.
Out
();
out
->
mutable_data
<
bool
>
();
BinaryLogicalCompute
<
bool
,
LogicalOrFunctor
<
bool
>>
(
inputX
,
inputY
,
out
);
}
#endif
#ifdef LOGICAL_NOT_OP
template
<
>
bool
LogicalNotKernel
<
CPU
,
float
>::
Init
(
LogicalNotParam
<
CPU
>*
param
)
{
return
true
;
}
template
<
>
void
LogicalNotKernel
<
CPU
,
float
>::
Compute
(
const
LogicalNotParam
<
CPU
>&
param
)
{
auto
*
inputX
=
param
.
InputX
();
auto
*
out
=
param
.
Out
();
out
->
mutable_data
<
bool
>
();
UnaryLogicalCompute
<
bool
,
LogicalNotFunctor
<
bool
>>
(
inputX
,
out
);
}
#endif
#ifdef LOGICAL_XOR_OP
template
<
>
bool
LogicalXorKernel
<
CPU
,
float
>::
Init
(
LogicalXorParam
<
CPU
>*
param
)
{
return
true
;
}
template
<
>
void
LogicalXorKernel
<
CPU
,
float
>::
Compute
(
const
LogicalXorParam
<
CPU
>&
param
)
{
auto
*
inputX
=
param
.
InputX
();
auto
*
inputY
=
param
.
InputY
();
auto
*
out
=
param
.
Out
();
out
->
mutable_data
<
bool
>
();
BinaryLogicalCompute
<
bool
,
LogicalXorFunctor
<
bool
>>
(
inputX
,
inputY
,
out
);
}
#endif
}
// namespace operators
}
// namespace paddle_mobile
src/operators/kernel/logical_kernel.h
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include "framework/operator.h"
#include "operators/op_param.h"
namespace
paddle_mobile
{
namespace
operators
{
#ifdef LOGICAL_AND_OP
DECLARE_KERNEL
(
LogicalAnd
,
LogicalAndParam
);
#endif
#ifdef LOGICAL_OR_OP
DECLARE_KERNEL
(
LogicalOr
,
LogicalOrParam
);
#endif
#ifdef LOGICAL_NOT_OP
DECLARE_KERNEL
(
LogicalNot
,
LogicalNotParam
);
#endif
#ifdef LOGICAL_XOR_OP
DECLARE_KERNEL
(
LogicalXor
,
LogicalXorParam
);
#endif
}
// namespace operators
}
// namespace paddle_mobile
src/operators/logical_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "operators/logical_op.h"
namespace
paddle_mobile
{
namespace
operators
{
#define DEFINE_LOGICAL_INFERSHAPE(OpName) \
template <typename Dtype, typename T> \
void OpName##Op<Dtype, T>::InferShape() const { \
const auto &input_dims = this->param_.InputX()->dims(); \
this->param_.Out()->Resize(input_dims); \
}
#ifdef LOGICAL_AND_OP
DEFINE_LOGICAL_INFERSHAPE
(
LogicalAnd
);
#endif // TLOGICAL_AND_OP
#ifdef LOGICAL_OR_OP
DEFINE_LOGICAL_INFERSHAPE
(
LogicalOr
);
#endif // TLOGICAL_OR_OP
#ifdef LOGICAL_NOT_OP
DEFINE_LOGICAL_INFERSHAPE
(
LogicalNot
);
#endif // LOGICAL_NOT_OP
#ifdef LOGICAL_XOR_OP
DEFINE_LOGICAL_INFERSHAPE
(
LogicalXor
);
#endif // TLOGICAL_XOR_OP
}
// namespace operators
}
// namespace paddle_mobile
namespace
ops
=
paddle_mobile
::
operators
;
#ifdef LOGICAL_AND_OP
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU
(
logical_and
,
ops
::
LogicalAndOp
);
#endif
#endif // LOGICAL_AND_OP
#ifdef LOGICAL_OR_OP
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU
(
logical_or
,
ops
::
LogicalOrOp
);
#endif
#endif // LOGICAL_OR_OP
#ifdef LOGICAL_NOT_OP
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU
(
logical_not
,
ops
::
LogicalNotOp
);
#endif
#endif // LOGICAL_NOT_OP
#ifdef LOGICAL_XOR_OP
#ifdef PADDLE_MOBILE_CPU
REGISTER_OPERATOR_CPU
(
logical_xor
,
ops
::
LogicalXorOp
);
#endif
#endif // LOGICAL_XOR_OP
src/operators/logical_op.h
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <string>
#include "framework/operator.h"
#include "operators/kernel/logical_kernel.h"
#include "operators/op_param.h"
namespace
paddle_mobile
{
namespace
operators
{
#ifdef LOGICAL_AND_OP
DECLARE_OPERATOR
(
LogicalAnd
,
LogicalAndParam
,
LogicalAndKernel
);
#endif
#ifdef LOGICAL_OR_OP
DECLARE_OPERATOR
(
LogicalOr
,
LogicalOrParam
,
LogicalOrKernel
);
#endif
#ifdef LOGICAL_NOT_OP
DECLARE_OPERATOR
(
LogicalNot
,
LogicalNotParam
,
LogicalNotKernel
);
#endif
#ifdef LOGICAL_XOR_OP
DECLARE_OPERATOR
(
LogicalXor
,
LogicalXorParam
,
LogicalXorKernel
);
#endif
}
// namespace operators
}
// namespace paddle_mobile
src/operators/op_param.h
浏览文件 @
4dbbab5e
...
...
@@ -2942,5 +2942,102 @@ class CompareParam : public OpParam {
};
#endif // LESS_THAN_OP
#ifdef LOGICAL_AND_OP
template
<
typename
Dtype
>
class
LogicalAndParam
:
public
OpParam
{
typedef
typename
DtypeTensorTrait
<
Dtype
>::
gtype
GType
;
typedef
typename
DtypeTensorTrait
<
Dtype
>::
rtype
RType
;
public:
LogicalAndParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
GType
>
(
inputs
,
scope
);
input_y_
=
InputYFrom
<
GType
>
(
inputs
,
scope
);
output_
=
OutFrom
<
GType
>
(
outputs
,
scope
);
}
const
GType
*
InputX
()
const
{
return
input_x_
;
}
const
GType
*
InputY
()
const
{
return
input_y_
;
}
GType
*
Out
()
const
{
return
output_
;
}
public:
GType
*
input_x_
;
GType
*
input_y_
;
GType
*
output_
;
};
#endif // LOGICAL_AND_OP
#ifdef LOGICAL_OR_OP
template
<
typename
Dtype
>
class
LogicalOrParam
:
public
OpParam
{
typedef
typename
DtypeTensorTrait
<
Dtype
>::
gtype
GType
;
typedef
typename
DtypeTensorTrait
<
Dtype
>::
rtype
RType
;
public:
LogicalOrParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
GType
>
(
inputs
,
scope
);
input_y_
=
InputYFrom
<
GType
>
(
inputs
,
scope
);
output_
=
OutFrom
<
GType
>
(
outputs
,
scope
);
}
const
GType
*
InputX
()
const
{
return
input_x_
;
}
const
GType
*
InputY
()
const
{
return
input_y_
;
}
GType
*
Out
()
const
{
return
output_
;
}
public:
GType
*
input_x_
;
GType
*
input_y_
;
GType
*
output_
;
};
#endif // LOGICAL_OR_OP
#ifdef LOGICAL_NOT_OP
template
<
typename
Dtype
>
class
LogicalNotParam
:
public
OpParam
{
typedef
typename
DtypeTensorTrait
<
Dtype
>::
gtype
GType
;
typedef
typename
DtypeTensorTrait
<
Dtype
>::
rtype
RType
;
public:
LogicalNotParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
GType
>
(
inputs
,
scope
);
output_
=
OutFrom
<
GType
>
(
outputs
,
scope
);
}
const
GType
*
InputX
()
const
{
return
input_x_
;
}
GType
*
Out
()
const
{
return
output_
;
}
public:
GType
*
input_x_
;
GType
*
output_
;
};
#endif // LOGICAL_NOT_OP
#ifdef LOGICAL_XOR_OP
template
<
typename
Dtype
>
class
LogicalXorParam
:
public
OpParam
{
typedef
typename
DtypeTensorTrait
<
Dtype
>::
gtype
GType
;
typedef
typename
DtypeTensorTrait
<
Dtype
>::
rtype
RType
;
public:
LogicalXorParam
(
const
VariableNameMap
&
inputs
,
const
VariableNameMap
&
outputs
,
const
AttributeMap
&
attrs
,
const
Scope
&
scope
)
{
input_x_
=
InputXFrom
<
GType
>
(
inputs
,
scope
);
input_y_
=
InputYFrom
<
GType
>
(
inputs
,
scope
);
output_
=
OutFrom
<
GType
>
(
outputs
,
scope
);
}
const
GType
*
InputX
()
const
{
return
input_x_
;
}
const
GType
*
InputY
()
const
{
return
input_y_
;
}
GType
*
Out
()
const
{
return
output_
;
}
public:
GType
*
input_x_
;
GType
*
input_y_
;
GType
*
output_
;
};
#endif // LOGICAL_XOR_OP
}
// namespace operators
}
// namespace paddle_mobile
test/CMakeLists.txt
浏览文件 @
4dbbab5e
...
...
@@ -421,4 +421,20 @@ if (NOT FOUND_MATCH)
ADD_EXECUTABLE
(
test-vgg16ssd net/test_vgg16ssd.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-vgg16ssd paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-logical-and-op operators/test_logical_and_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-logical-and-op paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-logical-or-op operators/test_logical_or_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-logical-or-op paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-logical-not-op operators/test_logical_not_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-logical-not-op paddle-mobile
)
# gen test
ADD_EXECUTABLE
(
test-logical-xor-op operators/test_logical_xor_op.cpp test_helper.h test_include.h
)
target_link_libraries
(
test-logical-xor-op paddle-mobile
)
endif
()
test/operators/test_gru_unit_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "../test_include.h"
#include "operators/gru_unit_op.h"
namespace
paddle_mobile
{
int
TesGruUnitOp
(
const
std
::
vector
<
int
>
input_shape
,
int
axis
)
{
framework
::
DDim
x_dims
=
framework
::
make_ddim
(
input_shape
);
framework
::
DDim
norm_dims
=
x_dims
;
if
(
axis
<
0
)
{
axis
+=
x_dims
.
size
();
}
norm_dims
[
axis
]
=
1
;
VariableNameMap
inputs
;
VariableNameMap
outputs
;
auto
scope
=
std
::
make_shared
<
framework
::
Scope
>
();
inputs
[
"X"
]
=
std
::
vector
<
std
::
string
>
({
"input"
});
outputs
[
"Norm"
]
=
std
::
vector
<
std
::
string
>
({
"outputNorm"
});
outputs
[
"Out"
]
=
std
::
vector
<
std
::
string
>
({
"output"
});
auto
input_var
=
scope
.
get
()
->
Var
(
"input"
);
auto
input
=
input_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
float
>
(
input
,
x_dims
,
-
100.0
,
100.0
);
auto
norm_var
=
scope
.
get
()
->
Var
(
"outputNorm"
);
auto
norm
=
norm_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
float
>
(
norm
,
norm_dims
,
-
100.0
,
100.0
);
auto
output_var
=
scope
.
get
()
->
Var
(
"output"
);
framework
::
AttributeMap
attrs
;
auto
*
op
=
new
operators
::
GruUnitOp
<
CPU
,
float
>
(
"norm"
,
inputs
,
outputs
,
attrs
,
scope
);
op
->
InferShape
();
op
->
Init
();
op
->
Run
();
}
}
// namespace paddle_mobile
int
main
()
{
}
test/operators/test_logical_and_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "../test_include.h"
#include "operators/logical_op.h"
namespace
paddle_mobile
{
void
LogicalAnd
(
const
framework
::
Tensor
*
inputX
,
const
framework
::
Tensor
*
inputY
,
framework
::
Tensor
*
output
)
{
auto
x_data
=
inputX
->
data
<
bool
>
();
auto
y_data
=
inputY
->
data
<
bool
>
();
auto
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
inputX
->
numel
();
++
i
)
{
*
output_data
=
*
x_data
&&
*
y_data
;
x_data
++
;
y_data
++
;
output_data
++
;
}
}
int
TestLogicalAndOp
(
const
std
::
vector
<
int
>
input_shape
)
{
framework
::
DDim
input_dims
=
framework
::
make_ddim
(
input_shape
);
VariableNameMap
inputs
;
VariableNameMap
outputs
;
auto
scope
=
std
::
make_shared
<
framework
::
Scope
>
();
inputs
[
"X"
]
=
std
::
vector
<
std
::
string
>
({
"inputX"
});
inputs
[
"Y"
]
=
std
::
vector
<
std
::
string
>
({
"inputY"
});
outputs
[
"Out"
]
=
std
::
vector
<
std
::
string
>
({
"output"
});
auto
x_var
=
scope
.
get
()
->
Var
(
"inputX"
);
auto
x
=
x_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
x
,
input_dims
,
0
,
1
);
auto
y_var
=
scope
.
get
()
->
Var
(
"inputY"
);
auto
y
=
y_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
y
,
input_dims
,
0
,
1
);
auto
output_var
=
scope
.
get
()
->
Var
(
"output"
);
framework
::
AttributeMap
attrs
;
auto
*
op
=
new
operators
::
LogicalAndOp
<
CPU
,
float
>
(
"logical_and"
,
inputs
,
outputs
,
attrs
,
scope
);
op
->
InferShape
();
op
->
Init
();
op
->
Run
();
auto
output
=
output_var
->
template
Get
<
framework
::
LoDTensor
>();
framework
::
Tensor
output_cmp
;
bool
*
output_cmp_data
=
output_cmp
.
mutable_data
<
bool
>
(
output
->
dims
());
LogicalAnd
(
x
,
y
,
&
output_cmp
);
const
bool
*
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
output
->
numel
();
++
i
)
{
if
(
output_data
[
i
]
!=
output_cmp_data
[
i
])
{
LOG
(
kLOG_INFO
)
<<
"output_data["
<<
i
<<
"] = "
<<
output_data
[
i
]
<<
", output_cmp_data["
<<
i
<<
"] = "
<<
output_cmp_data
[
i
];
delete
op
;
exit
(
1
);
}
}
}
}
// namespace paddle_mobile
int
main
()
{
paddle_mobile
::
TestLogicalAndOp
({
1
,
1
,
2
,
3
});
paddle_mobile
::
TestLogicalAndOp
({
1
,
3
,
11
,
12
});
paddle_mobile
::
TestLogicalAndOp
({
1
,
16
,
32
,
32
});
std
::
cout
<<
"test logical_and op pass."
<<
std
::
endl
;
return
0
;
}
test/operators/test_logical_not_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "../test_include.h"
#include "operators/logical_op.h"
namespace
paddle_mobile
{
void
LogicalNot
(
const
framework
::
Tensor
*
inputX
,
framework
::
Tensor
*
output
)
{
auto
x_data
=
inputX
->
data
<
bool
>
();
auto
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
inputX
->
numel
();
++
i
)
{
*
output_data
=
!*
x_data
;
x_data
++
;
output_data
++
;
}
}
int
TestLogicalNotOp
(
const
std
::
vector
<
int
>
input_shape
)
{
framework
::
DDim
input_dims
=
framework
::
make_ddim
(
input_shape
);
VariableNameMap
inputs
;
VariableNameMap
outputs
;
auto
scope
=
std
::
make_shared
<
framework
::
Scope
>
();
inputs
[
"X"
]
=
std
::
vector
<
std
::
string
>
({
"inputX"
});
outputs
[
"Out"
]
=
std
::
vector
<
std
::
string
>
({
"output"
});
auto
x_var
=
scope
.
get
()
->
Var
(
"inputX"
);
auto
x
=
x_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
x
,
input_dims
,
0
,
1
);
auto
output_var
=
scope
.
get
()
->
Var
(
"output"
);
framework
::
AttributeMap
attrs
;
auto
*
op
=
new
operators
::
LogicalNotOp
<
CPU
,
float
>
(
"logical_not"
,
inputs
,
outputs
,
attrs
,
scope
);
op
->
InferShape
();
op
->
Init
();
op
->
Run
();
auto
output
=
output_var
->
template
Get
<
framework
::
LoDTensor
>();
framework
::
Tensor
output_cmp
;
bool
*
output_cmp_data
=
output_cmp
.
mutable_data
<
bool
>
(
output
->
dims
());
LogicalNot
(
x
,
&
output_cmp
);
const
bool
*
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
output
->
numel
();
++
i
)
{
if
(
output_data
[
i
]
!=
output_cmp_data
[
i
])
{
LOG
(
kLOG_INFO
)
<<
"output_data["
<<
i
<<
"] = "
<<
output_data
[
i
]
<<
", output_cmp_data["
<<
i
<<
"] = "
<<
output_cmp_data
[
i
];
delete
op
;
exit
(
1
);
}
}
}
}
// namespace paddle_mobile
int
main
()
{
paddle_mobile
::
TestLogicalNotOp
({
1
,
1
,
2
,
3
});
paddle_mobile
::
TestLogicalNotOp
({
1
,
3
,
11
,
12
});
paddle_mobile
::
TestLogicalNotOp
({
1
,
16
,
32
,
32
});
std
::
cout
<<
"test logical_not op pass."
<<
std
::
endl
;
return
0
;
}
test/operators/test_logical_or_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "../test_include.h"
#include "operators/logical_op.h"
namespace
paddle_mobile
{
void
LogicalOr
(
const
framework
::
Tensor
*
inputX
,
const
framework
::
Tensor
*
inputY
,
framework
::
Tensor
*
output
)
{
auto
x_data
=
inputX
->
data
<
bool
>
();
auto
y_data
=
inputY
->
data
<
bool
>
();
auto
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
inputX
->
numel
();
++
i
)
{
*
output_data
=
*
x_data
||
*
y_data
;
x_data
++
;
y_data
++
;
output_data
++
;
}
}
int
TestLogicalOrOp
(
const
std
::
vector
<
int
>
input_shape
)
{
framework
::
DDim
input_dims
=
framework
::
make_ddim
(
input_shape
);
VariableNameMap
inputs
;
VariableNameMap
outputs
;
auto
scope
=
std
::
make_shared
<
framework
::
Scope
>
();
inputs
[
"X"
]
=
std
::
vector
<
std
::
string
>
({
"inputX"
});
inputs
[
"Y"
]
=
std
::
vector
<
std
::
string
>
({
"inputY"
});
outputs
[
"Out"
]
=
std
::
vector
<
std
::
string
>
({
"output"
});
auto
x_var
=
scope
.
get
()
->
Var
(
"inputX"
);
auto
x
=
x_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
x
,
input_dims
,
0
,
1
);
auto
y_var
=
scope
.
get
()
->
Var
(
"inputY"
);
auto
y
=
y_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
y
,
input_dims
,
0
,
1
);
auto
output_var
=
scope
.
get
()
->
Var
(
"output"
);
framework
::
AttributeMap
attrs
;
auto
*
op
=
new
operators
::
LogicalOrOp
<
CPU
,
float
>
(
"logical_or"
,
inputs
,
outputs
,
attrs
,
scope
);
op
->
InferShape
();
op
->
Init
();
op
->
Run
();
auto
output
=
output_var
->
template
Get
<
framework
::
LoDTensor
>();
framework
::
Tensor
output_cmp
;
bool
*
output_cmp_data
=
output_cmp
.
mutable_data
<
bool
>
(
output
->
dims
());
LogicalOr
(
x
,
y
,
&
output_cmp
);
const
bool
*
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
output
->
numel
();
++
i
)
{
if
(
output_data
[
i
]
!=
output_cmp_data
[
i
])
{
LOG
(
kLOG_INFO
)
<<
"output_data["
<<
i
<<
"] = "
<<
output_data
[
i
]
<<
", output_cmp_data["
<<
i
<<
"] = "
<<
output_cmp_data
[
i
];
delete
op
;
exit
(
1
);
}
}
}
}
// namespace paddle_mobile
int
main
()
{
paddle_mobile
::
TestLogicalOrOp
({
1
,
1
,
2
,
3
});
paddle_mobile
::
TestLogicalOrOp
({
1
,
3
,
11
,
12
});
paddle_mobile
::
TestLogicalOrOp
({
1
,
16
,
32
,
32
});
std
::
cout
<<
"test logical_or op pass."
<<
std
::
endl
;
return
0
;
}
test/operators/test_logical_xor_op.cpp
0 → 100644
浏览文件 @
4dbbab5e
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "../test_include.h"
#include "operators/logical_op.h"
namespace
paddle_mobile
{
void
LogicalXor
(
const
framework
::
Tensor
*
inputX
,
const
framework
::
Tensor
*
inputY
,
framework
::
Tensor
*
output
)
{
auto
x_data
=
inputX
->
data
<
bool
>
();
auto
y_data
=
inputY
->
data
<
bool
>
();
auto
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
inputX
->
numel
();
++
i
)
{
bool
x
=
*
x_data
;
bool
y
=
*
y_data
;
*
output_data
=
(
x
||
y
)
&&
!
(
x
&&
y
);
x_data
++
;
y_data
++
;
output_data
++
;
}
}
int
TestLogicalXorOp
(
const
std
::
vector
<
int
>
input_shape
)
{
framework
::
DDim
input_dims
=
framework
::
make_ddim
(
input_shape
);
VariableNameMap
inputs
;
VariableNameMap
outputs
;
auto
scope
=
std
::
make_shared
<
framework
::
Scope
>
();
inputs
[
"X"
]
=
std
::
vector
<
std
::
string
>
({
"inputX"
});
inputs
[
"Y"
]
=
std
::
vector
<
std
::
string
>
({
"inputY"
});
outputs
[
"Out"
]
=
std
::
vector
<
std
::
string
>
({
"output"
});
auto
x_var
=
scope
.
get
()
->
Var
(
"inputX"
);
auto
x
=
x_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
x
,
input_dims
,
0
,
1
);
auto
y_var
=
scope
.
get
()
->
Var
(
"inputY"
);
auto
y
=
y_var
->
template
GetMutable
<
framework
::
LoDTensor
>();
SetupTensor
<
bool
>
(
y
,
input_dims
,
0
,
1
);
auto
output_var
=
scope
.
get
()
->
Var
(
"output"
);
framework
::
AttributeMap
attrs
;
auto
*
op
=
new
operators
::
LogicalXorOp
<
CPU
,
float
>
(
"logical_xor"
,
inputs
,
outputs
,
attrs
,
scope
);
op
->
InferShape
();
op
->
Init
();
op
->
Run
();
auto
output
=
output_var
->
template
Get
<
framework
::
LoDTensor
>();
framework
::
Tensor
output_cmp
;
bool
*
output_cmp_data
=
output_cmp
.
mutable_data
<
bool
>
(
output
->
dims
());
LogicalXor
(
x
,
y
,
&
output_cmp
);
const
bool
*
output_data
=
output
->
data
<
bool
>
();
for
(
int
i
=
0
;
i
<
output
->
numel
();
++
i
)
{
if
(
output_data
[
i
]
!=
output_cmp_data
[
i
])
{
LOG
(
kLOG_INFO
)
<<
"output_data["
<<
i
<<
"] = "
<<
output_data
[
i
]
<<
", output_cmp_data["
<<
i
<<
"] = "
<<
output_cmp_data
[
i
];
delete
op
;
exit
(
1
);
}
}
}
}
// namespace paddle_mobile
int
main
()
{
paddle_mobile
::
TestLogicalXorOp
({
1
,
1
,
2
,
3
});
paddle_mobile
::
TestLogicalXorOp
({
1
,
3
,
11
,
12
});
paddle_mobile
::
TestLogicalXorOp
({
1
,
16
,
32
,
32
});
std
::
cout
<<
"test logical_xor op pass."
<<
std
::
endl
;
return
0
;
}
test/test_helper.h
浏览文件 @
4dbbab5e
...
...
@@ -83,6 +83,26 @@ void SetupTensor(paddle_mobile::framework::Tensor *input,
}
}
template
<
>
void
SetupTensor
<
bool
>
(
paddle_mobile
::
framework
::
Tensor
*
input
,
paddle_mobile
::
framework
::
DDim
dims
,
bool
lower
,
bool
upper
)
{
static
unsigned
int
seed
=
100
;
std
::
mt19937
rng
(
seed
++
);
std
::
uniform_real_distribution
<
double
>
uniform_dist
(
0
,
1
);
bool
*
input_ptr
=
input
->
mutable_data
<
bool
>
(
dims
);
if
(
lower
==
upper
)
{
for
(
int
i
=
0
;
i
<
input
->
numel
();
++
i
)
{
input_ptr
[
i
]
=
lower
;
}
}
else
{
for
(
int
i
=
0
;
i
<
input
->
numel
();
++
i
)
{
input_ptr
[
i
]
=
uniform_dist
(
rng
)
>
0.5
;
}
}
}
template
<
typename
T
>
T
*
CreateInput
(
Tensor
*
input
,
DDim
dims
,
T
low
,
T
up
)
{
SetupTensor
<
T
>
(
input
,
dims
,
static_cast
<
float
>
(
low
),
static_cast
<
float
>
(
up
));
...
...
tools/op.cmake
浏览文件 @
4dbbab5e
...
...
@@ -281,6 +281,10 @@ if(NOT FOUND_MATCH)
set
(
TANH_OP ON
)
set
(
LOD_RESET_OP ON
)
set
(
LESS_THAN_OP ON
)
set
(
LOGICAL_AND_OP ON
)
set
(
LOGICAL_OR_OP ON
)
set
(
LOGICAL_NOT_OP ON
)
set
(
LOGICAL_XOR_OP ON
)
endif
()
# option(BATCHNORM_OP "" ON)
...
...
@@ -530,6 +534,18 @@ endif()
if
(
LESS_THAN_OP
)
add_definitions
(
-DLESS_THAN_OP
)
endif
()
if
(
LOGICAL_AND_OP
)
add_definitions
(
-DLOGICAL_AND_OP
)
endif
()
if
(
LOGICAL_OR_OP
)
add_definitions
(
-DLOGICAL_OR_OP
)
endif
()
if
(
LOGICAL_NOT_OP
)
add_definitions
(
-DLOGICAL_NOT_OP
)
endif
()
if
(
LOGICAL_XOR_OP
)
add_definitions
(
-DLOGICAL_XOR_OP
)
endif
()
if
(
TANH_OP
)
add_definitions
(
-DTANH_OP
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录