|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {getMainHeaderString as main, WebGPUProgram} from './webgpu_program'; |
| 19 | +import {computeDispatch, flatDispatchLayout} from './webgpu_util'; |
| 20 | + |
| 21 | +export class ResizeNearestNeigborBackpropProgram implements WebGPUProgram { |
| 22 | + outputShape: number[]; |
| 23 | + shaderKey: string; |
| 24 | + dispatchLayout: {x: number[]}; |
| 25 | + dispatch: [number, number, number]; |
| 26 | + variableNames = ['dy']; |
| 27 | + uniforms = |
| 28 | + `effectiveXSize : vec2<i32>, effectiveYSize : vec2<i32>, invHeightScale : f32, invWidthScale : f32, |
| 29 | + winHeight : i32, winWidth : i32,`; |
| 30 | + workgroupSize: [number, number, number] = [64, 1, 1]; |
| 31 | + alignCorners: boolean; |
| 32 | + size = true; |
| 33 | + |
| 34 | + constructor( |
| 35 | + inputShape: [number, number, number, number], alignCorners: boolean) { |
| 36 | + this.outputShape = inputShape; |
| 37 | + |
| 38 | + this.dispatchLayout = flatDispatchLayout(this.outputShape); |
| 39 | + this.dispatch = computeDispatch( |
| 40 | + this.dispatchLayout, this.outputShape, this.workgroupSize); |
| 41 | + |
| 42 | + this.alignCorners = alignCorners; |
| 43 | + this.shaderKey = `resizeNearestNeigborBackprop_${alignCorners}`; |
| 44 | + } |
| 45 | + |
| 46 | + getUserCode(): string { |
| 47 | + const userCode = ` |
| 48 | + ${main('index')} { |
| 49 | + if (index < uniforms.size) { |
| 50 | + let coords = getOutputCoords(); |
| 51 | + let b = coords[0]; |
| 52 | + let d = coords[3]; |
| 53 | + let r = coords[1]; |
| 54 | + let c = coords[2]; |
| 55 | +
|
| 56 | + var accumulator = 0.0; |
| 57 | +
|
| 58 | + // Compute bounds for where in dy we will look |
| 59 | + let startRLerp = floor(f32(r) * uniforms.invHeightScale); |
| 60 | + let startDyR = i32(floor(startRLerp - f32(uniforms.winHeight / 2))); |
| 61 | +
|
| 62 | + let startCLerp = floor(f32(c) * uniforms.invWidthScale); |
| 63 | + let startDyC = i32(floor(startCLerp - f32(uniforms.winWidth / 2))); |
| 64 | +
|
| 65 | + // Loop over dy |
| 66 | + for (var dyROffset = 0; dyROffset < uniforms.winHeight; dyROffset++) { |
| 67 | + let dyR = startDyR + dyROffset; |
| 68 | +
|
| 69 | + // Guard against the window exceeding the bounds of dy |
| 70 | + if (dyR < 0 || dyR >= uniforms.dyShape[1]) { |
| 71 | + continue; |
| 72 | + } |
| 73 | +
|
| 74 | + for (var dyCOffset = 0; dyCOffset < uniforms.winWidth; dyCOffset++) { |
| 75 | + let dyC = startDyC + dyCOffset; |
| 76 | +
|
| 77 | + // Guard against the window exceeding the bounds of dy |
| 78 | + if (dyC < 0 || dyC >= uniforms.dyShape[2]) { |
| 79 | + continue; |
| 80 | + } |
| 81 | +
|
| 82 | + let sourceFracRow = f32(uniforms.effectiveXSize[0]) * |
| 83 | + (f32(dyR) / f32(uniforms.effectiveYSize[0])); |
| 84 | +
|
| 85 | + let sourceFracCol = f32(uniforms.effectiveXSize[1]) * |
| 86 | + (f32(dyC) / f32(uniforms.effectiveYSize[1])); |
| 87 | +
|
| 88 | + let sourceNearestRow = |
| 89 | + i32(min(f32(uniforms.outShape[1] - 1), |
| 90 | + ${ |
| 91 | + this.alignCorners ? 'floor(sourceFracRow + 0.5)' : |
| 92 | + 'floor(sourceFracRow)'})); |
| 93 | +
|
| 94 | + let sourceNearestCol = |
| 95 | + i32(min(f32(uniforms.outShape[2] - 1), |
| 96 | + ${ |
| 97 | + this.alignCorners ? 'floor(sourceFracCol + 0.5)' : |
| 98 | + 'floor(sourceFracCol)'})); |
| 99 | +
|
| 100 | + if (r == sourceNearestRow && c == sourceNearestCol) { |
| 101 | + accumulator += getDy(b, dyR, dyC, d); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + // End loop over dy |
| 106 | +
|
| 107 | + setOutputAtIndex(index, accumulator); |
| 108 | + } |
| 109 | + } |
| 110 | + `; |
| 111 | + return userCode; |
| 112 | + } |
| 113 | +} |
0 commit comments