global.cpp 7.3 KB
Newer Older
1 2 3 4 5 6 7 8
#include "lite/global.h"
#include "common.h"
#include "lite-c/global_c.h"

namespace {
class ErrorMsg {
public:
    std::string& get_error_msg() { return error_msg; }
9 10 11 12 13 14 15 16 17 18
    ErrorCode get_error_code() { return error_code; }
    void set_error_msg(const std::string& msg, ErrorCode code) {
        error_msg = msg + ", Error Code: " + std::to_string(code);
        error_code = code;
    }

    void clear_error() {
        error_code = ErrorCode::OK;
        error_msg.clear();
    }
19 20 21

private:
    std::string error_msg;
22
    ErrorCode error_code;
23
};
24 25

static LITE_MUTEX mtx_error;
26
ErrorMsg& get_global_error() {
27
    static ErrorMsg error_msg;
28 29 30 31 32
    return error_msg;
}
}  // namespace

int LiteHandleException(const std::exception& e) {
33
    LITE_LOCK_GUARD(mtx_error);
34
    get_global_error().set_error_msg(e.what(), ErrorCode::LITE_INTERNAL_ERROR);
35 36 37
    return -1;
}

38 39 40 41 42 43 44 45 46 47
ErrorCode LITE_get_last_error_code() {
    LITE_LOCK_GUARD(mtx_error);
    return get_global_error().get_error_code();
}

void LITE_clear_last_error() {
    LITE_LOCK_GUARD(mtx_error);
    get_global_error().clear_error();
}

48
const char* LITE_get_last_error() {
49
    LITE_LOCK_GUARD(mtx_error);
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    return get_global_error().get_error_msg().c_str();
}

int LITE_get_version(int* major, int* minor, int* patch) {
    LITE_ASSERT(major && minor && patch, "The ptr pass to LITE api is null");
    lite::get_version(*major, *minor, *patch);
    return 0;
}

int LITE_get_device_count(LiteDeviceType device_type, size_t* count) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(count, "The ptr pass to LITE api is null");
    *count = lite::get_device_count(device_type);
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
66
int LITE_try_coalesce_all_free_memory() {
67 68 69 70 71
    LITE_CAPI_BEGIN();
    lite::try_coalesce_all_free_memory();
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
72 73 74
int LITE_register_decryption_and_key(
        const char* decrypt_name, const LiteDecryptionFunc func,
        const uint8_t* key_data, size_t key_size) {
75
    LITE_CAPI_BEGIN();
M
Megvii Engine Team 已提交
76
    LITE_ASSERT(decrypt_name && key_data && func, "The ptr pass to LITE api is null");
77 78 79 80 81 82
    std::vector<uint8_t> key;
    for (size_t i = 0; i < key_size; i++) {
        key.push_back(key_data[i]);
    }
    auto decrypt_func = [func](const void* input_data, size_t input_size,
                               const std::vector<uint8_t>& key) {
M
Megvii Engine Team 已提交
83
        auto size = func(input_data, input_size, key.data(), key.size(), nullptr);
84 85 86 87 88 89 90 91
        std::vector<uint8_t> output(size, 0);
        func(input_data, input_size, key.data(), key.size(), output.data());
        return output;
    };
    lite::register_decryption_and_key(decrypt_name, decrypt_func, key);
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
92 93 94
int LITE_update_decryption_or_key(
        const char* decrypt_name, const LiteDecryptionFunc func,
        const uint8_t* key_data, size_t key_size) {
95 96 97 98 99 100 101 102 103
    LITE_CAPI_BEGIN();
    std::vector<uint8_t> key;
    for (size_t i = 0; i < key_size; i++) {
        key.push_back(key_data[i]);
    }
    lite::DecryptionFunc decrypt_func = nullptr;
    if (func) {
        decrypt_func = [func](const void* input_data, size_t input_size,
                              const std::vector<uint8_t>& key) {
M
Megvii Engine Team 已提交
104
            auto size = func(input_data, input_size, key.data(), key.size(), nullptr);
105 106 107 108 109 110 111 112 113
            std::vector<uint8_t> output(size, 0);
            func(input_data, input_size, key.data(), key.size(), output.data());
            return output;
        };
    }
    lite::update_decryption_or_key(decrypt_name, decrypt_func, key);
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
114 115
int LITE_register_parse_info_func(
        const char* info_type, const LiteParseInfoFunc parse_func) {
116 117
    LITE_CAPI_BEGIN();
    LITE_ASSERT(info_type && parse_func, "The ptr pass to LITE api is null");
M
Megvii Engine Team 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    auto lite_func =
            [parse_func](
                    const void* info_data, size_t info_size,
                    const std::string model_name, lite::Config& config,
                    lite::NetworkIO& network_io,
                    std::unordered_map<std::string, lite::LiteAny>& separate_config_map,
                    std::string& extra_info) {
                LITE_MARK_USED_VAR(extra_info);
                size_t nr_threads = 1;
                int device_id = 0, is_cpu_inplace_mode = false, use_tensorrt = false;
                LiteNetworkIO c_io;
                LiteConfig c_config;
                auto ret = parse_func(
                        info_data, info_size, model_name.c_str(), &c_config, &c_io,
                        &device_id, &nr_threads, &is_cpu_inplace_mode, &use_tensorrt);
                config = convert_to_lite_config(c_config);
                network_io = convert_to_lite_io(c_io);
                if (device_id != 0) {
                    separate_config_map["device_id"] = device_id;
                }
                if (nr_threads != 1) {
139 140
                    separate_config_map["nr_threads"] =
                            static_cast<uint32_t>(nr_threads);
M
Megvii Engine Team 已提交
141 142 143 144 145 146 147 148 149
                }
                if (is_cpu_inplace_mode != false) {
                    separate_config_map["is_inplace_mode"] = is_cpu_inplace_mode;
                }
                if (use_tensorrt != false) {
                    separate_config_map["use_tensorrt"] = use_tensorrt;
                }
                return ret;
            };
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
    lite::register_parse_info_func(info_type, lite_func);
    LITE_CAPI_END();
}

int LITE_set_loader_lib_path(const char* loader_path) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(loader_path, "The ptr pass to LITE api is null");
    lite::set_loader_lib_path(loader_path);
    LITE_CAPI_END();
}

int LITE_set_persistent_cache(const char* cache_path, int always_sync) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(cache_path, "The ptr pass to LITE api is null");
    lite::set_persistent_cache(cache_path, always_sync);
    LITE_CAPI_END();
}

int LITE_set_tensor_rt_cache(const char* cache_path) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(cache_path, "The ptr pass to LITE api is null");
    lite::set_tensor_rt_cache(cache_path);
    LITE_CAPI_END();
}

int LITE_set_log_level(LiteLogLevel level) {
    LITE_CAPI_BEGIN();
    lite::set_log_level(level);
    LITE_CAPI_END();
}

int LITE_get_log_level(LiteLogLevel* level) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(level, "The ptr pass to LITE api is null");
    *level = lite::get_log_level();
    LITE_CAPI_END();
}

int LITE_dump_persistent_cache(const char* cache_path) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(cache_path, "The ptr pass to LITE api is null");
    lite::dump_persistent_cache(cache_path);
    LITE_CAPI_END();
}

int LITE_dump_tensor_rt_cache() {
    LITE_CAPI_BEGIN();
    lite::dump_tensor_rt_cache();
    LITE_CAPI_END();
}

201 202 203 204 205 206 207 208 209
int LITE_register_memory_pair(
        void* vir_ptr, void* phy_ptr, size_t length, LiteDeviceType device,
        LiteBackend backend) {
    LITE_CAPI_BEGIN();
    lite::register_memory_pair(vir_ptr, phy_ptr, length, device, backend);
    LITE_CAPI_END();
}

int LITE_clear_memory_pair(
210
        void* vir_ptr, void* phy_ptr, LiteDeviceType device, LiteBackend backend) {
211 212 213 214 215
    LITE_CAPI_BEGIN();
    lite::clear_memory_pair(vir_ptr, phy_ptr, device, backend);
    LITE_CAPI_END();
}

216 217 218 219 220 221 222 223
int LITE_lookup_physic_ptr(
        void* vir_ptr, void** phy_ptr, LiteDeviceType device, LiteBackend backend) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(vir_ptr && phy_ptr, "The ptr pass to vir and phy is nullptr");
    *phy_ptr = lite::lookup_physic_ptr(vir_ptr, device, backend);
    LITE_CAPI_END();
}

224
// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}