Skip to content

Conversation

@masahi
Copy link
Member

@masahi masahi commented Oct 6, 2022

When we use StructuralEqual/Hash, the actual equality testing / hashing work are deferred to RemapVarSEqualHandler and VarCountingSHashHandler class respectively, defined in .cc files. To customize equality / hash behavior, I want to be able to derive from these classes outside of structural_equal.cc or structural_hash.cc. This is the first step toward replacing my previous attempt #12706 to allow ignoring NDArray raw data in StructuralEqual/Hash.

So I propose to expose them as the "default" handler, while still hiding their implementation details. This lets me define a custom hasher that ignores NDArray content simply by:

class SHashHandlerIgnoreNDArray : public SHashHandlerDefault {
 protected:
  void DispatchSHash(const ObjectRef& object, bool map_free_vars) override {
    ICHECK(object.defined());
    if (auto ndarray = object.as<runtime::NDArray::Container>()) {
      SHashReducer hash_reduce(this, map_free_vars);
      NDArrayHash(ndarray, &hash_reduce, false);
    } else {
      SHashHandlerDefault::DispatchSHash(object, map_free_vars);
    }
  }
};

cc @tqchen @junrushao

@junrushao
Copy link
Member

Would love to have @tqchen's thoughts on this :-)

@tqchen
Copy link
Member

tqchen commented Oct 8, 2022

seems to be a fine change, would be good to get another pair of eyes for reviewing

Copy link
Member

@junrushao junrushao left a comment

Choose a reason for hiding this comment

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

LGTM

@masahi
Copy link
Member Author

masahi commented Oct 10, 2022

@tqchen good to merge?

@junrushao junrushao merged commit aedbe45 into apache:main Oct 10, 2022
@junrushao
Copy link
Member

Sorry for the delay!

junrushao pushed a commit that referenced this pull request Oct 17, 2022
…ng NDArray raw data (#13091)

A follow up to #13050, also builds on #13001. This PR enables the functionality in #12706 without changing the existing `StructuralEqual/Hash`.

A question for discussion: Should this be the default ModuleEquality used by MS? It has no effect for the `link-params = False` case, and it simplifies the MS tuning API usage for the `link-params = True` case (Hexagon etc).
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 10, 2022
…ng NDArray raw data (apache#13091)

A follow up to apache#13050, also builds on apache#13001. This PR enables the functionality in apache#12706 without changing the existing `StructuralEqual/Hash`.

A question for discussion: Should this be the default ModuleEquality used by MS? It has no effect for the `link-params = False` case, and it simplifies the MS tuning API usage for the `link-params = True` case (Hexagon etc).
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
…pache#13001)

When we use `StructuralEqual/Hash`, the actual equality testing / hashing work are deferred to `RemapVarSEqualHandler` and `VarCountingSHashHandler` class respectively, defined in `.cc` files. To customize equality / hash behavior, I want to be able to derive from these classes outside of `structural_equal.cc` or `structural_hash.cc`. This is the first step toward replacing my previous attempt apache#12706 to allow ignoring NDArray raw data in `StructuralEqual/Hash`.

So I propose to expose them as the "default" handler, while still hiding their implementation details. This lets me define a custom hasher that ignores NDArray content simply by:

```
class SHashHandlerIgnoreNDArray : public SHashHandlerDefault {
 protected:
  void DispatchSHash(const ObjectRef& object, bool map_free_vars) override {
    ICHECK(object.defined());
    if (auto ndarray = object.as<runtime::NDArray::Container>()) {
      SHashReducer hash_reduce(this, map_free_vars);
      NDArrayHash(ndarray, &hash_reduce, false);
    } else {
      SHashHandlerDefault::DispatchSHash(object, map_free_vars);
    }
  }
};
```
xinetzone pushed a commit to daobook/tvm that referenced this pull request Nov 25, 2022
…ng NDArray raw data (apache#13091)

A follow up to apache#13050, also builds on apache#13001. This PR enables the functionality in apache#12706 without changing the existing `StructuralEqual/Hash`.

A question for discussion: Should this be the default ModuleEquality used by MS? It has no effect for the `link-params = False` case, and it simplifies the MS tuning API usage for the `link-params = True` case (Hexagon etc).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants