Caffe2 - C++ API
A deep learning, cross platform ML framework
tensor_flatten.h
1 #pragma once
2 
3 #include <ATen/core/functional.h>
4 
5 #include <ATen/ATen.h>
6 #include <utility>
7 
8 namespace torch { namespace utils {
9 
10 inline at::Tensor flatten_dense_tensors(at::TensorList tensors) {
11  static auto flatten = [](const at::Tensor &t) { return t.contiguous().view({-1}); };
12  if (tensors.size() == 1)
13  return flatten(tensors[0]);
14  return at::cat(fmap(tensors, flatten));
15 }
16 
17 inline std::vector<at::Tensor> unflatten_dense_tensors(const at::Tensor& flat, at::TensorList tensors) {
18  std::vector<at::Tensor> outputs;
19  outputs.reserve(tensors.size());
20  size_t offset = 0;
21  for (const auto & tensor : tensors) {
22  auto numel = tensor.numel();
23  outputs.push_back(flat.narrow(0, offset, numel).view(tensor.sizes()));
24  offset += numel;
25  }
26  return outputs;
27 }
28 
29 
30 struct TensorGroup {
31  std::vector<at::Tensor> tensors;
32  size_t size = 0;
33 
34  at::Type& type() {
35  AT_ASSERT(!tensors.empty());
36  return tensors[0].type();
37  }
38 };
39 
40 // Helper function that takes a list of tensors and splits them into tensor
41 // groups by the size limit and outputs these tensor groups. If the input
42 // tensors are of different tensor types, they will be split into different
43 // groups as well.
44 //
45 // Two options of splitting provided to the user,
46 //
47 // Imagine the size_limit is 256 and the list of input tensors are:
48 // tensor_a(fp16 - 128 bytes),
49 // tensor_b(fp32 - 256 bytes),
50 // tensor_c(fp16 - 128 bytes),
51 //
52 // when fine_grained == false:
53 // The function will read the list of tensors sequentially and accumulate
54 // enough tensors for each data type until the size_limit, therefore:
55 // it will output: {{tensor_a, tensor_c}, {tensor_b}}
56 //
57 // when fine_grained == true:
58 // The function will read the list of tensors sequentially and accumulate
59 // enough tensors for all data types until the size_limit, and then split
60 // the accumulated tensors into different groups by data types, therefore:
61 // it will output: {{tensor_a}, {tensor_b}, {tensor_c}}
62 std::vector<TensorGroup> take_tensors(
63  at::TensorList tensors,
64  size_t size_limit,
65  bool fine_grained = false);
66 
67 void reorder_tensors_like(std::vector<at::Tensor>& tensors, at::TensorList order);
68 
69 std::pair<at::Tensor, at::Tensor> flatten_sparse_tensors(at::TensorList tensors);
70 
71 std::vector<at::Tensor> unflatten_sparse_tensors(
72  const at::Tensor& flat_indices,
73  const at::Tensor& flat_values,
74  at::TensorList tensors);
75 
76 }}
constexpr size_t size() const
size - Get the array size.
Definition: ArrayRef.h:138
Definition: jit_type.h:17
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41