3 #include "caffe2/core/blob_serialization.h" 7 constexpr
auto kBlobName =
"blob_name";
8 constexpr
auto kAddValue =
"add_value";
10 StoreSetOp::StoreSetOp(
const OperatorDef& operator_def, Workspace* ws)
11 : Operator<CPUContext>(operator_def, ws),
13 GetSingleArgument<
std::string>(kBlobName, operator_def.input(DATA))) {
16 bool StoreSetOp::RunOnDevice() {
19 OperatorBase::Input<std::unique_ptr<StoreHandler>>(HANDLER).
get();
20 handler->set(blobName_,
SerializeBlob(InputBlob(DATA), blobName_));
24 REGISTER_CPU_OPERATOR(StoreSet, StoreSetOp);
25 OPERATOR_SCHEMA(StoreSet)
29 Set a blob in a store. The key is the input blob's name and the value 30 is the data in that blob. The key can be overridden by specifying the 33 .Arg("blob_name",
"alternative key for the blob (optional)")
34 .Input(0,
"handler",
"unique_ptr<StoreHandler>")
35 .Input(1,
"data",
"data blob");
37 StoreGetOp::StoreGetOp(
const OperatorDef& operator_def, Workspace* ws)
38 : Operator<CPUContext>(operator_def, ws),
39 blobName_(GetSingleArgument<
std::string>(
41 operator_def.output(DATA))) {}
43 bool StoreGetOp::RunOnDevice() {
46 OperatorBase::Input<std::unique_ptr<StoreHandler>>(HANDLER).
get();
47 DeserializeBlob(handler->get(blobName_), OperatorBase::Outputs()[DATA]);
51 REGISTER_CPU_OPERATOR(StoreGet, StoreGetOp);
52 OPERATOR_SCHEMA(StoreGet)
56 Get a blob from a store. The key is the output blob's name. The key 57 can be overridden by specifying the 'blob_name' argument. 59 .Arg("blob_name",
"alternative key for the blob (optional)")
60 .Input(0,
"handler",
"unique_ptr<StoreHandler>")
61 .Output(0,
"data",
"data blob");
63 StoreAddOp::StoreAddOp(
const OperatorDef& operator_def, Workspace* ws)
64 : Operator<CPUContext>(operator_def, ws),
65 blobName_(GetSingleArgument<
std::string>(kBlobName,
"")),
66 addValue_(GetSingleArgument<int64_t>(kAddValue, 1)) {
67 CAFFE_ENFORCE(HasArgument(kBlobName));
70 bool StoreAddOp::RunOnDevice() {
72 OperatorBase::Input<std::unique_ptr<StoreHandler>>(HANDLER).
get();
73 Output(VALUE)->Resize(1);
74 Output(VALUE)->mutable_data<int64_t>()[0] =
75 handler->add(blobName_, addValue_);
79 REGISTER_CPU_OPERATOR(StoreAdd, StoreAddOp);
80 OPERATOR_SCHEMA(StoreAdd)
84 Add a value to a remote counter. If the key is not set, the store 85 initializes it to 0 and then performs the add operation. The operation 86 returns the resulting counter value. 88 .Arg("blob_name",
"key of the counter (required)")
89 .Arg(
"add_value",
"value that is added (optional, default: 1)")
90 .Input(0,
"handler",
"unique_ptr<StoreHandler>")
91 .Output(0,
"value",
"the current value of the counter");
93 StoreWaitOp::StoreWaitOp(
const OperatorDef& operator_def, Workspace* ws)
94 : Operator<CPUContext>(operator_def, ws),
95 blobNames_(GetRepeatedArgument<
std::string>(kBlobName)) {}
97 bool StoreWaitOp::RunOnDevice() {
99 OperatorBase::Input<std::unique_ptr<StoreHandler>>(HANDLER).
get();
100 if (InputSize() == 2 && Input(1).IsType<std::string>()) {
102 blobNames_.empty(),
"cannot specify both argument and input blob");
103 std::vector<std::string> blobNames;
104 auto* namesPtr = Input(1).data<std::string>();
105 for (
int i = 0; i < Input(1).size(); ++i) {
106 blobNames.push_back(namesPtr[i]);
108 handler->wait(blobNames);
110 handler->wait(blobNames_);
115 REGISTER_CPU_OPERATOR(StoreWait, StoreWaitOp);
116 OPERATOR_SCHEMA(StoreWait)
120 Wait for the specified blob names to be set. The blob names can be passed 121 either as an input blob with blob names or as an argument. 123 .Arg("blob_names",
"names of the blobs to wait for (optional)")
124 .Input(0,
"handler",
"unique_ptr<StoreHandler>")
125 .Input(1,
"names",
"names of the blobs to wait for (optional)");
void DeserializeBlob(const string &content, Blob *result)
Deserializes from a string containing either BlobProto or TensorProto.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void SerializeBlob(const Blob &blob, const string &name, BlobSerializerBase::SerializationAcceptor acceptor, int chunk_size)
Serializes the given blob, if possible.