-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Operator] Add repeat_interleave_self_int op
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import torch | ||
import triton | ||
|
||
from ..utils.pointwise_dynamic import pointwise_dynamic | ||
from ..utils.shape_utils import c_contiguous_stride, volume | ||
from ..utils.tensor_wrapper import StridedBuffer | ||
|
||
|
||
@pointwise_dynamic(num_inputs=1, promotion_methods=[(0, "DEFAULT")]) | ||
@triton.jit | ||
def copy_func(x): | ||
return x | ||
|
||
|
||
def repeat_interleave_self_int(inp, repeats, dim=None, *, output_size=None): | ||
if dim is None: | ||
nelems = volume(inp.shape) | ||
inp_shape = [ | ||
nelems, | ||
] | ||
inp_stride = [ | ||
1, | ||
] | ||
output_shape = [ | ||
nelems, | ||
] | ||
dim = 0 | ||
else: | ||
if (dim < -inp.ndim) or (dim >= inp.ndim): | ||
raise IndexError( | ||
"Dimension out of range (expected to be in range of [{}, {}], but got {})".format( | ||
-inp.ndim, inp.ndim - 1, dim | ||
) | ||
) | ||
inp_shape = list(inp.shape) | ||
inp_stride = list(inp.stride()) | ||
output_shape = list(inp.shape) | ||
|
||
if dim < 0: | ||
dim = dim + len(inp_shape) | ||
|
||
output_shape[dim] *= repeats | ||
|
||
if output_size is not None and output_size != output_shape[dim]: | ||
raise RuntimeError( | ||
"repeat_interleave: Invalid output_size, expected {} but got {}".format( | ||
output_shape[dim], output_size | ||
) | ||
) | ||
|
||
output = torch.empty(output_shape, dtype=inp.dtype, device=inp.device) | ||
|
||
if repeats == 0: | ||
return output | ||
|
||
in_view_stride = inp_stride[: dim + 1] + [0] + inp_stride[dim + 1 :] | ||
out_view_shape = inp_shape[: dim + 1] + [repeats] + inp_shape[dim + 1 :] | ||
out_view_stride = c_contiguous_stride(out_view_shape) | ||
|
||
in_view = StridedBuffer(inp, out_view_shape, in_view_stride) | ||
out_view = StridedBuffer(output, out_view_shape, out_view_stride) | ||
ndim = len(out_view_shape) | ||
copy_func.instantiate(ndim)(in_view, out0=out_view) | ||
return output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters