-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[ Hackathon 3rd No.2 ] add paddle.iinfo #45321
Changes from 39 commits
204fd69
4008fc1
645247e
8da6c55
17e54e0
89ad9f8
3e4c1fc
dbea60b
802ad53
2d2ffd0
1cd38bd
5589aa8
c2ba90f
f67e3d7
e44f818
c5fa801
74ab8d5
9b318ab
94ef052
169add9
a3bb736
75484ec
4a724e9
145ee68
01e7c7f
b78dc08
f61de70
5c6f96f
ae1d259
eb54abe
0d352f5
dd92f76
afe4e96
cf221c9
05dd1a7
a013bdf
bbdb3b5
32ea2fa
623179f
9113d68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
OccupyMars2025 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import paddle | ||
import unittest | ||
import numpy as np | ||
|
||
|
||
class TestIInfoAndFInfoAPI(unittest.TestCase): | ||
|
||
def test_invalid_input(self): | ||
for dtype in [ | ||
paddle.float16, paddle.float32, paddle.float64, paddle.bfloat16, | ||
paddle.complex64, paddle.complex128, paddle.bool | ||
]: | ||
with self.assertRaises(ValueError): | ||
_ = paddle.iinfo(dtype) | ||
|
||
def test_iinfo(self): | ||
for paddle_dtype, np_dtype in [(paddle.int64, np.int64), | ||
(paddle.int32, np.int32), | ||
(paddle.int16, np.int16), | ||
(paddle.int8, np.int8), | ||
(paddle.uint8, np.uint8)]: | ||
xinfo = paddle.iinfo(paddle_dtype) | ||
xninfo = np.iinfo(np_dtype) | ||
self.assertEqual(xinfo.bits, xninfo.bits) | ||
self.assertEqual(xinfo.max, xninfo.max) | ||
self.assertEqual(xinfo.min, xninfo.min) | ||
self.assertEqual(xinfo.dtype, xninfo.dtype) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是进行整数的比较,用 "=="运算符就可以比较,感觉在这里还是可以用self.assertEqual |
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
# limitations under the License. | ||
|
||
from ..fluid.core import VarDesc | ||
from ..fluid.core import iinfo as core_iinfo | ||
|
||
dtype = VarDesc.VarType | ||
dtype.__qualname__ = "dtype" | ||
|
@@ -34,4 +35,37 @@ | |
|
||
bool = VarDesc.VarType.BOOL | ||
|
||
__all__ = [] | ||
|
||
def iinfo(dtype): | ||
""" | ||
|
||
paddle.iinfo is a function that returns an object that represents the numerical properties of | ||
an integer paddle.dtype. | ||
This is similar to `numpy.iinfo <https://numpy.org/doc/stable/reference/generated/numpy.iinfo.html#numpy-iinfo>`_. | ||
|
||
Args: | ||
dtype(paddle.dtype): One of paddle.uint8, paddle.int8, paddle.int16, paddle.int32, and paddle.int64. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 明确说明支持哪些类型 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改了 |
||
Returns: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
An iinfo object, which has the following 4 attributes: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 明确说明返回的对象包含哪些属性,针对每个属性,解释清楚其含义 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
- min: int, The smallest representable integer number. | ||
- max: int, The largest representable integer number. | ||
- bits: int, The number of bits occupied by the type. | ||
- dtype: str, The string name of the argument dtype. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
import paddle | ||
|
||
iinfo_uint8 = paddle.iinfo(paddle.uint8) | ||
print(iinfo_uint8) | ||
# paddle.iinfo(min=0, max=255, bits=8, dtype=uint8) | ||
print(iinfo_uint8.min) # 0 | ||
print(iinfo_uint8.max) # 255 | ||
print(iinfo_uint8.bits) # 8 | ||
print(iinfo_uint8.dtype) # uint8 | ||
|
||
""" | ||
return core_iinfo(dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我自己测试发现用 "oss <<"的方法才能正确显示浮点数,而 std::to_string() 会将很小的正浮点数显示为0