-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathop_scatter.cpp
More file actions
171 lines (137 loc) · 4.15 KB
/
op_scatter.cpp
File metadata and controls
171 lines (137 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cinttypes>
#include <cstdint>
#include <cstring>
#include <executorch/kernels/portable/cpu/scalar_utils.h>
#include <executorch/kernels/portable/cpu/util/index_util.h>
#include <executorch/runtime/kernel/kernel_includes.h>
namespace torch {
namespace executor {
namespace native {
using Tensor = executorch::aten::Tensor;
using ScalarType = executorch::aten::ScalarType;
namespace {
template <typename CTYPE>
void scatter_src_helper(
const Tensor& in,
int64_t dim,
const Tensor& index,
const Tensor& src,
Tensor& out) {
const CTYPE* in_data = in.const_data_ptr<CTYPE>();
const long* index_data = index.const_data_ptr<long>();
const CTYPE* src_data = src.const_data_ptr<CTYPE>();
CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
memcpy(out_data, in_data, in.nbytes());
if (dim < 0) {
dim += nonzero_dim(in);
}
for (size_t ix = 0; ix < index.numel(); ++ix) {
// @lint-ignore CLANGTIDY facebook-hte-CArray
size_t ix_coord[kTensorDimensionLimit];
indexToCoordinate(index, ix, ix_coord);
size_t src_ix = coordinateToIndex(src, ix_coord);
// @lint-ignore CLANGTIDY facebook-hte-CArray
size_t out_coord[kTensorDimensionLimit];
for (size_t i = 0; i < out.dim(); ++i) {
if (i == dim) {
out_coord[i] = index_data[ix];
} else {
out_coord[i] = ix_coord[i];
}
}
size_t out_ix = coordinateToIndex(out, out_coord);
out_data[out_ix] = src_data[src_ix];
}
}
template <typename CTYPE, typename CTYPE_VAL>
void scatter_value_helper(
const Tensor& in,
int64_t dim,
const Tensor& index,
CTYPE_VAL val,
Tensor& out) {
const CTYPE* in_data = in.const_data_ptr<CTYPE>();
const long* index_data = index.const_data_ptr<long>();
CTYPE* out_data = out.mutable_data_ptr<CTYPE>();
memcpy(out_data, in_data, in.nbytes());
if (dim < 0) {
dim += nonzero_dim(in);
}
for (size_t ix = 0; ix < index.numel(); ++ix) {
// @lint-ignore CLANGTIDY facebook-hte-CArray
size_t ix_coord[kTensorDimensionLimit];
indexToCoordinate(index, ix, ix_coord);
// @lint-ignore CLANGTIDY facebook-hte-CArray
size_t out_coord[kTensorDimensionLimit];
for (size_t i = 0; i < out.dim(); ++i) {
if (i == dim) {
out_coord[i] = index_data[ix];
} else {
out_coord[i] = ix_coord[i];
}
}
size_t out_ix = coordinateToIndex(out, out_coord);
out_data[out_ix] = static_cast<CTYPE>(val);
}
}
} // namespace
Tensor& scatter_src_out(
KernelRuntimeContext& context,
const Tensor& in,
int64_t dim,
const Tensor& index,
const Tensor& src,
Tensor& out) {
(void)context;
ET_KERNEL_CHECK(
context,
check_scatter_src_args(in, dim, index, src, out),
InvalidArgument,
out);
ET_KERNEL_CHECK(
context,
resize_tensor(out, in.sizes()) == Error::Ok,
InvalidArgument,
out);
constexpr auto name = "scatter.src_out";
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, name, CTYPE, [&]() {
scatter_src_helper<CTYPE>(in, dim, index, src, out);
});
return out;
}
Tensor& scatter_value_out(
KernelRuntimeContext& ctx,
const Tensor& in,
int64_t dim,
const Tensor& index,
const Scalar& value,
Tensor& out) {
(void)ctx;
ET_KERNEL_CHECK(
ctx,
check_scatter_value_args(in, dim, index, value, out),
InvalidArgument,
out);
ET_KERNEL_CHECK(
ctx, resize_tensor(out, in.sizes()) == Error::Ok, InvalidArgument, out);
ScalarType val_type = utils::get_scalar_dtype(value);
constexpr auto name = "scatter.value_out";
ET_SWITCH_SCALAR_OBJ_TYPES(val_type, ctx, name, CTYPE_VAL, [&] {
CTYPE_VAL val;
ET_KERNEL_CHECK(ctx, utils::extract_scalar(value, &val), InvalidArgument, );
ET_SWITCH_REALHBBF16_TYPES(in.scalar_type(), ctx, name, CTYPE, [&]() {
scatter_value_helper<CTYPE>(in, dim, index, val, out);
});
});
return out;
}
} // namespace native
} // namespace executor
} // namespace torch