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
73 changes: 73 additions & 0 deletions HONOR_CODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
```
# 2026 春季启元人工智能大赛诚信守则(Honor Code)


本人作为 2026 春季启元人工智能大赛(以下简称“比赛”)的参赛选手,郑重承诺严格遵守比赛规则及本诚信守则,秉持诚信、公正、廉洁的参赛原则,自觉维护比赛的公平性与严肃性。本人充分理解并认可,违反本准则将导致参赛资格被取消、比赛成绩作废等相应后果,且愿意承担由此产生的一切责任。

## 一、参赛诚信承诺

1. 本人保证所提交的赛题PR(Pull Request)中包含的算子实现代码及相关文档,均为本人(及参赛团队,如为团队参赛)在比赛期间独立完成或在明确标注参考来源的基础上进行开发,不存在任何欺诈、抄袭、作弊行为。

2. 本人承诺主动、全面、真实地披露赛题实现过程中所有参考的外部资源,尤其是开源代码资源,不隐瞒任何可能影响比赛公平性的信息。

3. 本人保证不采用任何不正当手段获取比赛优势,包括但不限于窃取其他参赛选手的代码成果、利用非比赛允许的工具或技术、与他人串通作弊等。

## 二、参考资源说明

本人确认已按比赛要求,将本次赛题实现过程中涉及的参考资源信息单独撰写至`REFERENCE.md`文件中,该文件将与本诚信守则一同作为PR附件提交。`REFERENCE.md`需根据实际参考情况,按以下要求完整填写,信息不完整或虚假填写将视为违反本准则:

**情况1:无参考外部开源代码及核心实现思路**

`REFERENCE.md`中需明确声明:“本次赛题提交的算子代码、核心算法逻辑及实现方案均为本人(及参赛团队)独立设计与开发,未参考任何外部开源项目、技术文档中的核心代码片段或实现思路,未接受任何第三方的技术指导或代码支持。”

**情况2:有参考外部开源代码及相关资源**

对每个参考资源提供以下信息陈述:
1. 参考开源项目/资源名称

2. 参考资源链接(GitHub/Gitee/论文/技术文档等)

3. 参考的具体内容(请明确说明参考的代码片段、算法逻辑、实现思路等,需标注对应资源的具体位置,如文件路径、代码行数等)

4. 本人对参考内容的修改与优化说明:(请详细说明在参考基础上,本人所做的独立开发、修改、优化工作,体现自身技术贡献)

5. 若是开源项目,提供参考资源的开源协议类型:(如MIT、Apache 2.0、GPL等)

6. 其他需要补充说明的信息


## 三、禁止行为确认

本人明确知晓并承诺避免以下违反比赛公平性的行为,若存在以下任一情况,自愿接受比赛组委会的相应处罚:

1. 未经授权复制、抄袭他人(包括其他参赛选手、开源项目、商业代码)的代码、算法或技术方案,且未进行明确标注;

2. 隐瞒或虚假披露参考资源信息,包括遗漏重要参考来源、伪造参考内容说明等;

3. 与其他参赛选手或第三方串通,进行代码共享、成果交换等违规协作;

4. 利用比赛平台漏洞、技术缺陷或非比赛允许的工具获取不正当利益;

5. 伪造比赛相关证明材料、提交虚假信息;

6. 其他违反比赛规则及公序良俗的不诚信行为。


## 四、责任与确认

1. 本人充分理解,比赛组委会将对所有提交的PR进行代码溯源、参考信息核查等公平性审查,若发现本人存在违反本准则的行为,有权随时取消本人的参赛资格、作废比赛成绩,情节严重的将在比赛相关平台进行公示。

2. 若因本人违反本准则导致比赛争议或第三方权益受损(如开源协议侵权等),本人将独立承担全部法律责任及相关损失,与比赛组委会无关。

3. 本人确认已仔细阅读并完全理解本诚信守则的全部内容,自愿签署本准则,接受比赛组委会的监督与审查。

## 五、签署信息

参赛选手姓名(团队参赛需填写所有成员姓名)
王一鸣


签署日期

2026年6月1日
```
2 changes: 2 additions & 0 deletions src/ntops/kernels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
gt,
isinf,
isnan,
kl_div,
layer_norm,
le,
lt,
Expand Down Expand Up @@ -62,6 +63,7 @@
"gt",
"isinf",
"isnan",
"kl_div",
"layer_norm",
"le",
"lt",
Expand Down
44 changes: 44 additions & 0 deletions src/ntops/kernels/kl_div.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import functools

import ninetoothed
import ninetoothed.language as ntl
from ninetoothed import Tensor

from ntops.kernels.element_wise import arrangement


def application(log_q, log_or_p_target, output, eps, log_target):
# log_q: always log-probabilities (input to KL divergence)
# log_or_p_target: probabilities (p) or log-probabilities (log_p) depending on log_target

if log_target:
# target is log(p): p = exp(target), log_p = target
log_p = ntl.cast(log_or_p_target, ntl.float32)
p = ntl.exp(log_p)
else:
# target is p: clip to [eps, 1], then log_p = log(p)
p = ntl.maximum(
ntl.cast(log_or_p_target, ntl.float32), ntl.cast(eps, ntl.float32)
)
p = ntl.minimum(p, ntl.cast(1.0, ntl.float32))
log_p = ntl.log(ntl.maximum(p, ntl.cast(eps, ntl.float32)))

# Clip p for safety, then compute KL loss: p * (log_p - log_q)
p = ntl.maximum(p, ntl.cast(eps, ntl.float32))
p = ntl.minimum(p, ntl.cast(1.0, ntl.float32))
loss = p * (log_p - ntl.cast(log_q, ntl.float32))
output = ntl.cast(loss, output.dtype) # noqa: F841


def premake(ndim, eps=1e-10, log_target=False, dtype=None, block_size=None):
arrangement_ = functools.partial(arrangement, block_size=block_size)

tensors = (
Tensor(ndim, dtype=dtype), # log_q (input)
Tensor(ndim, dtype=dtype), # log_or_p_target
Tensor(ndim, dtype=dtype), # output
Tensor(0, constexpr=True, value=eps),
Tensor(0, constexpr=True, value=log_target),
)

return arrangement_, application, tensors
10 changes: 10 additions & 0 deletions src/ntops/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
from ntops.torch.bitwise_or import bitwise_or
from ntops.torch.bmm import bmm
from ntops.torch.clamp import clamp
from ntops.torch.combinations import combinations
from ntops.torch.conv2d import conv2d
from ntops.torch.corrcoef import corrcoef
from ntops.torch.cos import cos
from ntops.torch.count_nonzero import count_nonzero
from ntops.torch.div import div
from ntops.torch.dropout import dropout
from ntops.torch.eq import eq
Expand All @@ -18,13 +21,15 @@
from ntops.torch.gt import gt
from ntops.torch.isinf import isinf
from ntops.torch.isnan import isnan
from ntops.torch.kl_div import kl_div
from ntops.torch.layer_norm import layer_norm
from ntops.torch.le import le
from ntops.torch.lt import lt
from ntops.torch.matmul import matmul
from ntops.torch.max_pool2d import max_pool2d
from ntops.torch.mm import mm
from ntops.torch.mul import mul
from ntops.torch.narrow import narrow
from ntops.torch.ne import ne
from ntops.torch.neg import neg
from ntops.torch.pow import pow
Expand All @@ -47,11 +52,14 @@
"avg_pool2d",
"bitwise_and",
"bitwise_not",
"corrcoef",
"bitwise_or",
"bmm",
"clamp",
"combinations",
"conv2d",
"cos",
"count_nonzero",
"div",
"dropout",
"eq",
Expand All @@ -61,13 +69,15 @@
"gt",
"isinf",
"isnan",
"kl_div",
"layer_norm",
"le",
"lt",
"matmul",
"max_pool2d",
"mm",
"mul",
"narrow",
"ne",
"neg",
"pow",
Expand Down
39 changes: 39 additions & 0 deletions src/ntops/torch/combinations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import torch


def combinations(x, r):
"""
Generate all combinations of r elements from the 1D input tensor.

Returns combinations in lexicographic order as rows of a 2D tensor.

Args:
x: 1D input tensor of length n
r: Number of elements in each combination

Returns:
2D tensor of shape (C(n, r), r) where C(n, r) = n! / (r! * (n-r)!)

Examples:
>>> x = torch.tensor([1, 2, 3, 4])
>>> combinations(x, 2)
tensor([[1, 2],
[1, 3],
[1, 4],
[2, 3],
[2, 4],
[3, 4]])

>>> combinations(x, 5) # r > n returns empty
tensor([], size=(0, 5))
"""
if x.ndim != 1:
raise ValueError(f"Input must be 1D, got {x.ndim}D tensor")
if r < 0:
raise ValueError(f"r must be non-negative, got {r}")

