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
1 change: 1 addition & 0 deletions python/infinicore/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from .triplet_margin_with_distance_loss import triplet_margin_with_distance_loss
from .unfold import unfold
from .upsample_bilinear import upsample_bilinear
from .upsample_nearest import upsample_nearest

__all__ = [
"adaptive_max_pool1d",
Expand Down
7 changes: 7 additions & 0 deletions python/infinicore/nn/functional/interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ def interpolate(
"Per-dimension scale_factor is not supported; pass a scalar (or equal values)."
)

if mode == "nearest" and spatial_ndim == 1:
from .upsample_nearest import upsample_nearest

if size_list:
return upsample_nearest(input, size=size_list[0])
return upsample_nearest(input, scale_factor=scale_list[0])

align_i = 0 if align_corners is None else int(bool(align_corners))

return Tensor(
Expand Down
22 changes: 20 additions & 2 deletions python/infinicore/ops/logical_and.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@

def logical_and(input: Tensor, other: Tensor, *, out=None) -> Tensor:
r"""Computes the element-wise logical AND of the given input tensors."""
if input.device.type not in ("cpu"):
if input.device.type != "cpu":
assert infinicore.use_ntops
return infinicore.ntops.torch.logical_and(input, other, out=out)
lhs = infinicore.empty(input.shape, dtype=infinicore.bool, device=input.device)
rhs = infinicore.empty(other.shape, dtype=infinicore.bool, device=other.device)
input_zero = infinicore.zeros(
input.shape, dtype=input.dtype, device=input.device
)
other_zero = infinicore.zeros(
other.shape, dtype=other.dtype, device=other.device
)

infinicore.ntops.torch.ne(input, input_zero, out=lhs)
infinicore.ntops.torch.ne(other, other_zero, out=rhs)

if out is None:
out = infinicore.empty(
input.shape, dtype=infinicore.bool, device=input.device
)

infinicore.ntops.torch.bitwise_and(lhs, rhs, out=out)
return out

if out is None:
return Tensor(_infinicore.logical_and(input._underlying, other._underlying))
Expand Down
18 changes: 15 additions & 3 deletions python/infinicore/ops/logical_not.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@

def logical_not(input: Tensor, *, out=None) -> Tensor:
r"""Computes the element-wise logical NOT of the given input tensors."""
# 1. 非CPU平台调用 ntops 实现
if input.device.type not in ("cpu"):
if input.device.type != "cpu":
assert infinicore.use_ntops
return infinicore.ntops.torch.logical_not(input, out=out)
input_zero = infinicore.zeros(
input.shape, dtype=input.dtype, device=input.device
)
result = infinicore.empty(
input.shape, dtype=infinicore.bool, device=input.device
)

infinicore.ntops.torch.eq(input, input_zero, out=result)

if out is None:
return result

infinicore.ntops.torch.bitwise_and(result, result, out=out)
return out

# 2. 如果没有提供 out,创建一个新的 Tensor 并返回
if out is None:
Expand Down
6 changes: 5 additions & 1 deletion test/infinicore/ops/fmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ def parse_test_cases():
tol = _TOLERANCE_MAP[dtype]
a_spec = TensorSpec.from_tensor(in_shape, in_strides, dtype)
b_spec = TensorSpec.from_tensor(
in_shape if other_shape is None else other_shape, None, dtype
in_shape if other_shape is None else other_shape,
None,
dtype,
scale=0.9,
bias=0.1,
)

cases.append(
Expand Down
8 changes: 4 additions & 4 deletions test/infinicore/ops/index_copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def parse_test_cases():
# index for index_copy should be 1-D with length equal to source.size(dim)
index_len = src_shape[dim]

unique_index = torch.randperm(target_shape[dim])[:index_len].to(torch.int64)
index_spec = TensorSpec.from_tensor(
(index_len,),
None,
infinicore.int64,
init_mode=TensorInitializer.RANDINT,
low=0,
high=target_shape[dim],
init_mode=TensorInitializer.MANUAL,
set_tensor=unique_index,
)
src_spec = TensorSpec.from_tensor(src_shape, None, infinicore.float32)

Expand Down Expand Up @@ -90,7 +90,7 @@ def torch_operator(self, *args, **kwargs):
return torch.index_copy(*args, **kwargs)

def infinicore_operator(self, *args, **kwargs):
return infinicore.index_copy(*args, **kwargs)
return infinicore.index_copy(*args, **kwargs)


def main():
Expand Down
38 changes: 37 additions & 1 deletion test/infinicore/ops/logdet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import infinicore
import torch
from framework import BaseOperatorTest, TensorSpec, TestCase, GenericTestRunner
from framework.tensor import TensorInitializer

# Test cases format: (matrix_shape, strides_or_None)
# logdet(input) — returns (sign, logabsdet) in PyTorch
Expand All @@ -28,12 +29,47 @@
_TENSOR_DTYPES = [infinicore.float32]


def _stable_matrix_spec(shape, strides, dtype):
rows, cols = shape
if rows != cols:
raise ValueError("logdet test matrices must be square")

matrix = torch.full(shape, 0.01, dtype=torch.float32)
matrix += torch.eye(rows, dtype=torch.float32) * (rows + 1.0)

if strides is None:
return TensorSpec.from_tensor(
shape,
None,
dtype,
init_mode=TensorInitializer.MANUAL,
set_tensor=matrix,
)

storage_size = 1
for size, stride in zip(shape, strides):
storage_size += (size - 1) * abs(stride)

storage = torch.zeros((storage_size,), dtype=torch.float32)
for i in range(rows):
for j in range(cols):
storage[i * strides[0] + j * strides[1]] = matrix[i, j]

return TensorSpec.from_tensor(
shape,
strides,
dtype,
init_mode=TensorInitializer.MANUAL,
set_tensor=storage,
)


def parse_test_cases():
test_cases = []
for shape, strides in _TEST_CASES_DATA:
for dtype in _TENSOR_DTYPES:
tol = _TOLERANCE_MAP.get(dtype, {"atol": 1e-5, "rtol": 1e-4})
spec = TensorSpec.from_tensor(shape, strides, dtype)
spec = _stable_matrix_spec(shape, strides, dtype)

test_cases.append(
TestCase(
Expand Down
Loading