@@ -22,6 +22,7 @@ limitations under the License. */
22
22
23
23
#include " paddle/fluid/framework/feed_fetch_method.h"
24
24
#include " paddle/fluid/inference/api/api_impl.h"
25
+ #include " paddle/fluid/inference/api/helper.h"
25
26
#include " paddle/fluid/inference/api/timer.h"
26
27
#include " paddle/fluid/platform/profiler.h"
27
28
@@ -215,57 +216,20 @@ bool NativePaddlePredictor::SetFeed(const std::vector<PaddleTensor> &inputs,
215
216
template <typename T>
216
217
void NativePaddlePredictor::GetFetchOne (const framework::LoDTensor &fetch,
217
218
PaddleTensor *output) {
218
- std::vector<int > shape;
219
- auto dims_i = fetch.dims ();
220
- auto lod = fetch.lod ();
221
- const T *output_ptr = fetch.data <T>();
222
- auto num = fetch.numel ();
223
- std::vector<T> data;
224
- if (0 == lod.size ()) {
225
- std::copy (output_ptr, output_ptr + num, std::back_inserter (data));
226
- for (int j = 0 ; j < dims_i.size (); ++j) {
227
- shape.push_back (dims_i[j]);
228
- }
229
- } else {
230
- // for batch detection
231
- // image[0] -> output[0] shape {145, 6}
232
- // image[1] -> output[1] shape {176, 6}
233
- // then,
234
- // the batch output shape {321, 6}
235
- // the lod {{0, 145, 321}}
236
- // so we should append output[0] to {176, 6}
237
- size_t max_dim = 0 ;
238
- for (size_t j = 1 ; j < lod[0 ].size (); j++) {
239
- max_dim = std::max (max_dim, lod[0 ][j] - lod[0 ][j - 1 ]);
240
- }
241
- size_t common_dim = lod[0 ].back () == 0 ? 0 : num / lod[0 ].back ();
242
- if (max_dim > 0 ) {
243
- data.resize ((lod[0 ].size () - 1 ) * max_dim * common_dim, 0 );
244
- }
245
- for (size_t j = 1 ; j < lod[0 ].size (); j++) {
246
- size_t start = lod[0 ][j - 1 ] * common_dim;
247
- size_t end = lod[0 ][j] * common_dim;
248
- if (end > start) {
249
- std::copy (output_ptr + start, output_ptr + end,
250
- data.begin () + (j - 1 ) * max_dim * common_dim);
251
- }
252
- }
253
- shape.push_back (lod[0 ].size () - 1 );
254
- shape.push_back (max_dim);
255
- for (int j = 1 ; j < dims_i.size (); ++j) {
256
- shape.push_back (dims_i[j]);
257
- }
258
- }
259
-
260
- output->shape = shape;
261
- auto &buffer = output->data ;
262
- if (buffer.empty () || buffer.length () < sizeof (T) * data.size ()) {
263
- buffer.Resize (sizeof (T) * data.size ());
264
- }
265
- std::memcpy (buffer.data (), data.data (), sizeof (T) * data.size ());
266
- // copy LoD
267
- for (const auto &level : fetch.lod ()) {
268
- output->lod .emplace_back (level);
219
+ // set shape.
220
+ auto shape = framework::vectorize (fetch.dims ());
221
+ output->shape .assign (shape.begin (), shape.end ());
222
+ // set data.
223
+ const T *data = fetch.data <T>();
224
+ int num_elems = inference::VecReduceToInt (shape);
225
+ output->data .Resize (num_elems * sizeof (T));
226
+ // The fetched tensor output by fetch op, should always in CPU memory, so just
227
+ // copy.
228
+ memcpy (output->data .data (), data, num_elems * sizeof (T));
229
+ // set lod
230
+ output->lod .clear ();
231
+ for (auto &level : fetch.lod ()) {
232
+ output->lod .emplace_back (level.begin (), level.end ());
269
233
}
270
234
}
271
235
0 commit comments