1 #include "caffe2/core/tensor.h" 3 #include "caffe2/core/blob_stats.h" 7 CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(12,
Tensor);
9 TensorPrinter::TensorPrinter(
10 const std::string& tensor_name,
11 const std::string& file_name,
13 : to_file_(!file_name.empty()),
14 limit_(limit ? limit : k_limit_default_),
15 tensor_name_(tensor_name) {
19 log_file_.reset(
new std::ofstream(
20 file_name, std::ofstream::out | std::ofstream::trunc));
23 "Failed to open TensorPrinter file ",
26 log_file_->rdstate());
30 TensorPrinter::~TensorPrinter() {
31 if (log_file_.get()) {
36 void TensorPrinter::PrintMeta(
const Tensor& tensor) {
38 (*log_file_) << MetaStr(tensor) << std::endl;
40 LOG(INFO) << MetaStr(tensor);
44 std::string TensorPrinter::MetaStr(
const Tensor& tensor) {
45 std::stringstream meta_stream;
46 meta_stream <<
"Tensor " << tensor_name_ <<
" of type " 47 << tensor.dtype().name() <<
". Dims: (";
48 for (
const auto dim : tensor.sizes()) {
49 meta_stream << dim <<
",";
52 return meta_stream.str();
55 TypeMeta GetTensorType(
const void* c) {
61 static CaffeMap<TypeIdentifier, TypeCall> type_call_registry_{
62 {TypeMeta::Id<Tensor>(), GetTensorType}};
64 TypeCall GetTypeCallFunction(TypeIdentifier
id) {
65 auto f = type_call_registry_.find(
id);
66 if (f == type_call_registry_.end()) {
72 void RegisterTypeCallFunction(TypeIdentifier
id, TypeCall c) {
73 type_call_registry_[id] = c;
78 vector<int64_t> GetTensorInfo(
81 DeviceOption* device) {
85 CHECK(tc->unsafeGetTensorImpl());
86 CHECK(tc->unsafeGetTensorImpl()->storage().unsafeGetStorageImpl());
87 *capacity = tc->storage().capacity();
88 ExtractDeviceOption(device, tc->GetDevice());
89 return tc->sizes().vec();
93 static CaffeMap<TypeIdentifier, TensorInfoCall> tensor_info_call_registry_{
94 {TypeMeta::Id<Tensor>(), GetTensorInfo}};
98 TensorInfoCall GetTensorInfoFunction(TypeIdentifier
id) {
99 auto f = tensor_info_call_registry_.find(
id);
100 if (f == tensor_info_call_registry_.end()) {
106 void RegisterTensorInfoFunction(TypeIdentifier
id, TensorInfoCall c) {
107 tensor_info_call_registry_[id] = c;
110 void TensorVectorResize(
111 std::vector<Tensor>& tensors,
114 tensors.reserve(size);
115 for (
auto i = 0; i < size; ++i) {
116 tensors.emplace_back(type);
123 tensor.raw_mutable_data(options.
dtype());
131 CAFFE_ENFORCE(options.
device_opt() != c10::nullopt);
137 if (tensor->GetDeviceType() == options.
device().type()) {
138 if (tensor->sizes() != dims) {
140 tensor->Resize(dims);
142 if (tensor->dtype() == options.
dtype()) {
143 tensor->raw_mutable_data();
145 C10_LOG_EVERY_MS(WARNING, 1000)
146 <<
"Changing the data type of Tensor is discouraged." 147 <<
" Attempt to change data type from: " << tensor->dtype()
148 <<
" to: " << options.
dtype();
150 *tensor = caffe2::empty(dims, options);
157 VLOG(1) <<
"Create new mutable object " << TypeMeta::TypeName<Tensor>()
158 <<
" dims: " << dims;
159 *tensor = caffe2::empty(dims, options);
162 void ReinitializeAndCopyFrom(
167 auto device_type = options.
device().type();
168 CAFFE_ENFORCE(t !=
nullptr,
"Target tensor ptr is null.");
169 if (!*t || device_type != t->GetDeviceType()) {
173 !t->dtype_initialized() || t->dtype() == src.dtype(),
174 "We don't allow a change of data type in ReinitializeAndCopyFrom. Attempt to " 179 t->CopyFrom(src, async);
182 void Tensor::enforce_invariants() {
183 if (impl_.get() ==
nullptr) {
184 throw std::runtime_error(
"TensorImpl with nullptr is not supported");
187 !impl_->is_variable(),
188 "Caffe2 tensor wrapper doesn't support autograd variables");
192 "Caffe2 tensor wrapper supports only regular non-sparse tensors");
194 impl_->is_contiguous(),
195 "Caffe2 tensor wrapper supports only contiguous tensors");
201 size_t sizeBytes(
const Blob& blob)
const override {
203 auto nbytes = tensor.nbytes();
204 if (nbytes > 0 && tensor.IsType<std::string>()) {
205 const auto* data = tensor.data<std::string>();
206 for (
int i = 0; i < tensor.numel(); ++i) {
207 nbytes += data[i].size();
213 REGISTER_BLOB_STAT_GETTER(
Tensor, TensorStatGetter);
Blob is a general container that hosts a typed pointer.
C10_NODISCARD TensorOptions device(c10::optional< Device > device) const noexcept
Return a copy of TensorOptions with device set to the given one, or cleared if device is nullopt...
void ReinitializeTensor(Tensor *tensor, at::IntArrayRef dims, at::TensorOptions options)
Reinitialize a Tensor to given dims and options if necessary, note that this will not do anything if ...
c10::optional< Device > device_opt() const noexcept
Returns the device of the TensorOptions, or c10::nullopt if device is not specified.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
int GetGPUIDForPointer(const void *ptr)
Gets the GPU id that the current pointer is located at.
C10_NODISCARD TensorOptions dtype(c10::optional< caffe2::TypeMeta > dtype) const noexcept
Return a copy of TensorOptions with dtype set to the given one.
const T & Get() const
Gets the const reference of the stored object.