Skip to content
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

[one-optimize] Fold ResizeNearestNeighbor Op #14407

Open
jinevening opened this issue Dec 4, 2024 · 0 comments
Open

[one-optimize] Fold ResizeNearestNeighbor Op #14407

jinevening opened this issue Dec 4, 2024 · 0 comments
Labels
good first issue Good for newcomers

Comments

@jinevening
Copy link
Contributor

jinevening commented Dec 4, 2024

What

Let's support constant folding of ResizeBiliear Op

image

Why

DETR model's positional embedding code generates such a pattern.

def get_abs_pos(abs_pos, has_cls_token, hw):
    """
    Calculate absolute positional embeddings. If needed, resize embeddings and remove cls_token
        dimension for the original embeddings.
    Args:
        abs_pos (Tensor): absolute positional embeddings with (1, num_position, C).
        has_cls_token (bool): If true, has 1 embedding in abs_pos for cls token.
        hw (Tuple): size of input image tokens.
    Returns:
        Absolute positional embeddings after processing with shape (1, H, W, C)
    """
    h, w = hw
    if has_cls_token:
        abs_pos = abs_pos[:, 1:]
    xy_num = abs_pos.shape[1]
    size = int(math.sqrt(xy_num))
    assert size * size == xy_num

    if size != h or size != w:
        new_abs_pos = F.interpolate(
            abs_pos.reshape(1, size, size, 1024).permute(0, 3, 1, 2),
            size=(h, w),
            mode="nearest",
        ) # ResizeNearestNeighbor was generated here
        return new_abs_pos.permute(0, 2, 3, 1)
    else:
        return abs_pos.reshape(1, h, w, 1024)
@jinevening jinevening changed the title [one-optimize] Fold resize bilear Op [one-optimize] Fold ResizeNearestNeighbor Op Dec 4, 2024
@jinevening jinevening added the good first issue Good for newcomers label Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant