Skip to content

Commit afab516

Browse files
lucylqYIWENX14
authored andcommitted
[executorch][core] Add TensorLayout to core (#7845)
Introduce TensorLayout class, used to describe external tensors. Currently contains: - scalar_type - sizes - dim_order Differential Revision: [D67048723](https://our.internmc.facebook.com/intern/diff/D67048723/) ghstack-source-id: 262004745 Pull Request resolved: #7761
1 parent 2b23762 commit afab516

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

runtime/core/targets.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def define_common_targets():
4343
"freeable_buffer.h",
4444
"result.h",
4545
"span.h",
46+
"tensor_layout.h",
4647
],
4748
visibility = [
4849
"//executorch/...",

runtime/core/tensor_layout.h

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
#include <executorch/runtime/core/exec_aten/util/scalar_type_util.h>
13+
#include <executorch/runtime/core/span.h>
14+
15+
namespace executorch {
16+
namespace runtime {
17+
18+
namespace {
19+
size_t calculate_nbytes(
20+
const Span<const int32_t>& sizes,
21+
const exec_aten::ScalarType& scalar_type) {
22+
ssize_t n = 1;
23+
for (ssize_t i = 0; i < sizes.size(); i++) {
24+
ET_CHECK(sizes[i] >= 0);
25+
n *= sizes[i];
26+
}
27+
// Use the full namespace to disambiguate from c10::elementSize.
28+
return n * executorch::runtime::elementSize(scalar_type);
29+
}
30+
} // namespace
31+
32+
/**
33+
* Metadata describing the layout of external tensors (tensors that are not
34+
stored in the PTE file).
35+
*
36+
* The NamedDataMap used to create the TensorLayout must outlive the
37+
TensorLayout.
38+
*/
39+
class TensorLayout {
40+
public:
41+
TensorLayout(
42+
executorch::aten::ScalarType scalar_type,
43+
Span<const int32_t> sizes,
44+
Span<const uint8_t> dim_order)
45+
: sizes_(sizes),
46+
dim_order_(dim_order),
47+
scalar_type_(scalar_type),
48+
nbytes_(calculate_nbytes(sizes_, scalar_type_)) {}
49+
50+
TensorLayout(const TensorLayout&) = default;
51+
TensorLayout(TensorLayout&&) = default;
52+
TensorLayout& operator=(const TensorLayout&) = default;
53+
TensorLayout& operator=(TensorLayout&& other) = default;
54+
~TensorLayout() = default;
55+
56+
/// Returns the sizes of the tensor.
57+
Span<const int32_t> sizes() const {
58+
return sizes_;
59+
}
60+
61+
/// Returns the dim order of the tensor.
62+
Span<const uint8_t> dim_order() const {
63+
return dim_order_;
64+
}
65+
66+
/// Returns the scalar type of the tensor.
67+
executorch::aten::ScalarType scalar_type() const {
68+
return scalar_type_;
69+
}
70+
71+
/// Returns the size of the tensor in bytes.
72+
size_t nbytes() const {
73+
return nbytes_;
74+
}
75+
76+
private:
77+
/// The sizes of the tensor.
78+
Span<const int32_t> sizes_;
79+
80+
/// The dim order of the tensor.
81+
Span<const uint8_t> dim_order_;
82+
83+
/// The scalar type of the tensor.
84+
executorch::aten::ScalarType scalar_type_;
85+
86+
/// The size in bytes of the tensor.
87+
size_t nbytes_;
88+
};
89+
90+
} // namespace runtime
91+
} // namespace executorch

runtime/core/test/targets.bzl

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def define_common_targets():
1515
],
1616
)
1717

18+
runtime.cxx_test(
19+
name = "tensor_layout_test",
20+
srcs = ["tensor_layout_test.cpp"],
21+
deps = [
22+
"//executorch/runtime/core:core",
23+
"//executorch/runtime/core/exec_aten:lib",
24+
],
25+
)
26+
1827
runtime.cxx_test(
1928
name = "error_handling_test",
2029
srcs = [
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#include <executorch/runtime/core/exec_aten/exec_aten.h>
10+
#include <executorch/runtime/core/tensor_layout.h>
11+
12+
#include <gtest/gtest.h>
13+
14+
using namespace ::testing;
15+
using executorch::aten::ScalarType;
16+
using executorch::runtime::Span;
17+
using executorch::runtime::TensorLayout;
18+
19+
TEST(TestTensorLayout, Ctor) {
20+
int32_t sizes[2] = {1, 2};
21+
uint8_t dim_order[2] = {0, 1};
22+
23+
Span<const int32_t> sizes_span = {sizes, sizes + 2};
24+
Span<const uint8_t> dim_order_span = {dim_order, dim_order + 2};
25+
26+
TensorLayout layout =
27+
TensorLayout(ScalarType::Float, sizes_span, dim_order_span);
28+
29+
EXPECT_EQ(layout.scalar_type(), ScalarType::Float);
30+
31+
EXPECT_EQ(layout.sizes().size(), sizes_span.size());
32+
EXPECT_EQ(layout.sizes()[0], sizes_span[0]);
33+
EXPECT_EQ(layout.sizes()[1], sizes_span[1]);
34+
35+
EXPECT_EQ(layout.dim_order().size(), dim_order_span.size());
36+
EXPECT_EQ(layout.dim_order()[0], dim_order_span[0]);
37+
EXPECT_EQ(layout.dim_order()[1], dim_order_span[1]);
38+
39+
EXPECT_EQ(layout.nbytes(), 8);
40+
}

0 commit comments

Comments
 (0)