From 35715dc46491f66918fa859c524054620ce6eb01 Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 10:13:31 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20`empty=5Flike`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/__init__.py | 2 ++ python/infinicore/tensor.py | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/python/infinicore/__init__.py b/python/infinicore/__init__.py index 15f9ade74..9b28c513c 100644 --- a/python/infinicore/__init__.py +++ b/python/infinicore/__init__.py @@ -33,6 +33,7 @@ from infinicore.tensor import ( Tensor, empty, + empty_like, from_blob, ones, strided_empty, @@ -75,6 +76,7 @@ "matmul", "rearrange", "empty", + "empty_like", "from_blob", "ones", "strided_empty", diff --git a/python/infinicore/tensor.py b/python/infinicore/tensor.py index d40d8d10d..130c47871 100644 --- a/python/infinicore/tensor.py +++ b/python/infinicore/tensor.py @@ -93,6 +93,16 @@ def empty(size, *, dtype=None, device=None, pin_memory=False): ) +def empty_like(input, *, dtype=None, device=None): + if dtype is None: + dtype = input.dtype + + if device is None: + device = input.device + + return empty(input.size(), dtype=dtype, device=device) + + def strided_empty(size, strides, *, dtype=None, device=None, pin_memory=False): return Tensor( _infinicore.strided_empty( From 63d703fe8e3602eedaa3c8227676767c50a81d9d Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 10:15:00 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=9C=A8=20`silu`=20=E7=9A=84=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E4=B8=BA=20`None`=20=E6=97=B6=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E8=B0=83=E7=94=A8=20`ntops`=20=E7=9A=84=20`silu`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/nn/functional.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/python/infinicore/nn/functional.py b/python/infinicore/nn/functional.py index a49c88cb1..3ed747b9d 100644 --- a/python/infinicore/nn/functional.py +++ b/python/infinicore/nn/functional.py @@ -45,6 +45,9 @@ def silu(input: infinicore.Tensor, inplace: bool = False, out=None) -> infinicor r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise. """ + if infinicore.use_ntops and out is None: + return infinicore.ntops.torch.silu(input, inplace=inplace) + if inplace: _infinicore.silu_(input._underlying, input._underlying) return input From 0d36257a8aa33695cb77ee5500d4062e8385ca31 Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 13:19:35 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=8A=A0=E5=85=A5=20`ruff.yml`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ruff.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/ruff.yml diff --git a/.github/workflows/ruff.yml b/.github/workflows/ruff.yml new file mode 100644 index 000000000..6d3602cd5 --- /dev/null +++ b/.github/workflows/ruff.yml @@ -0,0 +1,14 @@ +name: Ruff +on: [push, pull_request] +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 + with: + src: './python/' + - uses: chartboost/ruff-action@v1 + with: + src: './python/' + args: format --check From b74b3cfe04f75623e68d91d3eefa3d9b40b440ce Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 13:47:41 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20Ruff=20=E4=B8=AD?= =?UTF-8?q?=E6=8F=90=E5=88=B0=E7=9A=84=20`python`=20=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/__init__.py | 4 +-- python/infinicore/nn/functional.py | 39 +++++++++++++++--------------- python/infinicore/tensor.py | 2 +- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/python/infinicore/__init__.py b/python/infinicore/__init__.py index 9b28c513c..c6bb00a31 100644 --- a/python/infinicore/__init__.py +++ b/python/infinicore/__init__.py @@ -1,5 +1,6 @@ import contextlib +from infinicore import nn as nn from infinicore.device import device from infinicore.dtype import ( bfloat16, @@ -41,12 +42,11 @@ zeros, ) -from infinicore import nn as nn - __all__ = [ # Classes. "device", "dtype", + "Tensor", # Data Types. "bfloat16", "bool", diff --git a/python/infinicore/nn/functional.py b/python/infinicore/nn/functional.py index 3ed747b9d..f901f2f6c 100644 --- a/python/infinicore/nn/functional.py +++ b/python/infinicore/nn/functional.py @@ -4,12 +4,8 @@ __all__ = ["causal_softmax", "rms_norm", "silu", "swiglu"] -def causal_softmax( - input: infinicore.Tensor, - out=None -) -> infinicore.Tensor: - r"""Apply a causal softmax function. - """ +def causal_softmax(input: infinicore.Tensor, out=None) -> infinicore.Tensor: + r"""Apply a causal softmax function.""" if out is None: return infinicore.Tensor(_infinicore.causal_softmax(input._underlying)) @@ -20,16 +16,17 @@ def causal_softmax( def rms_norm( - input: infinicore.Tensor, - normalized_shape: list[int], - weight: infinicore.Tensor, - eps: float = 1e-5, - out=None + input: infinicore.Tensor, + normalized_shape: list[int], + weight: infinicore.Tensor, + eps: float = 1e-5, + out=None, ) -> infinicore.Tensor: - r"""Apply Root Mean Square Layer Normalization. - """ + r"""Apply Root Mean Square Layer Normalization.""" - assert normalized_shape == weight.shape, "normalized_shape does not match weight.shape." + assert normalized_shape == weight.shape, ( + "normalized_shape does not match weight.shape." + ) if out is None: return infinicore.Tensor( @@ -41,9 +38,10 @@ def rms_norm( return out -def silu(input: infinicore.Tensor, inplace: bool = False, out=None) -> infinicore.Tensor: - r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise. - """ +def silu( + input: infinicore.Tensor, inplace: bool = False, out=None +) -> infinicore.Tensor: + r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise.""" if infinicore.use_ntops and out is None: return infinicore.ntops.torch.silu(input, inplace=inplace) @@ -61,11 +59,12 @@ def silu(input: infinicore.Tensor, inplace: bool = False, out=None) -> infinicor def swiglu(input: infinicore.Tensor, other: infinicore.Tensor, out=None): - r"""Apply the Swish-Gated Linear Unit (SwiGLU) function, element-wise. - """ + r"""Apply the Swish-Gated Linear Unit (SwiGLU) function, element-wise.""" if out is None: - return infinicore.Tensor(_infinicore.swiglu(input._underlying, other._underlying)) + return infinicore.Tensor( + _infinicore.swiglu(input._underlying, other._underlying) + ) _infinicore.swiglu_(out._underlying, input._underlying, other._underlying) diff --git a/python/infinicore/tensor.py b/python/infinicore/tensor.py index 130c47871..b4eada011 100644 --- a/python/infinicore/tensor.py +++ b/python/infinicore/tensor.py @@ -77,7 +77,7 @@ def view(self, shape): def debug(self, filename=None): """Print tensor data or save to file for debugging - + Args: filename: Optional filename to save raw binary data. If None, prints to stdout. """ From c2344eb99d8ccd10ca6381ff73621df0543b557c Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 14:00:17 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AA=E7=8E=AF?= =?UTF-8?q?=E5=BC=95=E7=94=A8=E5=8F=8A=20CPU=20`silu`=20=E7=9A=84=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/__init__.py | 4 +++- python/infinicore/nn/functional.py | 29 ++++++++++++----------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/python/infinicore/__init__.py b/python/infinicore/__init__.py index c6bb00a31..8b11ca8a7 100644 --- a/python/infinicore/__init__.py +++ b/python/infinicore/__init__.py @@ -1,6 +1,6 @@ import contextlib -from infinicore import nn as nn +import infinicore.nn as nn from infinicore.device import device from infinicore.dtype import ( bfloat16, @@ -43,6 +43,8 @@ ) __all__ = [ + # Modules. + "nn", # Classes. "device", "dtype", diff --git a/python/infinicore/nn/functional.py b/python/infinicore/nn/functional.py index f901f2f6c..e4a7519d4 100644 --- a/python/infinicore/nn/functional.py +++ b/python/infinicore/nn/functional.py @@ -1,14 +1,15 @@ import infinicore from infinicore.lib import _infinicore +from infinicore.tensor import Tensor __all__ = ["causal_softmax", "rms_norm", "silu", "swiglu"] -def causal_softmax(input: infinicore.Tensor, out=None) -> infinicore.Tensor: +def causal_softmax(input: Tensor, out=None) -> Tensor: r"""Apply a causal softmax function.""" if out is None: - return infinicore.Tensor(_infinicore.causal_softmax(input._underlying)) + return Tensor(_infinicore.causal_softmax(input._underlying)) _infinicore.causal_softmax_(out._underlying, input._underlying) @@ -16,12 +17,12 @@ def causal_softmax(input: infinicore.Tensor, out=None) -> infinicore.Tensor: def rms_norm( - input: infinicore.Tensor, + input: Tensor, normalized_shape: list[int], - weight: infinicore.Tensor, + weight: Tensor, eps: float = 1e-5, out=None, -) -> infinicore.Tensor: +) -> Tensor: r"""Apply Root Mean Square Layer Normalization.""" assert normalized_shape == weight.shape, ( @@ -29,21 +30,17 @@ def rms_norm( ) if out is None: - return infinicore.Tensor( - _infinicore.rms_norm(input._underlying, weight._underlying, eps) - ) + return Tensor(_infinicore.rms_norm(input._underlying, weight._underlying, eps)) _infinicore.rms_norm_(out._underlying, input._underlying, weight._underlying, eps) return out -def silu( - input: infinicore.Tensor, inplace: bool = False, out=None -) -> infinicore.Tensor: +def silu(input: Tensor, inplace: bool = False, out=None) -> Tensor: r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise.""" - if infinicore.use_ntops and out is None: + if infinicore.use_ntops and input.device.type in ("cuda", "musa") and out is None: return infinicore.ntops.torch.silu(input, inplace=inplace) if inplace: @@ -51,20 +48,18 @@ def silu( return input if out is None: - return infinicore.Tensor(_infinicore.silu(input._underlying)) + return Tensor(_infinicore.silu(input._underlying)) _infinicore.silu_(out._underlying, input._underlying) return out -def swiglu(input: infinicore.Tensor, other: infinicore.Tensor, out=None): +def swiglu(input: Tensor, other: Tensor, out=None): r"""Apply the Swish-Gated Linear Unit (SwiGLU) function, element-wise.""" if out is None: - return infinicore.Tensor( - _infinicore.swiglu(input._underlying, other._underlying) - ) + return Tensor(_infinicore.swiglu(input._underlying, other._underlying)) _infinicore.swiglu_(out._underlying, input._underlying, other._underlying) From d3b0330a32b6e7c937ac69bea4cd34612c36f381 Mon Sep 17 00:00:00 2001 From: Jiacheng Huang Date: Tue, 11 Nov 2025 14:05:24 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=9C=A8=20`functional.py`=20=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E7=AE=97=E5=AD=90=E7=9A=84=20`out`=20=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=89=8D=E5=8A=A0=20`*`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/infinicore/nn/functional.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/python/infinicore/nn/functional.py b/python/infinicore/nn/functional.py index e4a7519d4..ea969052c 100644 --- a/python/infinicore/nn/functional.py +++ b/python/infinicore/nn/functional.py @@ -21,6 +21,7 @@ def rms_norm( normalized_shape: list[int], weight: Tensor, eps: float = 1e-5, + *, out=None, ) -> Tensor: r"""Apply Root Mean Square Layer Normalization.""" @@ -37,7 +38,7 @@ def rms_norm( return out -def silu(input: Tensor, inplace: bool = False, out=None) -> Tensor: +def silu(input: Tensor, inplace: bool = False, *, out=None) -> Tensor: r"""Apply the Sigmoid Linear Unit (SiLU) function, element-wise.""" if infinicore.use_ntops and input.device.type in ("cuda", "musa") and out is None: @@ -55,7 +56,7 @@ def silu(input: Tensor, inplace: bool = False, out=None) -> Tensor: return out -def swiglu(input: Tensor, other: Tensor, out=None): +def swiglu(input: Tensor, other: Tensor, *, out=None): r"""Apply the Swish-Gated Linear Unit (SwiGLU) function, element-wise.""" if out is None: