diff options
Diffstat (limited to 'reference_model/include')
-rw-r--r-- | reference_model/include/debug_modes.def | 20 | ||||
-rw-r--r-- | reference_model/include/debug_types.h | 57 | ||||
-rw-r--r-- | reference_model/include/func_config.h | 41 | ||||
-rw-r--r-- | reference_model/include/func_debug.h | 245 | ||||
-rw-r--r-- | reference_model/include/graph_status.h | 25 | ||||
-rw-r--r-- | reference_model/include/model_common.h | 28 | ||||
-rw-r--r-- | reference_model/include/model_runner.h | 87 | ||||
-rw-r--r-- | reference_model/include/version.h | 23 |
8 files changed, 526 insertions, 0 deletions
diff --git a/reference_model/include/debug_modes.def b/reference_model/include/debug_modes.def new file mode 100644 index 0000000..51b151d --- /dev/null +++ b/reference_model/include/debug_modes.def @@ -0,0 +1,20 @@ + +// Copyright (c) 2020, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Defines the debugging printing modes + +DEBUG_MODE(CONFIG,0) // Configuration parsing/initialization +DEBUG_MODE(GT,1) // Graph traverser +DEBUG_MODE(OP,2) // Operation diff --git a/reference_model/include/debug_types.h b/reference_model/include/debug_types.h new file mode 100644 index 0000000..bd93f19 --- /dev/null +++ b/reference_model/include/debug_types.h @@ -0,0 +1,57 @@ + +// Copyright (c) 2020, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/* + * Filename: src/debug_types.h + * Description: + * Defines fundamental debugger datatypes for the functional model + */ + +#ifndef DEBUG_TYPES_H_ +#define DEBUG_TYPES_H_ + +#ifdef __cplusplus +extern "C" +{ +#endif + + // Debug verbosity mask + typedef enum func_debug_verbosity_e + { + DEBUG_VERB_NONE = 0x00, + DEBUG_VERB_INFO = 0x01, // Informational debugging messages + DEBUG_VERB_IFACE = 0x02, // Interface debugging support + DEBUG_VERB_LOW = 0x04, // Low, medium, and high levels of debug printout + DEBUG_VERB_MED = 0x08, + DEBUG_VERB_HIGH = 0x10 + } func_debug_verbosity_e; + + // Generated debug modes enumeration + typedef enum func_debug_mode_e + { + DEBUG_NONE = 0x0, +#define DEBUG_MODE(NAME, BIT) DEBUG_##NAME = (1UL << BIT), +#include "debug_modes.def" +#undef DEBUG_MODE + DEBUG_ALL = 0xffffffffffffffffUL + } func_debug_mode_e; + +#define DEBUG_INST_ALL 0xffffffffffffffffUL + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/reference_model/include/func_config.h b/reference_model/include/func_config.h new file mode 100644 index 0000000..41df135 --- /dev/null +++ b/reference_model/include/func_config.h @@ -0,0 +1,41 @@ + +// Copyright (c) 2020-2022, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FUNC_CONFIG_H_ +#define FUNC_CONFIG_H_ + +#include <iostream> +#include <stdio.h> + +struct func_config_t +{ + std::string operator_fbs = "tosa.fbs"; + std::string test_desc = "desc.json"; + std::string flatbuffer_dir = ""; + std::string output_dir = ""; + std::string tosa_file = ""; + std::string ifm_name = ""; + std::string ifm_file = ""; + std::string ofm_name = ""; + std::string ofm_file = ""; + uint32_t eval = 1; + uint32_t validate_only = 0; + uint32_t output_tensors = 1; + uint32_t tosa_profile = 1; + uint32_t dump_intermediates = 0; + std::string fp_format = "0.5"; +}; + +#endif diff --git a/reference_model/include/func_debug.h b/reference_model/include/func_debug.h new file mode 100644 index 0000000..d762026 --- /dev/null +++ b/reference_model/include/func_debug.h @@ -0,0 +1,245 @@ + +// Copyright (c) 2020, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef FUNC_DEBUG_H +#define FUNC_DEBUG_H + +#include "debug_types.h" +#include <assert.h> +#include <cinttypes> +#include <signal.h> +#include <stdio.h> +#include <string> +#include <vector> + +void func_print_backtrace(FILE* out, int sig = SIGABRT); + +void func_enable_signal_handlers(); + +// STRINGIFY2 is needed expand expression passed to STRINGIFY +#define STRINGIFY2(s) #s +#define STRINGIFY(s) STRINGIFY2(s) + +// If TRACED_LOG is defined, add file:line to log messages +#if defined(TRACED_LOG) +#define WHERE "@" __FILE__ ":" STRINGIFY(__LINE__) +#else +#define WHERE +#endif + +#if defined(COLORIZED_LOG) +#define COL(col, fmt) "\x1b[3" col "m" fmt "\x1b[0m" +#define COL_FATAL(fmt) COL("1;41", fmt) +#define COL_WARN(fmt) COL("1;43", fmt) +#define COL_INFO(fmt) COL("2", fmt) +#define COL_IFACE(fmt) fmt +#define COL_LOW(fmt) COL("35", fmt) +#define COL_MED(fmt) COL("2;33", fmt) +#define COL_HIGH(fmt) COL("2;32", fmt) +#else +#define COL_FATAL(fmt) fmt +#define COL_WARN(fmt) fmt +#define COL_INFO(fmt) fmt +#define COL_IFACE(fmt) fmt +#define COL_LOW(fmt) fmt +#define COL_MED(fmt) fmt +#define COL_HIGH(fmt) fmt +#endif + +struct func_debug_t +{ + uint32_t func_debug_verbosity = 0; // What verbosity level is set? (bitmask) + uint64_t func_debug_mask = 0; // Which units have debugging enabled? (bitmask) + uint64_t func_debug_inst_mask = 0; // Which instances have debugging enabled (bitmask) + uint64_t inst_id = 0; // The instance id for multiple model instances + FILE* func_debug_file = stderr; // Output file + bool is_output_unbuffered = false; // should log files be opened with unbuffered I/O. + + int init_debug(uint64_t inst_id); + int fini_debug(); + int set_file(const std::string& filename); + void set_mask(const std::string& str); + void set_mask(const uint64_t mask); + void print_masks(FILE* out); + void set_verbosity(const std::string& str); + void set_verbosity(const uint32_t verb); + void set_inst_mask(const char* mask); + void set_inst_mask(const uint64_t mask); + void set_output_unbuffered(const bool is_unbuffered); + std::string get_debug_mask_help_string(); + std::string get_debug_verbosity_help_string(); +}; + +#ifndef ASSERT +#define ASSERT(COND) \ + if (!(COND)) \ + { \ + fprintf(stderr, COL_FATAL("ASSERTION AT %s:%d %s(): (%s)\n"), __FILE__, __LINE__, __func__, #COND); \ + func_print_backtrace(stderr); \ + assert(COND); \ + } +#endif + +#ifndef ASSERT_MSG +#define ASSERT_MSG(COND, fmt, ...) \ + if (!(COND)) \ + { \ + fprintf(stderr, COL_FATAL("ASSERTION AT %s:%d %s(): (%s)\n"), __FILE__, __LINE__, __func__, #COND); \ + fprintf(stderr, COL_FATAL(fmt) "\n", ##__VA_ARGS__); \ + func_print_backtrace(stderr); \ + assert(COND); \ + } +#endif + +#ifndef REQUIRE +#define REQUIRE(COND, fmt, ...) \ + if (!(COND)) \ + { \ + fprintf(g_func_debug.func_debug_file, COL_FATAL("REQUIRE() fails AT %s:%d %s(): (%s)\n"), __FILE__, __LINE__, \ + __func__, #COND); \ + fprintf(g_func_debug.func_debug_file, COL_FATAL(fmt) "\n", ##__VA_ARGS__); \ + this->parent_sgt->setGraphStatus(GraphStatus::TOSA_UNPREDICTABLE); \ + } +#endif + +#ifndef ERROR_IF +#define ERROR_IF(COND, fmt, ...) \ + if ((COND)) \ + { \ + if (this->parent_sgt->getGraphStatus() != GraphStatus::TOSA_UNPREDICTABLE) \ + { \ + this->parent_sgt->setGraphStatus(GraphStatus::TOSA_ERROR); \ + } \ + fprintf(g_func_debug.func_debug_file, COL_FATAL("ERROR_IF() fails AT %s:%d %s(): (%s)\n"), __FILE__, __LINE__, \ + __func__, #COND); \ + fprintf(g_func_debug.func_debug_file, COL_FATAL(fmt) "\n", ##__VA_ARGS__); \ + this->dumpNode(g_func_debug.func_debug_file); \ + func_print_backtrace(g_func_debug.func_debug_file); \ + return 1; \ + } +#endif + +// Assertion specific to allocating memory +#ifndef ASSERT_MEM +#define ASSERT_MEM(OBJ) \ + if (!(OBJ)) \ + { \ + fprintf(stderr, COL_FATAL("ASSERTION AT %s:%d %s(): (" #OBJ "): out of memory\n"), __FILE__, __LINE__, \ + __func__); \ + func_print_backtrace(stderr); \ + assert(OBJ); \ + } +#endif + +#ifndef FATAL_ERROR +#define FATAL_ERROR(fmt, ...) \ + fprintf(stderr, COL_FATAL("FATAL ERROR AT %s:%d %s():\n"), __FILE__, __LINE__, __func__); \ + fprintf(stderr, COL_FATAL(fmt) "\n", ##__VA_ARGS__); \ + func_print_backtrace(stderr); \ + abort(); +#endif + +void func_debug_warning( + func_debug_t* func_debug, const char* file, const char* func, const int line, const char* fmt, ...); +#ifndef WARNING +#define WARNING(...) func_debug_warning(&g_func_debug, __FILE__, __func__, __LINE__, __VA_ARGS__) +#endif + +#ifndef WARNING_STDERR +#define WARNING_STDERR(fmt, ...) \ + fprintf(stderr, COL_WARN("WARNING AT %s:%d %s():\n"), __FILE__, __LINE__, __func__); \ + fprintf(stderr, COL_WARN(fmt) "\n", ##__VA_ARGS__); +#endif + + + +// Is this debug verbosity and unit level enabled? +// Provide compiler hints that this is unlikely +// Two versions, depending on whether DEBUG_INSTANCE_EXPR is defined in a file or not +// +// For .cpp files whose units have discrete instance IDs, define DEBUG_INSTANCE_EXPR to evalute +// to the instance ID variable. The use of this define in header files is discouraged. + +#ifdef DEBUG_INSTANCE_EXPR +// Expression for whether the debugging verbosity + debugging unit is enabled for free-form printouts +#ifdef DEBUG_INSTANCE_EXPR_2 +#define DEBUG_ENABLED(VERB, LEVEL) \ + (__builtin_expect((g_func_debug.func_debug_mask == DEBUG_ALL || g_func_debug.func_debug_mask & (DEBUG_##LEVEL)) && \ + (g_func_debug.func_debug_inst_mask & (uint64_t(1) << (DEBUG_INSTANCE_EXPR))) && \ + (g_func_debug.func_debug_verbosity & (VERB)), \ + 0)) +// Debug printing macro +#define DEBUG(VERB, LEVEL, FMT, ...) \ + if (DEBUG_ENABLED(VERB, LEVEL)) \ + { \ + fprintf(g_func_debug.func_debug_file, "[%d:" #LEVEL "_%02d_%02d" WHERE "]: " FMT "\n", \ + (int)g_func_debug.inst_id, (int)(DEBUG_INSTANCE_EXPR), (int)(DEBUG_INSTANCE_EXPR_2), ##__VA_ARGS__); \ + } + +// Prints just the debugging prefix for properly marking free-form printouts +#define DEBUG_PREFIX(LEVEL) \ + fprintf(g_func_debug.func_debug_file, "[%d" #LEVEL "_%02d_%02d" WHERE "]: ", (int)g_func_debug.inst_id, \ + (int)(DEBUG_INSTANCE_EXPR), (int)(DEBUG_INSTANCE_EXPR_2)) + +#else // !DEBUG_INSTANCE_EXPR_2 + +#define DEBUG_ENABLED(VERB, LEVEL) \ + (__builtin_expect((g_func_debug.func_debug_mask == DEBUG_ALL || g_func_debug.func_debug_mask & (DEBUG_##LEVEL)) && \ + (g_func_debug.func_debug_inst_mask & (uint64_t(1) << (DEBUG_INSTANCE_EXPR))) && \ + (g_func_debug.func_debug_verbosity & (VERB)), \ + 0)) +// Debug printing macro +#define DEBUG(VERB, LEVEL, FMT, ...) \ + if (DEBUG_ENABLED(VERB, LEVEL)) \ + { \ + fprintf(g_func_debug.func_debug_file, "[%d:" #LEVEL "_%02d" WHERE "]: " FMT "\n", (int)g_func_debug.inst_id, \ + (int)(DEBUG_INSTANCE_EXPR), ##__VA_ARGS__); \ + } + +// Prints just the debugging prefix for properly marking free-form printouts +#define DEBUG_PREFIX(LEVEL) \ + fprintf(g_func_debug.func_debug_file, "[%d:" #LEVEL "_%02d" WHERE "]: ", (int)g_func_debug.inst_id, \ + (int)(DEBUG_INSTANCE_EXPR)) + +#endif // DEBUG_INSTANCE_EXPR_2 + +#else // !DEBUG_INSTANCE_EXPR + +// Expression for whether the debugging verbosity + debugging unit is enabled for free-form printouts +#define DEBUG_ENABLED(VERB, LEVEL) \ + (__builtin_expect((g_func_debug.func_debug_mask == DEBUG_ALL || g_func_debug.func_debug_mask & (DEBUG_##LEVEL)) && \ + (g_func_debug.func_debug_verbosity & (VERB)), \ + 0)) +// Debug printing macro +#define DEBUG(VERB, LEVEL, FMT, ...) \ + if (DEBUG_ENABLED(VERB, LEVEL)) \ + { \ + fprintf(g_func_debug.func_debug_file, "[%d:" #LEVEL WHERE "]: " FMT "\n", (int)g_func_debug.inst_id, \ + ##__VA_ARGS__); \ + } + +// Prints just the debugging prefix for properly marking free-form printouts +#define DEBUG_PREFIX(LEVEL) fprintf(g_func_debug.func_debug_file, "[" #LEVEL WHERE "]: ") + +#endif + +// Macros for different verbosity levels +#define DEBUG_INFO(LEVEL, FMT, ...) DEBUG(DEBUG_VERB_INFO, LEVEL, COL_INFO(FMT), ##__VA_ARGS__) +#define DEBUG_IFACE(LEVEL, FMT, ...) DEBUG(DEBUG_VERB_IFACE, LEVEL, COL_IFACE(FMT), ##__VA_ARGS__) +#define DEBUG_LOW(LEVEL, FMT, ...) DEBUG(DEBUG_VERB_LOW, LEVEL, COL_LOW(FMT), ##__VA_ARGS__) +#define DEBUG_MED(LEVEL, FMT, ...) DEBUG(DEBUG_VERB_MED, LEVEL, COL_MED(FMT), ##__VA_ARGS__) +#define DEBUG_HIGH(LEVEL, FMT, ...) DEBUG(DEBUG_VERB_HIGH, LEVEL, COL_HIGH(FMT), ##__VA_ARGS__) + +#endif diff --git a/reference_model/include/graph_status.h b/reference_model/include/graph_status.h new file mode 100644 index 0000000..f3be004 --- /dev/null +++ b/reference_model/include/graph_status.h @@ -0,0 +1,25 @@ +// Copyright (c) 2022, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef GRAPH_STATUS_H +#define GRAPH_STATUS_H + +enum class GraphStatus : int +{ + TOSA_VALID = 0, + TOSA_UNPREDICTABLE = 1, + TOSA_ERROR = 2, +}; + +#endif // GRAPH_STATUS_H diff --git a/reference_model/include/model_common.h b/reference_model/include/model_common.h new file mode 100644 index 0000000..d6dab6d --- /dev/null +++ b/reference_model/include/model_common.h @@ -0,0 +1,28 @@ + +// Copyright (c) 2020, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef MODEL_COMMON_H +#define MODEL_COMMON_H + +#include <iostream> +#include <stdio.h> + +#include "func_config.h" +#include "func_debug.h" + +extern func_config_t g_func_config; +extern func_debug_t g_func_debug; + +#endif diff --git a/reference_model/include/model_runner.h b/reference_model/include/model_runner.h new file mode 100644 index 0000000..4629467 --- /dev/null +++ b/reference_model/include/model_runner.h @@ -0,0 +1,87 @@ + +// Copyright (c) 2022, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef MODEL_RUNNER_H_ +#define MODEL_RUNNER_H_ + +#include "model_common.h" +#include "graph_status.h" + +#include "tosa_serialization_handler.h" + +namespace TosaReference +{ + +class ModelRunnerImpl; + +/* + * This interface allows a user to initialize, run and get the output from a model. + * See model_runner_simple_sample.cpp for example on how this interface can be used. + */ +class IModelRunner +{ +public: + IModelRunner(); + IModelRunner(const func_config_t& func_config, const func_debug_t& func_debug); + + ~IModelRunner(); + + /* + * Functional and debug configurations can also be set. + * See func_config.h and func_debug.h for possible options. + */ + void setFuncConfig(func_config_t& func_config); + void setFuncDebug(func_debug_t& func_debug); + + /* + * Initialize the model. + * The TosaSerializationHandler is validated and then converted to a SubgraphTraverser internally. + * This SubgraphTraverser is initialized, allocated and validated. + * The status of the graph will be returned upon completion. + */ + GraphStatus initialize(tosa::TosaSerializationHandler& serialization_handler); + + /* + * Run the model using the internal SubgraphTraverser created during initialization. + * If validate_only is specified run() will simply return the graph status. + * Otherwise, the graph will be run and the output tensors will be generated if the graph is valid. + * The output tensors can then be retrieved with getOutput(). + * NOTE: initialize() must be called before run(). Also, setInput() must be called for all inputs in the model. + */ + GraphStatus run(); + + /* + * Set the input tensors for the model. + * The input_name much match the input tensor name in the model. + * NOTE: setInput() must be called for each input tensor before run() is called. + */ + template <typename T> + int setInput(std::string input_name, std::vector<T> vals); + + /* + * Retrieve the output tensors from the graph after running. + * The output_name much match the output tensor name in the model. + * NOTE: run() must be called before outputs are retrieved. + */ + template <typename T> + std::vector<T> getOutput(std::string output_name); + +private: + std::unique_ptr<ModelRunnerImpl> model_runner_impl; +}; + +}; // namespace TosaReference + +#endif diff --git a/reference_model/include/version.h b/reference_model/include/version.h new file mode 100644 index 0000000..cd1d598 --- /dev/null +++ b/reference_model/include/version.h @@ -0,0 +1,23 @@ +// Copyright (c) 2022, ARM Limited. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef VERSION_H +#define VERSION_H + +#define TOSA_REFERENCE_MODEL_VERSION_MAJOR 0 +#define TOSA_REFERENCE_MODEL_VERSION_MINOR 41 +#define TOSA_REFERENCE_MODEL_VERSION_PATCH 0 +#define TOSA_REFERENCE_MODEL_VERSION_DRAFT true + +#endif //VERSION_H |