Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 1 | // Copyright (c) 2023, ARM Limited. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include "verify.h" |
| 16 | |
| 17 | #include "func_debug.h" |
| 18 | #include "model_common.h" |
| 19 | #include "verifiers.h" |
| 20 | #include "verify_utils.h" |
| 21 | |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace TosaReference |
| 25 | { |
| 26 | |
| 27 | bool verify(const CTensor* ref, const CTensor* refBnd, const CTensor* imp, const VerifyConfig& cfg) |
| 28 | { |
| 29 | switch (cfg.mode) |
| 30 | { |
| 31 | case VerifyMode::DotProduct: { |
| 32 | return verifyDotProduct(ref, refBnd, imp, cfg.dotProductInfo); |
Jack Frankland | aafc850 | 2023-09-13 11:03:50 +0100 | [diff] [blame] | 33 | } |
| 34 | case VerifyMode::Exact: { |
| 35 | return verifyExact(ref, imp); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 36 | } |
Jack Frankland | 12ee1a7 | 2023-09-20 09:08:34 +0100 | [diff] [blame] | 37 | case VerifyMode::ReduceProduct: { |
| 38 | return verifyReduceProduct(ref, imp, cfg.reduceProductInfo.m, cfg.reduceProductInfo.n); |
| 39 | } |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 40 | case VerifyMode::Ulp: { |
Jeremy Johnson | a4d907e | 2023-10-26 13:53:14 +0100 | [diff] [blame] | 41 | return verifyULP(ref, imp, cfg.ulpInfo); |
Jack Frankland | 62737b1 | 2023-09-13 15:47:48 +0100 | [diff] [blame] | 42 | } |
Jeremy Johnson | 9a75838 | 2023-11-07 16:27:35 +0000 | [diff] [blame] | 43 | case VerifyMode::AbsError: { |
| 44 | return verifyAbsError(ref, refBnd, imp); |
| 45 | } |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 46 | default: { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 47 | WARNING("[Verifier] Unsupported verification mode."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 48 | break; |
| 49 | } |
| 50 | } |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | } // namespace TosaReference |
| 55 | |
| 56 | extern "C" |
| 57 | { |
| 58 | bool tvf_verify_data(const tosa_tensor_t* ref, |
| 59 | const tosa_tensor_t* ref_bnd, |
| 60 | const tosa_tensor_t* imp, |
| 61 | const char* config_json) |
| 62 | { |
| 63 | // Check inputs for nullptr |
| 64 | if (!ref || !imp || !config_json) |
| 65 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 66 | WARNING("[Verifier] One of the inputs is missing."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // Extract verification config |
| 71 | if (!ref->name) |
| 72 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 73 | WARNING("[Verifier] Tensor name is not specified."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | auto cfg = TosaReference::parseVerifyConfig(ref->name, config_json); |
| 77 | if (!cfg) |
| 78 | { |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // Validate shape |
| 83 | if (ref->num_dims != imp->num_dims) |
| 84 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 85 | WARNING("[Verifier] Tensors have different number of dimensions."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 86 | return false; |
| 87 | } |
| 88 | if (!ref->shape || !imp->shape) |
| 89 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 90 | WARNING("[Verifier] One of tensors' shape is missing."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | if (std::vector(ref->shape, ref->shape + ref->num_dims) != std::vector(imp->shape, imp->shape + imp->num_dims)) |
| 94 | { |
Jeremy Johnson | fc5e34e | 2023-10-24 14:45:12 +0100 | [diff] [blame] | 95 | WARNING("[Verifier] Tensors have different shapes."); |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 96 | return false; |
| 97 | } |
| 98 | |
Jeremy Johnson | bb0935f | 2023-09-14 16:43:48 +0100 | [diff] [blame] | 99 | // Validate data-type |
| 100 | if (ref->data_type == tosa_datatype_fp64_t) |
| 101 | { |
| 102 | if (cfg->dataType != TosaReference::mapToDType(imp->data_type)) |
| 103 | { |
Jeremy Johnson | 6ce3502 | 2023-11-21 11:22:22 +0000 | [diff] [blame^] | 104 | WARNING("[Verifier] Incorrect implementation tensor data type."); |
Jeremy Johnson | bb0935f | 2023-09-14 16:43:48 +0100 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | else |
| 109 | { |
Jeremy Johnson | 6ce3502 | 2023-11-21 11:22:22 +0000 | [diff] [blame^] | 110 | WARNING("[Verifier] Reference tensor data type is not FP64, please use ref-model --precise_mode."); |
| 111 | return false; |
Jeremy Johnson | bb0935f | 2023-09-14 16:43:48 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Georgios Pinitas | 7021ef0 | 2023-08-22 08:25:57 +0100 | [diff] [blame] | 114 | // Run verification |
| 115 | return verify(ref, ref_bnd, imp, *cfg); |
| 116 | } |
| 117 | } // extern "C" |