Skip to content

Commit d4207ce

Browse files
committed
fix: Invalidate certain Cpu operations if tensor sizes are large
The kernels and operators are chosen based on upstream integrations. There will be other patches that continue modifying the validate() calls within other operators and kernels. Resolves: COMPMID-8646 Change-Id: Id6a7d2933ada9f679e4325b79db443204245b7e4 Signed-off-by: Gunes Bayir <[email protected]>
1 parent 5d011ee commit d4207ce

File tree

53 files changed

+375
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+375
-45
lines changed

src/core/CPP/Validate.h

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2021 Arm Limited.
2+
* Copyright (c) 2018-2021, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -21,8 +21,8 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
#ifndef ARM_COMPUTE_CPP_VALIDATE_H
25-
#define ARM_COMPUTE_CPP_VALIDATE_H
24+
#ifndef ACL_SRC_CORE_CPP_VALIDATE_H
25+
#define ACL_SRC_CORE_CPP_VALIDATE_H
2626

2727
#include "arm_compute/core/CPP/CPPTypes.h"
2828
#include "arm_compute/core/Validate.h"
@@ -53,6 +53,37 @@ error_on_unsupported_cpu_fp16(const char *function, const char *file, const int
5353
return Status{};
5454
}
5555

56+
/** Return an error if the tensor sizes are too large.
57+
*
58+
* @param[in] function Function in which the error occurred.
59+
* @param[in] file Name of the file where the error occurred.
60+
* @param[in] line Line on which the error occurred.
61+
* @param[in] tensor_infos Tensor infos to validate.
62+
*
63+
* @return Status
64+
*/
65+
template <typename... Ts>
66+
inline Status error_on_unsupported_size(const char *function, const char *file, const int line, Ts &&...tensor_infos)
67+
{
68+
constexpr size_t max_size_in_bytes = (1U << 31) - 1;
69+
70+
const ITensorInfo *tensor_array[] = {std::forward<Ts>(tensor_infos)...};
71+
72+
for (const ITensorInfo *tensor_info : tensor_array)
73+
{
74+
if (tensor_info != nullptr && tensor_info->data_type() != DataType::UNKNOWN)
75+
{
76+
ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(
77+
(tensor_info->total_size() > max_size_in_bytes ||
78+
(tensor_info->tensor_shape().total_size() * tensor_info->element_size() * tensor_info->num_channels() >
79+
max_size_in_bytes)),
80+
function, file, line, "Maximum supported tensor size is 2^31-1 bytes");
81+
}
82+
}
83+
84+
return Status{};
85+
}
86+
5687
/** Return an error if the data type of the passed tensor info is BFLOAT16 and BFLOAT16 support is not compiled in.
5788
*
5889
* @param[in] function Function in which the error occurred.
@@ -122,5 +153,12 @@ error_on_unsupported_cpu_bf16(const char *function, const char *file, const int
122153

123154
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(tensor) \
124155
ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unsupported_cpu_bf16(__func__, __FILE__, __LINE__, tensor))
156+
157+
#define ARM_COMPUTE_ERROR_ON_SIZE_UNSUPPORTED(...) \
158+
ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_unsupported_size(__func__, __FILE__, __LINE__, __VA_ARGS__))
159+
160+
#define ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(...) \
161+
ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unsupported_size(__func__, __FILE__, __LINE__, __VA_ARGS__))
162+
125163
} // namespace arm_compute
126-
#endif /* ARM_COMPUTE_CPP_VALIDATE_H */
164+
#endif // ACL_SRC_CORE_CPP_VALIDATE_H

src/core/NEON/kernels/NEBatchNormalizationLayerKernel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2021, 2023-2024 Arm Limited.
2+
* Copyright (c) 2017-2021, 2023-2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -111,6 +111,7 @@ Status validate_arguments(const ITensorInfo *input,
111111
ActivationLayerInfo act_info)
112112
{
113113
ARM_COMPUTE_UNUSED(epsilon);
114+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input, mean, var, beta, gamma);
114115

