Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Xiaomi
Mace
提交
1dcd83b1
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,发现更多精彩内容 >>
提交
1dcd83b1
编写于
4月 20, 2018
作者:
李
李寅
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add transpose 2D
上级
c384a6e2
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
153 addition
and
24 deletion
+153
-24
mace/kernels/transpose.h
mace/kernels/transpose.h
+33
-20
mace/ops/transpose.h
mace/ops/transpose.h
+4
-4
mace/ops/transpose_benchmark.cc
mace/ops/transpose_benchmark.cc
+93
-0
mace/ops/transpose_test.cc
mace/ops/transpose_test.cc
+23
-0
未找到文件。
mace/kernels/transpose.h
浏览文件 @
1dcd83b1
...
...
@@ -37,31 +37,44 @@ struct TransposeFunctor {
const
T
*
input_data
=
input
->
data
<
T
>
();
T
*
output_data
=
output
->
mutable_data
<
T
>
();
std
::
vector
<
index_t
>
in_stride
{
input_shape
[
1
]
*
input_shape
[
2
]
*
input_shape
[
3
],
input_shape
[
2
]
*
input_shape
[
3
],
input_shape
[
3
],
1
};
std
::
vector
<
index_t
>
out_stride
{
output_shape
[
1
]
*
output_shape
[
2
]
*
output_shape
[
3
],
output_shape
[
2
]
*
output_shape
[
3
],
output_shape
[
3
],
1
};
if
(
input
->
dim_size
()
==
2
)
{
MACE_CHECK
(
dims_
[
0
]
==
1
&&
dims_
[
1
]
==
0
,
"no need transform"
);
index_t
stride_i
=
input_shape
[
0
];
index_t
stride_j
=
input_shape
[
1
];
for
(
int
i
=
0
;
i
<
input_shape
[
0
];
++
i
)
{
for
(
int
j
=
0
;
j
<
input_shape
[
1
];
++
j
)
{
output_data
[
j
*
stride_i
+
i
]
=
input_data
[
i
*
stride_j
+
j
];
}
}
}
else
if
(
input
->
dim_size
()
==
4
)
{
std
::
vector
<
index_t
>
in_stride
{
input_shape
[
1
]
*
input_shape
[
2
]
*
input_shape
[
3
],
input_shape
[
2
]
*
input_shape
[
3
],
input_shape
[
3
],
1
};
std
::
vector
<
index_t
>
out_stride
{
output_shape
[
1
]
*
output_shape
[
2
]
*
output_shape
[
3
],
output_shape
[
2
]
*
output_shape
[
3
],
output_shape
[
3
],
1
};
std
::
vector
<
index_t
>
idim
(
4
,
0
);
std
::
vector
<
index_t
>
odim
(
4
,
0
);
for
(
odim
[
0
]
=
0
;
odim
[
0
]
<
output_shape
[
0
];
++
odim
[
0
])
{
for
(
odim
[
1
]
=
0
;
odim
[
1
]
<
output_shape
[
1
];
++
odim
[
1
])
{
for
(
odim
[
2
]
=
0
;
odim
[
2
]
<
output_shape
[
2
];
++
odim
[
2
])
{
for
(
odim
[
3
]
=
0
;
odim
[
3
]
<
output_shape
[
3
];
++
odim
[
3
])
{
idim
[
dims_
[
0
]]
=
odim
[
0
];
idim
[
dims_
[
1
]]
=
odim
[
1
];
idim
[
dims_
[
2
]]
=
odim
[
2
];
idim
[
dims_
[
3
]]
=
odim
[
3
];
std
::
vector
<
index_t
>
idim
(
4
,
0
);
std
::
vector
<
index_t
>
odim
(
4
,
0
);
for
(
odim
[
0
]
=
0
;
odim
[
0
]
<
output_shape
[
0
];
++
odim
[
0
])
{
for
(
odim
[
1
]
=
0
;
odim
[
1
]
<
output_shape
[
1
];
++
odim
[
1
])
{
for
(
odim
[
2
]
=
0
;
odim
[
2
]
<
output_shape
[
2
];
++
odim
[
2
])
{
for
(
odim
[
3
]
=
0
;
odim
[
3
]
<
output_shape
[
3
];
++
odim
[
3
])
{
idim
[
dims_
[
0
]]
=
odim
[
0
];
idim
[
dims_
[
1
]]
=
odim
[
1
];
idim
[
dims_
[
2
]]
=
odim
[
2
];
idim
[
dims_
[
3
]]
=
odim
[
3
];
output_data
[
odim
[
0
]
*
out_stride
[
0
]
+
odim
[
1
]
*
out_stride
[
1
]
+
odim
[
2
]
*
out_stride
[
2
]
+
odim
[
3
]]
=
input_data
[
idim
[
0
]
*
in_stride
[
0
]
+
idim
[
1
]
*
in_stride
[
1
]
+
idim
[
2
]
*
in_stride
[
2
]
+
idim
[
3
]];
output_data
[
odim
[
0
]
*
out_stride
[
0
]
+
odim
[
1
]
*
out_stride
[
1
]
+
odim
[
2
]
*
out_stride
[
2
]
+
odim
[
3
]]
=
input_data
[
idim
[
0
]
*
in_stride
[
0
]
+
idim
[
1
]
*
in_stride
[
1
]
+
idim
[
2
]
*
in_stride
[
2
]
+
idim
[
3
]];
}
}
}
}
}
else
{
MACE_NOT_IMPLEMENTED
;
}
}
...
...
mace/ops/transpose.h
浏览文件 @
1dcd83b1
...
...
@@ -28,16 +28,16 @@ class TransposeOp : public Operator<D, T> {
public:
TransposeOp
(
const
OperatorDef
&
operator_def
,
Workspace
*
ws
)
:
Operator
<
D
,
T
>
(
operator_def
,
ws
),
dims_
(
OperatorBase
::
GetRepeatedArgument
<
int
>
(
"dims"
)),
dims_
(
OperatorBase
::
GetRepeatedArgument
<
int
>
(
"dims"
)),
functor_
(
dims_
)
{}
bool
Run
(
StatsFuture
*
future
)
override
{
const
Tensor
*
input
=
this
->
Input
(
INPUT
);
Tensor
*
output
=
this
->
Output
(
OUTPUT
);
const
std
::
vector
<
index_t
>
&
input_shape
=
input
->
shape
();
MACE_CHECK
(
input_shape
.
size
()
==
4
&&
dims_
.
size
()
==
4
,
"rank should be 4"
);
MACE_CHECK
(
input_shape
.
size
()
==
4
&&
dims_
.
size
()
==
4
||
input_shape
.
size
()
==
2
&&
dims_
.
size
()
==
2
,
"rank should be 2 or 4"
);
std
::
vector
<
index_t
>
output_shape
;
for
(
int
i
=
0
;
i
<
dims_
.
size
();
++
i
)
{
output_shape
.
push_back
(
input_shape
[
dims_
[
i
]]);
...
...
mace/ops/transpose_benchmark.cc
0 → 100644
浏览文件 @
1dcd83b1
// Copyright 2018 Xiaomi, Inc. 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 <string>
#include <vector>
#include "mace/core/operator.h"
#include "mace/core/testing/test_benchmark.h"
#include "mace/ops/ops_test_util.h"
namespace
mace
{
namespace
ops
{
namespace
test
{
namespace
{
template
<
DeviceType
D
,
typename
T
>
void
TransposeBenchmark
(
int
iters
,
std
::
vector
<
index_t
>
shape
,
std
::
vector
<
int
>
dims
)
{
mace
::
testing
::
StopTiming
();
OpsTestNet
net
;
// Add input data
net
.
AddRandomInput
<
D
,
float
>
(
"Input"
,
shape
);
OpDefBuilder
(
"Transpose"
,
"TransposeBM"
)
.
Input
(
"Input"
)
.
Output
(
"Output"
)
.
AddIntsArg
(
"dims"
,
dims
)
.
Finalize
(
net
.
NewOperatorDef
());
// Warm-up
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
net
.
RunOp
(
D
);
}
net
.
Sync
();
mace
::
testing
::
StartTiming
();
while
(
iters
--
)
{
net
.
RunOp
(
D
);
}
net
.
Sync
();
}
}
// namespace
#define BM_TRANSPOSE2D_MACRO(H, W, TYPE, DEVICE) \
static void BM_TRANSPOSE2D_##H##_##W##_##TYPE##_##DEVICE( \
int iters) { \
const int64_t tot = static_cast<int64_t>(iters) * H * W; \
mace::testing::MaccProcessed(tot); \
mace::testing::BytesProcessed(tot *(sizeof(TYPE))); \
TransposeBenchmark<DEVICE, TYPE>(iters, {H, W}, {1, 0}); \
} \
BENCHMARK(BM_TRANSPOSE2D_##H##_##W##_##TYPE##_##DEVICE)
#define BM_TRANSPOSE2D(H, W) \
BM_TRANSPOSE2D_MACRO(H, W, float, CPU);
#define BM_TRANSPOSE4D_MACRO(N, C, H, W, D0, D1, D2, D3, TYPE, DEVICE) \
static void \
BM_TRANSPOSE4D_##N##_##C##_##H##_##W##_##D0##D1##D2##D3##_##TYPE##_##DEVICE( \
int iters) { \
const int64_t tot = static_cast<int64_t>(iters) * N * C * H * W; \
mace::testing::MaccProcessed(tot); \
mace::testing::BytesProcessed(tot *(sizeof(TYPE))); \
TransposeBenchmark<DEVICE, TYPE>(iters, {N, C, H, W}, {D0, D1, D2, D3}); \
} \
BENCHMARK( \
BM_TRANSPOSE4D_##N##_##C##_##H##_##W##_##D0##D1##D2##D3##_##TYPE##_##DEVICE)
#define BM_TRANSPOSE4D(N, C, H, W, D0, D1, D2, D3) \
BM_TRANSPOSE4D_MACRO(N, C, H, W, D0, D1, D2, D3, float, CPU);
BM_TRANSPOSE4D
(
1
,
64
,
64
,
512
,
0
,
3
,
1
,
2
);
BM_TRANSPOSE4D
(
1
,
512
,
64
,
64
,
0
,
2
,
3
,
1
);
BM_TRANSPOSE2D
(
128
,
128
);
BM_TRANSPOSE2D
(
512
,
512
);
}
// namespace test
}
// namespace ops
}
// namespace mace
mace/ops/transpose_test.cc
浏览文件 @
1dcd83b1
...
...
@@ -49,6 +49,29 @@ TEST_F(TransposeOpTest, NCHW) {
TransposeNCHWTest
({
1
,
64
,
48
,
128
});
}
TEST_F
(
TransposeOpTest
,
Rank2
)
{
// Construct graph
OpsTestNet
net
;
// Add input data
net
.
AddInputFromArray
<
CPU
,
float
>
(
"Input"
,
{
2
,
3
},
{
1
,
2
,
3
,
4
,
5
,
6
});
OpDefBuilder
(
"Transpose"
,
"TransposeNCHWTest"
)
.
Input
(
"Input"
)
.
Output
(
"Output"
)
.
AddIntsArg
(
"dims"
,
{
1
,
0
})
.
Finalize
(
net
.
NewOperatorDef
());
// Run on cpu
net
.
RunOp
();
net
.
AddInputFromArray
<
CPU
,
float
>
(
"ExpectedOutput"
,
{
3
,
2
},
{
1
,
4
,
2
,
5
,
3
,
6
});
ExpectTensorNear
<
float
>
(
*
net
.
GetOutput
(
"ExpectedOutput"
),
*
net
.
GetOutput
(
"Output"
));
}
}
// namespace test
}
// namespace ops
}
// namespace mace
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录