-
Notifications
You must be signed in to change notification settings - Fork 74k
/
dispatch_test.py
1039 lines (830 loc) · 37.3 KB
/
dispatch_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for operator dispatch."""
import collections
import typing
import numpy as np
from tensorflow.python.eager import context
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import extension_type
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_conversion
from tensorflow.python.framework import test_util
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import array_ops_stack
from tensorflow.python.ops import bitwise_ops
from tensorflow.python.ops import gen_math_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variables
from tensorflow.python.ops.linalg import linear_operator_diag
from tensorflow.python.ops.proto_ops import decode_proto
from tensorflow.python.platform import googletest
from tensorflow.python.platform import test
from tensorflow.python.platform import tf_logging
from tensorflow.python.types import core as core_tf_types
from tensorflow.python.util import deprecation
from tensorflow.python.util import dispatch
from tensorflow.python.util import nest
from tensorflow.python.util.tf_export import get_canonical_name_for_symbol
from tensorflow.python.util.tf_export import tf_export
class CustomTensor(object):
"""A fake composite tensor class, for testing type-based dispatching."""
def __init__(self, tensor, score):
self.tensor = ops.convert_to_tensor(tensor)
self.score = score
@tf_export("test_op")
@dispatch.add_dispatch_support
def test_op(x, y, z):
"""A fake op for testing dispatch of Python ops."""
return x + (2 * y) + (3 * z)
class TensorTracer(object):
"""An object used to trace TensorFlow graphs.
This is an example class that is used to test global op dispatchers. The
global op dispatcher for TensorTracers is defined below.
"""
def __init__(self, name, args=None, kwargs=None):
self.name = name
self.args = args
self.kwargs = kwargs
self.shape = array_ops.ones(shape=(4, 4)).shape
self.dtype = dtypes.float32
def __repr__(self):
if self.args is None and self.kwargs is None:
return self.name
else:
args = [str(x) for x in self.args]
args += sorted(
["{}={}".format(name, x) for (name, x) in self.kwargs.items()])
return "{}({})".format(self.name, ", ".join(args))
@property
def is_tensor_like(self):
return True
@classmethod
def _overload_all_operators(cls): # pylint: disable=invalid-name
"""Register overloads for all operators."""
for operator in ops.Tensor.OVERLOADABLE_OPERATORS:
cls._overload_operator(operator)
@classmethod
def _overload_operator(cls, operator): # pylint: disable=invalid-name
"""Overload an operator with the same overloading as `ops.Tensor`."""
tensor_oper = getattr(ops.Tensor, operator)
# Compatibility with Python 2:
# Python 2 unbound methods have type checks for the first arg,
# so we need to extract the underlying function
tensor_oper = getattr(tensor_oper, "__func__", tensor_oper)
setattr(cls, operator, tensor_oper)
TensorTracer._overload_all_operators() # pylint: disable=protected-access
class TensorTracerOpDispatcher(dispatch.GlobalOpDispatcher):
"""Global op dispatcher for TensorTracer."""
def _flatten_with_slice_flattening(self, x):
flat = []
for val in nest.flatten(x):
if isinstance(val, slice):
flat.extend((val.start, val.stop, val.step))
else:
flat.append(val)
return flat
def handle(self, op, args, kwargs):
# Dispatcher only applies if at least one arg is a TensorTracer.
if not (any(self.is_tensor_tracer_arg(x) for x in args) or
any(self.is_tensor_tracer_arg(x) for x in kwargs.values())):
return self.NOT_SUPPORTED
symbol_name = get_canonical_name_for_symbol(op)
return TensorTracer(symbol_name, args, kwargs)
def is_tensor_tracer_arg(self, value):
return any(
isinstance(x, TensorTracer)
for x in self._flatten_with_slice_flattening(value))
@test_util.run_all_in_graph_and_eager_modes
class DispatchTest(test_util.TensorFlowTestCase):
def testAddDispatchForTypes_With_CppOp(self):
original_handlers = gen_math_ops.atan2._tf_fallback_dispatchers[:]
# Override the behavior of gen_math_ops.atan2 and make it look like add.
@dispatch.dispatch_for_types(gen_math_ops.atan2, CustomTensor)
def custom_atan2(y, x, name=None): # pylint: disable=unused-variable
return CustomTensor(
gen_math_ops.add(y.tensor, x.tensor, name), (x.score + y.score) / 2.0)
self.assertEqual(
len(math_ops.atan2._tf_fallback_dispatchers),
len(original_handlers) + 1)
# Test that we see the overridden behavior when using CustomTensors.
x = CustomTensor([1., 2., 3.], 2.0)
y = CustomTensor([7., 8., 2.], 0.0)
x_plus_y = gen_math_ops.atan2(y, x)
self.assertAllEqual(self.evaluate(x_plus_y.tensor), [8, 10, 5])
self.assertNear(x_plus_y.score, 1.0, 0.001)
# Test that we still get the right behavior when using normal Tensors.
a = [1., 2., 3.]
b = [7., 8., 2.]
a_plus_b = gen_math_ops.atan2(a, b)
self.assertAllClose(a_plus_b, [0.14189707, 0.24497867, 0.98279375])
# Test that we still get a TypeError or ValueError if we pass some
# type that's not supported by any dispatcher.
with self.assertRaises((TypeError, ValueError)):
gen_math_ops.atan2(a, None)
# Clean up
gen_math_ops.atan2._tf_fallback_dispatchers = original_handlers
def testAddDispatchForTypes_With_PythonOp(self):
original_handlers = test_op._tf_fallback_dispatchers[:]
@dispatch.dispatch_for_types(test_op, CustomTensor)
def override_for_test_op(x, y, z): # pylint: disable=unused-variable
return CustomTensor(
test_op(x.tensor, y.tensor, z.tensor),
(x.score + y.score + z.score) / 3.0)
x = CustomTensor([1, 2, 3], 0.2)
y = CustomTensor([7, 8, 2], 0.4)
z = CustomTensor([0, 1, 2], 0.6)
result = test_op(x, y, z)
self.assertAllEqual(self.evaluate(result.tensor), [15, 21, 13])
self.assertNear(result.score, 0.4, 0.001)
# Clean up
test_op._tf_fallback_dispatchers = original_handlers
def testDispatchForTypes_SignatureMismatch(self):
with self.assertRaisesRegex(
AssertionError, "The decorated function's "
"signature must exactly match.*"):
@dispatch.dispatch_for_types(test_op, CustomTensor)
def override_for_test_op(a, b, c): # pylint: disable=unused-variable
return CustomTensor(
test_op(a.tensor, b.tensor, c.tensor),
(a.score + b.score + c.score) / 3.0)
def testDispatchForTypes_OpDoesNotSupportDispatch(self):
def some_op(x, y):
return x + y
with self.assertRaisesRegex(AssertionError, "Dispatching not enabled for"):
@dispatch.dispatch_for_types(some_op, CustomTensor)
def override_for_some_op(x, y): # pylint: disable=unused-variable
return x if x.score > 0 else y
@test.mock.patch.object(tf_logging, "warning", autospec=True)
def testInteractionWithDeprecationWarning(self, mock_warning):
@deprecation.deprecated(date=None, instructions="Instructions")
@dispatch.add_dispatch_support
def some_op(x):
return x
some_op(5)
message = mock_warning.call_args[0][0] % mock_warning.call_args[0][1:]
self.assertRegex(
message, r".*some_op \(from __main__\) is deprecated and will be "
"removed in a future version.*")
def testGlobalDispatcher(self):
original_global_dispatchers = dispatch._GLOBAL_DISPATCHERS
try:
TensorTracerOpDispatcher().register()
x = TensorTracer("x")
y = TensorTracer("y")
trace = math_ops.reduce_sum(math_ops.add(math_ops.abs(x), y), axis=3)
self.assertEqual(
str(trace), "math.reduce_sum(math.add(math.abs(x), y), axis=3)")
proto_val = TensorTracer("proto")
trace = decode_proto(proto_val, "message_type", ["field"], ["float32"])
self.assertIn("io.decode_proto(bytes=proto,", str(trace))
finally:
# Clean up.
dispatch._GLOBAL_DISPATCHERS = original_global_dispatchers
def testGlobalDispatcherConvertToTensor(self):
original_global_dispatchers = dispatch._GLOBAL_DISPATCHERS
try:
TensorTracerOpDispatcher().register()
x = TensorTracer("x")
y = TensorTracer("y")
trace = math_ops.add(
math_ops.abs(tensor_conversion.convert_to_tensor_v2_with_dispatch(x)),
y,
)
self.assertEqual(
str(trace), "math.add(math.abs(convert_to_tensor(x)), y)")
finally:
# Clean up.
dispatch._GLOBAL_DISPATCHERS = original_global_dispatchers
def testGlobalDispatcherGetItem(self):
original_global_dispatchers = dispatch._GLOBAL_DISPATCHERS
try:
TensorTracerOpDispatcher().register()
x = TensorTracer("x")
trace = x[0]
self.assertEqual(str(trace), "__operators__.getitem(x, 0)")
x = TensorTracer("x")
y = TensorTracer("y")
trace = x[y]
self.assertEqual(str(trace), "__operators__.getitem(x, y)")
x = TensorTracer("x")
y = TensorTracer("y")
trace = x[:y] # pylint: disable=invalid-slice-index
self.assertEqual(
str(trace), "__operators__.getitem(x, slice(None, y, None))")
x = array_ops.ones(shape=(3, 3))
y = TensorTracer("y")
trace = x[y]
self.assertEqual(str(trace), "__operators__.getitem(%s, y)" % x)
trace = x[:y] # pylint: disable=invalid-slice-index
self.assertEqual(
str(trace), "__operators__.getitem(%s, slice(None, y, None))" % x)
finally:
# Clean up.
dispatch._GLOBAL_DISPATCHERS = original_global_dispatchers
def testGlobalDispatcherLinearOperators(self):
original_global_dispatchers = dispatch._GLOBAL_DISPATCHERS
try:
TensorTracerOpDispatcher().register()
x = TensorTracer("x")
# To grab the eigenvalues the diag operator just calls convert_to_tensor
# (twice) in this case.
trace = linear_operator_diag.LinearOperatorDiag(x).eigvals()
self.assertEqual(
str(trace),
"convert_to_tensor(convert_to_tensor(x, dtype=None, dtype_hint=None, "
"name=diag))")
# The diagonal tensor addition gets traced even though the linear_operator
# API only uses dispatchable ops instead of directly exposing dispatching.
trace = linear_operator_diag.LinearOperatorDiag(x).add_to_tensor(x)
self.assertIn(
"linalg.set_diag(convert_to_tensor(x, name=x), __operators__.add("
"convert_to_tensor(x, dtype=None, dtype_hint=None, name=diag), "
"linalg.diag_part(convert_to_tensor(x, name=x)), "
"name=", str(trace))
# The dispatch-supporting ops the non-singular check calls out to
# get traced.
trace = linear_operator_diag.LinearOperatorDiag(x).assert_non_singular()
self.assertIn("debugging.assert_less", str(trace))
self.assertIn(
"message=Singular operator: Diagonal contained zero values.",
str(trace))
finally:
# Clean up.
dispatch._GLOBAL_DISPATCHERS = original_global_dispatchers
class MaskedTensor(extension_type.ExtensionType):
"""Simple ExtensionType for testing v2 dispatch."""
values: ops.Tensor
mask: ops.Tensor
class SillyTensor(extension_type.ExtensionType):
"""Simple ExtensionType for testing v2 dispatch."""
value: ops.Tensor
how_silly: float
@test_util.run_all_in_graph_and_eager_modes
class DispatchV2Test(test_util.TensorFlowTestCase):
def testDispatchForOneSignature(self):
@dispatch.dispatch_for_api(math_ops.add, {
"x": MaskedTensor,
"y": MaskedTensor
})
def masked_add(x, y, name=None):
with ops.name_scope(name):
return MaskedTensor(x.values + y.values, x.mask & y.mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y.values)
self.assertAllEqual(z.mask, x.mask & y.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchSignatureWithUnspecifiedParameter(self):
@dispatch.dispatch_for_api(math_ops.add, {"x": MaskedTensor})
def masked_add(x, y):
if y is None:
return x
y_values = y.values if isinstance(y, MaskedTensor) else y
y_mask = y.mask if isinstance(y, MaskedTensor) else True
return MaskedTensor(x.values + y_values, x.mask & y_mask)
try:
a = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
b = constant_op.constant([10, 20, 30, 40, 50])
c = [10, 20, 30, 40, 50]
d = 50
e = None
# As long as `x` is a MaskedTensor, the dispatcher will be called
# (regardless of the type for `y`):
self.assertAllEqual(math_ops.add(a, b).values, [11, 22, 33, 44, 55])
self.assertAllEqual(math_ops.add(a, c).values, [11, 22, 33, 44, 55])
self.assertAllEqual(math_ops.add(a, d).values, [51, 52, 53, 54, 55])
self.assertAllEqual(math_ops.add(a, e).values, [1, 2, 3, 4, 5])
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchForMultipleSignatures(self):
@dispatch.dispatch_for_api(math_ops.add, {"x": MaskedTensor},
{"y": MaskedTensor})
def masked_add(x, y, name=None):
with ops.name_scope(name):
x_values = x.values if isinstance(x, MaskedTensor) else x
x_mask = x.mask if isinstance(x, MaskedTensor) else True
y_values = y.values if isinstance(y, MaskedTensor) else y
y_mask = y.mask if isinstance(y, MaskedTensor) else True
return MaskedTensor(x_values + y_values, x_mask & y_mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = constant_op.constant([10, 20, 30, 40, 50])
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y)
self.assertAllEqual(z.mask, x.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchForList(self):
@dispatch.dispatch_for_api(array_ops.concat,
{"values": typing.List[MaskedTensor]})
def masked_concat(values, axis, name=None):
with ops.name_scope(name):
return MaskedTensor(
array_ops.concat([v.values for v in values], axis),
array_ops.concat([v.mask for v in values], axis))
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1], [1, 1, 0])
z = array_ops.concat([x, y], axis=0)
self.assertAllEqual(z.values, array_ops.concat([x.values, y.values], 0))
self.assertAllEqual(z.mask, array_ops.concat([x.mask, y.mask], 0))
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_concat)
def testDispatchForUnion(self):
MaybeMasked = typing.Union[MaskedTensor, ops.Tensor]
@dispatch.dispatch_for_api(math_ops.add, {
"x": MaybeMasked,
"y": MaybeMasked
})
def masked_add(x, y, name=None):
with ops.name_scope(name):
x_values = x.values if isinstance(x, MaskedTensor) else x
x_mask = x.mask if isinstance(x, MaskedTensor) else True
y_values = y.values if isinstance(y, MaskedTensor) else y
y_mask = y.mask if isinstance(y, MaskedTensor) else True
return MaskedTensor(x_values + y_values, x_mask & y_mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = constant_op.constant([10, 20, 30, 40, 50])
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y)
self.assertAllEqual(z.mask, x.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchForTensorLike(self):
MaskedOrTensorLike = typing.Union[MaskedTensor, core_tf_types.TensorLike]
@dispatch.dispatch_for_api(math_ops.add)
def masked_add(x: MaskedOrTensorLike, y: MaskedOrTensorLike, name=None):
with ops.name_scope(name):
x_values = x.values if isinstance(x, MaskedTensor) else x
x_mask = x.mask if isinstance(x, MaskedTensor) else True
y_values = y.values if isinstance(y, MaskedTensor) else y
y_mask = y.mask if isinstance(y, MaskedTensor) else True
return MaskedTensor(x_values + y_values, x_mask & y_mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y1 = [10, 20, 30, 40, 50]
y2 = np.array([10, 20, 30, 40, 50])
y3 = constant_op.constant([10, 20, 30, 40, 50])
y4 = variables.Variable([5, 4, 3, 2, 1])
if not context.executing_eagerly():
self.evaluate(variables.global_variables_initializer())
for y in [y1, y2, y3, y4]:
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y)
self.assertAllEqual(z.mask, x.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchForOptional(self):
# Note: typing.Optional[X] == typing.Union[X, NoneType].
@dispatch.dispatch_for_api(
array_ops.where_v2, {
"condition": MaskedTensor,
"x": typing.Optional[MaskedTensor],
"y": typing.Optional[MaskedTensor]
})
def masked_where(condition, x=None, y=None, name=None):
del condition, x, y, name
return "stub"
try:
x = MaskedTensor([True, False, True, True, True], [1, 0, 1, 1, 1])
self.assertEqual(array_ops.where_v2(x), "stub")
self.assertEqual(array_ops.where_v2(x, x, x), "stub")
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_where)
def testDispatchForSignatureFromAnnotations(self):
@dispatch.dispatch_for_api(math_ops.add)
def masked_add(x: MaskedTensor, y: MaskedTensor, name=None):
with ops.name_scope(name):
return MaskedTensor(x.values + y.values, x.mask & y.mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y.values)
self.assertAllEqual(z.mask, x.mask & y.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchForPositionalSignature(self):
@dispatch.dispatch_for_api(math_ops.add, {0: MaskedTensor, 1: MaskedTensor})
def masked_add(x, y, name=None):
with ops.name_scope(name):
return MaskedTensor(x.values + y.values, x.mask & y.mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
z = math_ops.add(x, y)
self.assertAllEqual(z.values, x.values + y.values)
self.assertAllEqual(z.mask, x.mask & y.mask)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchWithVarargs(self):
@dispatch.dispatch_for_api(math_ops.add, {
"x": MaskedTensor,
"y": MaskedTensor
})
def masked_add(*args, **kwargs):
self.assertAllEqual(args[0].values, x.values)
self.assertAllEqual(args[1].values, y.values)
self.assertEmpty(kwargs)
return "stub"
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
self.assertEqual(math_ops.add(x, y), "stub")
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchWithKwargs(self):
@dispatch.dispatch_for_api(math_ops.add, {
"x": MaskedTensor,
"y": MaskedTensor
})
def masked_add(*args, **kwargs):
self.assertAllEqual(kwargs["x"].values, x.values)
self.assertAllEqual(kwargs["y"].values, y.values)
self.assertEmpty(args)
return "stub"
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
self.assertEqual(math_ops.add(x=x, y=y), "stub")
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchErrorForBadAPI(self):
def api_without_dispatch_support(x):
return x + 1
with self.assertRaisesRegex(ValueError, ".* does not support dispatch."):
@dispatch.dispatch_for_api(api_without_dispatch_support,
{"x": MaskedTensor})
def my_version(x): # pylint: disable=unused-variable
del x
def testDispatchErrorForNoSignature(self):
with self.assertRaisesRegex(ValueError,
"must be called with at least one signature"):
@dispatch.dispatch_for_api(math_ops.add)
def my_add(x, y, name=None): # pylint: disable=unused-variable
del x, y, name
def testDispatchErrorSignatureMismatchParamName(self):
with self.assertRaisesRegex(
ValueError, r"Dispatch function's signature \(x, why, name=None\) does "
r"not match API's signature \(x, y, name=None\)."):
@dispatch.dispatch_for_api(math_ops.add, {"x": MaskedTensor})
def my_add(x, why, name=None): # pylint: disable=unused-variable
del x, why, name
def testDispatchErrorSignatureMismatchExtraParam(self):
with self.assertRaisesRegex(
ValueError, r"Dispatch function's signature \(x, y, name=None, extra_"
r"arg=None\) does not match API's signature \(x, y, name=None\)."):
@dispatch.dispatch_for_api(math_ops.add, {"x": MaskedTensor})
def my_add(x, y, name=None, extra_arg=None): # pylint: disable=unused-variable
del x, y, name, extra_arg
def testDispatchErrorForUnsupportedTypeAnnotation(self):
with self.assertRaisesRegex(
ValueError,
"Type annotation .* is not currently supported by dispatch."):
@dispatch.dispatch_for_api(math_ops.add,
{"x": typing.Tuple[MaskedTensor]})
def my_add(x, y, name=None): # pylint: disable=unused-variable
del x, y, name
def testDispatchErrorForUnknownParameter(self):
with self.assertRaisesRegex(
ValueError, "signature includes annotation for unknown parameter 'z'."):
@dispatch.dispatch_for_api(math_ops.add, {"z": MaskedTensor})
def my_add(x, y, name=None): # pylint: disable=unused-variable
del x, y, name
def testDispatchErrorUnsupportedKeywordOnlyAnnotation(self):
@dispatch.add_dispatch_support
def foo(x, *, y):
return x + y
with self.assertRaisesRegex(
ValueError, "Dispatch currently only supports type "
"annotations for positional parameters"):
@dispatch.dispatch_for_api(foo, {"y": MaskedTensor})
def masked_foo(x, *, y): # pylint: disable=unused-variable
del x, y
def testDispatchErrorBadSignatureType(self):
with self.assertRaisesRegex(
TypeError, "signatures must be dictionaries mapping parameter "
"names to type annotations"):
@dispatch.dispatch_for_api(math_ops.add, [MaskedTensor])
def my_add(x, y, name=None): # pylint: disable=unused-variable
del x, y, name
with self.assertRaisesRegex(
TypeError, "signatures must be dictionaries mapping parameter "
"names to type annotations"):
@dispatch.dispatch_for_api(math_ops.multiply, {None: MaskedTensor})
def my_multiply(x, y, name=None): # pylint: disable=unused-variable
del x, y, name
def testDispatchErrorNotCallable(self):
with self.assertRaisesRegex(TypeError,
"Expected dispatch_target to be callable"):
dispatch.dispatch_for_api(math_ops.abs, {0: MaskedTensor})("not_callable")
def testRegisterDispatchableType(self):
Car = collections.namedtuple("Car", ["size", "speed"])
dispatch.register_dispatchable_type(Car)
@dispatch.dispatch_for_api(math_ops.add, {"x": Car, "y": Car})
def add_car(x, y, name=None):
with ops.name_scope(name):
return Car(x.size + y.size, x.speed + y.speed)
try:
x = Car(constant_op.constant(1), constant_op.constant(3))
y = Car(constant_op.constant(10), constant_op.constant(20))
z = math_ops.add(x, y)
self.assertAllEqual(z.size, 11)
self.assertAllEqual(z.speed, 23)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(add_car)
def testTypeCheckersAreCached(self):
checker1 = dispatch.make_type_checker(int)
checker2 = dispatch.make_type_checker(int)
self.assertIs(checker1, checker2)
def testDispatchTargetWithNoNameArgument(self):
@dispatch.dispatch_for_api(math_ops.add, {
"x": MaskedTensor,
"y": MaskedTensor
})
def masked_add(x, y):
return MaskedTensor(x.values + y.values, x.mask & y.mask)
try:
x = MaskedTensor([1, 2, 3, 4, 5], [1, 0, 1, 1, 1])
y = MaskedTensor([1, 1, 1, 1, 1], [1, 1, 0, 1, 0])
# pass name w/ keyword arg
a = math_ops.add(x, y, name="MyAdd")
if not context.executing_eagerly(): # names not defined in eager mode.
self.assertRegex(a.values.name, r"^MyAdd/add.*")
self.assertRegex(a.mask.name, r"^MyAdd/and.*")
# pass name w/ positional arg
b = math_ops.add(x, y, "B")
if not context.executing_eagerly(): # names not defined in eager mode.
self.assertRegex(b.values.name, r"^B/add.*")
self.assertRegex(b.mask.name, r"^B/and.*")
# default name value
c = math_ops.add(x, y)
if not context.executing_eagerly(): # names not defined in eager mode.
self.assertRegex(c.values.name, r"^add.*")
self.assertRegex(c.mask.name, r"^and.*")
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
def testDispatchApiWithNoNameArg(self):
# Note: The "tensor_equals" API has no "name" argument.
signature = {"self": MaskedTensor, "other": MaskedTensor}
@dispatch.dispatch_for_api(math_ops.tensor_equals, signature)
def masked_tensor_equals(self, other):
del self, other
dispatch.unregister_dispatch_for(masked_tensor_equals) # clean up.
with self.assertRaisesRegexp(
ValueError, r"Dispatch function's signature \(self, other, name=None\) "
r"does not match API's signature \(self, other\)\."):
@dispatch.dispatch_for_api(math_ops.tensor_equals, signature)
def masked_tensor_equals_2(self, other, name=None):
del self, other, name
del masked_tensor_equals_2 # avoid pylint unused variable warning.
def testDispatchWithIterableParams(self):
# The add_n API supports having `inputs` be an iterable (and not just
# a sequence).
@dispatch.dispatch_for_api(math_ops.add_n,
{"inputs": typing.List[MaskedTensor]})
def masked_add_n(inputs):
masks = array_ops_stack.stack([x.mask for x in inputs])
return MaskedTensor(
math_ops.add_n([x.values for x in inputs]),
math_ops.reduce_all(masks, axis=0))
try:
generator = (MaskedTensor([i], [True]) for i in range(5))
y = math_ops.add_n(generator)
self.assertAllEqual(y.values, [0 + 1 + 2 + 3 + 4])
self.assertAllEqual(y.mask, [True])
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add_n)
def testBadIterableParametersError(self):
fn = lambda x: [t + 1 for t in x]
with self.assertRaisesRegex(
TypeError, "iterable_parameters should be a list or tuple of string"):
dispatch.add_dispatch_support(iterable_parameters="x")(fn)
def testUnregisterDispatchTargetBadTargetError(self):
fn = lambda x: x + 1
with self.assertRaisesRegex(ValueError, "Function .* was not registered"):
dispatch.unregister_dispatch_for(fn)
def testAddDuplicateApiDisptacherError(self):
some_op = lambda x: x
some_op = dispatch.add_type_based_api_dispatcher(some_op)
with self.assertRaisesRegex(
ValueError, ".* already has a type-based API dispatcher."):
some_op = dispatch.add_type_based_api_dispatcher(some_op)
def testGetApisWithTypeBasedDispatch(self):
dispatch_apis = dispatch.apis_with_type_based_dispatch()
self.assertIn(math_ops.add, dispatch_apis)
self.assertIn(array_ops.concat, dispatch_apis)
def testTypeBasedDispatchTargetsFor(self):
MaskedTensorList = typing.List[typing.Union[MaskedTensor, ops.Tensor]]
try:
@dispatch.dispatch_for_api(math_ops.add)
def masked_add(x: MaskedTensor, y: MaskedTensor):
del x, y
@dispatch.dispatch_for_api(array_ops.concat)
def masked_concat(values: MaskedTensorList, axis):
del values, axis
@dispatch.dispatch_for_api(math_ops.add)
def silly_add(x: SillyTensor, y: SillyTensor):
del x, y
@dispatch.dispatch_for_api(math_ops.abs)
def silly_abs(x: SillyTensor):
del x
# Note: `expeced` does not contain keys or values from SillyTensor.
targets = dispatch.type_based_dispatch_signatures_for(MaskedTensor)
expected = {math_ops.add: [{"x": MaskedTensor, "y": MaskedTensor}],
array_ops.concat: [{"values": MaskedTensorList}]}
self.assertEqual(targets, expected)
finally:
# Clean up dispatch table.
dispatch.unregister_dispatch_for(masked_add)
dispatch.unregister_dispatch_for(masked_concat)
dispatch.unregister_dispatch_for(silly_add)
dispatch.unregister_dispatch_for(silly_abs)
def testDispatchForUnaryElementwiseAPIs(self):
@dispatch.dispatch_for_unary_elementwise_apis(MaskedTensor)
def unary_elementwise_api_handler(api_func, x):
return MaskedTensor(api_func(x.values), x.mask)
try:
x = MaskedTensor([1, -2, -3], [True, True, False])
# Test calls with positional & keyword argument (& combinations)
abs_x = math_ops.abs(x)
sign_x = math_ops.sign(x=x)
neg_x = math_ops.negative(x, "neg_x")
invert_x = bitwise_ops.invert(x, name="invert_x")
ones_like_x = array_ops.ones_like(x, name="ones_like_x")
ones_like_x_float = array_ops.ones_like(
x, dtypes.float32, name="ones_like_x_float")
self.assertAllEqual(abs_x.values, [1, 2, 3])
self.assertAllEqual(sign_x.values, [1, -1, -1])
self.assertAllEqual(neg_x.values, [-1, 2, 3])
self.assertAllEqual(invert_x.values, [-2, 1, 2])
self.assertAllEqual(ones_like_x.values, [1, 1, 1])
self.assertAllEqual(ones_like_x_float.values, [1., 1., 1.])
for result in [
abs_x, sign_x, neg_x, invert_x, ones_like_x, ones_like_x_float
]:
self.assertAllEqual(result.mask, [True, True, False])
if not context.executing_eagerly(): # names not defined in eager mode.
self.assertRegex(neg_x.values.name, r"^neg_x/Neg:.*")
self.assertRegex(invert_x.values.name, r"^invert_x/.*")
self.assertRegex(ones_like_x.values.name, r"^ones_like_x/.*")
self.assertRegex(ones_like_x_float.values.name,
r"^ones_like_x_float/.*")
finally:
dispatch.unregister_dispatch_for(unary_elementwise_api_handler)
def testDispatchForBinaryElementwiseAPIs(self):
@dispatch.dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)
def binary_elementwise_api_handler(api_func, x, y):
return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)
try:
x = MaskedTensor([1, -2, -3], [True, True, False])
y = MaskedTensor([10, 20, 30], [True, False, True])
# Test calls with positional & keyword arguments (& combinations)
x_times_y = math_ops.multiply(x, y)
x_plus_y = math_ops.add(x, y=y)
x_minus_y = math_ops.subtract(x=x, y=y)
min_x_y = math_ops.minimum(x, y, "min_x_y")
y_times_x = math_ops.multiply(y, x, name="y_times_x")
y_plus_x = math_ops.add(y, y=x, name="y_plus_x")
y_minus_x = math_ops.subtract(x=y, y=x, name="y_minus_x")
self.assertAllEqual(x_times_y.values, [10, -40, -90])
self.assertAllEqual(x_plus_y.values, [11, 18, 27])
self.assertAllEqual(x_minus_y.values, [-9, -22, -33])
self.assertAllEqual(min_x_y.values, [1, -2, -3])
self.assertAllEqual(y_times_x.values, [10, -40, -90])
self.assertAllEqual(y_plus_x.values, [11, 18, 27])
self.assertAllEqual(y_minus_x.values, [9, 22, 33])
for result in [
x_times_y, x_plus_y, x_minus_y, min_x_y, y_times_x, y_plus_x,
y_minus_x
]:
self.assertAllEqual(result.mask, [True, False, False])
if not context.executing_eagerly(): # names not defined in eager mode.
self.assertRegex(min_x_y.values.name, r"^min_x_y/Minimum:.*")
self.assertRegex(min_x_y.mask.name, r"^min_x_y/and:.*")
self.assertRegex(y_times_x.values.name, r"^y_times_x/.*")
self.assertRegex(y_plus_x.values.name, r"^y_plus_x/.*")
self.assertRegex(y_minus_x.values.name, r"^y_minus_x/.*")
finally:
dispatch.unregister_dispatch_for(binary_elementwise_api_handler)
def testDuplicateDispatchForUnaryElementwiseAPIsError(self):
@dispatch.dispatch_for_unary_elementwise_apis(MaskedTensor)
def handler(api_func, x):
return MaskedTensor(api_func(x.values), x.mask)
try:
with self.assertRaisesRegex(
ValueError, r"A unary elementwise dispatch handler \(.*\) has "
"already been registered for .*"):
@dispatch.dispatch_for_unary_elementwise_apis(MaskedTensor)
def another_handler(api_func, x):
return MaskedTensor(api_func(x.values), ~x.mask)
del another_handler
finally:
dispatch.unregister_dispatch_for(handler)
def testDuplicateDispatchForBinaryElementwiseAPIsError(self):
@dispatch.dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)
def handler(api_func, x, y):
return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)
try:
with self.assertRaisesRegex(
ValueError, r"A binary elementwise dispatch handler \(.*\) has "
"already been registered for .*"):
@dispatch.dispatch_for_binary_elementwise_apis(MaskedTensor,
MaskedTensor)
def another_handler(api_func, x, y):
return MaskedTensor(api_func(x.values, y.values), x.mask)
del another_handler
finally:
dispatch.unregister_dispatch_for(handler)
def testRegisterUnaryElementwiseApiAfterHandler(self):
# Test that it's ok to call register_unary_elementwise_api after
# dispatch_for_unary_elementwise_apis.
@dispatch.dispatch_for_unary_elementwise_apis(MaskedTensor)
def handler(api_func, x):
return MaskedTensor(api_func(x.values), x.mask)
try:
@dispatch.register_unary_elementwise_api
@dispatch.add_dispatch_support
def some_op(x):
return x * 2
x = MaskedTensor([1, 2, 3], [True, False, True])
y = some_op(x)
self.assertAllEqual(y.values, [2, 4, 6])
self.assertAllEqual(y.mask, [True, False, True])
finally:
dispatch.unregister_dispatch_for(handler)
def testRegisterBinaryElementwiseApiAfterHandler(self):
# Test that it's ok to call register_binary_elementwise_api after
# dispatch_for_binary_elementwise_apis.
@dispatch.dispatch_for_binary_elementwise_apis(MaskedTensor, MaskedTensor)
def handler(api_func, x, y):
return MaskedTensor(api_func(x.values, y.values), x.mask & y.mask)
try:
@dispatch.register_binary_elementwise_api
@dispatch.add_dispatch_support
def some_op(x, y):