115116
const auto *uk = get_implementation(BatchNormalizationSelectorData{input->data_type(), CPUInfo::get()});
116117
ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
@@ -127,6 +128,7 @@ Status validate_arguments(const ITensorInfo *input,
127128

128129
if (nullptr != output)
129130
{
131+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
130132
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
131133
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
132134
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);

src/core/NEON/kernels/NEFFTDigitReverseKernel.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2021 Arm Limited.
2+
* Copyright (c) 2019-2021, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -29,6 +29,7 @@
2929
#include "arm_compute/core/Validate.h"
3030
#include "arm_compute/core/Window.h"
3131

32+
#include "src/core/CPP/Validate.h"
3233
#include "src/core/helpers/AutoConfiguration.h"
3334
#include "src/core/helpers/WindowHelpers.h"
3435

@@ -48,14 +49,21 @@ Status validate_arguments(const ITensorInfo *input,
4849
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(idx, 1, DataType::U32);
4950
ARM_COMPUTE_RETURN_ERROR_ON(std::set<unsigned int>({0, 1}).count(config.axis) == 0);
5051
ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[config.axis] != idx->tensor_shape().x());
52+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input, idx);
5153

5254
// Checks performed when output is configured
5355
if ((output != nullptr) && (output->total_size() != 0))
5456
{
57+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
5558
ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 2);
5659
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
5760
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
5861
}
62+
else
63+
{
64+
const auto output_info = TensorInfo(input->tensor_shape(), 2, input->data_type());
65+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(&output_info);
66+
}
5967

6068
return Status{};
6169
}

src/core/NEON/kernels/NEFFTRadixStageKernel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2021 Arm Limited.
2+
* Copyright (c) 2019-2021, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -29,6 +29,7 @@
2929
#include "arm_compute/core/Utils.h"
3030
#include "arm_compute/core/Window.h"
3131

32+
#include "src/core/CPP/Validate.h"
3233
#include "src/core/helpers/AutoConfiguration.h"
3334
#include "src/core/helpers/WindowHelpers.h"
3435
#include "src/core/NEON/wrapper/traits.h"
@@ -1003,11 +1004,13 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, c
10031004
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
10041005
ARM_COMPUTE_RETURN_ERROR_ON(config.axis > 1);
10051006
ARM_COMPUTE_RETURN_ERROR_ON(NEFFTRadixStageKernel::supported_radix().count(config.radix) == 0);
1007+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
10061008
ARM_COMPUTE_UNUSED(config);
10071009

10081010
// Checks performed when output is configured
10091011
if ((output != nullptr) && (output->total_size() != 0))
10101012
{
1013+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
10111014
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
10121015
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
10131016
}

src/core/NEON/kernels/NEFFTScaleKernel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2021 Arm Limited.
2+
* Copyright (c) 2019-2021, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -29,6 +29,7 @@
2929
#include "arm_compute/core/Validate.h"
3030
#include "arm_compute/core/Window.h"
3131

32+
#include "src/core/CPP/Validate.h"
3233
#include "src/core/helpers/AutoConfiguration.h"
3334
#include "src/core/helpers/WindowHelpers.h"
3435
#include "src/core/NEON/wrapper/wrapper.h"
@@ -55,10 +56,12 @@ void scale_complex(float *c_in, float *c_out, bool is_conjugate, float scale)
5556
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
5657
{
5758
ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
59+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
5860

5961
// Checks performed when output is configured
6062
if ((output != nullptr) && (output->total_size() != 0))
6163
{
64+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
6265
ARM_COMPUTE_RETURN_ERROR_ON(output->num_channels() != 1 && output->num_channels() != 2);
6366
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
6467
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);

src/core/NEON/kernels/NEPadLayerKernel.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2022 Arm Limited.
2+
* Copyright (c) 2019-2022, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -31,6 +31,7 @@
3131
#include "arm_compute/core/utils/misc/ShapeCalculator.h"
3232
#include "arm_compute/core/Validate.h"
3333

34+
#include "src/core/CPP/Validate.h"
3435
#include "src/core/helpers/AutoConfiguration.h"
3536
#include "src/core/helpers/WindowHelpers.h"
3637
#include "src/core/NEON/wrapper/wrapper.h"
@@ -45,17 +46,25 @@ Status validate_arguments(const ITensorInfo *input,
4546
const PaddingMode mode)
4647
{
4748
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
49+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
4850
ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
4951
ARM_COMPUTE_RETURN_ERROR_ON_MSG(mode != PaddingMode::CONSTANT, "Only constant padding mode is supported");
5052
ARM_COMPUTE_RETURN_ERROR_ON_MSG(paddings.size() > 4, "Padding list bigger than 4 dimensions");
53+
54+
const TensorShape expected_output_shape =
55+
arm_compute::misc::shape_calculator::compute_padded_shape(input->tensor_shape(), paddings);
5156
if (output->total_size() != 0)
5257
{
53-
const TensorShape expected_output_shape =
54-
arm_compute::misc::shape_calculator::compute_padded_shape(input->tensor_shape(), paddings);
58+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
5559
const TensorInfo expected_output_info = input->clone()->set_tensor_shape(expected_output_shape);
5660
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &expected_output_info);
5761
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
5862
}
63+
else
64+
{
65+
const auto output_info = TensorInfo(expected_output_shape, input->num_channels(), input->data_type());
66+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(&output_info);
67+
}
5968
return Status{};
6069
}
6170
} // namespace

src/core/NEON/kernels/NEReductionOperationKernel.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017-2024 Arm Limited.
2+
* Copyright (c) 2017-2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -255,6 +255,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, u
255255

256256
ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
257257
ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
258+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
258259

259260
if (input->num_channels() == 1)
260261
{
@@ -274,6 +275,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, u
274275

275276
if (output->total_size() != 0)
276277
{
278+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
277279
bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN);
278280
if (!is_arg_min_max)
279281
{

src/core/NEON/kernels/NEReorderKernel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "arm_compute/runtime/Scheduler.h"
3131

3232
#include "src/common/utils/Log.h"
33+
#include "src/core/CPP/Validate.h"
3334
#include "src/core/NEON/kernels/arm_gemm/transform.hpp"
3435

3536
#include <map>
@@ -250,6 +251,7 @@ Status NEReorderKernel::validate(const ITensorInfo *input,
250251
ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() != DataType::F32);
251252
ARM_COMPUTE_RETURN_ERROR_ON(output->data_type() != DataType::F32 && output->data_type() != DataType::BFLOAT16);
252253
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
254+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input, output);
253255

254256
int input_x_dim;
255257
int input_k_dim;

src/core/NEON/kernels/NEReverseKernel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2021, 2023-2024 Arm Limited.
2+
* Copyright (c) 2018-2021, 2023-2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -27,6 +27,7 @@
2727
#include "arm_compute/core/Validate.h"
2828
#include "arm_compute/core/Window.h"
2929

30+
#include "src/core/CPP/Validate.h"
3031
#include "src/core/helpers/AutoConfiguration.h"
3132
#include "src/core/helpers/WindowHelpers.h"
3233
#include "src/core/NEON/wrapper/wrapper.h"
@@ -52,10 +53,12 @@ validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const IT
5253
ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() > 4,
5354
"Current implementation only supports up to 4 dimensions.");
5455
ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis->dimension(0) > 4, "Only up to 4 dimensions can be reversed");
56+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
5557

5658
// Checks performed when output is configured
5759
if (output->total_size() != 0)
5860
{
61+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
5962
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
6063
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
6164
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);

src/core/NEON/kernels/NEStridedSliceKernel.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018-2021, 2023 Arm Limited.
2+
* Copyright (c) 2018-2021, 2023, 2025 Arm Limited.
33
*
44
* SPDX-License-Identifier: MIT
55
*
@@ -57,6 +57,7 @@ Status validate_arguments(const ITensorInfo *input,
5757
ARM_COMPUTE_RETURN_ERROR_ON(strides.num_dimensions() > input->num_dimensions());
5858
ARM_COMPUTE_RETURN_ERROR_ON(
5959
std::any_of(strides.cbegin(), strides.cbegin() + strides.num_dimensions(), [](int i) { return i == 0; }));
60+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(input);
6061

6162
// Get expected output shape
6263
const TensorShape exp_output_shape = arm_compute::misc::shape_calculator::compute_strided_slice_shape(
@@ -66,10 +67,16 @@ Status validate_arguments(const ITensorInfo *input,
6667
// Checks output if configured
6768
if (output->total_size() != 0)
6869
{
70+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(output);
6971
const TensorInfo exp_output_info = output->clone()->set_tensor_shape(exp_output_shape);
7072
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &exp_output_info);
7173
ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
7274
}
75+
else
76+
{
77+
const auto output_info = TensorInfo(exp_output_shape, input->num_channels(), input->data_type());
78+
ARM_COMPUTE_RETURN_ERROR_ON_SIZE_UNSUPPORTED(&output_info);
79+
}
7380

7481
return Status{};
7582
}

0 commit comments

Comments
 (0)