(
val: TensorLike|WebGLData|WebGPUData, dtype?: DataType)
| 23 | import {bytesPerElement} from './util_base'; |
| 24 | |
| 25 | export 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 | |
| 57 | function deepAssertShapeConsistency( |
| 58 | val: TensorLike, shape: number[], indices: number[]) { |
no test coverage detected
searching dependent graphs…