Skip to content

Commit

Permalink
Revert "[inductor] Do variance calculation in opmath type (pytorch#11…
Browse files Browse the repository at this point in the history
…5181)"

This reverts commit 42390a0.

Reverted pytorch#115181 on behalf of https://github.com/atalman due to OSSCI oncall, broke periodic tests ([comment](pytorch#115181 (comment)))
  • Loading branch information
pytorchmergebot committed Dec 14, 2023
1 parent 0fe014b commit ca4caf4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
13 changes: 0 additions & 13 deletions test/inductor/test_torchinductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,19 +1125,6 @@ def fn(a):
self.common(fn, ((torch.rand((10, 3, 352, 352), dtype=torch.float32),)))
self.common(fn, ((torch.rand((14923), dtype=torch.float32),)))

def test_multilayer_var_lowp(self):
if self.device == "cpu" and IS_MACOS and not IS_X86:
atol, rtol = 1e-5, 5e-3
else:
atol, rtol = None, None

def fn(a):
return torch.var(a)

run_test = functools.partial(self.common, atol=atol, rtol=rtol)
run_test(fn, (torch.rand((16, 16, 352, 352), dtype=torch.float16),))
run_test(fn, (torch.rand((14923), dtype=torch.float16),))

def test_embedding_bag_byte_unpack(self):
if self.device != "cpu":
raise unittest.SkipTest("No CUDA implementation (it returns empty)")
Expand Down
39 changes: 14 additions & 25 deletions torch/_inductor/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
dtype_to_type,
elementwise_dtypes,
ELEMENTWISE_TYPE_PROMOTION_KIND,
get_computation_dtype,
is_boolean_dtype,
is_float_dtype,
is_integer_dtype,
Expand Down Expand Up @@ -4575,7 +4574,7 @@ def var_mean_sum_(x, axis, correction, keepdim, return_mean):
denom = ExpandView.create(denom, list(sum_result.get_size()))
x_var = div(sum_result, denom)
if not return_mean:
return (x_var,)
return x_var

x_mean = x_mean if keepdim else squeeze(x_mean, axis)
return x_var, x_mean
Expand Down Expand Up @@ -4637,39 +4636,29 @@ def scale_fn(data):
if return_mean:
mean.realize()
return var, mean
return (var,)


def var_mean_helper_(x, *, axis, correction, keepdim, return_mean):
out_dtype = x.get_dtype()
compute_dtype = get_computation_dtype(out_dtype)
x = to_dtype(x, compute_dtype, copy=False)
kwargs = dict(
x=x,
axis=axis,
correction=correction,
keepdim=keepdim,
return_mean=return_mean,
)
output = (
var_mean_sum_(**kwargs)
if use_two_step_variance(x, axis=axis, keepdim=keepdim)
else var_mean_welford_(**kwargs)
)
output = tuple(to_dtype(x, out_dtype, copy=False) for x in output)
return output[0] if not return_mean else output
return var


@register_lowering([aten.var, prims.var])
def var_(x, axis=None, *, correction=None, keepdim=False):
return var_mean_helper_(
if use_two_step_variance(x, axis=axis, keepdim=keepdim):
return var_mean_sum_(
x, axis=axis, correction=correction, keepdim=keepdim, return_mean=False
)

return var_mean_welford_(
x, axis=axis, correction=correction, keepdim=keepdim, return_mean=False
)


@register_lowering(aten.var_mean)
def var_mean(x, axis=None, *, correction=None, keepdim=False):
return var_mean_helper_(
if use_two_step_variance(x, axis=axis, keepdim=keepdim):
return var_mean_sum_(
x, axis=axis, correction=correction, keepdim=keepdim, return_mean=True
)

return var_mean_welford_(
x, axis=axis, correction=correction, keepdim=keepdim, return_mean=True
)

Expand Down

0 comments on commit ca4caf4

Please sign in to comment.