[Tensor] add Parameter inheritance for ColoParameter #1041
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
实验了一下钻石继承。
由于mro机制的存在,Python的多继承中所有通过super()调用的子类的方法都是通过对类的mro列表使用bfs遍历,保证只调用一次,只需要多继承顺序中将ColoTensor写在Parameter左边即可。这里的test()方法加在了Parameter和ColoTensor中,ColoParameter没有overload这个方法,可以看到因为默认的overload是super().xxx,确实是ColoTensor被调用了而Parameter没有调用。
然后是init时候的对象属性冗余测试。init时候我们没有调用super而是直接使用torch的make_subclass,可以看到init只执行ColoParameter的方法,也不会重复去调用Parameter的init方法。
目前看下来这样写是没有副作用的。
一个简单的example。
而Python钻石继承会出现多次调用问题的场景如下:
只有显式指定调用多个来自多继承的方法才会重复调用。