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

Function inferShape

tfjs-core/src/tensor_util_env.ts:25–55  ·  view source on GitHub ↗
(
    val: TensorLike|WebGLData|WebGPUData, dtype?: DataType)

Source from the content-addressed store, hash-verified

23import {bytesPerElement} from './util_base';
24
25export function inferShape(
26 val: TensorLike|WebGLData|WebGPUData, dtype?: DataType): number[] {
27 let firstElem: typeof val = val;
28
29 if (isTypedArray(val)) {
30 return dtype === 'string' ? [] : [val.length];
31 }
32
33 if (isWebGLData(val)) {
34 const usedChannels = val.channels || 'RGBA';
35 return [val.height, val.width * usedChannels.length];
36 } else if (isWebGPUData(val)) {
37 return [val.buffer.size / (dtype == null ? 4 : bytesPerElement(dtype))];
38 }
39 if (!Array.isArray(val)) {
40 return []; // Scalar.
41 }
42 const shape: number[] = [];
43
44 while (Array.isArray(firstElem) ||
45 isTypedArray(firstElem) && dtype !== 'string') {
46 shape.push(firstElem.length);
47 firstElem = firstElem[0];
48 }
49 if (Array.isArray(val) &&
50 env().getBool('TENSORLIKE_CHECK_SHAPE_CONSISTENCY')) {
51 deepAssertShapeConsistency(val, shape, []);
52 }
53
54 return shape;
55}
56
57function deepAssertShapeConsistency(
58 val: TensorLike, shape: number[], indices: number[]) {

Callers 10

expectArraysPredicateFunction · 0.90
util_test.tsFile · 0.90
tensor2dFunction · 0.90
tensor5dFunction · 0.90
tensor4dFunction · 0.90
tensor3dFunction · 0.90
tensor1dFunction · 0.90
tensor6dFunction · 0.90
tensorFunction · 0.90
convertToTensorFunction · 0.85

Calls 8

isTypedArrayFunction · 0.90
isWebGLDataFunction · 0.90
isWebGPUDataFunction · 0.90
bytesPerElementFunction · 0.90
envFunction · 0.90
getBoolMethod · 0.80
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…