Skip to content

Commit 5c073a4

Browse files
authored
fix transfer cache thread_local bug (#14581)
1 parent 87332bb commit 5c073a4

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

paddle/fluid/framework/transfer_scope_cache.cc

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,28 @@
1717
namespace paddle {
1818
namespace framework {
1919

20+
// Holds all the transfer scope across the process.
2021
std::unordered_map<size_t, Scope*>& global_transfer_data_cache() {
21-
thread_local auto* x = new std::unordered_map<size_t, Scope*>;
22+
typedef std::unordered_map<size_t, Scope*> map_t;
23+
thread_local std::unique_ptr<map_t> x(new map_t);
2224
return *x;
2325
}
2426

27+
// Holds all the transfer scope for this thread.
2528
std::unordered_set<Scope*>& global_transfer_scope_cache() {
26-
thread_local auto* x = new std::unordered_set<Scope*>;
29+
typedef std::unordered_set<Scope*> set_t;
30+
thread_local std::unique_ptr<set_t> x(new set_t);
2731
return *x;
2832
}
2933

34+
// Try to create a transfer scope. If one cached scope has match the
35+
// requirement, just return that one.
36+
// Inputs:
37+
// @type0: the source kernel type.
38+
// @type1: the target kernel type.
39+
// @scope: the execution scope of this op.
40+
// Returns: A scope used to hold the transfer data across the different kernel
41+
// type.
3042
Scope* TryCreateTransferScope(OpKernelType type0, OpKernelType type1,
3143
const Scope* scope) {
3244
Scope* new_scope{nullptr};
@@ -46,27 +58,5 @@ Scope* TryCreateTransferScope(OpKernelType type0, OpKernelType type1,
4658
return new_scope;
4759
}
4860

49-
void RemoveKidsFromTransferScopeCache(Scope* scope) {
50-
auto it = global_transfer_scope_cache().find(scope);
51-
if (it != global_transfer_scope_cache().end()) {
52-
global_transfer_scope_cache().erase(it);
53-
}
54-
for (auto* s : scope->kids()) {
55-
auto it = global_transfer_scope_cache().find(s);
56-
if (it != global_transfer_scope_cache().end()) {
57-
global_transfer_scope_cache().erase(it);
58-
}
59-
}
60-
61-
// remove global transfer data cache
62-
auto& cache = global_transfer_data_cache();
63-
for (auto it = cache.begin(); it != cache.end();) {
64-
if (it->second == scope)
65-
it = cache.erase(it);
66-
else
67-
it++;
68-
}
69-
}
70-
7161
} // namespace framework
7262
} // namespace paddle

0 commit comments

Comments
 (0)