|
|
|
@ -14,9 +14,11 @@
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <unordered_set>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "paddle/fluid/framework/lod_tensor_array.h"
|
|
|
|
|
#include "paddle/fluid/framework/scope.h"
|
|
|
|
|
#include "paddle/fluid/framework/variable.h"
|
|
|
|
|
|
|
|
|
|
namespace paddle {
|
|
|
|
|
namespace details {
|
|
|
|
@ -24,13 +26,28 @@ namespace details {
|
|
|
|
|
// Clean the TensorArray each batch to make the behavior the same with the
|
|
|
|
|
// training phase.
|
|
|
|
|
struct TensorArrayBatchCleaner {
|
|
|
|
|
TensorArrayBatchCleaner() {
|
|
|
|
|
valid_types_.insert(typeid(framework::Tensor));
|
|
|
|
|
valid_types_.insert(typeid(framework::LoDTensor));
|
|
|
|
|
}
|
|
|
|
|
// Collect the variables that are not Tensor or LoDTensor, and reset them to a
|
|
|
|
|
// bool(trick), because some of them are containers, and some operators just
|
|
|
|
|
// keep inserting new items without clearing the containers first; So the
|
|
|
|
|
// memory grow larger and larger in inference service deployed online.
|
|
|
|
|
void CollectNoTensorVars(framework::Scope *scope);
|
|
|
|
|
void ResetNoTensorVars();
|
|
|
|
|
|
|
|
|
|
// Fix the tensor array not clear in the inference scenarios.
|
|
|
|
|
void CollectTensorArrays(framework::Scope *scope);
|
|
|
|
|
void ResetTensorArray();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool flag_{true};
|
|
|
|
|
bool no_tensor_flag_{true};
|
|
|
|
|
std::vector<framework::LoDTensorArray *> arrays_;
|
|
|
|
|
|
|
|
|
|
std::unordered_set<std::type_index> valid_types_;
|
|
|
|
|
std::unordered_set<framework::Variable *> no_tensor_vars_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace details
|
|
|
|
|