如何在金属着色器中执行2个矢量的外产物?
因此,我正在开发一个神经网络以在GPU上在iOS中运行的神经网络,因此,使用我需要的矩阵符号(为了将错误反向传播)能够执行2个向量的外产品。
// Outer product of vector A and Vector B
kernel void outerProduct(const device float *inVectorA [[ buffer(0) ]],
const device float *inVectorB [[ buffer(1) ]],
device float *outVector [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]]) {
outVector[id] = inVectorA[id] * inVectorB[***?***]; // How to find this position on the thread group (or grid)?
}
So I'm developing a Neural Network to run in iOS on the GPU, so using matrix notation I need (in order to backpropagate the errors) be able to perform an outer product of 2 vectors.
// Outer product of vector A and Vector B
kernel void outerProduct(const device float *inVectorA [[ buffer(0) ]],
const device float *inVectorB [[ buffer(1) ]],
device float *outVector [[ buffer(2) ]],
uint id [[ thread_position_in_grid ]]) {
outVector[id] = inVectorA[id] * inVectorB[***?***]; // How to find this position on the thread group (or grid)?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用
thread_position_in_grid
错误地。如果要派遣2D网格,则应是uint2
或ushort2
,否则仅获得x
坐标。请参阅金属阴影语言规范)的表5.7。我不确定我们在谈论哪种外部产品,但我认为输出应该是矩阵。如果您要线性存储它,则计算
Outvector
的代码应该看起来像这样:另外,如果您要派发网格的大小,则
invectora
xInvectorB ,您可以在内核参数上使用属性
threads_per_grid
以找出网格的大小。另外,您可以将向量的大小与向量本身一起传递。
You are using
thread_position_in_grid
incorrectly. If you are dispatching a 2D grid, it should beuint2
orushort2
, otherwise it only gets thex
coordinate. Refer to table 5.7 in Metal Shading Language specification.I'm not sure which outer product are we talking about, but I think the output should be a matrix. If you are storing it linearly, then your code to calculate the
outVector
should look something like this:Also, if you are dispatching a grid exactly the size of
inVectorA
xinVectorB
, you can use attributethreads_per_grid
on a kernel argument to find out how big the grid is.Alternatively, you can just pass the sizes of the vectors alongside the vectors themselves.
我惊讶地发现金属没有2D跨产品(又称内部产品),所以在这里
回答您的问题:
I was surprised to learn that Metal doesn't have a 2D cross product (aka inner product), so here it is
So to answer you question: