Compute Library
 23.11
Validate.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_CPP_VALIDATE_H
25 #define ARM_COMPUTE_CPP_VALIDATE_H
26 
29 
30 namespace arm_compute
31 {
32 /** Return an error if the data type of the passed tensor info is FP16 and FP16 support is not compiled in.
33  *
34  * @param[in] function Function in which the error occurred.
35  * @param[in] file Name of the file where the error occurred.
36  * @param[in] line Line on which the error occurred.
37  * @param[in] tensor_info Tensor info to validate.
38  *
39  * @return Status
40  */
41 inline Status
42 error_on_unsupported_cpu_fp16(const char *function, const char *file, const int line, const ITensorInfo *tensor_info)
43 {
44  bool fp16_kernels_enabled = false;
45 #if defined(ARM_COMPUTE_ENABLE_FP16) && defined(ENABLE_FP16_KERNELS)
46  fp16_kernels_enabled = true;
47 #endif /* defined(ARM_COMPUTE_ENABLE_FP16) && defined(ENABLE_FP16_KERNELS) */
48 
49  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
51  (tensor_info->data_type() == DataType::F16) && (!CPUInfo::get().has_fp16() || !fp16_kernels_enabled), function,
52  file, line, "This CPU architecture does not support F16 data type, you need v8.2 or above");
53  return Status{};
54 }
55 
56 /** Return an error if the data type of the passed tensor info is BFLOAT16 and BFLOAT16 support is not compiled in.
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_info Tensor info to validate.
62  *
63  * @return Status
64  */
65 inline Status
66 error_on_unsupported_cpu_bf16(const char *function, const char *file, const int line, const ITensorInfo *tensor_info)
67 {
68  bool bf16_kernels_enabled = false;
69 #if defined(ARM_COMPUTE_ENABLE_BF16)
70  bf16_kernels_enabled = true;
71 #endif /* defined(ARM_COMPUTE_ENABLE_BF16) */
72 
73  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor_info == nullptr, function, file, line);
75  (tensor_info->data_type() == DataType::BFLOAT16) && (!CPUInfo::get().has_bf16() || !bf16_kernels_enabled),
76  function, file, line, "This CPU architecture does not support BFloat16 data type, you need v8.6 or above");
77  return Status{};
78 }
79 
80 /** Return an error if the data type of the passed tensor is FP16 and FP16 support is not compiled in.
81  *
82  * @param[in] function Function in which the error occurred.
83  * @param[in] file Name of the file where the error occurred.
84  * @param[in] line Line on which the error occurred.
85  * @param[in] tensor Tensor to validate.
86  *
87  * @return Status
88  */
89 inline Status
90 error_on_unsupported_cpu_fp16(const char *function, const char *file, const int line, const ITensor *tensor)
91 {
92  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
94  return Status{};
95 }
96 
97 /** Return an error if the data type of the passed tensor is BFLOAT16 and BFLOAT16 support is not compiled in.
98  *
99  * @param[in] function Function in which the error occurred.
100  * @param[in] file Name of the file where the error occurred.
101  * @param[in] line Line on which the error occurred.
102  * @param[in] tensor Tensor to validate.
103  *
104  * @return Status
105  */
106 inline Status
107 error_on_unsupported_cpu_bf16(const char *function, const char *file, const int line, const ITensor *tensor)
108 {
109  ARM_COMPUTE_RETURN_ERROR_ON_LOC(tensor == nullptr, function, file, line);
111  return Status{};
112 }
113 
114 #define ARM_COMPUTE_ERROR_ON_CPU_F16_UNSUPPORTED(tensor) \
115  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_unsupported_cpu_fp16(__func__, __FILE__, __LINE__, tensor))
116 
117 #define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor) \
118  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unsupported_cpu_fp16(__func__, __FILE__, __LINE__, tensor))
119 
120 #define ARM_COMPUTE_ERROR_ON_CPU_BF16_UNSUPPORTED(tensor) \
121  ARM_COMPUTE_ERROR_THROW_ON(::arm_compute::error_on_unsupported_cpu_bf16(__func__, __FILE__, __LINE__, tensor))
122 
123 #define ARM_COMPUTE_RETURN_ERROR_ON_CPU_BF16_UNSUPPORTED(tensor) \
124  ARM_COMPUTE_RETURN_ON_ERROR(::arm_compute::error_on_unsupported_cpu_bf16(__func__, __FILE__, __LINE__, tensor))
125 } // namespace arm_compute
126 #endif /* ARM_COMPUTE_CPP_VALIDATE_H */
arm_compute::DataType::BFLOAT16
@ BFLOAT16
16-bit brain floating-point number
arm_compute::CPUInfo::get
static CPUInfo & get()
Access the KernelLibrary singleton.
Definition: CPPTypes.cpp:41
arm_compute::ITensor
Interface for CPU tensor.
Definition: ITensor.h:36
arm_compute::TensorInfo::data_type
DataType data_type() const override
Data type used for each element of the tensor.
Definition: TensorInfo.h:253
ARM_COMPUTE_RETURN_ON_ERROR
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:205
arm_compute::CPUInfo::has_bf16
bool has_bf16() const
Checks if the cpu model supports bf16.
Definition: CPPTypes.cpp:64
arm_compute::error_on_unsupported_cpu_bf16
Status error_on_unsupported_cpu_bf16(const char *function, const char *file, const int line, const ITensorInfo *tensor_info)
Return an error if the data type of the passed tensor info is BFLOAT16 and BFLOAT16 support is not co...
Definition: Validate.h:66
arm_compute::CPUInfo::has_fp16
bool has_fp16() const
Checks if the cpu model supports fp16.
Definition: CPPTypes.cpp:59
arm_compute::Status
Status class.
Definition: Error.h:52
tensor
CLTensor * tensor
Pointer to the auxiliary tensor.
Definition: ClWorkloadRuntime.cpp:67
arm_compute::error_on_unsupported_cpu_fp16
Status error_on_unsupported_cpu_fp16(const char *function, const char *file, const int line, const ITensorInfo *tensor_info)
Return an error if the data type of the passed tensor info is FP16 and FP16 support is not compiled i...
Definition: Validate.h:42
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::DataType::F16
@ F16
16-bit floating-point number
arm_compute::CLTensor::info
TensorInfo * info() const override
Interface to be implemented by the child class to return the tensor's metadata.
Definition: CLTensor.cpp:41
arm_compute::ITensorInfo
Store the tensor's metadata.
Definition: ITensorInfo.h:44
Validate.h
CPPTypes.h
tensor_info
TensorInfo tensor_info
Associated tensor info.
Definition: ClWorkloadRuntime.cpp:68
ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG
#define ARM_COMPUTE_RETURN_ERROR_ON_LOC_MSG(cond, func, file, line, msg)
If the condition is true, an error is thrown.
Definition: Error.h:285
ARM_COMPUTE_RETURN_ERROR_ON_LOC
#define ARM_COMPUTE_RETURN_ERROR_ON_LOC(cond, func, file, line)
If the condition is true, an error is returned.
Definition: Error.h:307