Skip to content

Commit 54dbf0a

Browse files
authored
Split delinearize_index out of broadcast_util (#9056)
Left an #include behind for backward compatibility. Just needed so that broadcast_indexes_range.h can use delinearize_index
1 parent 94dca7a commit 54dbf0a

File tree

5 files changed

+82
-53
lines changed

5 files changed

+82
-53
lines changed

kernels/portable/cpu/util/broadcast_util.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -269,28 +269,6 @@ ET_NODISCARD Error get_broadcast_target_size(
269269
a.sizes(), b.sizes(), out_sizes, out_sizes_len, out_dim);
270270
}
271271

272-
void delinearize_index(
273-
size_t linear_index,
274-
executorch::aten::ArrayRef<Tensor::SizesType> shape,
275-
size_t* out_indexes,
276-
const size_t out_indexes_len) {
277-
ET_CHECK(shape.size() <= out_indexes_len);
278-
for (size_t i = 0; i < shape.size(); ++i) {
279-
auto dim = shape.size() - 1 - i;
280-
auto dim_size = shape[dim];
281-
out_indexes[dim] = linear_index % dim_size;
282-
linear_index /= dim_size;
283-
}
284-
}
285-
286-
void delinearize_index(
287-
size_t linear_index,
288-
const Tensor& t,
289-
size_t* out_indexes,
290-
const size_t out_indexes_len) {
291-
delinearize_index(linear_index, t.sizes(), out_indexes, out_indexes_len);
292-
}
293-
294272
size_t linearize_access_indexes(
295273
ArrayRef<size_t> indexes_broadcast_to,
296274
ssize_t broadcast_to_ndim,

kernels/portable/cpu/util/broadcast_util.h

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <c10/util/irange.h>
1212
#include <executorch/kernels/portable/cpu/util/broadcast_indexes_range.h>
13+
#include <executorch/kernels/portable/cpu/util/delinearize_index.h>
1314
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1415
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
1516

@@ -207,36 +208,6 @@ ET_NODISCARD inline Error resize_to_broadcast_target_size(
207208
ET_DEPRECATED void free_broadcast_tensor(
208209
const executorch::aten::Tensor& broadcast_tensor);
209210

210-
/**
211-
* Delinearize a flattened index to per-dimension indexes.
212-
*
213-
* @param[in] linear_index The flattened index
214-
* @param[in] shape The tensor shape
215-
* @param[out] out_indexes The per-dimension indexes
216-
* @param[in] out_indexes_len The maximum size of the out_indexes array
217-
* @returns void
218-
*/
219-
void delinearize_index(
220-
size_t linear_index,
221-
executorch::aten::ArrayRef<Tensor::SizesType> shape,
222-
size_t* out_indexes,
223-
const size_t out_indexes_len);
224-
225-
/**
226-
* Delinearize a flattened index to per-dimension indexes.
227-
*
228-
* @param[in] linear_index The flattened index
229-
* @param[in] t The tensor object
230-
* @param[out] out_indexes The per-dimension indexes
231-
* @param[in] out_indexes_len The maximum size of the out_indexes array
232-
* @returns void
233-
*/
234-
void delinearize_index(
235-
size_t linear_index,
236-
const Tensor& t,
237-
size_t* out_indexes,
238-
const size_t out_indexes_len);
239-
240211
/**
241212
* Return the linear index for broatcast_from tensor, given the indexes and
242213
* number of dimensions of broadcast_to tensor, and the shape and strides
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
#include <executorch/kernels/portable/cpu/util/delinearize_index.h>
9+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
10+
11+
namespace torch::executor {
12+
void delinearize_index(
13+
size_t linear_index,
14+
executorch::aten::ArrayRef<Tensor::SizesType> shape,
15+
size_t* out_indexes,
16+
const size_t out_indexes_len) {
17+
ET_CHECK(shape.size() <= out_indexes_len);
18+
for (size_t i = 0; i < shape.size(); ++i) {
19+
auto dim = shape.size() - 1 - i;
20+
auto dim_size = shape[dim];
21+
out_indexes[dim] = linear_index % dim_size;
22+
linear_index /= dim_size;
23+
}
24+
}
25+
26+
void delinearize_index(
27+
size_t linear_index,
28+
const Tensor& t,
29+
size_t* out_indexes,
30+
const size_t out_indexes_len) {
31+
delinearize_index(linear_index, t.sizes(), out_indexes, out_indexes_len);
32+
}
33+
} // namespace torch::executor
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
#pragma once
10+
11+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
12+
13+
namespace torch::executor {
14+
/**
15+
* Delinearize a flattened index to per-dimension indexes.
16+
*
17+
* @param[in] linear_index The flattened index
18+
* @param[in] shape The tensor shape
19+
* @param[out] out_indexes The per-dimension indexes
20+
* @param[in] out_indexes_len The maximum size of the out_indexes array
21+
* @returns void
22+
*/
23+
void delinearize_index(
24+
size_t linear_index,
25+
executorch::aten::ArrayRef<Tensor::SizesType> shape,
26+
size_t* out_indexes,
27+
const size_t out_indexes_len);
28+
29+
/**
30+
* Delinearize a flattened index to per-dimension indexes.
31+
*
32+
* @param[in] linear_index The flattened index
33+
* @param[in] t The tensor object
34+
* @param[out] out_indexes The per-dimension indexes
35+
* @param[in] out_indexes_len The maximum size of the out_indexes array
36+
* @returns void
37+
*/
38+
void delinearize_index(
39+
size_t linear_index,
40+
const Tensor& t,
41+
size_t* out_indexes,
42+
const size_t out_indexes_len);
43+
} // namespace torch::executor

kernels/portable/cpu/util/targets.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ def define_common_targets():
6666

6767
runtime.cxx_library(
6868
name = "broadcast_util",
69-
srcs = ["broadcast_util.cpp"],
69+
srcs = [
70+
"broadcast_util.cpp",
71+
"delinearize_index.cpp",
72+
],
7073
exported_headers = [
7174
"broadcast_util.h",
75+
"delinearize_index.h",
7276
],
7377
exported_deps = [
7478
":broadcast_indexes_range",

0 commit comments

Comments
 (0)