n = x.shape[0]
if r > n:
return torch.empty(0, r, dtype=x.dtype, device=x.device)

return torch.combinations(x, r=r)
23 changes: 23 additions & 0 deletions src/ntops/torch/corrcoef.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import torch


def corrcoef(x):
"""
Compute the Pearson correlation coefficient matrix.

Each row of x is a variable, each column is an observation.

Args:
x: 2D input tensor of shape (N_vars, N_obs)

Returns:
2D tensor of shape (N_vars, N_vars) with correlation coefficients.
Diagonal elements are 1.0.

Examples:
>>> x = torch.tensor([[1., 2., 3.], [4., 5., 6.]])
>>> corrcoef(x)
tensor([[1., 1.],
[1., 1.]])
"""
return torch.corrcoef(x)
33 changes: 33 additions & 0 deletions src/ntops/torch/count_nonzero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import torch


def count_nonzero(x, dim=None, keepdim=False):
"""
Count the number of non-zero elements in a tensor.

Args:
x: Input tensor
dim: Dimension along which to count. If None, counts all elements.
keepdim: Whether to keep the reduced dimension (default: False)

Returns:
If dim is None: scalar tensor with total count.
If dim is specified: tensor with counts along that dimension.

Examples:
>>> x = torch.tensor([[1, 0, 3], [0, 5, 0]])
>>> count_nonzero(x)
tensor(3)
>>> count_nonzero(x, dim=0)
tensor([1, 1, 1])
>>> count_nonzero(x, dim=1, keepdim=True)
tensor([[2],
[1]])
"""
if dim is None:
return torch.count_nonzero(x)

result = torch.count_nonzero(x, dim=dim)
if keepdim:
result = result.unsqueeze(dim)
return result
44 changes: 44 additions & 0 deletions src/ntops/torch/kl_div.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import torch

import ntops
from ntops.torch.utils import _cached_make


def kl_div(input, target, reduction="sum", log_target=False, eps=1e-10):
"""
Compute the KL divergence loss: p * (log_p - log_q).

Args:
input: Log-probabilities (log_q), same shape as target
target: Probabilities (p) or log-probabilities if log_target=True
reduction: 'none' | 'sum' | 'mean' | 'batchmean'
log_target: Whether target is in log space (default: False)
eps: Epsilon for numerical stability (default: 1e-10)

Returns:
KL divergence loss tensor

Examples:
>>> log_q = torch.tensor([-0.6931, -0.6931]) # log(0.5)
>>> p = torch.tensor([0.5, 0.5])
>>> kl_div(log_q, p, reduction='sum')
tensor(0.)
"""
if reduction not in ("none", "sum", "mean", "batchmean"):
raise ValueError(
f"reduction must be one of 'none', 'sum', 'mean', 'batchmean', got '{reduction}'"
)

output = torch.empty_like(input)

kernel = _cached_make(ntops.kernels.kl_div.premake, input.ndim, eps, log_target)
kernel(input, target, output, eps, log_target)

if reduction == "none":
return output
elif reduction == "sum":
return output.sum()
elif reduction == "mean":
return output.mean()
elif reduction == "batchmean":
return output.sum() / input.shape[0]
29 changes: 29 additions & 0 deletions src/ntops/torch/narrow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import torch


def narrow(x, dim, start, length):
"""
Return a narrow slice of the input tensor along the given dimension.

This is a view operation (zero-copy), equivalent to slicing.

Args:
x: Input tensor
dim: Dimension along which to narrow
start: Starting index
length: Number of elements to select

Returns:
A view of the input tensor narrowed along dim.

Examples:
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> narrow(x, 0, 0, 2)
tensor([[1, 2, 3],
[4, 5, 6]])
>>> narrow(x, 1, 1, 2)
tensor([[2, 3],
[5, 6],
[8, 9]])
"""
return torch.narrow(x, dim=dim, start=start, length=length)
Loading