Skip to content

Commit ac79266

Browse files
committed
Finish T1-1-11: gcd、select_scatter、nll_loss、glu、gt
1 parent 148b475 commit ac79266

33 files changed

Lines changed: 974 additions & 15 deletions

include/infinicore/ops/gcd.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
8+
class Gcd {
9+
public:
10+
using schema = void (*)(Tensor, Tensor, Tensor);
11+
static void execute(Tensor input, Tensor other, Tensor output);
12+
static common::OpDispatcher<schema> &dispatcher();
13+
};
14+
15+
Tensor gcd(Tensor input, Tensor other);
16+
void gcd_(Tensor input, Tensor other, Tensor output);
17+
18+
} // namespace infinicore::op

include/infinicore/ops/glu.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
8+
class Glu {
9+
public:
10+
using schema = void (*)(Tensor, Tensor, int);
11+
static void execute(Tensor input, Tensor output, int dim);
12+
static common::OpDispatcher<schema> &dispatcher();
13+
};
14+
15+
Tensor glu(Tensor input, int dim);
16+
void glu_(Tensor input, Tensor output, int dim);
17+
18+
} // namespace infinicore::op

include/infinicore/ops/gt.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
8+
class Gt {
9+
public:
10+
using schema = void (*)(Tensor, Tensor, Tensor);
11+
static void execute(Tensor input, Tensor other, Tensor output);
12+
static common::OpDispatcher<schema> &dispatcher();
13+
};
14+
15+
Tensor gt(Tensor input, Tensor other);
16+
void gt_(Tensor input, Tensor other, Tensor output);
17+
18+
} // namespace infinicore::op
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
#include <optional>
6+
7+
namespace infinicore::op {
8+
9+
class NLLLoss {
10+
public:
11+
using schema = void (*)(Tensor, Tensor, std::optional<Tensor>, Tensor, int64_t);
12+
static void execute(Tensor input, Tensor target, std::optional<Tensor> weight, Tensor output, int64_t ignore_index);
13+
static common::OpDispatcher<schema> &dispatcher();
14+
};
15+
16+
Tensor nll_loss(Tensor input, Tensor target, std::optional<Tensor> weight, int64_t ignore_index);
17+
void nll_loss_(Tensor input, Tensor target, std::optional<Tensor> weight, Tensor output, int64_t ignore_index);
18+
19+
} // namespace infinicore::op
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include "../device.hpp"
4+
#include "common/op.hpp"
5+
6+
namespace infinicore::op {
7+
8+
class SelectScatter {
9+
public:
10+
using schema = void (*)(Tensor, Tensor, int64_t, int64_t, Tensor);
11+
static void execute(Tensor input, Tensor src, int64_t dim, int64_t index, Tensor output);
12+
static common::OpDispatcher<schema> &dispatcher();
13+
};
14+
15+
Tensor select_scatter(Tensor input, Tensor src, int64_t dim, int64_t index);
16+
void select_scatter_(Tensor input, Tensor src, int64_t dim, int64_t index, Tensor output);
17+
18+
} // namespace infinicore::op

python/infinicore/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
from infinicore.ops.rearrange import rearrange
5555
from infinicore.ops.squeeze import squeeze
5656
from infinicore.ops.unsqueeze import unsqueeze
57+
from infinicore.ops.gcd import gcd
58+
from infinicore.ops.gt import gt
59+
from infinicore.ops.select_scatter import select_scatter
5760
from infinicore.tensor import (
5861
Tensor,
5962
empty,
@@ -134,6 +137,9 @@
134137
"strided_empty",
135138
"strided_from_blob",
136139
"zeros",
140+
"gcd",
141+
"select_scatter",
142+
"gt",
137143
]
138144

139145
use_ntops = False

python/infinicore/nn/functional/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from .rope import RopeAlgo, rope
77
from .silu import silu
88
from .swiglu import swiglu
9+
from .nll_loss import nll_loss
10+
from .glu import glu
911

1012
__all__ = [
1113
"causal_softmax",
@@ -17,4 +19,6 @@
1719
"embedding",
1820
"rope",
1921
"RopeAlgo",
22+
"nll_loss",
23+
"glu",
2024
]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import infinicore
2+
from infinicore.lib import _infinicore
3+
from infinicore.tensor import Tensor
4+
5+
def glu(input: Tensor, dim: int = -1) -> Tensor:
6+
7+
if infinicore.use_ntops and input.device.type in ("cuda", "musa"):
8+
return infinicore.ntops.torch.glu(input, dim)
9+
10+
return Tensor(_infinicore.glu(input._underlying, dim))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import infinicore
2+
from infinicore.lib import _infinicore
3+
from infinicore.tensor import Tensor
4+
5+
def nll_loss(
6+
input: Tensor,
7+
target: Tensor,
8+
weight: Tensor | None = None,
9+
ignore_index: int = -100,
10+
reduction: str = "mean",
11+
*,
12+
out=None,
13+
) -> Tensor:
14+
15+
if infinicore.use_ntops and input.device.type in ("cuda", "musa"):
16+
return infinicore.ntops.torch.nll_loss(
17+
input, target, weight=weight, ignore_index=ignore_index, reduction=reduction
18+
)
19+
20+
weight_underlying = weight._underlying if weight is not None else None
21+
22+
if out is None:
23+
return Tensor(
24+
_infinicore.nll_loss(
25+
input._underlying,
26+
target._underlying,
27+
weight_underlying,
28+
ignore_index
29+
)
30+
)
31+
32+
_infinicore.nll_loss_(
33+
input._underlying,
34+
target._underlying,
35+
weight_underlying,
36+
out._underlying,
37+
ignore_index
38+
)
39+
return out

python/infinicore/ops/gcd.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import infinicore
2+
from infinicore.lib import _infinicore
3+
from infinicore.tensor import Tensor
4+
5+
def gcd(input: Tensor, other: Tensor, *, out=None) -> Tensor:
6+
r"""Computes the element-wise greatest common divisor (GCD)."""
7+
8+
if infinicore.use_ntops and input.device.type in ("cuda", "musa"):
9+
return infinicore.ntops.torch.gcd(input, other, out=out)
10+
11+
if out is None:
12+
return Tensor(_infinicore.gcd(input._underlying, other._underlying))
13+
14+
_infinicore.gcd_(input._underlying, other._underlying, out._underlying)
15+
return out

0 commit comments

Comments
 (0)