Coverage Report

Created: 2021-04-06 03:49

/home/liu/buildslave/linux-x64-runtests/build/lib/nnc/ccv_cnnp_model_io.c
Line
Count
Source (jump to first uncovered line)
1
#include "ccv_nnc.h"
2
#include "ccv_nnc_easy.h"
3
#include "ccv_nnc_internal.h"
4
#include "ccv_internal.h"
5
#include "_ccv_cnnp_model.h"
6
#include "3rdparty/sqlite3/sqlite3.h"
7
#ifdef HAVE_CUDA
8
#include "gpu/ccv_nnc_compat.h"
9
#endif
10
11
#ifdef NDEBUG
12
#define SQLITE_ENFORCE(stmt) (void)(stmt)
13
#else
14
2
#define SQLITE_ENFORCE assert
15
#endif
16
17
int ccv_cnnp_model_write(const ccv_cnnp_model_t* const model, void* const handle, const char* const name)
18
1
{
19
1
  sqlite3* conn = (sqlite3*)handle;
20
1
  assert(conn);
21
1
  ccv_cnnp_compiled_data_t* const compiled_data = model->compiled_data;
22
1
  assert(compiled_data); // The model has to be compiled.
23
1
  SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, "BEGIN", 0, 0, 0));
24
1
  int i, j;
25
1
  const int parallel_count = ccv_max(model->parallel_count, 1);
26
1
  const int parameter_size = compiled_data->parameters->rnum;
27
1
  const int internal_size = compiled_data->internals->rnum;
28
1
  char internal_name[2048 + 16];
29
11
  for (i = 0; i < parameter_size; 
i++10
)
30
10
  {
31
10
    const char* const id = *(char**)ccv_array_get(compiled_data->ids.parameters, i);
32
10
    if (name)
33
0
      snprintf(internal_name, 2048 + 16, "__%s__[%s]", name, id);
34
10
    else
35
10
      snprintf(internal_name, 2048 + 16, "%s", id);
36
10
    ccv_nnc_tensor_write(compiled_data->tensors.parameters[i], conn, internal_name);
37
10
  }
38
2
  for (i = 0; i < parallel_count; 
i++1
)
39
1
    for (j = 0; j < internal_size; 
j++0
)
40
0
    {
41
0
      const char* const id = *(char**)ccv_array_get(compiled_data->ids.internals, j);
42
0
      if (name)
43
0
        snprintf(internal_name, 2048 + 16, "__%s__[%s(%d)]", name, id, i);
44
0
      else
45
0
        snprintf(internal_name, 2048 + 16, "%s(%d)", id, i);
46
0
      ccv_nnc_tensor_write(compiled_data->tensors.internals[i * internal_size + j], conn, internal_name);
47
0
    }
48
1
  SQLITE_ENFORCE(SQLITE_OK == sqlite3_exec(conn, "COMMIT", 0, 0, 0));
49
1
  return CCV_IO_FINAL;
50
1
}
51
52
int ccv_cnnp_model_read(void* const handle, const char* const name, const ccv_cnnp_model_t* const model_out)
53
1
{
54
1
  sqlite3* conn = (sqlite3*)handle;
55
1
  assert(conn);
56
1
  ccv_cnnp_compiled_data_t* const compiled_data = model_out->compiled_data;
57
1
  assert(compiled_data); // The model has to be compiled.
58
1
  const int tensors_init = !!compiled_data->tensors_init.v;
59
1
  if (!tensors_init)
60
1
    ccv_cnnp_model_tensors_init(model_out, compiled_data);
61
1
  int i, j;
62
1
  const int parallel_count = ccv_max(model_out->parallel_count, 1);
63
1
  const int parameter_size = compiled_data->parameters->rnum;
64
1
  const int internal_size = compiled_data->internals->rnum;
65
1
  char internal_name[2048 + 16];
66
11
  for (i = 0; i < parameter_size; 
i++10
)
67
10
  {
68
10
    const char* const id = *(char**)ccv_array_get(compiled_data->ids.parameters, i);
69
10
    if (name)
70
0
      snprintf(internal_name, 2048 + 16, "__%s__[%s]", name, id);
71
10
    else
72
10
      snprintf(internal_name, 2048 + 16, "%s", id);
73
10
    if (ccv_nnc_tensor_read(conn, internal_name, compiled_data->tensors.parameters + i) == CCV_IO_FINAL)
74
10
    {
75
10
      const int d = ((ccv_nnc_tensor_symbol_t*)ccv_array_get(compiled_data->parameters, i))->d;
76
10
      compiled_data->tensors_init.v[d >> 5] |= (1u << (d & 0x1f));
77
10
    }
78
10
  }
79
2
  for (i = 0; i < parallel_count; 
i++1
)
80
1
    for (j = 0; j < internal_size; 
j++0
)
81
0
    {
82
0
      const char* const id = *(char**)ccv_array_get(compiled_data->ids.internals, j);
83
0
      if (name)
84
0
        snprintf(internal_name, 2048 + 16, "__%s__[%s(%d)]", name, id, i);
85
0
      else
86
0
        snprintf(internal_name, 2048 + 16, "%s(%d)", id, i);
87
0
      if (ccv_nnc_tensor_read(conn, internal_name, compiled_data->tensors.internals + i * internal_size + j) == CCV_IO_FINAL)
88
0
      {
89
0
        const int d = ((ccv_nnc_tensor_symbol_t*)ccv_array_get(compiled_data->internals, i))->d;
90
0
        compiled_data->tensors_init.v[d >> 5] |= (1u << (d & 0x1f));
91
0
      }
92
0
    }
93
1
  return CCV_IO_FINAL;
94
1
}
95
96
void ccv_cnnp_model_checkpoint(ccv_cnnp_model_t* const model, const char* const fn, const int flags)
97
2
{
98
2
  ccv_cnnp_compiled_data_t* const compiled_data = model->compiled_data;
99
2
  assert(compiled_data); // The model has to be compiled.
100
2
  sqlite3* conn = 0;
101
2
  if (SQLITE_OK != sqlite3_open(fn, &conn))
102
0
    return;
103
2
  const int tensors_init = !!compiled_data->tensors_init.v;
104
2
  if (!tensors_init || 
flags == CCV_CNNP_MODEL_CHECKPOINT_READ_ONLY1
)
105
1
  {
106
1
    ccv_cnnp_model_read(conn, 0, model);
107
1
    sqlite3_close(conn);
108
1
    return;
109
1
  }
110
1
  ccv_cnnp_model_write(model, conn, 0);
111
1
  sqlite3_close(conn);
112
1
}