Skip to content

Commit ea082fa

Browse files
committed
Move tensor_shape_to_c_string back to runtime/core
I moved it to kernels/portable due to build issues, but it doesn't make sense that kernels/portable code can't use this if it's in runtime/core, so let's dig in. ghstack-source-id: c98fe3d ghstack-comment-id: 2641470188 Pull Request resolved: #8296
1 parent 4d9cecc commit ea082fa

File tree

14 files changed

+48
-77
lines changed

14 files changed

+48
-77
lines changed

kernels/portable/cpu/util/broadcast_util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88

99
#include <executorch/kernels/portable/cpu/util/repeat_util.h>
10-
#include <executorch/kernels/portable/cpu/util/tensor_util.h>
1110
#include <executorch/runtime/core/exec_aten/exec_aten.h>
1211
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
12+
#include <executorch/runtime/core/exec_aten/util/tensor_shape_to_c_string.h>
1313
#include <string.h>
1414

1515
namespace torch {

kernels/portable/cpu/util/targets.bzl

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def define_common_targets():
7373
compiler_flags = ["-Wno-missing-prototypes"],
7474
deps = [
7575
":repeat_util",
76-
":tensor_util",
7776
"//executorch/runtime/kernel:kernel_includes",
77+
"//executorch/runtime/core/exec_aten/util:tensor_shape_to_c_string",
7878
"//executorch/runtime/core/exec_aten/util:tensor_util",
7979
],
8080
visibility = ["//executorch/kernels/portable/cpu/...", "//executorch/kernels/optimized/cpu/...", "@EXECUTORCH_CLIENTS"],
@@ -268,16 +268,6 @@ def define_common_targets():
268268
visibility = ["//executorch/kernels/portable/cpu/..."],
269269
)
270270

271-
runtime.cxx_library(
272-
name = "tensor_util",
273-
srcs = ["tensor_util.cpp"],
274-
exported_headers = ["tensor_util.h"],
275-
deps = [
276-
"//executorch/runtime/kernel:kernel_includes",
277-
],
278-
visibility = ["//executorch/kernels/portable/cpu/..."],
279-
)
280-
281271
runtime.cxx_library(
282272
name = "upsample_util",
283273
srcs = ["upsample_util.cpp"],

kernels/portable/cpu/util/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)
1919

2020
include(${EXECUTORCH_ROOT}/build/Test.cmake)
2121

22-
set(_test_srcs broadcast_test.cpp reduce_test.cpp tensor_util_test.cpp)
22+
set(_test_srcs broadcast_test.cpp reduce_test.cpp)
2323

2424
et_cxx_test(
2525
kernels_portable_cpu_util_test SOURCES ${_test_srcs} EXTRA_LIBS

kernels/portable/cpu/util/test/targets.bzl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,3 @@ def define_common_targets():
2121
"//executorch/kernels/portable/cpu/util:reduce_util",
2222
],
2323
)
24-
25-
runtime.cxx_test(
26-
name = "tensor_util_test",
27-
srcs = ["tensor_util_test.cpp"],
28-
deps = [
29-
"//executorch/kernels/portable/cpu/util:tensor_util",
30-
],
31-
)

runtime/core/exec_aten/util/dim_order_util.h

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -260,35 +260,6 @@ ET_NODISCARD inline Error stride_to_dim_order(
260260
return Error::Ok;
261261
}
262262

263-
/**
264-
* Print a string representation of an ArrayRef of tensor sizes into a
265-
* user-provided string buffer. If the user buffer is too small, the string
266-
* will be truncated. The output is of the format (1,2,3,4).
267-
*
268-
* Note that we cannot use ArrayRef here due to a circular dependency (see
269-
* above comments).
270-
*/
271-
template <class SizesType>
272-
inline void sizes_to_string(
273-
char* output,
274-
size_t output_size,
275-
SizesType* sizes,
276-
size_t rank) {
277-
auto remaining_size = output_size;
278-
for (auto i = 0; remaining_size > 0 && i < rank; i++) {
279-
snprintf(
280-
output,
281-
remaining_size,
282-
"%s%zd",
283-
i == 0 ? "(" : ",",
284-
static_cast<size_t>(sizes[i]));
285-
auto len = strlen(output);
286-
output += len;
287-
remaining_size -= len;
288-
}
289-
snprintf(output, remaining_size, ")");
290-
}
291-
292263
} // namespace runtime
293264
} // namespace executorch
294265

