8000 OpInfo: mvlgamma by kshitij12345 · Pull Request #56907 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

OpInfo: mvlgamma #56907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions test/test_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7764,10 +7764,6 @@ def tmp(dtype, device):
('mode', '', _small_3d, lambda t, d: [], 1e-5, 1e-5, 1e-5, _types, _cpu_types, False),
('mode', 'dim', _small_3d, lambda t, d: [1], 1e-5, 1e-5, 1e-5, _types, _cpu_types, False),
('mode', 'neg_dim', _small_3d, lambda t, d: [-1], 1e-5, 1e-5, 1e-5, _types, _cpu_types, False),
('mvlgamma', '2d_p=1', lambda t, d: _small_2d(t, d).clamp(0.1, 10), lambda t, d: [1],
1e-5, 1e-5, 1e-5, _float_types_no_half),
('mvlgamma', '2d_p=2', lambda t, d: _small_2d(t, d).clamp(0.6, 10), lambda t, d: [2],
1e-5, 1e-5, 1e-5, _float_types_no_half),
('remainder', 'value', _small_3d, lambda t, d: [3], 1e-1, 1e-2, 1e-5, _signed_types),
('remainder', 'negative_value', _small_3d, lambda t, d: [-3], 1e-1, 1e-2, 1e-5, _signed_types),
('remainder', 'tensor', _small_3d,
Expand Down
19 changes: 9 additions & 10 deletions test/test_unary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ def generate_tensors_from_vals(vals, device, dtype, domain):
assert len(vals) < _medium_length # medium tensor should contain all vals
assert _medium_length % 4 == 0 # ensure vectorized code coverage

if not dtype.is_complex:
# Filter values based on Operators domain.
# Note: Complex numbers don't belong to ordered field,
# so we don't filter for them.
if domain[0] is not None:
vals = list(filter(lambda x: x >= domain[0], vals))
if domain[1] is not None:
vals = list(filter(lambda x: x < domain[1], vals))

# Constructs the large tensor containing vals
large_tensor = make_tensor(_large_size, device=device, dtype=dtype, low=domain[0], high=domain[1])

Expand Down Expand Up @@ -643,16 +652,6 @@ def test_frexp_assert_raises(self, device):
r"torch\.frexp\(\) expects exponent to have int dtype but got .+"):
torch.frexp(input, out=(mantissa, exponent))

# TODO opinfo mvlgamma
@unittest.skipIf(not TEST_SCIPY, "Scipy not found")
def test_mvlgamma(self, device):
from scipy.special import multigammaln
for d in range(1, 5):
input = torch.empty(10, device=device).uniform_(d, 10)
res_torch = torch.mvlgamma(input, d)
res_scipy = multigammaln(input.cpu().numpy(), d)
self.assertEqual(res_torch.cpu().numpy(), res_scipy, atol=1e-5, rtol=0)

def test_mvlgamma_argcheck(self, device):
def run_test(d):
input = torch.linspace((d - 2) / 2, 10, 10, device=device)
Expand Down
90 changes: 86 additions & 4 deletions torch/testing/_internal/common_methods_invocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2853,6 +2853,67 @@ def generator():
return list(generator())


def sample_inputs_mvlgamma(op_info, device, dtype, requires_grad, **kwargs):
make_arg = partial(make_tensor, device=device, dtype=dtype, requires_grad=requires_grad)
tensor_shapes = ((S, S), ())
ns = (1, 2, 3, 4, 5)

# Since the accepted lower bound for input
# to mvlgamma depends on `p` argument,
# the following function computes the lower bound
# which we pass to `make_tensor`.
def compute_min_val(p):
return (p - 1.) / 2

def generator():
for shape, n in product(tensor_shapes, ns):
min_val = compute_min_val(n)
yield SampleInput(make_arg(shape, low=min_val), args=(n,))

return list(generator())


# Since `mvlgamma` has multiple entries,
# there are multiple common skips for the additional
# entries. Following function is a helper to that end.
def skips_mvlgamma(skip_redundant=False):
skips = (
# outside domain values are hard error for mvlgamma op.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really interesting and a possible follow-up issue; maybe a good first issue for a new contributor?

SkipInfo('TestUnaryUfuncs', 'test_float_domains'),
)
if not skip_redundant:
# Redundant tests
skips = skips + ( # type: ignore[assignment]
SkipInfo('TestGradients'),
SkipInfo('TestOpInfo'),
SkipInfo('TestCommon'),
)
return skips


