Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
1ef61f3f
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
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看板
提交
1ef61f3f
编写于
7月 20, 2018
作者:
D
dolphin8
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
reshape & softmax & pool fix
上级
cb9db5f5
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
81 addition
and
37 deletion
+81
-37
metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
...ddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
+47
-32
metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift
...e-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift
+21
-4
metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift
...obile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift
+1
-0
metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift
...obile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift
+1
-0
metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift
metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift
+5
-0
metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift
metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift
+6
-1
未找到文件。
metal/paddle-mobile/paddle-mobile/Operators/Kernels/Kernels.metal
浏览文件 @
1ef61f3f
...
...
@@ -95,73 +95,88 @@ kernel void texture2d_to_2d_array(texture2d<float, access::read> inTexture [[tex
outTexture.write(input, gid.xy, 0);
}
kernel void pool(texture2d_array<half, access::read> inTexture [[texture(0)]],
texture2d_array<half, access::write> outTexture [[texture(1)]],
const device int * ksize [[buffer(0)]],
const device int * stride [[buffer(1)]],
const device int * padding [[buffer(2)]],
const device int * poolType [[buffer(3)]],
struct PoolParam {
int ksizeX;
int ksizeY;
int strideX;
int strideY;
int paddingX;
int paddingY;
int poolType;
};
kernel void pool(texture2d_array<float, access::read> inTexture [[texture(0)]],
texture2d_array<float, access::write> outTexture [[texture(1)]],
constant PoolParam &pm [[buffer(0)]],
uint3 gid [[thread_position_in_grid]]) {
if (gid.x >= outTexture.get_width() ||
gid.y >= outTexture.get_height() ||
gid.z >= outTexture.get_array_size()) return;
int xmin = gid.x *
stride[0] - padding[0]
;
int xmax = min(xmin +
ksize[0]
, int(inTexture.get_width()));
int xmin = gid.x *
pm.strideX - pm.paddingX
;
int xmax = min(xmin +
pm.ksizeX
, int(inTexture.get_width()));
xmin = max(xmin, 0);
int ymin = gid.y *
stride[1] - padding[1]
;
int ymax = min(ymin +
ksize[1], int(inTexture.get_width
()));
int ymin = gid.y *
pm.strideX - pm.paddingX
;
int ymax = min(ymin +
pm.ksizeX, int(inTexture.get_height
()));
ymin = max(ymin, 0);
half
4 r = 0;
if (
*
poolType == 0) {
float
4 r = 0;
if (
pm.
poolType == 0) {
r = inTexture.read(uint2(xmin, ymin), gid.z);
for (int
32_t
x = xmin; x < xmax; x++) {
for (int x = xmin; x < xmax; x++) {
for (int y = ymin; y < ymax; y++) {
r = fmax(r, inTexture.read(uint2(x, y), gid.z));
}
}
} else if (
*
poolType == 1) {
for (int
32_t
x = xmin; x < xmax; x++) {
} else if (
pm.
poolType == 1) {
for (int x = xmin; x < xmax; x++) {
for (int y = ymin; y < ymax; y++) {
r += inTexture.read(uint2(x, y), gid.z);
}
}
r /=
ksize[0] * ksize[1]
;
r /=
pm.ksizeX * pm.ksizeY
;
}
// float4 r;
// r[0] = 1.0 * pm.ksizeX;
// r[1] = 2.0;
// r[2] = 3.0;
// r[3] = 4.0;
outTexture.write(r, gid.xy, gid.z);
}
kernel void reshape(texture2d_array<
half
, access::read> inTexture [[texture(0)]],
texture2d<
half
, access::write> outTexture [[texture(1)]],
kernel void reshape(texture2d_array<
float
, access::read> inTexture [[texture(0)]],
texture2d<
float
, access::write> outTexture [[texture(1)]],
uint3 gid [[thread_position_in_grid]]) {
if (gid.x >= outTexture.get_width() ||
gid.y >= outTexture.get_height()) return;
int zz = gid.
y
/ 4;
int cc = gid.
y
% 4;
half
4 r = inTexture.read(uint2(0, 0), zz);
int zz = gid.
x
/ 4;
int cc = gid.
x
% 4;
float
4 r = inTexture.read(uint2(0, 0), zz);
r[0] = r[cc];
r[1] = 0;
r[2] = 0;
r[3] = 0;
outTexture.write(r, gid.xy, gid.z);
}
kernel void softmax(texture2d<
half, access::read> inTexture [[texture(1
)]],
texture2d<
half, access::write> outTexture [[texture(2
)]],
kernel void softmax(texture2d<
float, access::read> inTexture [[texture(0
)]],
texture2d<
float, access::write> outTexture [[texture(1
)]],
uint3 gid [[thread_position_in_grid]]) {
if (gid.x >= outTexture.get_width() ||
gid.y >= outTexture.get_height()) return;
// int xsize = inTexture.get_width();
int ysize = inTexture.get_height();
half maxv = inTexture.read(uint2(0, 0), gid.z)[0];
for (int y = 0; y < ysize; y++) {
half r = inTexture.read(uint2(0, y), gid.z)[0];
int xsize = inTexture.get_width();
float maxv = inTexture.read(uint2(0, 0), gid.z)[0];
for (int x = 0; x < xsize; x++) {
float r = inTexture.read(uint2(x, 0), gid.z)[0];
maxv = max(maxv, r);
}
half
sum = 0;
for (int
y = 0; y < ysize; y
++) {
half r = inTexture.read(uint2(0, y
), gid.z)[0];
float
sum = 0;
for (int
x = 0; x < xsize; x
++) {
float r = inTexture.read(uint2(x, 0
), gid.z)[0];
sum += exp(r - maxv);
}
half
4 rr = inTexture.read(gid.xy, gid.z);
float
4 rr = inTexture.read(gid.xy, gid.z);
rr[0] = exp(rr[0] - maxv) / sum;
outTexture.write(rr, gid.xy, gid.z);
}
metal/paddle-mobile/paddle-mobile/Operators/Kernels/PoolKernel.swift
浏览文件 @
1ef61f3f
...
...
@@ -14,6 +14,16 @@
import
Foundation
struct
PoolMetalParam
{
let
ksizeX
:
Int32
let
ksizeY
:
Int32
let
strideX
:
Int32
let
strideY
:
Int32
let
paddingX
:
Int32
let
paddingY
:
Int32
let
poolType
:
Int32
}
class
PoolKernel
<
P
:
PrecisionType
>
:
Kernel
,
Computable
{
func
compute
(
commandBuffer
:
MTLCommandBuffer
,
param
:
PoolParam
<
P
>
)
throws
{
guard
let
encoder
=
commandBuffer
.
makeComputeCommandEncoder
()
else
{
...
...
@@ -22,9 +32,6 @@ class PoolKernel<P: PrecisionType>: Kernel, Computable{
print
(
"Pool compute"
)
encoder
.
setTexture
(
param
.
input
.
metalTexture
,
index
:
0
)
encoder
.
setTexture
(
param
.
output
.
metalTexture
,
index
:
1
)
encoder
.
setBytes
(
UnsafeRawPointer
(
param
.
ksize
),
length
:
param
.
ksize
.
count
*
4
,
index
:
0
)
encoder
.
setBytes
(
UnsafeRawPointer
(
param
.
stride
),
length
:
param
.
stride
.
count
*
4
,
index
:
1
)
encoder
.
setBytes
(
UnsafeRawPointer
(
param
.
padding
),
length
:
param
.
padding
.
count
*
4
,
index
:
2
)
var
poolType
:
Int32
switch
param
.
poolType
{
case
"max"
:
...
...
@@ -34,7 +41,17 @@ class PoolKernel<P: PrecisionType>: Kernel, Computable{
default
:
throw
PaddleMobileError
.
predictError
(
message
:
" unknown pooltype "
+
param
.
poolType
)
}
encoder
.
setBytes
(
&
poolType
,
length
:
4
,
index
:
3
)
var
pmp
=
PoolMetalParam
.
init
(
ksizeX
:
param
.
ksize
[
0
],
ksizeY
:
param
.
ksize
[
1
],
strideX
:
param
.
stride
[
0
],
strideY
:
param
.
stride
[
1
],
paddingX
:
param
.
padding
[
0
],
paddingY
:
param
.
padding
[
1
],
poolType
:
poolType
)
encoder
.
setBytes
(
&
pmp
,
length
:
MemoryLayout
<
PoolMetalParam
>.
size
,
index
:
0
)
encoder
.
dispatch
(
computePipline
:
pipline
,
outTexture
:
param
.
output
.
metalTexture
)
encoder
.
endEncoding
()
}
...
...
metal/paddle-mobile/paddle-mobile/Operators/Kernels/ReshapeKernel.swift
浏览文件 @
1ef61f3f
...
...
@@ -26,6 +26,7 @@ class ReshapeKernel<P: PrecisionType>: Kernel, Computable{
print
(
"Reshape compute"
)
encoder
.
setTexture
(
param
.
input
.
metalTexture
,
index
:
0
)
encoder
.
setTexture
(
param
.
output
.
metalTexture
,
index
:
1
)
encoder
.
dispatch
(
computePipline
:
pipline
,
outTexture
:
param
.
output
.
metalTexture
)
encoder
.
endEncoding
()
}
}
metal/paddle-mobile/paddle-mobile/Operators/Kernels/SoftmaxKernel.swift
浏览文件 @
1ef61f3f
...
...
@@ -23,6 +23,7 @@ class SoftmaxKernel<P: PrecisionType>: Kernel, Computable{
print
(
"softmax compute"
)
encoder
.
setTexture
(
param
.
input
.
metalTexture
,
index
:
0
)
encoder
.
setTexture
(
param
.
output
.
metalTexture
,
index
:
1
)
encoder
.
dispatch
(
computePipline
:
pipline
,
outTexture
:
param
.
output
.
metalTexture
)
encoder
.
endEncoding
()
}
...
...
metal/paddle-mobile/paddle-mobile/Operators/ReshapeOp.swift
浏览文件 @
1ef61f3f
...
...
@@ -42,4 +42,9 @@ class ReshapeOp<P: PrecisionType>: Operator<ReshapeKernel<P>, ReshapeParam<P>>,
throw
error
}
}
func
delogOutput
()
{
print
(
"reshape delog"
)
let
_
:
P
?
=
para
.
input
.
metalTexture
.
logDesc
(
header
:
"reshape input: "
,
stridable
:
false
)
let
_
:
P
?
=
para
.
output
.
metalTexture
.
logDesc
(
header
:
"reshape output: "
,
stridable
:
false
)
}
}
metal/paddle-mobile/paddle-mobile/Operators/SoftmaxOp.swift
浏览文件 @
1ef61f3f
...
...
@@ -31,7 +31,7 @@ class SoftmaxParam<P: PrecisionType>: OpParam {
class
SoftmaxOp
<
P
:
PrecisionType
>
:
Operator
<
SoftmaxKernel
<
P
>
,
SoftmaxParam
<
P
>>
,
Runable
,
Creator
,
InferShaperable
{
func
inferShape
()
{
para
.
output
.
dim
=
para
.
input
.
dim
//
para.output.dim = para.input.dim
}
typealias
OpType
=
SoftmaxOp
<
P
>
...
...
@@ -42,4 +42,9 @@ class SoftmaxOp<P: PrecisionType>: Operator<SoftmaxKernel<P>, SoftmaxParam<P>>,
throw
error
}
}
func
delogOutput
()
{
print
(
"softmax delog"
)
let
_
:
P
?
=
para
.
input
.
metalTexture
.
logDesc
(
header
:
"softmax input: "
,
stridable
:
false
)
let
_
:
P
?
=
para
.
output
.
metalTexture
.
logDesc
(
header
:
"softmax output: "
,
stridable
:
false
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录