【发布时间】:2017-01-25 08:35:15
【问题描述】:
对于带有 Metal Performance Shader 的 iOS 应用,我想为 CNN 平均池化层反向传播编写一个 GPU 加速函数。这与图像上采样几乎相同。例如,如果输入是
2 5 6
3 6 7
8 9 0
上采样图像应该是
2 2 5 5 6 6
2 2 5 5 6 6
3 3 6 6 7 7
3 3 6 6 7 7
8 8 9 9 0 0
8 8 9 9 0 0
我编写了以下内核函数:
kernel void upsample(texture2d<float, access::read> inTexture [[texture(0)]],
texture2d<float, access::write> outTexture [[texture(1)]],
uint2 gid [[thread_position_in_grid]])
{
const float4 color = inTexture.read(gid);
uint2 p;
p = uint2(gid.x * 2, gid.y * 2);
outtexture.write(color, p);
p = uint2(gid.x * 2 + 1, gid.y * 2);
outtexture.write(color, p);
p = uint2(gid.x * 2, gid.y * 2 + 1);
outtexture.write(color, p);
p = uint2(gid.x * 2 + 1, gid.y * 2 + 1);
outtexture.write(color, p);
}
但我不确定这是否正确。我怎么知道原始的“gid”与输入纹理中的坐标有关,但与输出纹理中的坐标无关?
【问题讨论】:
-
附带说明,如果您希望通过大型数据集最大限度地提高性能,我的经验表明,您可以通过使用固定 GPU 硬件来实现显着提高的性能。只需设置渲染管道,加载纹理,配置适当大小的渲染目标,将纹理采样器设置为“最近”放大率,然后让固定硬件进行缩放。在我进行的其他类似测试中,我发现固定硬件做这种事情的速度比计算内核快几倍。