pytorch  1.8.2
About: PyTorch provides Tensor computation (like NumPy) with strong GPU acceleration and Deep Neural Networks (in Python) built on a tape-based autograd system. LTS (Long Term Support) release.
  Fossies Dox: pytorch-1.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pack_segments.cc
Go to the documentation of this file.
2
3namespace caffe2 {
4
5template <>
6template <typename T>
8 return DispatchHelper<
10 T>::call(this, Input(DATA));
11}
12
13template <>
14template <typename T, typename Data_T>
16 const auto& data = Input(DATA);
17 const auto& lengths = Input(LENGTHS);
18
19 Tensor* presence_mask = nullptr;
20 if (return_presence_mask_) {
22 }
23
24 CAFFE_ENFORCE_GE(data.dim(), 1, "DATA should be at least 1-D");
25 CAFFE_ENFORCE_EQ(lengths.dim(), 1, "LENGTH should be 1-D");
26
27 // Find the length of the longest sequence.
28 const T* l = lengths.template data<T>();
29 T max_length = 0;
30 int64_t total_length = 0;
31 for (T i = 0; i < lengths.size(0); ++i) {
32 max_length = std::max(max_length, l[i]);
33 total_length += l[i];
34 }
35 if (max_length_ != -1) {
36 max_length = max_length_;
37 }
38
39 // Total lengths must be the same as data.dims(0)
41 data.size(0),
42 total_length,
43 " PackSegments requires that the sum of the lengths ",
44 total_length,
45 " is equal to the first data dimension ",
46 data.size(0));
47
48 auto shape =
49 data.sizes().vec(); // Shape of output is batch_size x max_len x ...
50 shape[0] = max_length;
51 shape.insert(shape.begin(), lengths.numel());
52 auto* output = Output(0, shape, at::dtype(data.dtype()));
53
54 // create output tensor
55 auto* out = static_cast<char*>(output->raw_mutable_data(data.dtype()));
56
57 bool* presence_mask_data = nullptr;
58 if (return_presence_mask_) {
59 // Shape of presence is batch_size x max_len
60 std::vector<int64_t> presence_shape{lengths.numel(), max_length};
61 presence_mask->Resize(presence_shape);
62 presence_mask_data = presence_mask->template mutable_data<bool>();
63 }
64
65 if (!data.size(0)) {
66 // Return empty output (with the proper shape)
67 return true;
68 }
69
70 // Do padding
71 // Ignore string since math::Set does not support string.
72 // For all other cases, the behavior should mimic the GPU version where the
73 // padding is always zero for types other than float.
74 // TODO(xinyizhang): potentially restructure to clean up the logic here.
75 if (output->template IsType<float>()) {
77 output->numel(),
78 padding_,
79 output->template mutable_data<float>(),
80 &context_);
81 } else if (output->template IsType<int32_t>()) {
82 math::Set<int32_t, CPUContext>(
83 output->numel(),
84 0,
85 output->template mutable_data<int32_t>(),
86 &context_);
87 } else if (output->template IsType<int64_t>()) {
88 math::Set<int64_t, CPUContext>(
89 output->numel(),
90 0,
91 output->template mutable_data<int64_t>(),
92 &context_);
93 } else if (output->template IsType<char>()) {
95 output->numel(), 0, output->template mutable_data<char>(), &context_);
96 }
97 if (return_presence_mask_) {
98 memset(presence_mask_data, (int)false, presence_mask->numel());
99 }
100
101 auto block_size = data.size_from_dim(1);
102 auto block_bytesize = data.itemsize() * block_size;
103 const auto* d = static_cast<const char*>(data.raw_data());
104 int64_t start = 0;
105 for (int64_t i = 0; i < lengths.size(0); ++i) {
106 auto len = l[i] <= max_length ? l[i] : max_length;
108 data.dtype(),
109 len * block_size,
110 d + block_bytesize * start,
111 out + block_bytesize * max_length * i);
112 if (return_presence_mask_) {
113 memset(presence_mask_data + max_length * i, (int)true, len);
114 }
115 start += l[i];
116 }
117
118 return true;
119}
120
121template <>
122template <typename T>
124 return DispatchHelper<
126 T>::call(this, Input(DATA));
127}
128
129template <>
130template <typename T, typename Data_T>
132 const auto& data = Input(DATA);
133 const auto& lengths = Input(LENGTHS);
134 auto* output = Output(0);
135
136 CAFFE_ENFORCE_GE(data.dim(), 2, "DATA should be at least 2-D");
137 CAFFE_ENFORCE_EQ(lengths.dim(), 1, "LENGTH should be 1-D");
138 if (max_length_ != -1) {
140 max_length_,
141 data.size(1),
142 "max_length should be equal to the second dimension of the packed segments");
143 }
144 const T* l = lengths.template data<T>();
145
146 int64_t total_l = 0;
147 if (max_length_ != -1) {
148 for (int64_t i = 0; i < lengths.size(0); ++i) {
149 total_l += (int64_t)(l[i] <= max_length_ ? l[i] : max_length_);
150 }
151 } else {
152 total_l = std::accumulate(l, l + lengths.size(0), (int64_t)0);
153 }
154
155 auto shape = data.sizes().vec();
157 shape[0], lengths.size(0), "LENGTH should match DATA in dimension 0");
158 shape.erase(shape.begin());
159 shape[0] = total_l;
160 output->Resize(shape);
161 // create output tensor
162 auto* out = static_cast<char*>(output->raw_mutable_data(data.dtype()));
163 if (!(data.size(0) && data.size(1))) {
164 return true;
165 }
166 auto block_size = data.size_from_dim(2);
167 auto block_bytesize = data.itemsize() * block_size;
168 const auto* d = static_cast<const char*>(data.raw_data());
169 int64_t start = 0;
170 for (int64_t i = 0; i < lengths.size(0); ++i) {
171 auto len = l[i];
172 if (max_length_ != -1 && l[i] > max_length_) {
173 len = max_length_;
174 }
176 data.dtype(),
177 len * block_size,
178 d + block_bytesize * data.size(1) * i,
179 out + block_bytesize * start);
180 start += len;
181 }
182 return true;
183}
184
187
188OPERATOR_SCHEMA(PackSegments)
189 .NumInputs(2)
190 .NumOutputs(1, 2)
191 .SetDoc(
192 "Map N dim tensor to N+1 dim based on length blob. Sequences that \
193 are shorter than the longest sequence are padded with zeros.")
194 .Input(
195 0,
196 "lengths",
197 "1-d int/long tensor contains the length in each of the output.")
198 .Input(1, "tensor", "N dim Tensor.")
199 .Output(
200 0,
201 "packed_tensor",
202 "N + 1 dim Tensor"
203 "where dim(1) is the max length"
204 ", dim(0) is the batch size.")
205 .Output(
206 1,
207 "presence_mask",
208 "2 dim boolean tensor"
209 ", false where packed_tensor is padded, true otherwise.")
210 .Arg("max_length", "The pre-defined max_length for the packed segments")
211 .Arg(
212 "pad_minf",
213 "Padding number in the packed segments. Use true to pad \
214 -infinity, otherwise pad zeros")
215 .Arg(
216 "return_presence_mask",
217 "bool whether to return presence mask, false by default");
218OPERATOR_SCHEMA(UnpackSegments)
219 .NumInputs(2)
220 .NumOutputs(1)
221 .SetDoc("Map N+1 dim tensor to N dim based on length blob")
222 .Input(
223 0,
224 "lengths",
225 "1-d int/long tensor contains the length in each of the input.")
226 .Input(1, "tensor", "N+1 dim Tensor.")
227 .Output(0, "packed_tensor", "N dim Tensor")
228 .Arg("max_length", "The pre-defined max_length for the packed segments");
229
232 vector<OperatorDef> GetGradientDefs() override {
233 return SingleGradientDef(
234 "UnpackSegments",
235 "",
236 vector<string>{I(0), GO(0)},
237 vector<string>{GI(1)});
238 }
239};
241
244 vector<OperatorDef> GetGradientDefs() override {
245 return SingleGradientDef(
246 "PackSegments", "", vector<string>{I(0), GO(0)}, vector<string>{GI(1)});
247 }
248};
250} // namespace caffe2
251
253 PackSegments,
254 "_caffe2::PackSegments("
255 "Tensor lengths, "
256 "Tensor tensor, "
257 "int max_length = -1, "
258 "bool pad_minf = False, "
259 "bool return_presence_mask = False"
260 ") -> (Tensor packed_tensor, Tensor presence_mask)",
262
264 UnpackSegments,
265 "_caffe2::UnpackSegments("
266 "Tensor lengths, "
267 "Tensor tensor, "
268 "int max_length = -1"
269 ") -> (Tensor packed_tensor)",
#define CAFFE_ENFORCE_GE(x, y,...)
Definition: Logging.h:261
void CopyItemsSameDevice(const caffe2::TypeMeta meta, size_t n, const void *src, void *dst)
Definition: context_base.h:106
vector< OperatorDef > GetGradientDefs() override
vector< OperatorDef > GetGradientDefs() override
GradientMakerBase(const OperatorDef &def, const vector< GradientWrapper > &g_output)
static vector< OperatorDef > SingleGradientDef(const Args &... args)
a helper function to allow one to create one single operator def, which is usually the case for many ...
int call(int id)
CPUContext * context_
constexpr Symbol len(static_cast< unique_t >(_keys::aten_len))
C10_EXPORT void Set< char, CPUContext >(const std::int64_t N, const char alpha, char *Y, CPUContext *)
Definition: elementwise.cc:527
C10_EXPORT void Set< float, CPUContext >(const std::int64_t N, const float alpha, float *Y, CPUContext *)
Definition: elementwise.cc:520
Copyright (c) 2016-present, Facebook, Inc.
Definition: blob.h:13
REGISTER_CPU_OPERATOR(ATen, ATenOp< CPUContext >)
OPERATOR_SCHEMA(ATen)
CAFFE_ENFORCE_EQ(in.size(), 1, "New shape must not be specified by the input blob and the " "argument `shape` at the same time.")
the other dimension is proportionally scaled Defaults to Whether or not to mirror the image Defaults to Vector of means per color Standard deviation by which to normalize color channels Defaults to Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults Bounding box coordinate Defaults if the input is in Caffe format Defaults to Number of CPU decode transform threads Defaults to Name of the Type of The sizes of any outputs besides the data and shortest side desired for image resize Defaults to[-1, -1] or no random resize desired data
NumInputs(2) .NumOutputs(1) .SetDoc(R"DOC( Broadcast the input tensor to a materialized new tensor using given shape. Broadcast rule is similar to "numpy.array(input) *numpy.ones(shape)" Two corresponding dimensions must have the same or one of them equals to In order to align with PyTorch s shape is allowed to have entries equal which means to preserve the size of the corresponding dimension in shape
Definition: expand_op.cc:37
d int long tensor contains the length in each of the output N dim Tensor where presence_mask
float T
Definition: cc_bmm_bg_op.h:11
SparseLengths8BitsRowwiseOp< CPUContext, 0, 1 >::LENGTHS uint8 tensor obtained with Vector with the same sum of elements as the first dimension of DATA Input(3, "scale_bias", "Matrix of floats, each row r_i of which stores a pair " "s_i, b_i -- scale and bias for i-th row") .Output(0
Maximum number of candidates to carry over to next activation step float Tensor sized[max_activation_length, batch_size, alphabet_size] of network optional int vector containing sequence lengths
true SparseLengthsFused4BitRowwiseFakeFP16Op< CPUContext, true >::WEIGHTS uint8 tensor obtained with Vector with the same sum of elements as the first dimension of DATA output
this op outputs a copy of the input tensor where values from the height and width dimensions are moved to the batch dimension After the zero padding is according to the pad both height and width of the input must be divisible by the block_size Only NCHW order is currently supported Github block_size
*type depends on dtype
required base learning rate default used only for inv policy type default sampling rate on iterations default True in alter policy int64_t
false SparseLengthsFused4BitRowwiseFakeFP16Op< CPUContext, false >::LENGTHS DATA
REGISTER_GRADIENT(CTC, GetCTCGradient)
false SparseLengthsFused4BitRowwiseFakeFP16Op< CPUContext, false >::LENGTHS uint8 tensor obtained with LENGTHS
SparseLengths8BitsRowwiseOp< CPUContext, 1, 1 >::LENGTHS uint8 tensor obtained with Integer vector containing indices of the first dimension of DATA for the slices that are being aggregated Matrix of each row r_i of which stores a pair b_i scale and bias for i th row Output(0, "output", "output")
c10::BFloat16 max(c10::BFloat16 a, c10::BFloat16 b)
Definition: BFloat16-math.h:33
static void accumulate(std::vector< Variable > &buffer, const size_t pos, Variable &&var)
C10_EXPORT_CAFFE2_OP_TO_C10_CPU(PackSegments, "_caffe2::PackSegments(" "Tensor lengths, " "Tensor tensor, " "int max_length = -1, " "bool pad_minf = False, " "bool return_presence_mask = False" ") -> (Tensor packed_tensor, Tensor presence_mask)", caffe2::PackSegmentsOp< caffe2::CPUContext >)