runtime/core/exec_aten/util/targets.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,16 @@ def define_common_targets():
7272
# specify library directory path.
7373
force_static = True,
7474
)
75+
76+
runtime.cxx_library(
77+
name = "tensor_shape_to_c_string" + aten_suffix,
78+
srcs = ["tensor_shape_to_c_string.cpp"],
79+
exported_deps = [
80+
"//executorch/runtime/core/exec_aten:lib" + aten_suffix,
81+
],
82+
exported_headers = ["tensor_shape_to_c_string.h"],
83+
visibility = [
84+
"//executorch/...",
85+
"@EXECUTORCH_CLIENTS",
86+
],
87+
)

kernels/portable/cpu/util/tensor_util.cpp renamed to runtime/core/exec_aten/util/tensor_shape_to_c_string.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#include "tensor_util.h"
2-
31
/*
42
* Copyright (c) Meta Platforms, Inc. and affiliates.
53
* All rights reserved.
@@ -8,7 +6,7 @@
86
* LICENSE file in the root directory of this source tree.
97
*/
108

11-
#include <executorch/runtime/core/exec_aten/util/tensor_util.h>
9+
#include <executorch/runtime/core/exec_aten/util/tensor_shape_to_c_string.h>
1210

1311
#include <executorch/runtime/platform/assert.h>
1412

runtime/core/exec_aten/util/test/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)
1919

2020
include(${EXECUTORCH_ROOT}/build/Test.cmake)
2121

22-
set(_test_srcs scalar_type_util_test.cpp
23-
operator_impl_example_test.cpp dim_order_util_test.cpp
22+
set(_test_srcs
23+
dim_order_util_test.cpp operator_impl_example_test.cpp
24+
scalar_type_util_test.cpp tensor_shape_to_c_string_test.cpp
25+
tensor_util_test.cpp
2426
)
2527

2628
et_cxx_test(runtime_core_exec_aten_util_test SOURCES ${_test_srcs} EXTRA_LIBS)

runtime/core/exec_aten/util/test/targets.bzl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ def define_common_targets():
4646
"//executorch/runtime/core/exec_aten/util:tensor_util" + aten_suffix,
4747
],
4848
)
49+
50+
runtime.cxx_test(
51+
name = "tensor_shape_to_c_string_test",
52+
srcs = ["tensor_shape_to_c_string_test.cpp"],
53+
deps = [
54+
"//executorch/runtime/core/exec_aten/util:tensor_shape_to_c_string",
55+
],
56+
)

kernels/portable/cpu/util/test/tensor_util_test.cpp renamed to runtime/core/exec_aten/util/test/tensor_shape_to_c_string_test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
#include <gtest/gtest.h>
99

10-
#include <executorch/kernels/portable/cpu/util/tensor_util.h>
1110
#include <executorch/runtime/core/exec_aten/exec_aten.h>
11+
#include <executorch/runtime/core/exec_aten/util/tensor_shape_to_c_string.h>
1212
#include <executorch/runtime/platform/runtime.h>
1313
#include <array>
1414

@@ -17,7 +17,7 @@ using executorch::runtime::Span;
1717
using executorch::runtime::tensor_shape_to_c_string;
1818
using executorch::runtime::internal::kMaximumPrintableTensorShapeElement;
1919

