-
Notifications
You must be signed in to change notification settings - Fork 751
TensorLayout updates #7870
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TensorLayout updates #7870
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * 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 <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||
| #include <executorch/runtime/core/span.h> | ||
| #include <executorch/runtime/core/tensor_layout.h> | ||
|
|
||
| namespace executorch { | ||
| namespace runtime { | ||
|
|
||
| namespace { | ||
| Result<size_t> calculate_nbytes( | ||
| const Span<const int32_t>& sizes, | ||
| const exec_aten::ScalarType& scalar_type) { | ||
| ssize_t n = 1; | ||
| for (ssize_t i = 0; i < sizes.size(); i++) { | ||
| if (sizes[i] < 0) { | ||
| return Error::InvalidArgument; | ||
| } | ||
| n *= sizes[i]; | ||
| } | ||
| // Use the full namespace to disambiguate from c10::elementSize. | ||
| return n * executorch::runtime::elementSize(scalar_type); | ||
| } | ||
| } // namespace | ||
|
|
||
| Result<TensorLayout> TensorLayout::create( | ||
| Span<const int32_t> sizes, | ||
| Span<const uint8_t> dim_order, | ||
| executorch::aten::ScalarType scalar_type) { | ||
| auto nbytes = calculate_nbytes(sizes, scalar_type); | ||
| if (!nbytes.ok()) { | ||
| return nbytes.error(); | ||
| } | ||
|
|
||
| if (dim_order.size() != sizes.size()) { | ||
| return Error::InvalidArgument; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < dim_order.size(); i++) { | ||
| if (dim_order[i] >= sizes.size()) { | ||
| return Error::InvalidArgument; | ||
| } | ||
| } | ||
| return TensorLayout(sizes, dim_order, scalar_type, nbytes.get()); | ||
| } | ||
| } // namespace runtime | ||
| } // namespace executorch | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,55 +10,48 @@ | |
|
|
||
| #include <executorch/runtime/core/exec_aten/exec_aten.h> | ||
| #include <executorch/runtime/core/exec_aten/util/scalar_type_util.h> | ||
| #include <executorch/runtime/core/result.h> | ||
| #include <executorch/runtime/core/span.h> | ||
|
|
||
| namespace executorch { | ||
| namespace runtime { | ||
|
|
||
| namespace { | ||
| size_t calculate_nbytes( | ||
| const Span<const int32_t>& sizes, | ||
| const exec_aten::ScalarType& scalar_type) { | ||
| ssize_t n = 1; | ||
| for (ssize_t i = 0; i < sizes.size(); i++) { | ||
| ET_CHECK(sizes[i] >= 0); | ||
| n *= sizes[i]; | ||
| } | ||
| // Use the full namespace to disambiguate from c10::elementSize. | ||
| return n * executorch::runtime::elementSize(scalar_type); | ||
| } | ||
| } // namespace | ||
|
|
||
| /** | ||
| * Metadata describing the layout of external tensors (tensors that are not | ||
| stored in the PTE file). | ||
| * | ||
| * The NamedDataMap used to create the TensorLayout must outlive the | ||
| TensorLayout. | ||
| * Describes the layout of a tensor. | ||
| */ | ||
| class TensorLayout { | ||
| class ET_EXPERIMENTAL TensorLayout final { | ||
| public: | ||
| TensorLayout( | ||
| executorch::aten::ScalarType scalar_type, | ||
| Span<const int32_t> sizes, | ||
| Span<const uint8_t> dim_order) | ||
| : sizes_(sizes), | ||
| dim_order_(dim_order), | ||
| scalar_type_(scalar_type), | ||
| nbytes_(calculate_nbytes(sizes_, scalar_type_)) {} | ||
| TensorLayout() = delete; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need to do this since you declare a non-default ctor. https://en.cppreference.com/w/cpp/language/default_constructor
|
||
|
|
||
| TensorLayout(const TensorLayout&) = default; | ||
| TensorLayout(TensorLayout&&) = default; | ||
| TensorLayout& operator=(const TensorLayout&) = default; | ||
| TensorLayout& operator=(TensorLayout&& other) = default; | ||
| ~TensorLayout() = default; | ||
| /** | ||
| * Creates a TensorLayout from the given parameters. | ||
| * | ||
| * @param[in] sizes The sizes of the tensor. Note: the span passed here must | ||
| * outlive the TensorLayout and all copies of it. | ||
| * @param[in] dim_order The dim order of the tensor. Note: the span passed | ||
| * here must outlive the TensorLayout and all copies of it. | ||
| * @param[in] scalar_type The scalar type of the tensor. | ||
| * @return A Result containing the TensorLayout on success, or an error. | ||
| */ | ||
| static executorch::runtime::Result<TensorLayout> create( | ||
| Span<const int32_t> sizes, | ||
| Span<const uint8_t> dim_order, | ||
| executorch::aten::ScalarType scalar_type); | ||
|
|
||
| /// Returns the sizes of the tensor. | ||
| /** | ||
| * Returns the sizes of the tensor. | ||
| * | ||
| * NOTE: The TensorLayout must outlive the spans returned here. | ||
| */ | ||
| Span<const int32_t> sizes() const { | ||
| return sizes_; | ||
| } | ||
|
|
||
| /// Returns the dim order of the tensor. | ||
| /** | ||
| * Returns the dim order of the tensor. | ||
| * | ||
| * NOTE: The TensorLayout must outlive the spans returned here. | ||
| */ | ||
| Span<const uint8_t> dim_order() const { | ||
| return dim_order_; | ||
| } | ||
|
|
@@ -74,6 +67,15 @@ class TensorLayout { | |
| } | ||
|
|
||
| private: | ||
| TensorLayout( | ||
| Span<const int32_t> sizes, | ||
| Span<const uint8_t> dim_order, | ||
| executorch::aten::ScalarType scalar_type, | ||
| size_t nbytes) | ||
| : sizes_(sizes), | ||
| dim_order_(dim_order), | ||
| scalar_type_(scalar_type), | ||
| nbytes_(nbytes) {} | ||
| /// The sizes of the tensor. | ||
| Span<const int32_t> sizes_; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.