| 333 | * @return The inferred shape where -1 is replaced with the inferred size. |
| 334 | */ |
| 335 | export function inferFromImplicitShape( |
| 336 | shape: number[], size: number): number[] { |
| 337 | let shapeProd = 1; |
| 338 | let implicitIdx = -1; |
| 339 | |
| 340 | for (let i = 0; i < shape.length; ++i) { |
| 341 | if (shape[i] >= 0) { |
| 342 | shapeProd *= shape[i]; |
| 343 | } else if (shape[i] === -1) { |
| 344 | if (implicitIdx !== -1) { |
| 345 | throw Error( |
| 346 | `Shapes can only have 1 implicit size. ` + |
| 347 | `Found -1 at dim ${implicitIdx} and dim ${i}`); |
| 348 | } |
| 349 | implicitIdx = i; |
| 350 | } else if (shape[i] < 0) { |
| 351 | throw Error(`Shapes can not be < 0. Found ${shape[i]} at dim ${i}`); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if (implicitIdx === -1) { |
| 356 | if (size > 0 && size !== shapeProd) { |
| 357 | throw Error(`Size(${size}) must match the product of shape ${shape}`); |
| 358 | } |
| 359 | return shape; |
| 360 | } |
| 361 | |
| 362 | if (shapeProd === 0) { |
| 363 | throw Error( |
| 364 | `Cannot infer the missing size in [${shape}] when ` + |
| 365 | `there are 0 elements`); |
| 366 | } |
| 367 | if (size % shapeProd !== 0) { |
| 368 | throw Error( |
| 369 | `The implicit shape can't be a fractional number. ` + |
| 370 | `Got ${size} / ${shapeProd}`); |
| 371 | } |
| 372 | |
| 373 | const newShape = shape.slice(); |
| 374 | newShape[implicitIdx] = size / shapeProd; |
| 375 | return newShape; |
| 376 | } |
| 377 | |
| 378 | export function parseAxisParam( |
| 379 | axis: number|number[], shape: number[]): number[] { |