@@ -51,7 +51,8 @@ import * as binaryop_complex_gpu from './webgl/binaryop_complex_gpu';
51
51
import { BinaryOpComplexProgram } from './webgl/binaryop_complex_gpu' ;
52
52
import * as binaryop_gpu from './webgl/binaryop_gpu' ;
53
53
import { BinaryOpProgram } from './webgl/binaryop_gpu' ;
54
- import { BinaryOpPackedProgram , PACKED_DIV , PACKED_INT_DIV , PACKED_POW } from './webgl/binaryop_packed_gpu' ;
54
+ import * as binaryop_packed_gpu from './webgl/binaryop_packed_gpu' ;
55
+ import { BinaryOpPackedProgram } from './webgl/binaryop_packed_gpu' ;
55
56
import { ClipProgram } from './webgl/clip_gpu' ;
56
57
import { ClipPackedProgram } from './webgl/clip_packed_gpu' ;
57
58
import { ComplexAbsProgram } from './webgl/complex_abs_gpu' ;
@@ -768,8 +769,8 @@ export class MathBackendWebGL implements KernelBackend {
768
769
769
770
const dtype = upcastType ( a . dtype , b . dtype ) ;
770
771
771
- const program = new MatMulPackedProgram ( a . shape ,
772
- [ batch , outerShapeA , outerShapeB ] , transposeA , transposeB ) ;
772
+ const program = new MatMulPackedProgram (
773
+ a . shape , [ batch , outerShapeA , outerShapeB ] , transposeA , transposeB ) ;
773
774
const output =
774
775
this . makePackedTensor ( program . outputShape , dtype ) as Tensor3D ;
775
776
return this . compileAndRun < Tensor3D > ( program , [ a , b ] , output ) ;
@@ -784,8 +785,9 @@ export class MathBackendWebGL implements KernelBackend {
784
785
785
786
const dtype = upcastType ( a . dtype , b . dtype ) ;
786
787
787
- const program = new MatMulPackedProgram ( a . shape ,
788
- [ batch , outerShapeA , outerShapeB ] , transposeA , transposeB , ! ! bias ,
788
+ const program = new MatMulPackedProgram (
789
+ a . shape , [ batch , outerShapeA , outerShapeB ] , transposeA , transposeB ,
790
+ ! ! bias ,
789
791
activation ? mapActivationToShaderProgram ( activation , true ) : null ) ;
790
792
const output =
791
793
this . makePackedTensor ( program . outputShape , dtype ) as Tensor3D ;
@@ -1099,12 +1101,18 @@ export class MathBackendWebGL implements KernelBackend {
1099
1101
}
1100
1102
1101
1103
equal ( a : Tensor , b : Tensor ) : Tensor {
1104
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1105
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . EQUAL , 'bool' ) ;
1106
+ }
1102
1107
const program = new BinaryOpProgram ( binaryop_gpu . EQUAL , a . shape , b . shape ) ;
1103
1108
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
1104
1109
return this . compileAndRun ( program , [ a , b ] , output ) ;
1105
1110
}
1106
1111
1107
1112
notEqual ( a : Tensor , b : Tensor ) : Tensor {
1113
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1114
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . NOT_EQUAL , 'bool' ) ;
1115
+ }
1108
1116
const program =
1109
1117
new BinaryOpProgram ( binaryop_gpu . NOT_EQUAL , a . shape , b . shape ) ;
1110
1118
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
@@ -1116,12 +1124,19 @@ export class MathBackendWebGL implements KernelBackend {
1116
1124
return this . cpuBackend . less ( a , b ) ;
1117
1125
}
1118
1126
1127
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1128
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . LESS , 'bool' ) ;
1129
+ }
1130
+
1119
1131
const program = new BinaryOpProgram ( binaryop_gpu . LESS , a . shape , b . shape ) ;
1120
1132
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
1121
1133
return this . compileAndRun ( program , [ a , b ] , output ) ;
1122
1134
}
1123
1135
1124
1136
lessEqual ( a : Tensor , b : Tensor ) : Tensor {
1137
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1138
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . LESS_EQUAL , 'bool' ) ;
1139
+ }
1125
1140
const program =
1126
1141
new BinaryOpProgram ( binaryop_gpu . LESS_EQUAL , a . shape , b . shape ) ;
1127
1142
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
@@ -1133,12 +1148,20 @@ export class MathBackendWebGL implements KernelBackend {
1133
1148
return this . cpuBackend . greater ( a , b ) ;
1134
1149
}
1135
1150
1151
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1152
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . GREATER , 'bool' ) ;
1153
+ }
1154
+
1136
1155
const program = new BinaryOpProgram ( binaryop_gpu . GREATER , a . shape , b . shape ) ;
1137
1156
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
1138
1157
return this . compileAndRun ( program , [ a , b ] , output ) ;
1139
1158
}
1140
1159
1141
1160
greaterEqual ( a : Tensor , b : Tensor ) : Tensor {
1161
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1162
+ return this . packedBinaryOp (
1163
+ a , b , binaryop_packed_gpu . GREATER_EQUAL , 'bool' ) ;
1164
+ }
1142
1165
const program =
1143
1166
new BinaryOpProgram ( binaryop_gpu . GREATER_EQUAL , a . shape , b . shape ) ;
1144
1167
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
@@ -1151,13 +1174,19 @@ export class MathBackendWebGL implements KernelBackend {
1151
1174
}
1152
1175
1153
1176
logicalAnd ( a : Tensor , b : Tensor ) : Tensor {
1177
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1178
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . LOGICAL_AND , 'bool' ) ;
1179
+ }
1154
1180
const program =
1155
1181
new BinaryOpProgram ( binaryop_gpu . LOGICAL_AND , a . shape , b . shape ) ;
1156
1182
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
1157
1183
return this . compileAndRun ( program , [ a , b ] , output ) ;
1158
1184
}
1159
1185
1160
1186
logicalOr ( a : Tensor , b : Tensor ) : Tensor {
1187
+ if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1188
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . LOGICAL_OR , 'bool' ) ;
1189
+ }
1161
1190
const program =
1162
1191
new BinaryOpProgram ( binaryop_gpu . LOGICAL_OR , a . shape , b . shape ) ;
1163
1192
const output = this . makeOutputArray ( program . outputShape , 'bool' ) ;
@@ -1198,12 +1227,17 @@ export class MathBackendWebGL implements KernelBackend {
1198
1227
return this . cpuBackend . minimum ( a , b ) ;
1199
1228
}
1200
1229
1201
- const program = new BinaryOpProgram ( binaryop_gpu . MIN , a . shape , b . shape ) ;
1202
- return this . compileAndRun ( program , [ a , b ] ) ;
1230
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1231
+ new BinaryOpPackedProgram ( binaryop_packed_gpu . MIN , a . shape , b . shape ) :
1232
+ new BinaryOpProgram ( binaryop_gpu . MIN , a . shape , b . shape ) ;
1233
+ const customSetup = program . getCustomSetupFunc ( ) ;
1234
+ return this . compileAndRun ( program , [ a , b ] , null , customSetup ) ;
1203
1235
}
1204
1236
1205
1237
mod ( a : Tensor , b : Tensor ) : Tensor {
1206
- const program = new BinaryOpProgram ( binaryop_gpu . MOD , a . shape , b . shape ) ;
1238
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1239
+ new BinaryOpPackedProgram ( binaryop_packed_gpu . MOD , a . shape , b . shape ) :
1240
+ new BinaryOpProgram ( binaryop_gpu . MOD , a . shape , b . shape ) ;
1207
1241
const customSetup = program . getCustomSetupFunc ( ) ;
1208
1242
return this . compileAndRun ( program , [ a , b ] , null , customSetup ) ;
1209
1243
}
@@ -1222,8 +1256,11 @@ export class MathBackendWebGL implements KernelBackend {
1222
1256
return this . cpuBackend . maximum ( a , b ) ;
1223
1257
}
1224
1258
1225
- const program = new BinaryOpProgram ( binaryop_gpu . MAX , a . shape , b . shape ) ;
1226
- return this . compileAndRun ( program , [ a , b ] ) ;
1259
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1260
+ new BinaryOpPackedProgram ( binaryop_packed_gpu . MAX , a . shape , b . shape ) :
1261
+ new BinaryOpProgram ( binaryop_gpu . MAX , a . shape , b . shape ) ;
1262
+ const customSetup = program . getCustomSetupFunc ( ) ;
1263
+ return this . compileAndRun ( program , [ a , b ] , null , customSetup ) ;
1227
1264
}
1228
1265
1229
1266
all ( x : Tensor , axes : number [ ] ) : Tensor {
@@ -1245,7 +1282,9 @@ export class MathBackendWebGL implements KernelBackend {
1245
1282
}
1246
1283
1247
1284
squaredDifference ( a : Tensor , b : Tensor ) : Tensor {
1248
- const program =
1285
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1286
+ new BinaryOpPackedProgram (
1287
+ binaryop_gpu . SQUARED_DIFFERENCE , a . shape , b . shape ) :
1249
1288
new BinaryOpProgram ( binaryop_gpu . SQUARED_DIFFERENCE , a . shape , b . shape ) ;
1250
1289
return this . compileAndRun ( program , [ a , b ] ) ;
1251
1290
}
@@ -1254,7 +1293,7 @@ export class MathBackendWebGL implements KernelBackend {
1254
1293
const op = binaryop_gpu . DIV ;
1255
1294
const outputDtype = 'float32' ;
1256
1295
if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1257
- return this . packedBinaryOp ( a , b , PACKED_DIV , outputDtype ) ;
1296
+ return this . packedBinaryOp ( a , b , binaryop_packed_gpu . DIV , outputDtype ) ;
1258
1297
}
1259
1298
const program = new BinaryOpProgram ( op , a . shape , b . shape ) ;
1260
1299
const output = this . makeOutputArray ( program . outputShape , outputDtype ) ;
@@ -1265,7 +1304,8 @@ export class MathBackendWebGL implements KernelBackend {
1265
1304
const op = binaryop_gpu . INT_DIV ;
1266
1305
const outputDtype = 'int32' ;
1267
1306
if ( ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ) {
1268
- return this . packedBinaryOp ( a , b , PACKED_INT_DIV , outputDtype ) ;
1307
+ return this . packedBinaryOp (
1308
+ a , b , binaryop_packed_gpu . INT_DIV , outputDtype ) ;
1269
1309
}
1270
1310
const program = new BinaryOpProgram ( op , a . shape , b . shape ) ;
1271
1311
const output = this . makeOutputArray ( program . outputShape , outputDtype ) ;
@@ -1285,7 +1325,8 @@ export class MathBackendWebGL implements KernelBackend {
1285
1325
return this . compileAndRun < Tensor > ( program , [ a , b ] , output ) ;
1286
1326
}
1287
1327
1288
- private packedBinaryOp ( a : Tensor , b : Tensor , op : string , dtype : DataType ) {
1328
+ private packedBinaryOp (
1329
+ a : TensorHandle , b : TensorHandle , op : string , dtype : DataType ) {
1289
1330
const program = new BinaryOpPackedProgram ( op , a . shape , b . shape ) ;
1290
1331
const output = this . makePackedTensor ( program . outputShape , dtype ) as Tensor ;
1291
1332
return this . compileAndRun < Tensor > ( program , [ a , b ] , output ) ;
@@ -1305,14 +1346,14 @@ export class MathBackendWebGL implements KernelBackend {
1305
1346
] . map ( complexParts => {
1306
1347
const [ aPart , bPart ] = complexParts ;
1307
1348
1349
+ const aHandle = this . makeComplexComponentTensorHandle ( a , aPart ) ;
1350
+ const bHandle = this . makeComplexComponentTensorHandle ( b , bPart ) ;
1351
+
1308
1352
const program = new BinaryOpProgram ( op , a . shape , b . shape ) ;
1309
1353
const output = this . makeOutputArray (
1310
1354
program . outputShape ,
1311
1355
upcastType ( aPart . dtype , bPart . dtype ) ) as Tensor ;
1312
1356
1313
- const aHandle = this . makeComplexComponentTensorHandle ( a , aPart ) ;
1314
- const bHandle = this . makeComplexComponentTensorHandle ( b , bPart ) ;
1315
-
1316
1357
return this . compileAndRun < Tensor > ( program , [ aHandle , bHandle ] , output ) ;
1317
1358
} ) ;
1318
1359
@@ -1362,7 +1403,7 @@ export class MathBackendWebGL implements KernelBackend {
1362
1403
pow < T extends Tensor > ( a : T , b : Tensor ) : T {
1363
1404
const usePackedOp = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ;
1364
1405
const program = usePackedOp ?
1365
- new BinaryOpPackedProgram ( PACKED_POW , a . shape , b . shape ) :
1406
+ new BinaryOpPackedProgram ( binaryop_packed_gpu . POW , a . shape , b . shape ) :
1366
1407
new BinaryOpProgram ( binaryop_gpu . POW , a . shape , b . shape ) ;
1367
1408
const dtype = upcastType ( a . dtype , b . dtype ) ;
1368
1409
const output = usePackedOp ?
@@ -1454,7 +1495,9 @@ export class MathBackendWebGL implements KernelBackend {
1454
1495
}
1455
1496
1456
1497
prelu < T extends Tensor > ( x : T , alpha : T ) : T {
1457
- const program =
1498
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1499
+ new BinaryOpPackedProgram (
1500
+ binaryop_packed_gpu . PRELU , x . shape , alpha . shape ) :
1458
1501
new BinaryOpProgram ( binaryop_gpu . PRELU , x . shape , alpha . shape ) ;
1459
1502
return this . compileAndRun ( program , [ x , alpha ] ) as T ;
1460
1503
}
@@ -1465,7 +1508,9 @@ export class MathBackendWebGL implements KernelBackend {
1465
1508
}
1466
1509
1467
1510
eluDer < T extends Tensor > ( dy : T , y : T ) : T {
1468
- const program =
1511
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1512
+ new BinaryOpPackedProgram (
1513
+ binaryop_packed_gpu . ELU_DER , dy . shape , y . shape ) :
1469
1514
new BinaryOpProgram ( binaryop_gpu . ELU_DER , dy . shape , y . shape ) ;
1470
1515
return this . compileAndRun ( program , [ dy , y ] ) as T ;
1471
1516
}
@@ -1550,8 +1595,11 @@ export class MathBackendWebGL implements KernelBackend {
1550
1595
}
1551
1596
1552
1597
atan2 < T extends Tensor > ( a : T , b : T ) : T {
1553
- const program = new BinaryOpProgram ( binaryop_gpu . ATAN2 , a . shape , b . shape ) ;
1554
- return this . compileAndRun ( program , [ a , b ] ) as T ;
1598
+ const program = ENV . get ( 'WEBGL_PACK_BINARY_OPERATIONS' ) ?
1599
+ new BinaryOpPackedProgram ( binaryop_packed_gpu . ATAN2 , a . shape , b . shape ) :
1600
+ new BinaryOpProgram ( binaryop_gpu . ATAN2 , a . shape , b . shape ) ;
1601
+ const customSetup = program . getCustomSetupFunc ( ) ;
1602
+ return this . compileAndRun ( program , [ a , b ] , null , customSetup ) as T ;
1555
1603
}
1556
1604
1557
1605
sinh < T extends Tensor > ( x : T ) : T {
@@ -1680,12 +1728,13 @@ export class MathBackendWebGL implements KernelBackend {
1680
1728
1681
1729
const im2ColProgram =
1682
1730
new Im2ColProgram ( x2ColShape , xSqueezed . shape , convInfo ) ;
1683
- const im2Col = this . compileAndRun < Tensor2D > ( im2ColProgram , [ xSqueezed ] ) .
1684
- reshape ( [ 1 , x2ColShape [ 0 ] , x2ColShape [ 1 ] ] ) as Tensor3D ;
1731
+ const im2Col =
1732
+ this . compileAndRun < Tensor2D > ( im2ColProgram , [ xSqueezed ] ) . reshape ( [
1733
+ 1 , x2ColShape [ 0 ] , x2ColShape [ 1 ]
1734
+ ] ) as Tensor3D ;
1685
1735
1686
1736
const matmulProgram = new MatMulPackedProgram (
1687
- im2Col . shape , [ 1 , numCols , convInfo . outChannels ] , true ,
1688
- false ) ;
1737
+ im2Col . shape , [ 1 , numCols , convInfo . outChannels ] , true , false ) ;
1689
1738
const product =
1690
1739
this . compileAndRun < Tensor4D > ( matmulProgram , [ im2Col , w2Row ] ) ;
1691
1740
@@ -1825,9 +1874,9 @@ export class MathBackendWebGL implements KernelBackend {
1825
1874
1826
1875
reshape < R extends Rank > ( x : Tensor , shape : ShapeMap [ R ] ) : Tensor < R > {
1827
1876
const texData = this . texData . get ( x . dataId ) ;
1828
- if ( texData . isPacked && ! webgl_util . isReshapeFree ( x . shape , shape )
1829
- && ! ( texData . texture !== null &&
1830
- webgl_util . isReshapeFree ( texData . shape , shape ) ) ) {
1877
+ if ( texData . isPacked && ! webgl_util . isReshapeFree ( x . shape , shape ) &&
1878
+ ! ( texData . texture !== null &&
1879
+ webgl_util . isReshapeFree ( texData . shape , shape ) ) ) {
1831
1880
return this . packedReshape ( x , shape ) ;
1832
1881
}
1833
1882
return backend_util . reshapeTensor ( x , shape ) ;
0 commit comments