blob: d0b31c6ac8a5785155f79bbcce0d29b39381345c [file] [log] [blame]
Georgios Pinitas7021ef02023-08-22 08:25:57 +01001// 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
24namespace TosaReference
25{
26
27bool 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 Franklandaafc8502023-09-13 11:03:50 +010033 }
34 case VerifyMode::Exact: {
35 return verifyExact(ref, imp);
Georgios Pinitas7021ef02023-08-22 08:25:57 +010036 }
Jack Frankland12ee1a72023-09-20 09:08:34 +010037 case VerifyMode::ReduceProduct: {
38 return verifyReduceProduct(ref, imp, cfg.reduceProductInfo.m, cfg.reduceProductInfo.n);
39 }
Jack Frankland62737b12023-09-13 15:47:48 +010040 case VerifyMode::Ulp: {
Jeremy Johnsona4d907e2023-10-26 13:53:14 +010041 return verifyULP(ref, imp, cfg.ulpInfo);
Jack Frankland62737b12023-09-13 15:47:48 +010042 }
Jeremy Johnson9a758382023-11-07 16:27:35 +000043 case VerifyMode::AbsError: {
44 return verifyAbsError(ref, refBnd, imp);
45 }
Georgios Pinitas7021ef02023-08-22 08:25:57 +010046 default: {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010047 WARNING("[Verifier] Unsupported verification mode.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010048 break;
49 }
50 }
51 return false;
52}
53
54} // namespace TosaReference
55
56extern "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 Johnsonfc5e34e2023-10-24 14:45:12 +010066 WARNING("[Verifier] One of the inputs is missing.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010067 return false;
68 }
69
70 // Extract verification config
71 if (!ref->name)
72 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010073 WARNING("[Verifier] Tensor name is not specified.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010074 return false;
75 }
76 auto cfg = TosaReference::parseVerifyConfig(ref->name, config_json);
77 if (!cfg)
78 {
Georgios Pinitas7021ef02023-08-22 08:25:57 +010079 return false;
80 }
81
82 // Validate shape
83 if (ref->num_dims != imp->num_dims)
84 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010085 WARNING("[Verifier] Tensors have different number of dimensions.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010086 return false;
87 }
88 if (!ref->shape || !imp->shape)
89 {
Jeremy Johnsonfc5e34e2023-10-24 14:45:12 +010090 WARNING("[Verifier] One of tensors' shape is missing.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010091 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 Johnsonfc5e34e2023-10-24 14:45:12 +010095 WARNING("[Verifier] Tensors have different shapes.");
Georgios Pinitas7021ef02023-08-22 08:25:57 +010096 return false;
97 }
98
Jeremy Johnsonbb0935f2023-09-14 16:43:48 +010099 // Validate data-type
100 if (ref->data_type == tosa_datatype_fp64_t)
101 {
102 if (cfg->dataType != TosaReference::mapToDType(imp->data_type))
103 {
Jeremy Johnson6ce35022023-11-21 11:22:22 +0000104 WARNING("[Verifier] Incorrect implementation tensor data type.");
Jeremy Johnsonbb0935f2023-09-14 16:43:48 +0100105 return false;
106 }
107 }
108 else
109 {
Jeremy Johnson6ce35022023-11-21 11:22:22 +0000110 WARNING("[Verifier] Reference tensor data type is not FP64, please use ref-model --precise_mode.");
111 return false;
Jeremy Johnsonbb0935f2023-09-14 16:43:48 +0100112 }
113
Georgios Pinitas7021ef02023-08-22 08:25:57 +0100114 // Run verification
115 return verify(ref, ref_bnd, imp, *cfg);
116 }
117} // extern "C"