MCPcopy
hub / github.com/tensorflow/tfjs / readSync

Method readSync

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

Source from the content-addressed store, hash-verified

269 }
270
271 override readSync(dataId: DataId): BackendValues {
272 const texData = this.texData.get(dataId);
273 const {values, dtype, complexTensorInfos, slice, shape, isPacked} = texData;
274
275 // The presence of `slice` indicates this tensor is a shallow slice of a
276 // different tensor, and is using that original tensor's texture. Run
277 // `clone` in order to copy that texture and read from it.
278 if (slice != null) {
279 let program;
280 if (isPacked) {
281 program = new UnaryOpPackedProgram(shape, unary_op.CLONE);
282 } else {
283 program = new UnaryOpProgram(shape, unary_op.CLONE);
284 }
285 const res =
286 this.runWebGLProgram(program, [{dataId, shape, dtype}], dtype);
287 const data = this.readSync(res.dataId);
288 this.disposeIntermediateTensorInfo(res);
289 return data;
290 }
291 if (values != null) {
292 return this.convertAndCacheOnCPU(dataId);
293 }
294 if (dtype === 'string') {
295 return values;
296 }
297 const shouldTimeProgram = this.activeTimers != null;
298 let start: number;
299 if (shouldTimeProgram) {
300 start = util.now();
301 }
302
303 let result: Float32Array;
304 if (dtype === 'complex64') {
305 const realValues =
306 this.readSync(complexTensorInfos.real.dataId) as Float32Array;
307 const imagValues =
308 this.readSync(complexTensorInfos.imag.dataId) as Float32Array;
309 result = backend_util.mergeRealAndImagArrays(realValues, imagValues);
310 } else {
311 result = this.getValuesFromTexture(dataId);
312 }
313
314 if (shouldTimeProgram) {
315 this.downloadWaitMs += util.now() - start;
316 }
317 return this.convertAndCacheOnCPU(dataId, result);
318 }
319
320 override async read(dataId: DataId): Promise<BackendValues> {
321 if (this.pendingRead.has(dataId)) {

Callers 1

bufferSyncMethod · 0.95

Calls 6

runWebGLProgramMethod · 0.95
convertAndCacheOnCPUMethod · 0.95
getValuesFromTextureMethod · 0.95
nowMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected