tensor.cpp 11.7 KB
Newer Older
1 2 3 4
#include "lite/tensor.h"
#include <set>
#include <string>
#include <unordered_map>
M
Megvii Engine Team 已提交
5 6
#include "../../src/tensor_impl_base.h"
#include "common.h"
7
#include "ipc_helper.h"
M
Megvii Engine Team 已提交
8
#include "lite-c/tensor_c.h"
9

M
Megvii Engine Team 已提交
10 11
const LiteLayout default_layout = {
        .shapes = {0, 0, 0, 0, 0}, .ndim = 0, .data_type = LiteDataType::LITE_FLOAT};
12

M
Megvii Engine Team 已提交
13 14 15 16 17
const LiteTensorDesc default_desc = {
        .is_pinned_host = false,
        .layout = default_layout,
        .device_type = LiteDeviceType::LITE_CPU,
        .device_id = 0};
18
namespace {
19 20

static LITE_MUTEX mtx_tensor;
M
Megvii Engine Team 已提交
21
std::unordered_map<void*, std::shared_ptr<lite::Tensor>>& get_global_tensor_holder() {
22
    static std::unordered_map<void*, std::shared_ptr<lite::Tensor>> global_holder;
23 24
    return global_holder;
}
25 26

static LITE_MUTEX mtx_attr;
M
Megvii Engine Team 已提交
27
std::unordered_map<std::string, lite::LiteAny>& get_global_tensor_attr_holder() {
28
    static std::unordered_map<std::string, lite::LiteAny> global_holder;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    return global_holder;
}
}  // namespace

//! convert the lite::Layout to Layout
LiteLayout convert_to_clayout(const lite::Layout& layout) {
    LiteLayout clayout;
    clayout.ndim = layout.ndim;
    LITE_ASSERT(layout.ndim < LAYOUT_MAX_DIM, "layout ndim is to large");
    for (size_t i = 0; i < layout.ndim; i++) {
        clayout.shapes[i] = layout.shapes[i];
    }
    clayout.data_type = layout.data_type;
    return clayout;
}

//! convert the C Layout to lite::Layout
lite::Layout convert_to_layout(const LiteLayout& clayout) {
    lite::Layout layout;
    layout.ndim = clayout.ndim;
    LITE_ASSERT(layout.ndim < LAYOUT_MAX_DIM, "clayout ndim is to large");
    for (size_t i = 0; i < layout.ndim; i++) {
        layout.shapes[i] = clayout.shapes[i];
    }
    layout.data_type = clayout.data_type;
    return layout;
}

int LITE_make_tensor(const LiteTensorDesc tensor_describe, LiteTensor* tensor) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE_make_tensor is null");
    lite::Layout layout = convert_to_layout(tensor_describe.layout);
    auto lite_tensor = std::make_shared<lite::Tensor>(
            tensor_describe.device_id, tensor_describe.device_type, layout,
            tensor_describe.is_pinned_host);
64 65 66 67
    {
        LITE_LOCK_GUARD(mtx_tensor);
        get_global_tensor_holder()[lite_tensor.get()] = lite_tensor;
    }
68 69 70 71 72 73 74
    *tensor = lite_tensor.get();
    LITE_CAPI_END();
}

int LITE_destroy_tensor(LiteTensor tensor) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
75
    LITE_LOCK_GUARD(mtx_tensor);
76 77 78 79
    auto& global_holder = get_global_tensor_holder();
    if (global_holder.find(tensor) != global_holder.end()) {
        global_holder.erase(tensor);
    }
80 81 82 83 84 85 86 87 88 89 90
    LITE_CAPI_END();
}

int LITE_set_tensor_layout(LiteTensor tensor, const LiteLayout layout) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    auto tensor_ptr = static_cast<lite::Tensor*>(tensor);
    tensor_ptr->set_layout(convert_to_layout(layout));
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
91 92
int LITE_reset_tensor_memory(
        LiteTensor tensor, void* prepared_data, size_t data_length_in_byte) {
93 94 95
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(prepared_data, "The prepared_data pass to LITE c_api is null");
M
Megvii Engine Team 已提交
96
    static_cast<lite::Tensor*>(tensor)->reset(prepared_data, data_length_in_byte);
97 98 99
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
100
int LITE_reset_tensor(LiteTensor tensor, const LiteLayout layout, void* prepared_data) {
101 102 103
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(prepared_data, "The prepared_data pass to LITE c_api is null");
M
Megvii Engine Team 已提交
104
    static_cast<lite::Tensor*>(tensor)->reset(prepared_data, convert_to_layout(layout));
105 106 107 108 109 110 111 112 113 114 115 116 117 118
    LITE_CAPI_END();
}

int LITE_tensor_reshape(LiteTensor tensor, const int* shape, int size) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor && shape, "The tensor pass to LITE c_api is null");
    std::vector<int> shapes;
    for (int i = 0; i < size; i++) {
        shapes.push_back(shape[i]);
    }
    static_cast<lite::Tensor*>(tensor)->reshape(shapes);
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
119 120 121
int LITE_tensor_slice(
        const LiteTensor tensor, const size_t* start, const size_t* end,
        const size_t* step, size_t size, LiteTensor* slice_tensor) {
122
    LITE_CAPI_BEGIN();
M
Megvii Engine Team 已提交
123 124 125
    LITE_ASSERT(
            tensor && start && end && slice_tensor,
            "The tensor pass to LITE c_api is null");
126 127 128 129 130 131 132 133
    std::vector<size_t> starts, ends, steps;
    for (size_t i = 0; i < size; i++) {
        starts.push_back(start[i]);
        ends.push_back(end[i]);
        if (step) {
            steps.push_back(step[i]);
        }
    }
M
Megvii Engine Team 已提交
134
    auto ret_tensor = static_cast<lite::Tensor*>(tensor)->slice(starts, ends, steps);
135 136 137 138
    {
        LITE_LOCK_GUARD(mtx_tensor);
        get_global_tensor_holder()[ret_tensor.get()] = ret_tensor;
    }
139 140 141 142 143 144 145 146 147 148 149 150 151
    *slice_tensor = ret_tensor.get();
    LITE_CAPI_END();
}

int LITE_tensor_fill_zero(LiteTensor tensor) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    static_cast<lite::Tensor*>(tensor)->fill_zero();
    LITE_CAPI_END();
}

int LITE_tensor_copy(LiteTensor dst_tensor, const LiteTensor src_tensor) {
    LITE_CAPI_BEGIN();
M
Megvii Engine Team 已提交
152
    LITE_ASSERT(dst_tensor && src_tensor, "The tensor pass to LITE c_api is null");
153 154 155 156 157
    static_cast<lite::Tensor*>(dst_tensor)
            ->copy_from(*static_cast<lite::Tensor*>(src_tensor));
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
158
int LITE_tensor_share_memory_with(LiteTensor dst_tensor, const LiteTensor src_tensor) {
159
    LITE_CAPI_BEGIN();
M
Megvii Engine Team 已提交
160
    LITE_ASSERT(dst_tensor && src_tensor, "The tensor pass to LITE c_api is null");
161 162 163 164 165 166 167 168 169
    static_cast<lite::Tensor*>(dst_tensor)
            ->share_memory_with(*static_cast<lite::Tensor*>(src_tensor));
    LITE_CAPI_END();
}

int LITE_get_tensor_memory(const LiteTensor tensor, void** data) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(data, "The data ptr pass to LITE c_api is null");
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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    if (ipc_imp::is_server()) {
        *data = static_cast<lite::Tensor*>(tensor)->get_memory_ptr();
    } else {
        size_t need_size = sizeof(LiteTensor);
        IPC_INSTACE().check_shm_size(need_size);

        void* raw_shm_ptr = IPC_INSTACE().get_shm_ptr(nullptr);

        char* shm_ptr_c = static_cast<char*>(raw_shm_ptr);
        memcpy(shm_ptr_c, &tensor, sizeof(LiteTensor));

        IPC_HELP_REMOTE_CALL(raw_shm_ptr, ipc::RemoteFuncId::LITE_GET_TENSOR_MEMORY);

        int* ret_ptr = static_cast<int*>(raw_shm_ptr);
        auto ret = *ret_ptr;
        ret_ptr++;
        memcpy(data, ret_ptr, sizeof(void*));
        return ret;
    }
    LITE_CAPI_END();
}

void* LITE_memset(void* s, int c, size_t n) {
    if (ipc_imp::is_server()) {
        return memset(s, c, n);
    } else {
        size_t need_size = sizeof(void*) + sizeof(int) + sizeof(size_t);
        IPC_INSTACE().check_shm_size(need_size);

        void* raw_shm_ptr = IPC_INSTACE().get_shm_ptr(nullptr);

        char* shm_ptr_c = static_cast<char*>(raw_shm_ptr);
        memcpy(shm_ptr_c, &s, sizeof(void*));
        memcpy(shm_ptr_c + sizeof(void*), &c, sizeof(int));
        memcpy(shm_ptr_c + sizeof(void*) + sizeof(int), &n, sizeof(size_t));

        IPC_HELP_REMOTE_CALL(raw_shm_ptr, ipc::RemoteFuncId::LITE_MEMSET);

        return s;
    }
}

