1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
|
#include "caffe2/sgd/iter_op.h"
#ifdef CAFFE2_USE_MKLDNN
#include <caffe2/ideep/operators/operator_fallback_ideep.h>
#include <caffe2/ideep/utils/ideep_operator.h>
#endif
namespace caffe2 {
void MutexSerializer::Serialize(
const void* pointer,
TypeMeta typeMeta,
const string& name,
BlobSerializerBase::SerializationAcceptor acceptor) {
CAFFE_ENFORCE(typeMeta.Match<std::unique_ptr<std::mutex>>());
BlobProto blob_proto;
blob_proto.set_name(name);
blob_proto.set_type("std::unique_ptr<std::mutex>");
blob_proto.set_content("");
acceptor(name, SerializeBlobProtoAsString_EnforceCheck(blob_proto));
}
void MutexDeserializer::Deserialize(const BlobProto& /* unused */, Blob* blob) {
*blob->GetMutable<std::unique_ptr<std::mutex>>() =
caffe2::make_unique<std::mutex>();
}
REGISTER_CPU_OPERATOR(Iter, IterOp<CPUContext>);
REGISTER_CPU_OPERATOR(AtomicIter, AtomicIterOp<CPUContext>);
#ifdef CAFFE2_USE_MKLDNN
REGISTER_IDEEP_OPERATOR(AtomicIter, IDEEPFallbackOp<AtomicIterOp<CPUContext>>);
#endif
REGISTER_BLOB_SERIALIZER(
(TypeMeta::Id<std::unique_ptr<std::mutex>>()),
MutexSerializer);
REGISTER_BLOB_DESERIALIZER(std::unique_ptr<std::mutex>, MutexDeserializer);
OPERATOR_SCHEMA(Iter)
.NumInputs(0, 1)
.NumOutputs(1)
.EnforceInplace({{0, 0}})
.SetDoc(R"DOC(
Stores a singe integer, that gets incremented on each call to Run().
Useful for tracking the iteration count during SGD, for example.
)DOC");
OPERATOR_SCHEMA(AtomicIter)
.NumInputs(2)
.NumOutputs(1)
.EnforceInplace({{1, 0}})
.SetDoc(R"DOC(
Similar to Iter, but takes a mutex as the first input to make sure that
updates are carried out atomically. This can be used in e.g. Hogwild sgd
algorithms.
)DOC")
.Input(0, "mutex", "The mutex used to do atomic increment.")
.Input(1, "iter", "The iter counter as an int64_t TensorCPU.");
NO_GRADIENT(Iter);
NO_GRADIENT(AtomicIter);
} // namespace caffe2
|