Caffe2 - C++ API
A deep learning, cross platform ML framework
smart_tensor_printer.cc
1 #include "smart_tensor_printer.h"
2 
3 #include "caffe2/core/operator.h"
4 
5 namespace caffe2 {
6 
7 namespace {
8 
9 // Since DispatchHelper doesn't support passing arguments through the call()
10 // method to DoRunWithType we have to create an object that will hold these
11 // arguments explicitly.
12 struct ProxyPrinter {
13  template <typename T>
14  bool DoRunWithType() {
15  tensorPrinter->Print<T>(*tensor);
16  return true;
17  }
18 
19  void Print() {
20  // Pulled in printable types from caffe2/core/types.cc
21  // Unfortunately right now one has to add them by hand
22  DispatchHelper<TensorTypes<
23  float,
24  int,
25  std::string,
26  bool,
27  uint8_t,
28  int8_t,
29  uint16_t,
30  int16_t,
31  int64_t,
32  double,
33  char>>::call(this, tensor->dtype());
34  }
35 
36  const Tensor* tensor;
37  TensorPrinter* tensorPrinter;
38 };
39 }
40 
41 SmartTensorPrinter::SmartTensorPrinter(const std::string& tensor_name)
42  : tensorPrinter_(tensor_name) {}
43 
44 SmartTensorPrinter::SmartTensorPrinter(
45  const std::string& tensor_name,
46  const std::string& file_name)
47  : tensorPrinter_(tensor_name, file_name) {}
48 
49 SmartTensorPrinter::SmartTensorPrinter(
50  const std::string& tensor_name,
51  const std::string& file_name,
52  int limit)
53  : tensorPrinter_(tensor_name, file_name, limit) {}
54 
55 void SmartTensorPrinter::Print(const Tensor& tensor) {
56  ProxyPrinter printer;
57 
58  printer.tensor = &tensor;
59  printer.tensorPrinter = &tensorPrinter_;
60  printer.Print();
61 }
62 
63 SmartTensorPrinter& SmartTensorPrinter::DefaultTensorPrinter() {
64 // TODO(janusz): thread_local does not work under mac.
65 #if __APPLE__
66  CAFFE_THROW(
67  "SmartTensorPrinter does not work on mac yet due to thread_local.");
68 #else
69  static thread_local SmartTensorPrinter printer;
70  return printer;
71 #endif
72 }
73 
74 void SmartTensorPrinter::PrintTensor(const Tensor& tensor) {
75  DefaultTensorPrinter().Print(tensor);
76 }
77 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13