@@ -459,32 +459,41 @@ export class NodeJSKernelBackend extends KernelBackend {
459
459
private getMappedInputTensorIds (
460
460
inputs : Tensor [ ] , inputTensorInfos : ModelTensorInfo [ ] ) {
461
461
const tensorIds = this . getInputTensorIds ( inputs ) ;
462
+ const newTensors = [ ] ;
462
463
for ( let i = 0 ; i < inputs . length ; i ++ ) {
463
464
if ( inputTensorInfos [ i ] != null ) {
464
465
if ( inputTensorInfos [ i ] . tfDtype === 'DT_UINT8' ) {
465
466
const data = Uint8Array . from ( inputs [ i ] . dataSync ( ) ) ;
466
467
const inputTensorId = this . binding . createTensor (
467
468
inputs [ i ] . shape , this . binding . TF_UINT8 , data ) ;
468
469
tensorIds [ i ] = inputTensorId ;
470
+ newTensors . push ( i ) ;
469
471
} else if ( inputTensorInfos [ i ] . tfDtype === 'DT_INT64' ) {
470
472
const data =
471
473
encodeInt32ArrayAsInt64 ( inputs [ i ] . dataSync ( ) as Int32Array ) ;
472
474
const inputTensorId = this . binding . createTensor (
473
475
inputs [ i ] . shape , this . binding . TF_INT64 , data ) ;
474
476
tensorIds [ i ] = inputTensorId ;
477
+ newTensors . push ( i ) ;
475
478
}
476
479
}
477
480
}
478
- return tensorIds ;
481
+ return { tensorIds, newTensors } ;
479
482
}
480
483
481
484
runSavedModel (
482
485
id : number , inputs : Tensor [ ] , inputTensorInfos : ModelTensorInfo [ ] ,
483
486
outputOpNames : string [ ] ) : Tensor [ ] {
487
+ const { tensorIds, newTensors} =
488
+ this . getMappedInputTensorIds ( inputs , inputTensorInfos ) ;
484
489
const outputMetadata = this . binding . runSavedModel (
485
- id , this . getMappedInputTensorIds ( inputs , inputTensorInfos ) ,
486
- inputTensorInfos . map ( info => info . name ) . join ( ',' ) ,
490
+ id , tensorIds , inputTensorInfos . map ( info => info . name ) . join ( ',' ) ,
487
491
outputOpNames . join ( ',' ) ) ;
492
+ for ( let i = 0 ; i < tensorIds . length ; i ++ ) {
493
+ if ( newTensors . includes ( i ) ) {
494
+ this . binding . deleteTensor ( tensorIds [ i ] ) ;
495
+ }
496
+ }
488
497
return outputMetadata . map ( m => this . createOutputTensor ( m ) ) ;
489
498
}
490
499
@@ -542,9 +551,10 @@ export class NodeJSKernelBackend extends KernelBackend {
542
551
}
543
552
const opAttrs : TFEOpAttr [ ] =
544
553
[ { name : 'T' , type : this . binding . TF_ATTR_TYPE , value : typeAttr } ] ;
545
-
546
- this . binding . executeOp (
547
- 'WriteScalarSummary' , opAttrs , this . getInputTensorIds ( inputArgs ) , 0 ) ;
554
+ const ids = this . getInputTensorIds ( inputArgs ) ;
555
+ this . binding . executeOp ( 'WriteScalarSummary' , opAttrs , ids , 0 ) ;
556
+ // release the tensorflow tensor for Int64Scalar value of step
557
+ this . binding . deleteTensor ( ids [ 1 ] ) ;
548
558
} ) ;
549
559
}
550
560
@@ -561,9 +571,10 @@ export class NodeJSKernelBackend extends KernelBackend {
561
571
// and places the values in 30 buckets, while WriteSummary expects a
562
572
// tensor which already describes the bucket widths and counts.
563
573
//
564
- // If we were to use WriteHistogramSummary, we wouldn't have to implement
565
- // the "bucketization" of the input tensor, but we also wouldn't have
566
- // control over the number of buckets, or the description of the graph.
574
+ // If we were to use WriteHistogramSummary, we wouldn't have to
575
+ // implement the "bucketization" of the input tensor, but we also
576
+ // wouldn't have control over the number of buckets, or the description
577
+ // of the graph.
567
578
//
568
579
// Therefore, we instead use WriteSummary, which makes it possible to
569
580
// support these features. However, the trade-off is that we have to
@@ -594,8 +605,10 @@ export class NodeJSKernelBackend extends KernelBackend {
594
605
const typeAttr = this . typeAttributeFromTensor ( buckets ) ;
595
606
const opAttrs : TFEOpAttr [ ] =
596
607
[ { name : 'T' , type : this . binding . TF_ATTR_TYPE , value : typeAttr } ] ;
597
- this . binding . executeOp (
598
- 'WriteSummary' , opAttrs , this . getInputTensorIds ( inputArgs ) , 0 ) ;
608
+ const ids = this . getInputTensorIds ( inputArgs ) ;
609
+ this . binding . executeOp ( 'WriteSummary' , opAttrs , ids , 0 ) ;
610
+ // release the tensorflow tensor for Int64Scalar value of step
611
+ this . binding . deleteTensor ( ids [ 1 ] ) ;
599
612
} ) ;
600
613
}
601
614
@@ -609,9 +622,10 @@ export class NodeJSKernelBackend extends KernelBackend {
609
622
*
610
623
* @param data A `Tensor` of any shape. Must be castable to `float32`
611
624
* @param bucketCount Optional positive `number`
612
- * @returns A `Tensor` of shape `[k, 3]` and type `float32`. The `i`th row is
613
- * a triple `[leftEdge, rightEdge, count]` for a single bucket. The value of
614
- * `k` is either `bucketCount`, `1` or `0`.
625
+ * @returns A `Tensor` of shape `[k, 3]` and type `float32`. The `i`th row
626
+ * is
627
+ * a triple `[leftEdge, rightEdge, count]` for a single bucket. The value
628
+ * of `k` is either `bucketCount`, `1` or `0`.
615
629
*/
616
630
private buckets ( data : Tensor , bucketCount ?: number ) : Tensor < tf . Rank > {
617
631
if ( data . size === 0 ) {
0 commit comments