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

Method constructor

tfjs-backend-webgl/src/pad_packed_gpu.ts:30–86  ·  view source on GitHub ↗
(
      xShape: number[], paddings: Array<[number, number]>,
      constantValue: number)

Source from the content-addressed store, hash-verified

28 customUniforms = [{name: 'value', type: 'float' as UniformType}];
29
30 constructor(
31 xShape: number[], paddings: Array<[number, number]>,
32 constantValue: number) {
33 this.outputShape = paddings.map(
34 (p, i) => p[0] /* beforePad */ + xShape[i] + p[1] /* afterPad */);
35 const rank = xShape.length;
36 const dtype = getCoordsDataType(rank);
37
38 const start = paddings.map(p => p[0]).join(',');
39 const end = paddings.map((p, i) => p[0] + xShape[i]).join(',');
40 const coords = getChannels('rc', rank);
41 const source = getChannels('source', rank);
42 const cLimit = `${coords[rank - 1]} < ${this.outputShape[rank - 1]}`;
43 const innerDims =
44 rank === 1 ? 'source' : `vec2(${source.slice(-2).join()})`;
45
46 const componentSetup = [
47 `${dtype} rc = outputLoc;`, `${coords[rank - 1]} += 1;
48 if(${cLimit}) {
49 `,
50 rank === 1 ? '' : `}
51 rc = outputLoc;
52 ${coords[rank - 2]} += 1;
53 if(${coords[rank - 2]} < ${this.outputShape[rank - 2]}) {`,
54 rank === 1 ? '' : ` ${coords[rank - 1]} += 1;
55 if(${cLimit}) {`
56 ];
57
58 const paddingArea = rank === 1 ?
59 'rc < start || rc >= end' :
60 'any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))';
61 let mainLoop = '';
62 for (let i = 0, j = rank === 1 ? 2 : 4; i < j; i++) {
63 mainLoop += `
64 ${componentSetup[i]}
65 if (${paddingArea}) {
66 result[${i}] = float(value);
67 } else {
68 ${dtype} source = rc - start;
69 result[${i}] = getChannel(getX(${source.join()}), ${innerDims});
70 }
71 `;
72 }
73 mainLoop += (rank === 1 ? `} ` : `}}`);
74
75 this.userCode = `
76 const ${dtype} start = ${dtype}(${start});
77 const ${dtype} end = ${dtype}(${end});
78
79 void main() {
80 ${dtype} outputLoc = getOutputCoords();
81 vec4 result = vec4(0.);
82 ${mainLoop}
83 setOutput(result);
84 }
85 `;
86 }
87}

Callers

nothing calls this directly

Calls 4

getCoordsDataTypeFunction · 0.90
getChannelsFunction · 0.90
joinMethod · 0.80
sliceMethod · 0.65

Tested by

no test coverage detected