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

module 'numpy' has no attribute 'asscalar' #176

Closed
zhangluustb opened this issue Jul 5, 2022 · 3 comments
Closed

module 'numpy' has no attribute 'asscalar' #176

zhangluustb opened this issue Jul 5, 2022 · 3 comments

Comments

@zhangluustb
Copy link

File "/usr/local/lib/python3.8/dist-packages/ppq-0.6.4-py3.8.egg/ppq/parser/util.py", line 27, in convert_value
value = np.asscalar(value[0])
File "/usr/local/lib/python3.8/dist-packages/numpy/init.py", line 311, in getattr
raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'asscalar'

numpy version 1.23.0

@ZhangZhiPku
Copy link
Collaborator

听说是numpy新版中移除了这个函数:

numpy.asscalar(a)
将大小为1的数组转换为其标量等效值。

1.16 版后已移除: 弃用,使用 numpy.ndarray.item() 相反。

@ZhangZhiPku
Copy link
Collaborator

问题将在下一次更新中修复,你可以使用下面的写法替换 util.py

def convert_value(
    value: Union[int, float, np.ndarray, torch.Tensor],
    export_as_float: bool, dtype: DataType = DataType.FP32) -> Union[float, list]:
    """Converting value from any to python native data dtype, ready for export.

    Args:
        value (Union[int, float, np.ndarray, torch.Tensor]): exporting value.
        export_as_list (bool): export value as a list.
        dtype (DataType, optional): exporting dtype.

    Returns:
        Union[float, list]: Converted value
    """
    if dtype not in {DataType.FP32, DataType.INT32}:
        raise ValueError(f'Can Only export dtype fp32 and int32, '
                         f'while you are requiring to dump a {dtype.name} value')
    value = convert_any_to_numpy(value, accepet_none=False)
    value = value.astype(dtype=DataType.to_numpy(dtype))
    if export_as_float:
        value = value[0].item()
        assert type(value) in {int, float}, (
            f'Trying to dump a tensorwise quantization value {value}. '
            f'It is Expected to be a int or float value, while {type(value)} was given')
        return value
    else:
        value = convert_any_to_numpy(value, accepet_none=False)
        return value.tolist()

@ZhangZhiPku
Copy link
Collaborator

问题已经在0.6.5的更新中修复

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

No branches or pull requests

2 participants