Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.
Merged
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
9 changes: 9 additions & 0 deletions test/test_torchinductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1733,6 +1733,15 @@ def fn(x):
(torch.randn([64]),),
)

def test_flip(self):
def fn(x):
return torch.flip(x, (-1,)), torch.flip(x, (0, 2)) - 2

self.common(
fn,
(torch.randn([1, 2, 6, 6]),),
)

def test_log2(self):
def fn(x):
return torch.log2(x), torch.log2(x + 1) - 2
Expand Down
4 changes: 4 additions & 0 deletions torchinductor/codegen/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ def exp(x):
def sqrt(x):
return f"std::sqrt({x})"

@staticmethod
def rsqrt(x):
return f"1 / std::sqrt({x})"

@staticmethod
def pow(a, b):
return f"std::pow({a}, {b})"
Expand Down
4 changes: 4 additions & 0 deletions torchinductor/codegen/triton.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def rand(seed, offset, _): # _ here to keep the contract identical to CPU rand
def randn(seed, offset, _): # _ here to keep the contract identical to CPU randn op
return f"tl.randn({seed}, {offset})"

@staticmethod
def rsqrt(x):
return f"tl.libdevice.rsqrt({x})"

@staticmethod
def pow(a, b):
return f"tl.libdevice.pow({a}, {b})"
Expand Down
1 change: 1 addition & 0 deletions torchinductor/decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
aten.tanh_backward,
aten.threshold_backward,
aten.transpose.int,
aten.tril.default,
aten.upsample_nearest2d_backward,
aten.upsample_bilinear2d.vec,
]
Expand Down
36 changes: 36 additions & 0 deletions torchinductor/lowering.py
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,28 @@ def accumulate(out_x, out_y, index_range1, index_range2=None):
)


@register_lowering(prims.rev.default)
def rev(x, dims):
# note - dims pre-canoncalized
x_loader = x.make_loader()
sizes = x.get_size()

def loader(idx):
idx = list(idx)
assert len(idx) == len(sizes)
for dim in dims:
idx[dim] = (sizes[dim] - 1) - idx[dim]

return x_loader(idx)

return Pointwise.create(
device=x.get_device(),
dtype=x.get_dtype(),
inner_fn=loader,
ranges=sizes,
)


@register_lowering(aten.constant_pad_nd, type_promote=False)
def constant_pad_nd(x, padding, fill_value=0):
assert (len(padding) % 2) == 0
Expand Down Expand Up @@ -2829,6 +2851,20 @@ def fn(*args):
)


# TODO - enable builtin and disable decomp to lower to ptx instruction
# Causes compilation to not complete on timm_vision_transformers inference
# @register_lowering(aten.rsqrt)
# def rsqrt(x):
# dtype = x.get_dtype()
# if is_integer_dtype(dtype) or is_boolean_dtype(dtype):
# x = to_dtype(x, torch.get_default_dtype())
#
# def _rsqrt(x):
# return ops.rsqrt(x)
#
# return make_pointwise(_rsqrt)(x)


@register_lowering([aten.sum, prims.sum])
def sum_(x, axis=None, keepdims=False, *, dtype=None):
if (
Expand Down