Caffe2 - C++ API
A deep learning, cross platform ML framework
load_save_op.cc
1 #include "caffe2/operators/load_save_op.h"
2 
3 namespace caffe2 {
4 
5 template <>
6 void LoadOp<CPUContext>::SetCurrentDevice(BlobProto* proto) {
7  if (proto->has_tensor()) {
8  proto->mutable_tensor()->mutable_device_detail()->set_device_type(CPU);
9  }
10 }
11 
12 REGISTER_CPU_OPERATOR(DBExists, DBExistsOp<CPUContext>);
13 REGISTER_CPU_OPERATOR(Load, LoadOp<CPUContext>);
14 REGISTER_CPU_OPERATOR(Save, SaveOp<CPUContext>);
15 REGISTER_CPU_OPERATOR(Checkpoint, CheckpointOp<CPUContext>);
16 // CPU Operator old name: do NOT use, we may deprecate this later.
17 REGISTER_CPU_OPERATOR(Snapshot, CheckpointOp<CPUContext>);
18 
19 OPERATOR_SCHEMA(DBExists)
20  .NumInputs(0)
21  .NumOutputs(1)
22  .SetDoc(R"DOC(
23 Checks if the DB exists.
24 )DOC")
25  .Output(0, "exists", "A scalar bool Tensor.")
26  .Arg(
27  "absolute_path",
28  "(int, default 0) if set, use the db path directly and do not prepend "
29  "the current root folder of the workspace.")
30  .Arg("db_name", "(string) the path to the db to load.")
31  .Arg("db_type", "(string) the type of the db.");
32 
33 OPERATOR_SCHEMA(Load)
34  .NumInputs(0, INT_MAX)
35  .NumOutputs(0, INT_MAX)
36  .SetDoc(R"DOC(
37 The Load operator loads a set of serialized blobs from a db or multiple dbs. It
38 takes [0, infinity) number of inputs and [0, infinity) number of outputs, using
39 the db keys to match the db entries with the outputs.
40 
41 If at least one input is passed, then it is assumed that that input blobs are a
42 set of DBReaders to load from. Otherwise the db or dbs argument is used to load
43 blobs from one single db or multiple dbs respectively. db_type argument is used
44 to specify the type of the input db/dbs.
45 )DOC")
46  .Arg(
47  "absolute_path",
48  "(int, default 0) if set, use the db path directly and do not prepend "
49  "the current root folder of the workspace.")
50  .Arg(
51  "add_prefix",
52  "(string, default=\"\") blobs will be prefixed with this when loading."
53  "Useful for avoiding collisions with blobs existing in the workspace."
54  "The output blob names specified to this op should include this prefix.")
55  .Arg(
56  "strip_prefix",
57  "(string, default=\"\") characters in the provided blob "
58  " names that match strip_prefix will be removed prior to loading."
59  " Also, characters that precede strip_prefix will be removed. Useful "
60  " for removing device scope from blob names.")
61  .Arg("db", "(string) the path to the db to load.")
62  .Arg(
63  "dbs",
64  "(list of strings) the paths to the dbs to load. This is used for loading"
65  " blobs from multiple databases. If it is set, argument in \"db\" will be"
66  " ignored.")
67  .Arg("db_type", "(string) the type of the db.")
68  .Arg(
69  "keep_device",
70  "(int, default 0) if nonzero, the blobs are loaded into the device that "
71  "is specified in the serialized BlobProto. Otherwise, the device will be "
72  "set as the one that the Load operator is being run under.")
73  .Arg(
74  "load_all",
75  "(int, default 0) if nonzero, will load all blobs pointed to by the db "
76  "to the workspace overwriting/creating blobs as needed.")
77  .Arg(
78  "allow_incomplete",
79  "(bool, default false) if true, will allow not loading all the output "
80  "blobs specified in the outputs")
81  .Arg(
82  "source_blob_names",
83  "(list of strings) if set, used instead of output "
84  "blob names, to specify which blobs in the db shall be loaded. Must be "
85  "the same length as number of output blobs.");
86 
87 OPERATOR_SCHEMA(Save)
88  .NumInputs(1, INT_MAX)
89  .NumOutputs(0)
90  .SetDoc(R"DOC(
91 The Save operator saves a set of blobs to a db. It takes [1, infinity) number
92 of inputs and has no output. The contents of the inputs are written into the
93 db specified by the arguments.
94 )DOC")
95  .Arg(
96  "absolute_path",
97  "(int, default 0) if set, use the db path directly and do not prepend "
98  "the current root folder of the workspace.")
99  .Arg(
100  "strip_prefix",
101  "(string, default=\"\") characters in the provided blob "
102  " names that match strip_prefix will be removed prior to saving."
103  " Also, characters that precede strip_prefix will be removed. Useful "
104  " for removing device scope from blob names.")
105  .Arg(
106  "blob_name_overrides",
107  "(list of strings) if set, used instead of original "
108  "blob names. Must be the same length as number of blobs.")
109  .Arg("db", "(string) the path to the db to load.")
110  .Arg("db_type", "(string) the type of the db.");
111 
112 OPERATOR_SCHEMA(Checkpoint)
113  .NumInputs(1, INT_MAX)
114  .NumOutputs(0)
115  .SetDoc(R"DOC(
116 The Checkpoint operator is similar to the Save operator, but allows one to save
117 to db every few iterations, with a db name that is appended with the iteration
118 count. It takes [1, infinity) number of inputs and has no output. The first
119 input has to be a TensorCPU of type int and has size 1 (i.e. the iteration
120 counter). This is determined whether we need to do checkpointing.
121 )DOC")
122  .Arg(
123  "absolute_path",
124  "(int, default 0) if set, use the db path directly and do not prepend "
125  "the current root folder of the workspace.")
126  .Arg(
127  "db",
128  "(string) a template string that one can combine with the "
129  "iteration to create the final db name. For example, "
130  "\"/home/lonestarr/checkpoint_%08d.db\"")
131  .Arg("db_type", "(string) the type of the db.")
132  .Arg(
133  "every",
134  "(int, default 1) the checkpointing is carried out when "
135  "(iter mod every) is zero.");
136 
137 OPERATOR_SCHEMA(Snapshot);
138 
139 NO_GRADIENT(Load);
140 SHOULD_NOT_DO_GRADIENT(DBExists);
141 SHOULD_NOT_DO_GRADIENT(Save);
142 SHOULD_NOT_DO_GRADIENT(Checkpoint);
143 SHOULD_NOT_DO_GRADIENT(Snapshot);
144 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...