Skip to content

Commit

Permalink
[PERF] Specialize pow(x,2) as x*x. llama-7B (#434)
Browse files Browse the repository at this point in the history
Right now `pow` with const exp argument is implemented simply. We
convert const to const tensor and run elementwise `pow` of 2 tensors. It
is simply but not always efficient.

llama2 (RMSNorm part) has `x*x` that implemented as `tensor.pow(2)`. 

Convert `pow(x,2)` to `x*x`.

Improvement on llama2-7B is around **0.237%**
  • Loading branch information
vadiklyutiy authored Aug 27, 2024
1 parent b4628be commit 595405d
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions python/hidet/graph/frontend/torch/register_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,8 @@ def tensor_where(self: Tensor, condition: Tensor, y: Union[Tensor, Number]):
@register_method(torch.Tensor.pow_)
def torch_pow(base: Union[Number, Tensor], exponent: Union[Number, Tensor]):
if isinstance(exponent, (int, float, bool)):
if exponent in (2, 2.0):
return ops.square(base)
exponent = full_like(base, exponent)
elif isinstance(base, (int, float, bool)):
base = full_like(exponent, base)
Expand Down

0 comments on commit 595405d

Please sign in to comment.