mirror of
https://github.com/NVIDIA/cutlass.git
synced 2026-05-11 17:00:05 +00:00
Add absf and floor to cute.math (#3156)
The ops are already exposed by the underlying dialect.
This commit is contained in:
@@ -49,6 +49,33 @@ def _math_op(func: Callable, fastmath: bool, *args, **kwargs):
|
||||
|
||||
|
||||
@dsl_user_op
|
||||
def absf(
|
||||
a: Union[TensorSSA, Numeric], fastmath: bool = False, *, loc=None, ip=None
|
||||
) -> Union[TensorSSA, Numeric]:
|
||||
"""Compute element-wise absolute value of the input tensor.
|
||||
|
||||
:param a: Input tensor
|
||||
:type a: Union[TensorSSA, Numeric]
|
||||
:param fastmath: Enable fast math optimizations, defaults to False
|
||||
:type fastmath: bool, optional
|
||||
:param loc: Source location information, defaults to None
|
||||
:type loc: Optional[Location]
|
||||
:param ip: Insertion point for IR generation, defaults to None
|
||||
:type ip: Optional[InsertionPoint]
|
||||
:return: Tensor containing the absolute value of each element in input tensor
|
||||
:rtype: Union[TensorSSA, Numeric]
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block::
|
||||
|
||||
x = cute.make_rmem_tensor(layout) # Create tensor
|
||||
y = x.load() # Load values
|
||||
z = absf(y) # Compute absolute value
|
||||
"""
|
||||
return _math_op(math.absf, fastmath, a, loc=loc, ip=ip)
|
||||
|
||||
|
||||
def acos(
|
||||
a: Union[TensorSSA, Numeric], fastmath: bool = False, *, loc=None, ip=None
|
||||
) -> Union[TensorSSA, Numeric]:
|
||||
@@ -166,6 +193,43 @@ def atan2(
|
||||
return _math_op(math.atan2, fastmath, a, b, loc=loc, ip=ip)
|
||||
|
||||
|
||||
@dsl_user_op
|
||||
def copysign(
|
||||
a: Union[TensorSSA, Numeric],
|
||||
b: Union[TensorSSA, Numeric],
|
||||
fastmath: bool = False,
|
||||
*,
|
||||
loc=None,
|
||||
ip=None,
|
||||
) -> Union[TensorSSA, Numeric]:
|
||||
"""Compute element-wise copysign of two tensors.
|
||||
|
||||
Returns a value with the magnitude of ``a`` and the sign of ``b``.
|
||||
|
||||
:param a: Input tensor providing magnitude
|
||||
:type a: Union[TensorSSA, Numeric]
|
||||
:param b: Input tensor providing sign
|
||||
:type b: Union[TensorSSA, Numeric]
|
||||
:param fastmath: Enable fast math optimizations, defaults to False
|
||||
:type fastmath: bool, optional
|
||||
:param loc: Source location information, defaults to None
|
||||
:type loc: Optional[Location]
|
||||
:param ip: Insertion point for IR generation, defaults to None
|
||||
:type ip: Optional[InsertionPoint]
|
||||
:return: Tensor where each element has the magnitude of ``a`` and the sign of ``b``
|
||||
:rtype: Union[TensorSSA, Numeric]
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block::
|
||||
|
||||
mag = cute.make_rmem_tensor(ptr1, layout).load() # magnitudes
|
||||
sgn = cute.make_rmem_tensor(ptr2, layout).load() # signs
|
||||
result = copysign(mag, sgn) # Combine magnitude and sign
|
||||
"""
|
||||
return _math_op(math.copysign, fastmath, a, b, loc=loc, ip=ip)
|
||||
|
||||
|
||||
@dsl_user_op
|
||||
def cos(
|
||||
a: Union[TensorSSA, Numeric], fastmath: bool = False, *, loc=None, ip=None
|
||||
@@ -282,6 +346,33 @@ def exp2(
|
||||
|
||||
|
||||
@dsl_user_op
|
||||
def floor(
|
||||
a: Union[TensorSSA, Numeric], fastmath: bool = False, *, loc=None, ip=None
|
||||
) -> Union[TensorSSA, Numeric]:
|
||||
"""Compute element-wise floor of the input tensor.
|
||||
|
||||
:param a: Input tensor
|
||||
:type a: Union[TensorSSA, Numeric]
|
||||
:param fastmath: Enable fast math optimizations, defaults to False
|
||||
:type fastmath: bool, optional
|
||||
:param loc: Source location information, defaults to None
|
||||
:type loc: Optional[Location]
|
||||
:param ip: Insertion point for IR generation, defaults to None
|
||||
:type ip: Optional[InsertionPoint]
|
||||
:return: Tensor containing the largest integer less than or equal to each element in input tensor
|
||||
:rtype: Union[TensorSSA, Numeric]
|
||||
|
||||
Example:
|
||||
|
||||
.. code-block::
|
||||
|
||||
x = cute.make_rmem_tensor(layout) # Create tensor
|
||||
y = x.load() # Load values
|
||||
z = floor(y) # Compute floor
|
||||
"""
|
||||
return _math_op(math.floor, fastmath, a, loc=loc, ip=ip)
|
||||
|
||||
|
||||
def log(
|
||||
a: Union[TensorSSA, Numeric], fastmath: bool = False, *, loc=None, ip=None
|
||||
) -> Union[TensorSSA, Numeric]:
|
||||
@@ -508,14 +599,17 @@ def tanh(
|
||||
|
||||
|
||||
__all__ = [
|
||||
"absf",
|
||||
"acos",
|
||||
"asin",
|
||||
"atan",
|
||||
"atan2",
|
||||
"copysign",
|
||||
"cos",
|
||||
"erf",
|
||||
"exp",
|
||||
"exp2",
|
||||
"floor",
|
||||
"log",
|
||||
"log10",
|
||||
"log2",
|
||||
|
||||
109
test/examples/CuTeDSL/test_math.py
Normal file
109
test/examples/CuTeDSL/test_math.py
Normal file
@@ -0,0 +1,109 @@
|
||||
# Copyright (c) 2025 - 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are met:
|
||||
|
||||
# 1. Redistributions of source code must retain the above copyright notice, this
|
||||
# list of conditions and the following disclaimer.
|
||||
|
||||
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
|
||||
# 3. Neither the name of the copyright holder nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
import pytest
|
||||
import torch
|
||||
|
||||
import cutlass
|
||||
import cutlass.cute as cute
|
||||
from cutlass.cute.runtime import from_dlpack
|
||||
|
||||
|
||||
@cute.kernel
|
||||
def _unary_ops_kernel(
|
||||
absf_inp: cute.Tensor, absf_out: cute.Tensor,
|
||||
floor_inp: cute.Tensor, floor_out: cute.Tensor,
|
||||
):
|
||||
tidx, _, _ = cute.arch.thread_idx()
|
||||
absf_out[tidx] = cute.math.absf(absf_inp[tidx])
|
||||
floor_out[tidx] = cute.math.floor(floor_inp[tidx])
|
||||
|
||||
|
||||
@cute.jit
|
||||
def _unary_ops_host(
|
||||
absf_inp: cute.Tensor, absf_out: cute.Tensor,
|
||||
floor_inp: cute.Tensor, floor_out: cute.Tensor,
|
||||
):
|
||||
_unary_ops_kernel(absf_inp, absf_out, floor_inp, floor_out).launch(
|
||||
grid=[1, 1, 1], block=[absf_inp.shape[0], 1, 1]
|
||||
)
|
||||
|
||||
|
||||
def test_unary_ops():
|
||||
absf_inp = torch.tensor([-3.5, 2.0, 0.0], device="cuda", dtype=torch.float32)
|
||||
absf_expected = torch.tensor([3.5, 2.0, 0.0], device="cuda", dtype=torch.float32)
|
||||
absf_out = torch.zeros(3, device="cuda", dtype=torch.float32)
|
||||
floor_inp = torch.tensor([3.7, -2.3, 5.0], device="cuda", dtype=torch.float32)
|
||||
floor_expected = torch.tensor([3.0, -3.0, 5.0], device="cuda", dtype=torch.float32)
|
||||
floor_out = torch.zeros(3, device="cuda", dtype=torch.float32)
|
||||
absf_inp_cute = from_dlpack(absf_inp)
|
||||
absf_out_cute = from_dlpack(absf_out)
|
||||
floor_inp_cute = from_dlpack(floor_inp)
|
||||
floor_out_cute = from_dlpack(floor_out)
|
||||
|
||||
args = (absf_inp_cute, absf_out_cute, floor_inp_cute, floor_out_cute)
|
||||
|
||||
cute.compile(_unary_ops_host, *args)(*args)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
assert torch.equal(absf_out, absf_expected)
|
||||
assert torch.equal(floor_out, floor_expected)
|
||||
|
||||
|
||||
@cute.kernel
|
||||
def _binary_ops_kernel(
|
||||
mag_inp: cute.Tensor, sign_inp: cute.Tensor, out: cute.Tensor,
|
||||
):
|
||||
tidx, _, _ = cute.arch.thread_idx()
|
||||
out[tidx] = cute.math.copysign(mag_inp[tidx], sign_inp[tidx])
|
||||
|
||||
|
||||
@cute.jit
|
||||
def _binary_ops_host(
|
||||
mag_inp: cute.Tensor, sign_inp: cute.Tensor, out: cute.Tensor,
|
||||
):
|
||||
_binary_ops_kernel(mag_inp, sign_inp, out).launch(
|
||||
grid=[1, 1, 1], block=[mag_inp.shape[0], 1, 1]
|
||||
)
|
||||
|
||||
|
||||
def test_binary_ops():
|
||||
mag_inp = torch.tensor([3.5, -2.0, 0.0, 1.0], device="cuda", dtype=torch.float32)
|
||||
sign_inp = torch.tensor([-1.0, 1.0, -1.0, 1.0], device="cuda", dtype=torch.float32)
|
||||
expected = torch.tensor([-3.5, 2.0, -0.0, 1.0], device="cuda", dtype=torch.float32)
|
||||
out = torch.zeros(4, device="cuda", dtype=torch.float32)
|
||||
mag_inp_cute = from_dlpack(mag_inp)
|
||||
sign_inp_cute = from_dlpack(sign_inp)
|
||||
out_cute = from_dlpack(out)
|
||||
|
||||
args = (mag_inp_cute, sign_inp_cute, out_cute)
|
||||
|
||||
cute.compile(_binary_ops_host, *args)(*args)
|
||||
torch.cuda.synchronize()
|
||||
|
||||
assert torch.equal(out, expected)
|
||||
Reference in New Issue
Block a user