Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/flag_gems/runtime/backend/_iluvatar/ops/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from .div import div_mode, div_mode_
from .reciprocal import reciprocal, reciprocal_

__all__ = [
"div_mode",
"div_mode_",
"reciprocal",
"reciprocal_",
]
37 changes: 37 additions & 0 deletions src/flag_gems/runtime/backend/_iluvatar/ops/reciprocal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging

import torch
import triton

from flag_gems.utils import pointwise_dynamic

logger = logging.getLogger(__name__)


@pointwise_dynamic(promotion_methods=[(0, "DEFAULT")])
@triton.jit
def reciprocal_kernel(x):
return 1.0 / x


def reciprocal(input: torch.Tensor) -> torch.Tensor:
"""Compute reciprocal of input tensor: output = 1.0 / input"""
logger.debug("GEMS RECIPROCAL - Iluvatar")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
logger.debug("GEMS RECIPROCAL - Iluvatar")
logger.debug("GEMS_ILUVATAR RECIPROCAL")

# Empty tensor protection (Rule GR-030)
if input.numel() == 0:
return torch.empty_like(input)

output = reciprocal_kernel(input)
return output


def reciprocal_(input: torch.Tensor) -> torch.Tensor:
"""In-place reciprocal: input = 1.0 / input"""
logger.debug("GEMS RECIPROCAL_ - Iluvatar in-place")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above.

# Empty tensor protection (Rule GR-030)
if input.numel() == 0:
return input

# In-place variant
out = reciprocal_kernel(input, out0=input)
return out
Loading