# To test reference numerics against multiple values of argument `p`,
# we make multiple OpInfo entries with each entry corresponding to different value of p.
# We run the op tests from test_ops.py only for `p=1` to avoid redundancy in testing.
# Class `MvlGammaInfo` already contains the basic information related to the operator,
# it only takes arguments like `domain`, `skips` and `sample_kwargs`, which
# differ between the entries.
class MvlGammaInfo(UnaryUfuncInfo):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment for why this class is helpful

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

def __init__(self, variant_test_name, domain, skips, sample_kwargs):
super(MvlGammaInfo, self).__init__(
'mvlgamma',
ref=reference_mvlgamma if TEST_SCIPY else _NOTHING,
variant_test_name=variant_test_name,
domain=domain,
decorators=(precisionOverride({torch.float16: 5e-2}),),
dtypes=floating_types(),
dtypesIfCPU=floating_types(),
dtypesIfCUDA=floating_types_and(torch.half),
sample_inputs_func=sample_inputs_mvlgamma,
supports_out=False,
skips=skips,
sample_kwargs=sample_kwargs)


def sample_inputs_entr(op_info, device, dtype, requires_grad, **kwargs):
low, _ = op_info.domain

Expand Down Expand Up @@ -3330,6 +3391,14 @@ def reference_polygamma(x, n):
np_dtype = torch_to_numpy_dtype_dict[torch.get_default_dtype()]
return scipy.special.polygamma(n, x).astype(np_dtype)


def reference_mvlgamma(x, d):
if x.dtype == np.float16:
return scipy.special.multigammaln(x, d).astype(np.float16)

return scipy.special.multigammaln(x, d)


def gradcheck_wrapper_hermitian_input(op, input, *args, **kwargs):
"""Gradcheck wrapper for functions that take Hermitian matrices as input.

Expand Down Expand Up @@ -4541,6 +4610,22 @@ def gradcheck_wrapper_triangular_input(op, input, *args, upper=False, **kwargs):
op=torch.mode,
dtypes=all_types_and(torch.float16, torch.bfloat16, torch.bool),
sample_inputs_func=sample_inputs_mode,),
MvlGammaInfo(variant_test_name='mvlgamma_p_1',
domain=(1e-4, float('inf')),
skips=skips_mvlgamma(),
sample_kwargs=lambda device, dtype, input: ({'p': 1}, {'d': 1})),
MvlGammaInfo(variant_test_name='mvlgamma_p_3',
domain=(1.1, float('inf')),
skips=skips_mvlgamma(skip_redundant=True) + (
SkipInfo('TestUnaryUfuncs', 'test_reference_numerics_hard', dtypes=(torch.float16,)),
),
sample_kwargs=lambda device, dtype, input: ({'p': 3}, {'d': 3})),
MvlGammaInfo(variant_test_name='mvlgamma_p_5',
domain=(2.1, float('inf')),
skips=skips_mvlgamma(skip_redundant=True) + (
SkipInfo('TestUnaryUfuncs', 'test_reference_numerics_hard', dtypes=(torch.float16,)),
),
sample_kwargs=lambda device, dtype, input: ({'p': 5}, {'d': 5})),
OpInfo('ne',
aliases=('not_equal',),
dtypes=all_types_and_complex_and(torch.bool, torch.bfloat16, torch.float16),
Expand Down Expand Up @@ -5105,6 +5190,7 @@ def gradcheck_wrapper_triangular_input(op, input, *args, upper=False, **kwargs):
OpInfo('polar',
dtypes=floating_types(),
sample_inputs_func=sample_inputs_polar),
# TODO(@kshitij12345): Refactor similar to `mvlgamma` entries.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a TODO here for polygamma.
Looks like a good first issue :)

# To test reference numerics against multiple values of argument `n`,
# we make multiple OpInfo entries with each entry corresponding to different value of n (currently 0 to 4).
# We run the op tests from test_ops.py only for `n=0` to avoid redundancy in testing.
Expand Down Expand Up @@ -5837,10 +5923,6 @@ def method_tests():
('renorm', (S, S, S), (1, 2, 3), 'norm_1'),
('renorm', (S, S, S), (inf, 2, 0.5), 'norm_inf'),
('log_softmax', (S, S, S), (1, torch.float64,), 'kwarg_dtype_would_break_jit_loader', (True,)),
('mvlgamma', torch.empty(S,).uniform_(0.5, 1), [1], "p=1"),
('mvlgamma', torch.empty(S,).uniform_(1, 2), [2], "p=2"),
('mvlgamma', torch.empty(S, S).uniform_(1.5, 3), [3], "p=3"),
('mvlgamma', torch.empty(S, S).uniform_(2.5, 5), [5], "p=5"),
('zero_', (S, S, S), NO_ARGS),
('zero_', (), NO_ARGS, 'scalar'),
('norm', (S, S), (), 'default'),
Expand Down
0