-
Notifications
You must be signed in to change notification settings - Fork 825
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
Use symbol dtype #5641
Merged
Merged
Use symbol dtype #5641
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lixinqi
reviewed
Jul 29, 2021
lixinqi
approved these changes
Jul 30, 2021
Speed stats:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
在api层面,使用Symbol DType
目前已知的一个问题
单纯使用Symbol会导致,即使在x.dtype = oneflow.float32的情况下,id(x.dtype)!=id(oneflow.float32)
这个问题在之前的DType*版本并没有。对比了其他框架,paddle也存在类似的问题,而pytorch没有
houjiang的解决方案
(特别感谢houjiang跟我大晚上结对编程)
在api/python/framework/tensor.cpp做如下修改
在api/python/framework/dtype.cpp中,将返回值修改为 std::shared_ptr<Symbol>
并且导出类型修改为
直接使用Get来返回Symbol对象,并取其地址,以保证对象唯一性。
仅仅使用引用,可能在别的地方存在拷贝,导致前面的问题
而在python_args.cpp这里,我们将对象转换为Symbol*
代码走读
DType::data_type 构造函数
现在的DType的构造函数,是由一个宏,来去构造一个Symbol对象
这个宏传入一个data_type,比如是Float32,那么底下定义的构造函数,会返回一个const引用
DType::Get 函数
该函数构造了一个静态的Hashmap,使用的时候我们可以直接调用Get来查找,避免一次次构造对象,也能保证唯一性
它的key是 DataType::kxxx,对应的是DType::xxx()构造函数返回的对象。以Float32为例子,它的键值对是:
DataType::kFloat32 : DType::Float32()
使用方法
现在在Tensor内使用的都是
Symbol<DType>
,在其他底层实现依旧保持原有DataType而Tensor的dtype方法返回的也改为Symbol,如果想从Tensor取得DataType,可以:
x->dtype()->data_type();