20-
TEST(TensorUtilTest, TensorShapeToCStringBasic) {
20+
TEST(TensorShapeToCStringTest, Basic) {
2121
std::array<executorch::aten::SizesType, 3> sizes = {123, 456, 789};
2222
auto str = tensor_shape_to_c_string(
2323
Span<const executorch::aten::SizesType>(sizes.data(), sizes.size()));
@@ -29,7 +29,7 @@ TEST(TensorUtilTest, TensorShapeToCStringBasic) {
2929
EXPECT_STREQ(str.data(), "(1234567890)");
3030
}
3131

32-
TEST(TensorUtilTest, TensorShapeToCStringNegativeItems) {
32+
TEST(TensorShapeToCStringTest, NegativeItems) {
3333
std::array<executorch::aten::SizesType, 4> sizes = {-1, -3, -2, 4};
3434
auto str = tensor_shape_to_c_string(
3535
Span<const executorch::aten::SizesType>(sizes.data(), sizes.size()));
@@ -44,7 +44,7 @@ TEST(TensorUtilTest, TensorShapeToCStringNegativeItems) {
4444
EXPECT_EQ(str.data(), "(" + std::to_string(one_size[0]) + ")");
4545
}
4646
}
47-
TEST(TensorUtilTest, TensorShapeToCStringMaximumElement) {
47+
TEST(TensorShapeToCStringTest, MaximumElement) {
4848
std::array<executorch::aten::SizesType, 3> sizes = {
4949
123, std::numeric_limits<executorch::aten::SizesType>::max(), 789};
5050
auto str = tensor_shape_to_c_string(
@@ -60,7 +60,7 @@ TEST(TensorUtilTest, TensorShapeToCStringMaximumElement) {
6060
EXPECT_EQ(str.data(), expected_str);
6161
}
6262

63-
TEST(TensorUtilTest, TensorShapeToCStringMaximumLength) {
63+
TEST(TensorShapeToCStringTest, MaximumLength) {
6464
std::array<executorch::aten::SizesType, kTensorDimensionLimit> sizes;
6565
std::fill(sizes.begin(), sizes.end(), kMaximumPrintableTensorShapeElement);
6666

@@ -78,7 +78,7 @@ TEST(TensorUtilTest, TensorShapeToCStringMaximumLength) {
7878
EXPECT_EQ(expected_str, str.data());
7979
}
8080

81-
TEST(TensorUtilTest, TensorShapeToCStringExceedsDimensionLimit) {
81+
TEST(TensorShapeToCStringTest, ExceedsDimensionLimit) {
8282
std::array<executorch::aten::SizesType, kTensorDimensionLimit + 1> sizes;
8383
std::fill(sizes.begin(), sizes.end(), kMaximumPrintableTensorShapeElement);
8484

runtime/core/portable_type/targets.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ def define_common_targets():
3232
":scalar_type",
3333
"//executorch/runtime/core:core",
3434
"//executorch/runtime/core:tensor_shape_dynamism",
35-
"//executorch/runtime/core/exec_aten/util:scalar_type_util",
3635
"//executorch/runtime/core/exec_aten/util:dim_order_util",
36+
"//executorch/runtime/core/exec_aten/util:scalar_type_util",
37+
"//executorch/runtime/core/exec_aten/util:tensor_shape_to_c_string",
3738
"//executorch/runtime/core:tag",
3839
],
3940
)

runtime/core/portable_type/tensor_impl.cpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <executorch/runtime/core/exec_aten/util/dim_order_util.h>
1515
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
16+
#include <executorch/runtime/core/exec_aten/util/tensor_shape_to_c_string.h>
1617
#include <executorch/runtime/core/portable_type/qint_types.h>
1718
#include <executorch/runtime/core/portable_type/scalar_type.h>
1819
#include <executorch/runtime/platform/assert.h>
@@ -95,18 +96,12 @@ Error TensorImpl::internal_resize_contiguous(ArrayRef<SizesType> new_sizes) {
9596
case TensorShapeDynamism::STATIC:
9697
if (!std::equal(sizes_, sizes_ + dim_, new_sizes.begin())) {
9798
#ifdef ET_LOG_ENABLED
98-
std::array<char, 16> old_sizes_str, new_sizes_str;
99-
100-
executorch::runtime::sizes_to_string(
101-
old_sizes_str.data(),
102-
old_sizes_str.size(),
103-
sizes().data(),
104-
sizes().size());
105-
executorch::runtime::sizes_to_string(
106-
new_sizes_str.data(),
107-
new_sizes_str.size(),
108-
new_sizes.data(),
109-
new_sizes.size());
99+
auto old_sizes_str = executorch::runtime::tensor_shape_to_c_string(
100+
executorch::runtime::Span<const executorch::aten::SizesType>(
101+
sizes().data(), sizes().size()));
102+
auto new_sizes_str = executorch::runtime::tensor_shape_to_c_string(
103+
executorch::runtime::Span<const executorch::aten::SizesType>(
104+
new_sizes.data(), new_sizes.size()));
110105
#endif
111106

112107
ET_CHECK_OR_RETURN_ERROR(

test/utils/OSSTestConfig.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,11 @@
8585
{
8686
"directory": "runtime/core/exec_aten/util/test",
8787
"sources": [
88-
"tensor_util_test.cpp",
89-
"scalar_type_util_test.cpp",
88+
"dim_order_util_test.cpp",
9089
"operator_impl_example_test.cpp",
91-
"dim_order_util_test.cpp"
90+
"scalar_type_util_test.cpp",
91+
"tensor_shape_to_c_string_test.cpp",
92+
"tensor_util_test.cpp"
9293
]
9394
},
9495
{

0 commit comments

Comments
 (0)