-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathopinfos.py
9123 lines (7755 loc) · 311 KB
/
opinfos.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
from collections import namedtuple
from collections.abc import Callable, Generator, Iterable, Sequence
from functools import partial, wraps
import itertools
import math
from numbers import Number
import operator
from typing import Any
import numpy as np
import pytest
import random
# TODO: make this import conditional on Torch being available and querying if should test with torch
import torch
from looseversion import LooseVersion
from torch.testing import assert_close
import thunder.clang as clang
import thunder.core.devices as devices
import thunder.core.dtypes as datatypes
from thunder.core.dtypes import to_dtype, to_torch_dtype
import thunder.core.prims as prims
from thunder.core.pytree import tree_map
from thunder.core.symbol import Symbol
import thunder.executors as executors
from thunder.tests.framework import _all_devicetypes, JAX_AVAILABLE, custom_comparator, IS_WINDOWS
from thunder.tests.make_tensor import make_tensor, make_tensor_like
import thunder.tests.bf16
import thunder.torch as ltorch
#
# Helpful constants and utility functions
#
# TODO This is a hack to support comparisons like nvfuser_version > LooseVersion("0.0.3") even when
# nvfuser_version is None. A better approach would probably be to create a helper function
# nvfuser_atleast(X) which handles nvfuser_version being None properly
nvfuser_version: LooseVersion = (
LooseVersion(executors.get_nvfuser_executor().version) if executors.nvfuser_available() else LooseVersion("0.0.0")
)
# Useful when specifying the domain of an operation
# NOTE: Big enough such that -1 + eps != -1 in bfloat16
# TODO: improve domain specification to allow intervals to be open or closed at the left and right
# Today, the domain is assumed to be closed on the left and open on the right, that is: [x, y)
eps = 1e-2
# NOTE This wrapper is necessary because prims cannot be compiled directly as they are not callable
# TODO Review if this is still necessary
def prims_wrapper(prim):
def fn_(*args, **kwargs):
return prim(*args, **kwargs)
return fn_
def round_remainder(x, y):
return x - torch.round(x / y) * y
# Randomly select a fraction of the elements in a tensor and set them to specified value
def replace_random_percentage(a: torch.Tensor, value: Number, percentage: float) -> torch.Tensor:
flat = torch.flatten(a.detach().clone())
num_values_to_replace: int = math.floor(flat.numel() * percentage)
choice_np = np.random.choice(np.arange(0, flat.numel()), (num_values_to_replace,), replace=False)
choice = torch.asarray(choice_np, device=a.device)
flat[choice] = value
return flat.reshape(a.shape).requires_grad_(a.requires_grad)
def make_number(**kwargs):
v = make_tensor((), device="cpu", **kwargs).item()
return v
# Returns a noncontiguous tensor (with the same shape and values as t)
# The noncontiguous tensor is constructed such that elements in the innermost
# dimension are separated by zeros or (whenever possible) nans
# TODO: consider more complicated noncontiguity schemes
def noncontiguous_like(t):
# Short-circuits if t is already noncontiguous
if not t.is_contiguous():
return t
# Choose a "weird" value that won't be accessed
if t.dtype.is_floating_point or t.dtype.is_complex:
value = math.nan
elif t.dtype == torch.bool:
value = True
else:
value = 12
result = t.new_empty(t.shape + (2,))
result[..., 0] = value
result[..., 1] = t.detach()
result = result[..., 1]
result.requires_grad_(t.requires_grad)
return result
_torch_to_numpy_dtype_map = {
torch.bool: np.bool_,
torch.uint8: np.uint8,
torch.int8: np.int8,
torch.int16: np.int16,
torch.int32: np.int32,
torch.int64: np.int64,
torch.float16: np.float16,
torch.float32: np.float32,
torch.float64: np.float64,
torch.complex64: np.complex64,
torch.complex128: np.complex128,
}
_torch_to_jax_dtype_map = None
if JAX_AVAILABLE:
import jax
import jax.numpy as jnp
_torch_to_jax_dtype_map = {
torch.bool: jnp.bool_,
torch.uint8: jnp.uint8,
torch.int8: jnp.int8,
torch.int16: jnp.int16,
torch.int32: jnp.int32,
torch.int64: jnp.int64,
torch.bfloat16: jnp.bfloat16,
torch.float16: jnp.float16,
torch.float32: jnp.float32,
torch.float64: jnp.float64,
torch.complex64: jnp.complex64,
torch.complex128: jnp.complex128,
}
class TorchTensorComp:
"""
This class provides a very simple wrapper over torch.testing.assert_close,
and is used as a default comparator for per-SampleInput comparisons.
"""
__slots__ = [
"kwargs",
]
def __init__(self, **kwargs):
self.kwargs = kwargs
def __call__(self, a, b, **kwargs):
# Call assert_close with parameters defined as a union
# of `kwargs` and `self.kwargs` with the preference
# given to `self.kwargs`.
assert_close(a, b, **(kwargs | self.kwargs))
class SampleInput:
"""Represents sample inputs to a function."""
__slots__ = [
"args",
"kwargs",
"comp",
]
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
self.comp = None
def __repr__(self):
return f"[SampleInput args={self.args} kwargs={self.kwargs}]"
def set_comparator(self, comp):
self.comp = comp
return self
def noncontiguous(self):
def to_noncontiguous(t):
if isinstance(t, torch.Tensor):
return noncontiguous_like(t)
elif isinstance(t, torch.dtype):
return t
return t
args, kwargs = tree_map(to_noncontiguous, self.args), tree_map(to_noncontiguous, self.kwargs)
return SampleInput(*args, **kwargs).set_comparator(self.comp)
def to(self, dtype: torch.dtype):
def _to(x):
if isinstance(x, torch.Tensor):
return x.to(dtype)
return x
args, kwargs = tree_map(_to, self.args), tree_map(_to, self.kwargs)
return SampleInput(*args, **kwargs)
def remove_singularities(self, op, eps):
singularity_fn = op.singularity_fn_producer(self)
if singularity_fn is None:
return self
def _push_away_from_singularities(x, dist_fn, eps):
"""This function takes a tensor and moves individual values away
from singularities in `eps` increments, until they are further than
`eps` away from them. The `dist_fn` returns the (signed)
distance from `x` to the nearest singularity."""
x_dist = dist_fn(x)
x_ = torch.where((x_dist >= 0) & (x_dist < eps), x + eps, x)
return torch.where((x_dist < 0) & (x_dist > -eps), x_ - eps, x_)
def _remove_singularities(x):
if isinstance(x, torch.Tensor) and datatypes.is_float_dtype(datatypes.to_dtype(x)):
return _push_away_from_singularities(x, singularity_fn, eps)
return x
args, kwargs = tree_map(_remove_singularities, self.args), tree_map(_remove_singularities, self.kwargs)
return SampleInput(*args, **kwargs)
# NOTE This conversion is always to a jax cpu array, we could consider
# converting to a jax gpu array, although we would probably have to update
# how we're installing jax in ci
def jax(self):
def to_jax(t):
if isinstance(t, torch.Tensor):
return jnp.array(t.cpu().numpy())
if isinstance(t, torch.dtype):
return _torch_to_jax_dtype_map[t]
return t
args, kwargs = tree_map(to_jax, self.args), tree_map(to_jax, self.kwargs)
return SampleInput(*args, **kwargs).set_comparator(self.comp)
def numpy(self):
def to_numpy(t):
if isinstance(t, torch.Tensor):
return t.cpu().numpy()
if isinstance(t, torch.dtype):
return _torch_to_numpy_dtype_map[t]
return t
args, kwargs = tree_map(to_numpy, self.args), tree_map(to_numpy, self.kwargs)
return SampleInput(*args, **kwargs).set_comparator(self.comp)
def thunder(self):
def to_thunder(t):
if isinstance(t, torch.dtype):
return to_dtype(t)
return t
args, kwargs = tree_map(to_thunder, self.args), tree_map(to_thunder, self.kwargs)
return SampleInput(*args, **kwargs).set_comparator(self.comp)
class DecorateInfo:
"""Describes which test, or type of tests, should be wrapped in the given decorator when testing an operator.
Any test that matches all provided arguments will be decorated. The decorator will only be applied if the active_if
argument is True.
"""
__slots__ = [
"decorator",
"test_template_name",
"executors",
"devicetypes",
"dtypes",
"active_if",
]
def __init__(
self,
decorator: Any,
test_template_name: None | str = None,
*,
executors: None | Iterable[str] = None,
devicetypes: None | Sequence[devices.DeviceType] = None,
dtypes=None,
active_if: bool = True,
):
self.decorator = decorator
self.test_template_name = test_template_name
self.executors: None | set[str] = {ex.lower() for ex in executors} if executors is not None else None
self.devicetypes: None | Sequence[devices.DeviceType] = devicetypes
if devicetypes is not None:
for x in devicetypes:
assert isinstance(
x, devices.DeviceType
), f"Found non-devicetype {x} when initializing a DecorateInfo's devicetypes"
self.dtypes = None if dtypes is None else datatypes.resolve_dtypes(dtypes)
self.active_if = active_if
def is_active(
self, test_template_name, executor, device_or_devicetype: str | devices.Device | devices.DeviceType, dtype
):
# Acquires devicetype
devicetype_: devices.DeviceType
if isinstance(device_or_devicetype, str):
devicetype_ = devices.to_device(device_or_devicetype).devicetype
elif isinstance(device_or_devicetype, devices.Device):
devicetype_ = device_or_devicetype.devicetype
else:
assert False, f"Unknown device or devicetype {device_or_devicetype}, expect a string, device, or devicetype"
executor_match = self.executors is None or executor.name.lower() in self.executors
test_name_match = self.test_template_name is None or self.test_template_name == test_template_name
devicetype_match = self.devicetypes is None or devicetype_ in self.devicetypes
dtype_match = self.dtypes is None or dtype in self.dtypes
return self.active_if and executor_match and test_name_match and devicetype_match and dtype_match
Domain = namedtuple("Domain", "low high")
class OpInfo:
"""Operator information and helper functions for acquiring it."""
def __init__(
self,
op: Symbol | Callable,
*,
name: str | None = None,
devicetypes: Sequence[devices.DeviceType] | None = None,
dtypes=None,
supports_grad: bool = False,
sample_input_generator,
reference_input_generator=None,
error_input_generator=None,
benchmark_generator=None,
method_variant=None,
operator_variant=None,
torch_reference=None,
numpy_reference=None,
jax_reference=None,
test_directives=(),
domain=(None, None),
singularity_fn=None,
singularity_fn_producer=None,
test_torch_compile_executor=False,
):
self.op = op
# Acquires or infers the name of the operation
name_: str
if name is not None:
name_ = name
elif isinstance(op, Symbol):
name_ = op.name
else:
assert isinstance(op, Callable)
name_ = op.__name__
self.name = name_
self._devicetypes = devicetypes if devicetypes is not None else _all_devicetypes()
# Validates devicetypes
for devtyp in self._devicetypes:
assert isinstance(devtyp, devices.DeviceType), "OpInfo devicetypes must be DeviceTypes"
self._dtypes = dtypes if dtypes is not None else (datatypes.exact, datatypes.inexact)
self.supports_grad = supports_grad
self.sample_input_generator = sample_input_generator
self.reference_input_generator = reference_input_generator
self.error_input_generator = error_input_generator
self.benchmark_generator = benchmark_generator
self.method_variant = method_variant
self.operator_variant = operator_variant
self.torch_reference = torch_reference
self.numpy_reference = numpy_reference
self.jax_reference = jax_reference
self.test_directives = test_directives
self.domain = Domain(*domain)
self.singularity_fn = singularity_fn
# singularity_fn_producers are expected to produce a singularity_fn based on a given SampleInput.
# This can be useful in cases when the definition of the op function invovles kwargs of the input.
self.singularity_fn_producer = (
(lambda _: singularity_fn) if singularity_fn_producer is None else singularity_fn_producer
)
self.test_torch_compile_executor = test_torch_compile_executor
def __call__(self, *args, **kwargs):
"""Calls the function variant of the operator."""
return self.op(*args, **kwargs)
# TODO Maybe allow sample input generation not using torch?
# NOTE Today all sample inputs are generated with PyTorch, so Thunder objects,
# like dtypes, need to be translated into PyTorch objects
def sample_inputs(
self, device: str | devices.Device, dtype: datatypes.dtype, *, requires_grad: bool = False, **kwargs
) -> Generator:
torch_dtype = to_torch_dtype(dtype)
return self.sample_input_generator(self, device, torch_dtype, requires_grad, **kwargs)
def reference_inputs(
self, device: str | devices.Device, dtype: datatypes.dtype, *, requires_grad: bool = False, **kwargs
) -> Generator:
torch_dtype = to_torch_dtype(dtype)
return self.reference_input_generator(self, device, torch_dtype, requires_grad, **kwargs)
def error_inputs(self, device: devices.Device, **kwargs):
return self.error_input_generator(self, device, **kwargs)
# NOTE Today all benchmarks are generated with PyTorch, so Thunder objects,
# like dtypes, need to be translated into PyTorch objects
def benchmarks(self, device: devices.Device, dtype: datatypes.dtype, *, requires_grad: bool = False, **kwargs):
torch_dtype = to_torch_dtype(dtype)
return self.benchmark_generator(self, device, dtype, requires_grad, **kwargs)
def devicetypes(self):
return set(self._devicetypes)
# TODO Add per-device dtype support
def dtypes(self, devicetype: devices.DeviceType = None):
if devicetype is not None:
raise NotImplementedError
return datatypes.resolve_dtypes(self._dtypes)
def test_decorators(self, test_name, executor, devicetype: devices.DeviceType, dtype: datatypes.dtype):
return [d.decorator for d in self.test_directives if d.is_active(test_name, executor, devicetype, dtype)]
opinfos: list[OpInfo] = []
def list_opinfos() -> None:
for opinfo in opinfos:
print(f"{opinfo.name}")
# Acquires an OpInfo by name
def get_opinfo(name: str) -> OpInfo:
for opinfo in opinfos:
if opinfo.name == name:
return opinfo
raise RuntimeError(f"Failed to find OpInfo {name}")
#
# Elementwise Unary OpInfos
#
# TODOA Create elementwise unary OpInfo subclass and maybe auto add to list
elementwise_unary_ops = []
# TODO Add small value, large value, and extremal-valued samples
def elementwise_unary_generator(
op,
device: torch.device,
dtype: torch.dtype,
requires_grad: bool,
*,
supports_numbers: bool = True,
small=False,
**kwargs,
):
low = None if op.domain.low is None else max(-9, op.domain.low)
high = None if op.domain.high is None else min(9, op.domain.high)
make_arg = partial(
make_tensor, device=device, dtype=dtype, low=low, high=high, requires_grad=requires_grad, **kwargs
)
shapes = (
# TODO: restore size zero cases
# (0, 2, 1),
# (5, 0, 3),
(),
(11,),
(4, 4),
(4, 2, 4, 5),
)
if not small:
shapes += (
(1024, 1024),
(64, 64, 64),
)
# Typical inputs
for shape in shapes:
yield SampleInput(make_arg(shape))
# Noncontiguous inputs
for shape in shapes:
yield SampleInput(make_arg(shape, noncontiguous=True))
# Arbitrarily strided inputs
# shape, strides, offset
strided_cases = (
((5, 6, 2), (1, 1, 7), 2),
((5, 5, 4), (1, 1, 7), 2),
((5, 5, 2), (4, 5, 7), 3),
((5, 5, 2), (5, 5, 7), 3),
((5, 5, 2), (5, 5, 5), 3),
((9, 5, 2), (0, 1, 7), 3),
)
for shape, strides, offset in strided_cases:
a = make_arg(
500,
).as_strided(shape, strides, offset)
a = a.detach().requires_grad_(requires_grad)
yield SampleInput(a)
def elementwise_unary_benchmarks(op, device, dtype, requires_grad, **kwargs):
make_arg = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)
# name x shape
cases = (
("8x8", (8, 8)),
("64x64", (64, 64)),
("1024x1024", (1024, 1024)),
)
for name, shape in cases:
yield name, SampleInput(make_arg(shape))
class ElementwiseOpInfo(OpInfo):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class ElementwiseUnaryOpInfo(ElementwiseOpInfo):
def __init__(
self,
*args,
sample_input_generator=elementwise_unary_generator,
benchmark_generator=elementwise_unary_benchmarks,
**kwargs,
):
super().__init__(
*args,
sample_input_generator=sample_input_generator,
benchmark_generator=elementwise_unary_benchmarks,
**kwargs,
)
elementwise_unary_ops.append(self)
# NOTE: many PyTorch operations don't accept numbers as inputs,
# so this helper wraps and unwraps numbers
def _elementwise_unary_torch(op):
@wraps(op)
def _fn(x, **kwargs):
if isinstance(x, torch.Tensor):
return op(x, **kwargs)
return op(torch.tensor(x), **kwargs).item()
return _fn
#
# Tensor Property OpInfos
#
tensor_properties: list[OpInfo] = []
is_complex_opinfo = OpInfo(
ltorch.is_complex,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.is_complex),
dtypes=(datatypes.all_dtypes),
)
tensor_properties.append(is_complex_opinfo)
def _is_cuda_torch(x: torch.Tensor) -> bool:
return x.is_cuda
is_cuda_opinfo = OpInfo(
_is_cuda_torch,
sample_input_generator=partial(elementwise_unary_generator, supports_numbers=False),
torch_reference=_is_cuda_torch,
dtypes=(datatypes.all_dtypes),
)
tensor_properties.append(is_cuda_opinfo)
def _is_nested_torch(x: torch.Tensor) -> bool:
return x.is_nested
is_nested_opinfo = OpInfo(
_is_nested_torch,
sample_input_generator=partial(elementwise_unary_generator, supports_numbers=False),
torch_reference=_is_nested_torch,
dtypes=(datatypes.all_dtypes),
)
tensor_properties.append(is_nested_opinfo)
def numel_sample_generator(op, device, dtype, requires_grad, **kwargs):
make = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)
cases = (
(0,),
(4, 2, 0),
(2, 2),
)
for shape in cases:
yield SampleInput(make(shape))
numel_opinfo = OpInfo(
ltorch.numel,
dtypes=(datatypes.floating,),
sample_input_generator=numel_sample_generator,
torch_reference=torch.numel,
)
tensor_properties.append(numel_opinfo)
def size_sample_generator(op, device, dtype, requires_grad, **kwargs):
make_t = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)
shapes = (
(),
(1,),
(
2,
2,
),
(0, 2, 1),
(2, 0, 1),
)
for shape in shapes:
t = make_t(shape)
yield SampleInput(t)
for d in range(len(shape)):
yield SampleInput(t, d)
size_opinfo = OpInfo(
ltorch.size,
sample_input_generator=size_sample_generator,
torch_reference=torch.Tensor.size,
)
tensor_properties.append(size_opinfo)
opinfos.extend(tensor_properties)
# NOTE: slightly different from generic _elementwise_unary_torch helper
# because this returns the input when given an unsigned type
@wraps(torch.abs)
def _abs_torch(x: torch.Tensor | Number):
if isinstance(x, torch.Tensor):
if datatypes.is_unsigned_dtype(to_dtype(x.dtype)):
return x
return torch.abs(x)
# Handles numbers
assert isinstance(x, Number)
if datatypes.is_unsigned_dtype(type(x)):
return x
return torch.abs(torch.tensor(x)).item()
abs_opinfo = ElementwiseUnaryOpInfo(
ltorch.abs,
supports_grad=True,
torch_reference=_abs_torch,
singularity_fn=lambda x: torch.where(x == 0, 1.0, x),
test_directives=(
# complex32 cpu abs is sometimes flaky in CI
DecorateInfo(
pytest.mark.skip,
"test_core_vs_torch_consistency",
dtypes=(datatypes.complex32,),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
logical_not_opinfo = OpInfo(
clang.logical_not,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.logical_not),
)
elementwise_unary_ops.append(logical_not_opinfo)
acos_opinfo = OpInfo(
ltorch.acos,
domain=(-1, 1),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.acos),
test_directives=(
# Torch doesn't support CPU float16 or complex32 acos
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
elementwise_unary_ops.append(acos_opinfo)
acosh_opinfo = OpInfo(
ltorch.acosh,
domain=(1, math.inf),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.acosh),
test_directives=(
# Torch doesn't support CPU float16 or complex32 acosh
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
DecorateInfo(
pytest.mark.xfail,
executors=("nvfuser",),
active_if=nvfuser_version < LooseVersion("0.0.3"),
),
),
)
elementwise_unary_ops.append(acosh_opinfo)
asin_opinfo = OpInfo(
clang.asin,
domain=(-1, 1),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.asin),
test_directives=(
# Torch doesn't support CPU float16 or complex32 asin
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
# TODO: RuntimeError: Unexpected operator type sqrt in d4 = sqrt(double(0.33680657142871817));
DecorateInfo(
pytest.mark.xfail,
"test_vjp_correctness",
executors=("nvfuser",),
),
),
)
elementwise_unary_ops.append(asin_opinfo)
asinh_opinfo = OpInfo(
clang.asinh,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.asinh),
test_directives=(
# Torch doesn't support CPU float16 or complex32 asinh
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
DecorateInfo(
pytest.mark.xfail,
executors=("nvfuser",),
active_if=nvfuser_version < LooseVersion("0.0.3"),
),
# Sets (slightly) more permissive atol and rtol precisions for complex64
# vs. assert_close's default atol=1e-5 and rtol=1.3e-6
DecorateInfo(
custom_comparator(partial(assert_close, atol=1e-4, rtol=1.3e-6)),
dtypes=(datatypes.complex64,),
),
),
)
elementwise_unary_ops.append(asinh_opinfo)
atan_opinfo = OpInfo(
clang.atan,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.atan),
test_directives=(
# Torch doesn't support CPU float16 or complex32 atan
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
elementwise_unary_ops.append(atan_opinfo)
atanh_opinfo = OpInfo(
clang.atanh,
domain=(-1 + eps, 1 - eps),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.atanh),
test_directives=(
# Torch doesn't support CPU float16 or complex32 atanh
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
elementwise_unary_ops.append(atanh_opinfo)
bitwise_not_opinfo = OpInfo(
clang.bitwise_not,
dtypes=(datatypes.exact,),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.bitwise_not),
)
elementwise_unary_ops.append(bitwise_not_opinfo)
ceil_opinfo = OpInfo(
clang.ceil,
dtypes=(datatypes.floating, datatypes.exact),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.ceil),
test_directives=(
# Torch doesn't support bool ceil
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.bool8,),
),
# Torch doesn't support cpu float16 ceil
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16,),
devicetypes=(devices.DeviceType.CPU,),
),
# PyTorch didn't support ceil on exact types before 1.13
DecorateInfo(
pytest.mark.skip,
"test_core_vs_torch_consistency",
dtypes=(datatypes.exact,),
devicetypes=(devices.DeviceType.CPU,),
active_if=LooseVersion(torch.__version__) < "1.13",
),
),
)
elementwise_unary_ops.append(ceil_opinfo)
cos_opinfo = OpInfo(
clang.cos,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.cos),
test_directives=(
# Torch doesn't support CPU float16 or complex32 cos
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
elementwise_unary_ops.append(cos_opinfo)
cosh_opinfo = OpInfo(
clang.cosh,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.cosh),
test_directives=(
# Torch doesn't support CPU float16 or complex32 cosh
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16, datatypes.complex32),
devicetypes=(devices.DeviceType.CPU,),
),
),
)
elementwise_unary_ops.append(cosh_opinfo)
# digamma is defined for all complex numbers EXCEPT negative integers and zero
digamma_opinfo = OpInfo(
clang.digamma,
# NOTE: Restrict domain to avoid singularities because of issue
# "OpInfos do not use singularity_fn to produce "more stable" samples."
domain=(eps, math.inf),
# NOTE: digamma returns NaN for all negative integers. It returns -Inf when x = 0.
singularity_fn=lambda x: torch.where(x > 0, x, (x - torch.round(x))),
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.digamma),
test_directives=(
# NOTE: Torch doesn't support CPU float16 digamma prior to v2.1
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16,),
devicetypes=(devices.DeviceType.CPU,),
active_if=LooseVersion(torch.__version__) < LooseVersion("2.1.0"),
),
DecorateInfo(
pytest.mark.xfail,
executors=("torch"),
dtypes=(datatypes.float16,),
devicetypes=(devices.DeviceType.CPU,),
active_if=LooseVersion(torch.__version__) < LooseVersion("2.1.0"),
),
# NOTE Neither Torch nor NvFuser supports bfloat16 digamma
DecorateInfo(
pytest.mark.xfail,
dtypes=(datatypes.bfloat16,),
devicetypes=(devices.DeviceType.CUDA,),
),
# NOTE Torch doesn't support complex digamma
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.complexfloating,),
),
DecorateInfo(
pytest.mark.xfail,
executors=("torch"),
dtypes=(datatypes.complexfloating,),
),
),
)
elementwise_unary_ops.append(digamma_opinfo)
erf_opinfo = OpInfo(
clang.erf,
supports_grad=True,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.erf),
test_directives=(
# Torch doesn't support CPU float16 erf
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16,),
devicetypes=(devices.DeviceType.CPU,),
),
# Torch doesn't support complex erf
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.complexfloating,),
),
),
)
elementwise_unary_ops.append(erf_opinfo)
erfc_opinfo = OpInfo(
clang.erfc,
sample_input_generator=elementwise_unary_generator,
torch_reference=_elementwise_unary_torch(torch.erfc),
test_directives=(
# Torch doesn't support CPU float16 erfc
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.float16,),
devicetypes=(devices.DeviceType.CPU,),
),
# Torch doesn't support complex erfc
DecorateInfo(
pytest.mark.xfail,
"test_core_vs_torch_consistency",
dtypes=(datatypes.complexfloating,),
),
),
)
elementwise_unary_ops.append(erfc_opinfo)
erfcinv_opinfo = OpInfo(
clang.erfcinv,
dtypes=(datatypes.floating,),
# erfcinv is only defined for x in [0, 2]
# We use [0.3, 0.7] to avoid the stability issues because we're using
# erfinv(1 - x) as the reference that is less accurate and less stable than
# erfcinv
# TODO Use a better reference (SciPy or pyerf)
domain=(0.3, 0.7),
sample_input_generator=partial(elementwise_unary_generator, supports_numbers=False),