Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the FInplaceIdentity #2572

Merged
merged 2 commits into from
Feb 18, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion nnvm/src/pass/plan_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <nnvm/op_attr_types.h>
#include <nnvm/top/tensor.h>
#include <memory>
#include <set>
#include "graph_algorithm.h"

namespace nnvm {
Expand Down Expand Up @@ -39,6 +40,22 @@ static int GetDTypeSize(int type_flag) {
}
}

static bool BitwiseCompatibleTypes(int type1, int type2) {
static std::set<std::pair<int, int>> compatible_pairs { {kUint8, kInt8},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this set is designed for cast op. For others, it may not suitable. For example, saturate_cast from kUint8 to kInt8 will change trunk data. Relu from kInt8 to kUint8 will also break this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's hard to imagine relu and saturate_cast to advertise themselves as identites.
That said, I made this exception for types to accommodate your usecase as I understand it, if you don't need that then I can just replace the check with strict type check, that will also be fine (and slightly safer).

Copy link
Contributor

@ZhennanQin ZhennanQin Feb 8, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relu on uint8 and saturate_cast with same dtype are exactly the same cases as cast, as they are redundant operations. According to your approach, they should add identity attributes. But this will break the cases relu from int8 to uint8 and saturate_cast from uint8 to int8.

If replacing the check with strict type check, then identity can't help on casting from uint8 to int8. Also, relu on fp32 will have trouble.

Overall, you can't find a unified relu here to cover all cases. A graph pass is a better solution as it can see the operator and make different relu for each operator.

{kUint16, kInt16},
{kUint32, kInt32},
{kUint64, kInt64} };
if (type1 == type2) {
return true;
}
if (compatible_pairs.find(std::make_pair(type1, type2)) != compatible_pairs.end() ||
compatible_pairs.find(std::make_pair(type2, type1)) != compatible_pairs.end()) {
return true;
}

return false;
}

// simple graph based allocator.
class GraphAllocator {
public:
Expand Down Expand Up @@ -218,10 +235,15 @@ size_t AllocMemory(const Graph& ret, const IndexedGraph& idx,
bool ignore_all_inputs = (fignore_inputs.count(inode.source->op()) != 0 &&
fignore_inputs[inode.source->op()](
inode.source->attrs).size() == inode.source->num_inputs());
// Identity should only be true if shape.Size() match and type
// is bitwise compatible
bool real_identity = identity[ipair] &&
shape_vec[eid_out].Size() == shape_vec[eid_in].Size() &&
BitwiseCompatibleTypes(dtype_vec[eid_out], dtype_vec[eid_in]);
if (taken[kv.first] == false &&
sid_out == GraphAllocator::kBadStorageID &&
sid_in >= 0 &&
((storage_ref_count[sid_in] == 1 && !ignore_all_inputs) || identity[ipair]) &&
((storage_ref_count[sid_in] == 1 && !ignore_all_inputs) || real_identity) &&
entry_ref_count[eid_out] > 0 &&
shape_vec[eid_out].Size() == shape_vec[eid_in].Size() &&
(dtype_vec[eid_out] == dtype_vec[eid_in] ||
Expand Down