(nameOrFn: string|ScopeFn<T>, fn?: ScopeFn<T>)
| 439 | } |
| 440 | |
| 441 | tidy<T extends TensorContainer>(nameOrFn: string|ScopeFn<T>, fn?: ScopeFn<T>): |
| 442 | T { |
| 443 | let name: string = null; |
| 444 | if (fn == null) { |
| 445 | // Called with only 1 argument. |
| 446 | if (typeof nameOrFn !== 'function') { |
| 447 | throw new Error('Please provide a function to tidy()'); |
| 448 | } |
| 449 | fn = nameOrFn; |
| 450 | } else { |
| 451 | // Called with 2 arguments. |
| 452 | if (typeof nameOrFn !== 'string' && !(nameOrFn instanceof String)) { |
| 453 | throw new Error( |
| 454 | 'When calling with two arguments, the first argument ' + |
| 455 | 'to tidy() must be a string'); |
| 456 | } |
| 457 | if (typeof fn !== 'function') { |
| 458 | throw new Error( |
| 459 | 'When calling with two arguments, the 2nd argument ' + |
| 460 | 'to tidy() must be a function'); |
| 461 | } |
| 462 | name = nameOrFn as string; |
| 463 | // TODO(nsthorat,smilkov): Do operation logging and performance |
| 464 | // profiling. |
| 465 | } |
| 466 | let result: T; |
| 467 | return this.scopedRun( |
| 468 | () => this.startScope(name), () => this.endScope(result), () => { |
| 469 | result = fn(); |
| 470 | if (result instanceof Promise) { |
| 471 | console.error('Cannot return a Promise inside of tidy.'); |
| 472 | } |
| 473 | return result; |
| 474 | }); |
| 475 | } |
| 476 | |
| 477 | private scopedRun<T>(start: () => void, end: () => void, f: () => T): T { |
| 478 | start(); |
no test coverage detected