int LITE_copy_server_tensor_memory(
        void* server_ptr, void* client_ptr, size_t size_in_byte) {
    LITE_CAPI_BEGIN();
    if (ipc_imp::is_server()) {
        LITE_ASSERT(
                false, "lite not in fork debug mode, please do not call this function");
    } else {
        size_t need_size = sizeof(void*) + sizeof(size_t);
        IPC_INSTACE().check_shm_size(need_size);
        IPC_INSTACE().check_shm_size(size_in_byte);

        void* raw_shm_ptr = IPC_INSTACE().get_shm_ptr(nullptr);

        char* shm_ptr_c = static_cast<char*>(raw_shm_ptr);
        memcpy(shm_ptr_c, &server_ptr, sizeof(void*));
        memcpy(shm_ptr_c + sizeof(void*), &size_in_byte, sizeof(size_t));

        IPC_HELP_REMOTE_CALL(
                raw_shm_ptr, ipc::RemoteFuncId::LITE_COPY_SERVER_TENSOR_MEMORY);
        memcpy(client_ptr, raw_shm_ptr, size_in_byte);
        return 0;
    }
234 235 236
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
237 238
int LITE_get_tensor_memory_with_index(
        const LiteTensor tensor, const size_t* index, size_t size, void** data) {
239
    LITE_CAPI_BEGIN();
M
Megvii Engine Team 已提交
240
    LITE_ASSERT(tensor && index && data, "The tensor pass to LITE c_api is null");
241 242 243 244 245 246 247 248 249 250 251 252
    std::vector<size_t> index_v;
    for (size_t i = 0; i < size; i++) {
        index_v.push_back(index[i]);
    }
    *data = static_cast<lite::Tensor*>(tensor)->get_memory_ptr(index_v);
    LITE_CAPI_END();
}

int LITE_get_tensor_total_size_in_byte(const LiteTensor tensor, size_t* size) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(size, "The size ptr pass to LITE c_api is null");
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    if (ipc_imp::is_server()) {
        *size = static_cast<lite::Tensor*>(tensor)->get_tensor_total_size_in_byte();
    } else {
        size_t need_size = sizeof(LiteTensor);
        IPC_INSTACE().check_shm_size(need_size);

        void* raw_shm_ptr = IPC_INSTACE().get_shm_ptr(nullptr);

        char* shm_ptr_c = static_cast<char*>(raw_shm_ptr);
        memcpy(shm_ptr_c, &tensor, sizeof(LiteTensor));

        IPC_HELP_REMOTE_CALL(
                raw_shm_ptr, ipc::RemoteFuncId::LITE_GET_TENSOR_TOTAL_SIZE_IN_BYTE);

        int* ret_ptr = static_cast<int*>(raw_shm_ptr);
        auto ret = *ret_ptr;
        ret_ptr++;
        memcpy(size, ret_ptr, sizeof(size_t));
        return ret;
    }
273 274 275 276 277 278 279
    LITE_CAPI_END();
}

int LITE_get_tensor_layout(const LiteTensor tensor, LiteLayout* layout) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(layout, "The layout ptr pass to LITE c_api is null");
M
Megvii Engine Team 已提交
280
    *layout = convert_to_clayout(static_cast<lite::Tensor*>(tensor)->get_layout());
281 282 283
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
284
int LITE_get_tensor_device_type(const LiteTensor tensor, LiteDeviceType* device_type) {
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(device_type, "The device ptr pass to LITE c_api is null");
    *device_type = static_cast<lite::Tensor*>(tensor)->get_device_type();
    LITE_CAPI_END();
}

int LITE_get_tensor_device_id(const LiteTensor tensor, int* device_id) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor && device_id, "The tensor pass to LITE c_api is null");
    *device_id = static_cast<lite::Tensor*>(tensor)->get_device_id();
    LITE_CAPI_END();
}

int LITE_is_pinned_host(const LiteTensor tensor, int* is_pinned_host) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
M
Megvii Engine Team 已提交
302
    LITE_ASSERT(is_pinned_host, "The is_pinned_host ptr pass to LITE c_api is null");
303 304 305 306 307 308 309 310 311 312 313 314
    *is_pinned_host = static_cast<lite::Tensor*>(tensor)->is_pinned_host();
    LITE_CAPI_END();
}

int LITE_is_memory_continue(const LiteTensor tensor, int* is_continue) {
    LITE_CAPI_BEGIN();
    LITE_ASSERT(tensor, "The tensor pass to LITE c_api is null");
    LITE_ASSERT(is_continue, "The is_continue ptr pass to LITE c_api is null");
    *is_continue = static_cast<lite::Tensor*>(tensor)->is_continue_memory();
    LITE_CAPI_END();
}

M
Megvii Engine Team 已提交
315 316 317
int LITE_tensor_concat(
        LiteTensor* tensors, int nr_tensor, int dim, LiteDeviceType dst_device,
        int device_id, LiteTensor* result_tensor) {
318
    LITE_CAPI_BEGIN();
319
    LITE_ASSERT(result_tensor, "The tensor pass to LITE c_api is null");
320 321 322 323
    std::vector<lite::Tensor> v_tensors;
    for (int i = 0; i < nr_tensor; i++) {
        v_tensors.push_back(*static_cast<lite::Tensor*>(tensors[i]));
    }
M
Megvii Engine Team 已提交
324
    auto tensor = lite::TensorUtils::concat(v_tensors, dim, dst_device, device_id);
325 326 327 328
    {
        LITE_LOCK_GUARD(mtx_tensor);
        get_global_tensor_holder()[tensor.get()] = tensor;
    }
329 330 331 332 333
    *result_tensor = tensor.get();
    LITE_CAPI_END()
}

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