2023-06-22 10:58:07 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2023 Nomic, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is licensed under the terms of the Software for Open Models License (SOM),
|
|
|
|
* version 1.0, as detailed in the LICENSE_SOM.txt file. A copy of this license should accompany
|
|
|
|
* this software. Except as expressly granted in the SOM license, all rights are reserved by Nomic, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#version 450
|
|
|
|
|
2023-09-21 17:00:10 +00:00
|
|
|
#include "common.comp"
|
2023-06-22 10:58:07 +00:00
|
|
|
|
2023-10-03 01:00:48 +00:00
|
|
|
#define nth 512
|
2023-06-22 10:58:07 +00:00
|
|
|
|
|
|
|
layout(local_size_x = nth) in;
|
|
|
|
|
|
|
|
layout(binding = 0) buffer restrict readonly tensorIn { float in_[]; };
|
|
|
|
layout(binding = 1) buffer restrict tensorOut { float out_[]; };
|
|
|
|
|
|
|
|
layout(push_constant) uniform PushConstants {
|
|
|
|
uint inOff;
|
|
|
|
uint outOff;
|
|
|
|
uint ne00;
|
|
|
|
uint nb01;
|
|
|
|
float eps;
|
|
|
|
} pcs;
|
|
|
|
|
|
|
|
shared float sum[nth];
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
const uint x = (gl_WorkGroupID.x*pcs.nb01/4) + pcs.inOff; // Based from in_
|
|
|
|
|
|
|
|
// parallel sum
|
|
|
|
sum[gl_LocalInvocationID.x] = 0.0;
|
|
|
|
for (uint i00 = gl_LocalInvocationID.x; i00 < pcs.ne00; i00 += nth) {
|
|
|
|
sum[gl_LocalInvocationID.x] += in_[x+i00] * in_[x+i00];
|
|
|
|
}
|
|
|
|
|
|
|
|
// reduce
|
|
|
|
barrier();
|
|
|
|
memoryBarrierShared();
|
|
|
|
[[unroll]] for (uint i = nth/2; i > 0; i /= 2) {
|
|
|
|
if (gl_LocalInvocationID.x < i) {
|
|
|
|
sum[gl_LocalInvocationID.x] += sum[gl_LocalInvocationID.x + i];
|
|
|
|
}
|
|
|
|
barrier();
|
|
|
|
memoryBarrierShared();
|
|
|
|
}
|
|
|
|
|
|
|
|
// broadcast
|
|
|
|
if (gl_LocalInvocationID.x == 0) {
|
|
|
|
sum[0] /= float(pcs.ne00);
|
|
|
|
}
|
|
|
|
barrier();
|
|
|
|
memoryBarrierShared();
|
|
|
|
|
|
|
|
const float scale = 1.0f/sqrt(sum[0] + pcs.eps);
|
|
|
|
|
2023-10-03 01:00:48 +00:00
|
|
|
const uint y = (gl_WorkGroupID.x*pcs.ne00) + pcs.outOff; // Based from out_
|
2023-06-22 10:58:07 +00:00
|
|
|
for (uint i00 = gl_LocalInvocationID.x; i00 < pcs.ne00; i00 += nth) {
|
|
|
|
out_[y+i00] = in_[x+i00] * scale;
|
|
|
|
}
|
|
|
|
}
|