-
Notifications
You must be signed in to change notification settings - Fork 816
fix: Do not mutate external TensorInfo in import_memory() #1270
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
base: main
Are you sure you want to change the base?
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 |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2016-2021 Arm Limited. | ||
| * Copyright (c) 2016-2021, 2026 Arm Limited. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
|
|
@@ -35,12 +35,16 @@ void ITensorAllocator::init(const TensorInfo &input, size_t alignment) | |
| _info_owned = input; | ||
| _info_external = nullptr; | ||
| _alignment = alignment; | ||
| _owns_info = true; | ||
| _is_imported = false; | ||
| } | ||
|
|
||
| void ITensorAllocator::soft_init(TensorInfo &input, size_t alignment) | ||
| { | ||
| _info_external = &input; | ||
| _alignment = alignment; | ||
| _owns_info = false; | ||
| _is_imported = false; | ||
| } | ||
|
|
||
| TensorInfo &ITensorAllocator::info() | ||
|
|
@@ -57,3 +61,36 @@ size_t ITensorAllocator::alignment() const | |
| { | ||
| return _alignment; | ||
| } | ||
|
|
||
| bool ITensorAllocator::owns_info() const | ||
| { | ||
| return _owns_info; | ||
| } | ||
|
|
||
| bool ITensorAllocator::is_imported() const | ||
| { | ||
| return _is_imported; | ||
| } | ||
|
|
||
| void ITensorAllocator::set_imported(bool imported) | ||
| { | ||
| _is_imported = imported; | ||
| } | ||
|
|
||
| bool ITensorAllocator::allocator_considers_resizable() const | ||
| { | ||
| if (_owns_info) | ||
| { | ||
| const TensorInfo *info_ptr = (_info_external != nullptr) ? _info_external : &_info_owned; | ||
| return (info_ptr != nullptr) ? info_ptr->is_resizable() : true; | ||
| } | ||
| return !_is_imported; | ||
| } | ||
|
|
||
| void ITensorAllocator::set_resizable_if_info_owned(bool is_resizable) | ||
|
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. Maybe we should return a bool from this to convey if we were able to change the status or not. It's not too critical, especially if we move it to protected fields, but could be useful. What do you think? |
||
| { | ||
| if (_owns_info) | ||
| { | ||
| info().set_is_resizable(is_resizable); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright (c) 2026 Arm Limited. | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to | ||
| * deal in the Software without restriction, including without limitation the | ||
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||
| * sell copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in all | ||
| * copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
| #include "arm_compute/core/TensorInfo.h" | ||
| #include "arm_compute/core/TensorShape.h" | ||
| #include "arm_compute/core/Types.h" | ||
|
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. Which types are needed from this? Can they be included via their specific headers? |
||
| #include "arm_compute/runtime/Tensor.h" | ||
|
|
||
| #include "tests/framework/Asserts.h" | ||
| #include "tests/framework/Macros.h" | ||
| #include "tests/validation/Validation.h" | ||
|
|
||
| namespace arm_compute | ||
| { | ||
| namespace test | ||
| { | ||
| namespace validation | ||
| { | ||
| TEST_SUITE(UNIT) | ||
| TEST_SUITE(TensorInfo) | ||
|
|
||
| TEST_CASE(ImportMemoryDoesNotMutateExternalInfo, framework::DatasetMode::ALL) | ||
| { | ||
| // Use F16 if available; otherwise fall back to F32. | ||
| DataType test_dt = DataType::F16; | ||
| #if !defined(ARM_COMPUTE_ENABLE_FP16) | ||
| test_dt = DataType::F32; | ||
| #endif | ||
|
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. Why this complexity? It doesn't depend on dtype. |
||
|
|
||
| TensorInfo out_info(TensorShape(16U, 4U, 4U), 1, test_dt, DataLayout::NHWC); | ||
| out_info.set_is_resizable(true); | ||
|
|
||
| Tensor out_tensor; | ||
| out_tensor.allocator()->init(out_info); | ||
| out_tensor.allocator()->allocate(); | ||
|
|
||
| // Simulate a shared TensorInfo used as a view wrapper. | ||
| TensorInfo shared_info(out_info); | ||
| shared_info.set_is_resizable(true); | ||
|
|
||
| Tensor view_tensor; | ||
| view_tensor.allocator()->soft_init(shared_info); | ||
|
|
||
| // Ensure it's still resizable before import. | ||
| ARM_COMPUTE_EXPECT(shared_info.is_resizable(), framework::LogLevel::ERRORS); | ||
|
|
||
| // Import memory into the view tensor. | ||
| ARM_COMPUTE_ASSERT(bool(view_tensor.allocator()->import_memory(out_tensor.buffer()))); | ||
|
|
||
| // Regression assert: import_memory must NOT mutate the caller-owned shared_info. | ||
| ARM_COMPUTE_EXPECT(shared_info.is_resizable(), framework::LogLevel::ERRORS); | ||
|
|
||
| // extend_padding should succeed (not throw). | ||
| ARM_COMPUTE_EXPECT_NO_THROW(shared_info.extend_padding(PaddingSize(1, 1, 1, 1)), framework::LogLevel::ERRORS); | ||
| } | ||
|
|
||
| TEST_SUITE_END() // TensorInfo | ||
| TEST_SUITE_END() // UNIT | ||
| } // namespace validation | ||
| } // namespace test | ||
| } // namespace arm_compute | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we don't need any of these to be in public interface of TensorAllocator classes. They can be
protected.