MCPcopy Create free account
hub / github.com/tensorflow/tfjs / getUserCode

Method getUserCode

tfjs-backend-webgpu/src/softmax_webgpu.ts:41–103  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39 }
40
41 getUserCode(): string {
42 const userCode = `
43 var<workgroup> buf : array<f32, ${this.workgroupSize[0]}>;
44 var<workgroup> rowMaxShared : f32;
45 var<workgroup> rowSumShared : f32;
46 const blockSize = ${this.workgroupSize[0]};
47 ${main('index')} {
48 let row = index / blockSize;
49 let tid = i32(localId.x);
50 let cols = uniforms.outShape[1];
51
52 var threadMax = -3.402823e+38f;
53 for (var col = tid; col < cols; col += blockSize) {
54 let value = getLogits(row, col);
55 threadMax = max(threadMax, value);
56 }
57 if (tid < cols) {
58 buf[tid] = threadMax;
59 }
60 workgroupBarrier();
61
62 var reduceSize = min(cols, blockSize);
63 for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) {
64 reduceSize = currSize + (reduceSize & 1);
65 if (tid < currSize) {
66 buf[tid] = max(buf[tid], buf[tid + reduceSize]);
67 }
68 workgroupBarrier();
69 }
70
71 if (tid == 0) {
72 rowMaxShared = buf[0];
73 }
74 workgroupBarrier();
75
76 var threadSum = 0.0;
77 for (var col = tid; col < cols; col += blockSize) {
78 let subExp = exp(getLogits(row, col) - rowMaxShared);
79 threadSum += subExp;
80 }
81 buf[tid] = threadSum;
82 workgroupBarrier();
83
84 for (var currSize = blockSize >> 1; currSize > 0; currSize = currSize >> 1) {
85 if (tid < currSize) {
86 buf[tid] = buf[tid] + buf[tid + currSize];
87 }
88 workgroupBarrier();
89 }
90
91 if (tid == 0) {
92 rowSumShared = buf[0];
93 }
94 workgroupBarrier();
95
96 for (var col = tid; col < cols; col += blockSize) {
97 let value = exp(getLogits(row, col) - rowMaxShared) / rowSumShared;
98 setOutputAtCoords(row, col, value);

Callers

nothing calls this directly

Calls 1

mainFunction · 0.50

Tested by

no test coverage detected