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

Function getInputByOutputSnippet

tfjs-backend-webgpu/src/webgpu_program.ts:534–625  ·  view source on GitHub ↗
(
    inputInfo: InputInfo, outShape: number[], component: number,
    isFlatDispatchLayout: boolean)

Source from the content-addressed store, hash-verified

532}
533
534function getInputByOutputSnippet(
535 inputInfo: InputInfo, outShape: number[], component: number,
536 isFlatDispatchLayout: boolean): string {
537 const texName = inputInfo.name;
538 const texFuncSnippet = texName.charAt(0).toUpperCase() + texName.slice(1);
539
540 const funcName = 'get' + texFuncSnippet + 'ByOutput';
541
542 const inRank = inputInfo.shape.length;
543 const outRank = outShape.length;
544 const type = getCoordsDataType(outRank);
545
546 // If the inShape equals the outShape and the dispatch layout is flat, we can
547 // directly use |gl_GlobalInvocationID.x| as the index and don't need coords
548 // conversion between these two shapes.
549 if (util.arraysEqual(inputInfo.shape, outShape) && isFlatDispatchLayout) {
550 return `
551 fn ${funcName}Index(globalIndex : i32) -> ${typeSnippet(component)} {
552 return ${typeSnippet(component)}(${texName}[globalIndex]);
553 }
554
555 fn ${funcName}Coords(coords : ${type}) -> ${typeSnippet(component)} {
556 return ${typeSnippet(component)}(${texName}[${
557 outRank > 1 ? 'getOutputIndexFromCoords(coords)' :
558 'coords'}${component === 1 ? '' : ` / ${component}`}]);
559 }
560 `;
561 }
562
563 const broadcastDims =
564 backend_util.getBroadcastDims(inputInfo.shape, outShape);
565 const rankDiff = outRank - inRank;
566
567 let coordsSnippet = '';
568
569 if (inRank === 0) {
570 return `
571 fn ${funcName}Index(globalIndex : i32) -> ${typeSnippet(component)}{
572 return get${texFuncSnippet}();
573 }
574
575 fn ${funcName}Coords(coords : ${type}) -> ${typeSnippet(component)}{
576 return get${texFuncSnippet}();
577 }
578 `;
579 } else {
580 if (outRank < 2 && broadcastDims.length >= 1) {
581 coordsSnippet = 'coords = 0;';
582 } else {
583 coordsSnippet =
584 broadcastDims.map(d => `coords.${getCoordsXYZ(d + rankDiff)} = 0;`)
585 .join('\n');
586 }
587 }
588
589 let unpackedCoordsSnippet = '';
590 if (outRank < 2 && inRank > 0) {
591 unpackedCoordsSnippet = 'coords';

Callers 1

getInputSnippetFunction · 0.85

Calls 5

typeSnippetFunction · 0.85
getCoordsXYZFunction · 0.85
joinMethod · 0.80
getCoordsDataTypeFunction · 0.70
sliceMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…