Skip to content

Commit f76fe8a

Browse files
committed
feat(/): add negative op, api and method.
1 parent 157e782 commit f76fe8a

File tree

9 files changed

+80
-2
lines changed

9 files changed

+80
-2
lines changed

apis/numnet_c_cxx_apis.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,24 @@ Status *Min(NativeTensor *inp, NativeTensor *oup, param::min *param,
378378
}
379379
}
380380

381+
Status *Negative(NativeTensor *inp, NativeTensor *oup, param::negative *param,
382+
ProviderEnum provider) {
383+
Tensor t_inp, t_oup;
384+
inp->ToTensor(t_inp, false);
385+
oup->ToTensor(t_oup, true);
386+
OpBase *impl = GetImpl(provider);
387+
if (impl == nullptr) {
388+
return new Status(StatusCategory::NUMNET, StatusCode::INVALID_ARGUMENT,
389+
"Unsupported provider.");
390+
}
391+
auto status = impl->negative(t_inp, t_oup, *param);
392+
if (status.is_ok()) {
393+
return nullptr;
394+
} else {
395+
return new Status(status);
396+
}
397+
}
398+
381399
Status *Argmxx(NativeTensor *inp, NativeTensor *oup, param::argmxx *param,
382400
ProviderEnum provider) {
383401
Tensor t_inp, t_oup;

apis/numnet_c_cxx_apis.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ Status *Max(NativeTensor *inp, NativeTensor *oup, param::max *param,
9090
Status *Min(NativeTensor *inp, NativeTensor *oup, param::min *param,
9191
ProviderEnum provider);
9292

93+
Status *Negative(NativeTensor *inp, NativeTensor *oup, param::negative *param,
94+
ProviderEnum provider);
95+
9396
Status *Argmxx(NativeTensor *inp, NativeTensor *oup, param::argmxx *param,
9497
ProviderEnum provider);
9598

core/op/common/negative.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "core/op/common/ops.h"
2+
3+
namespace nncore {
4+
namespace opr {
5+
6+
IMPL_SINGLE_INPUT_LAYOUT_DEDUCE(negative) {
7+
res.dtype = inp.dtype;
8+
res.ndim = inp.ndim;
9+
for (nn_size i = 0; i < inp.ndim; i++) {
10+
res[i] = inp[i];
11+
}
12+
return Status::OK();
13+
}
14+
15+
} // namespace opr
16+
} // namespace nncore

core/op/common/ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace param;
1818

1919
#define NN_FOREACH_SINGLE_INPUT_OP(cb) \
2020
cb(transpose) cb(permute) cb(repeat) cb(flip) cb(matrix_inverse) cb(rotate) \
21-
cb(pad) cb(sort) cb(onehot) cb(sum) cb(max) cb(min)
21+
cb(pad) cb(sort) cb(onehot) cb(sum) cb(max) cb(min) cb(negative)
2222

2323
#define NN_FOREACH_DOUBLE_INPUT_OP(cb) cb(matmul) cb(dot) cb(boolindex)
2424

core/op/common/param.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ struct min {
206206
void operator=(const min &) {}
207207
};
208208

209+
struct negative {};
210+
209211
struct argmxx {
210212
int axis;
211213
bool is_max;

core/op/naive/negative.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "core/op/naive/ops.h"
2+
3+
namespace nncore {
4+
namespace opr {
5+
namespace naive {
6+
7+
IMPL_NAIVE_SINGLE_INPUT_INTERNAL(flip) {
8+
nn_size n = loup.total_elems();
9+
nn_size src_idx[NN_MAX_NDIM];
10+
for (nn_size i = 0; i < n; i++) {
11+
loup.offset_to_indices(i, src_idx);
12+
ptr_oup[i] = -ptr_inp[linp.indices_to_offset(src_idx)];
13+
}
14+
return Status::OK();
15+
}
16+
17+
} // namespace naive
18+
} // namespace opr
19+
20+
} // namespace nncore

csharp/Tensor.NET/Native/NativeApi.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ internal static class NativeApi{
4646
public static extern IntPtr Max(IntPtr inp, IntPtr oup, IntPtr param, NativeProvider provider);
4747
[DllImport("libnumnet")]
4848
public static extern IntPtr Min(IntPtr inp, IntPtr oup, IntPtr param, NativeProvider provider);
49+
[DllImport("libnumnet")]
50+
public static extern IntPtr Negative(IntPtr inp, IntPtr oup, IntPtr param, NativeProvider provider);
4951

5052

5153
[DllImport("libnumnet")]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Tensornet.Math;
2+
using Tensornet.Common;
3+
using Tensornet.Native;
4+
5+
namespace Tensornet{
6+
public partial class Tensor<T>{
7+
public static Tensor<T> operator-(Tensor<T> self){
8+
Tensor<T> res = new Tensor<T>(new TensorLayout(self.TLayout, true));
9+
NegativeInternal(self, res);
10+
return res;
11+
}
12+
public static unsafe void NegativeInternal(Tensor<T> src, Tensor<T> dst){
13+
IntPtr status = NativeExecutor.Execute(NativeApi.Negative, src.TMemory, dst.TMemory, src.TLayout, dst.TLayout, IntPtr.Zero, Tensor<T>.Provider);
14+
NativeStatus.AssertOK(status);
15+
}
16+
}
17+
}

csharp/Tensor.NET/Tensor.NET.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<PackageId>Tensor.NET</PackageId>
9-
<Version>0.0.7</Version>
9+
<Version>0.0.8</Version>
1010
<Authors>AsakusaRinne</Authors>
1111
<Company>Null</Company>
1212
<Description>

0 commit comments

Comments
 (0)