MCPcopy Index your code
hub / github.com/tensorflow/tfjs / uploadToGPU

Method uploadToGPU

tfjs-backend-webgl/src/backend_webgl.ts:1096–1190  ·  view source on GitHub ↗
(dataId: DataId)

Source from the content-addressed store, hash-verified

1094 }
1095
1096 uploadToGPU(dataId: DataId): void {
1097 const texData = this.texData.get(dataId);
1098 const {shape, dtype, values, texture, usage, isPacked} = texData;
1099
1100 if (texture != null) {
1101 // Array is already on GPU. No-op.
1102 return;
1103 }
1104 const shouldTimeProgram = this.activeTimers != null;
1105 let start: number;
1106 if (shouldTimeProgram) {
1107 start = util.now();
1108 }
1109
1110 let texShape = texData.texShape;
1111 if (texShape == null) {
1112 // This texShape may not be the final texture shape. For packed or dense
1113 // textures, the texShape will be changed when textures are created.
1114 texShape = webgl_util.getTextureShapeFromLogicalShape(shape, isPacked);
1115 texData.texShape = texShape;
1116 }
1117
1118 if (values != null) {
1119 const shapeAs3D = webgl_util.getShapeAs3D(shape);
1120
1121 let program;
1122 let width = texShape[1], height = texShape[0];
1123 const isByteArray =
1124 values instanceof Uint8Array || values instanceof Uint8ClampedArray;
1125
1126 // texture for float array is PhysicalTextureType.PACKED_2X2_FLOAT32, we
1127 // need to make sure the upload uses the same packed size
1128 if (isPacked || !isByteArray) {
1129 [width, height] = tex_util.getPackedMatrixTextureShapeWidthHeight(
1130 texShape[0], texShape[1]);
1131 }
1132
1133 if (isPacked) {
1134 program = new EncodeMatrixPackedProgram(shapeAs3D, isByteArray);
1135 } else {
1136 program = new EncodeMatrixProgram(shapeAs3D, isByteArray);
1137 }
1138
1139 // TexShape for float array needs to be the original shape, which byte
1140 // array needs to be packed size. This allow the data upload shape to be
1141 // matched with texture creation logic.
1142 const tempDenseInputTexShape: [number, number] =
1143 isByteArray ? [height, width] : texShape;
1144 const tempDenseInputHandle =
1145 this.makeTensorInfo(tempDenseInputTexShape, dtype);
1146 const tempDenseInputTexData =
1147 this.texData.get(tempDenseInputHandle.dataId);
1148 if (isByteArray) {
1149 tempDenseInputTexData.usage = TextureUsage.PIXELS;
1150 } else {
1151 tempDenseInputTexData.usage = TextureUsage.UPLOAD;
1152 }
1153 tempDenseInputTexData.texShape = tempDenseInputTexShape;

Callers 3

getTextureMethod · 0.95
runWebGLProgramMethod · 0.95
sliceFunction · 0.45

Calls 11

makeTensorInfoMethod · 0.95
getTextureMethod · 0.95
runWebGLProgramMethod · 0.95
disposeDataMethod · 0.95
acquireTextureMethod · 0.95
envFunction · 0.90
deleteMethod · 0.80
nowMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected