Caffe2 - C++ API
A deep learning, cross platform ML framework
smart_tensor_printer.cc
1 
17 #include "smart_tensor_printer.h"
18 
19 #include "caffe2/core/operator.h"
20 
21 namespace caffe2 {
22 
23 namespace {
24 
25 // Since DispatchHelper doesn't support passing arguments through the call()
26 // method to DoRunWithType we have to create an object that will hold these
27 // arguments explicitly.
28 struct ProxyPrinter {
29  template <typename T>
30  bool DoRunWithType() {
31  tensorPrinter->Print<T>(*tensor);
32  return true;
33  }
34 
35  void Print() {
36  // Pulled in printable types from caffe2/core/types.cc
37  // Unfortunately right now one has to add them by hand
38  DispatchHelper<TensorTypes<
39  float,
40  int,
41  std::string,
42  bool,
43  uint8_t,
44  int8_t,
45  uint16_t,
46  int16_t,
47  int64_t,
48  double,
49  char>>::call(this, tensor->meta());
50  }
51 
52  const Tensor<CPUContext>* tensor;
53  TensorPrinter* tensorPrinter;
54 };
55 }
56 
57 SmartTensorPrinter::SmartTensorPrinter(const std::string& tensor_name)
58  : tensorPrinter_(tensor_name) {}
59 
60 SmartTensorPrinter::SmartTensorPrinter(
61  const std::string& tensor_name,
62  const std::string& file_name)
63  : tensorPrinter_(tensor_name, file_name) {}
64 
65 SmartTensorPrinter::SmartTensorPrinter(
66  const std::string& tensor_name,
67  const std::string& file_name,
68  int limit)
69  : tensorPrinter_(tensor_name, file_name, limit) {}
70 
71 void SmartTensorPrinter::Print(const Tensor<CPUContext>& tensor) {
72  ProxyPrinter printer;
73 
74  printer.tensor = &tensor;
75  printer.tensorPrinter = &tensorPrinter_;
76  printer.Print();
77 }
78 
79 SmartTensorPrinter& SmartTensorPrinter::DefaultTensorPrinter() {
80 // TODO(janusz): thread_local does not work under mac.
81 #if __APPLE__
82  CAFFE_THROW(
83  "SmartTensorPrinter does not work on mac yet due to thread_local.");
84 #else
85  static thread_local SmartTensorPrinter printer;
86  return printer;
87 #endif
88 }
89 
90 void SmartTensorPrinter::PrintTensor(const Tensor<CPUContext>& tensor) {
91  DefaultTensorPrinter().Print(tensor);
92 }
93 }
Copyright (c) 2016-present, Facebook, Inc.
Copyright (c) 2016-present, Facebook, Inc.