Created by: Aurelius84
Required(必填, multiple choices, two at most)
-
PR type(PR 类型) is (A): A. New features(新功能)---------------- D. Performance optimization(性能优化) B. Bug fixes(问题修复)------------------ E. Breaking changes(向后不兼容的改变) C. Function optimization(功能优化)------F. Others(其它)
-
PR changes(改动点)is ( D): A. OPs(operators)---------------------- C. Docs(文档) B. APIs(接口)--------------------------- D. Others(其它)
-
Use one sentence to describe what this PR does.(简述本次PR的目的和改动)
This PR supports python
len
transformation in dy2stat.
Optional(选填, If None, please delete it)
- If you modified docs, please make sure that both Chinese and English docs were modified and provide a preview screenshot. (文档必填)
PR功能
selected_rows
(前置功能)
1. shape_op支持len
语法依赖shape_op
,之前此op不支持selected_rows
类型的variable,此PR对此进行了兼容性支持。
对于selected_rows
类型的variable,shape_op
返回其内部持有Tensor的shape。
# SelectedRows X的基本信息:
x_rows = [0, 1, 5, 4, 19]
height = 20
row_numel = 2
shape(X)=[5,2]
2. 动转静支持len语法
-
LoDTensor类型
动态图下为VarBase,动态执行时是通过var.numpy()[0]返回的len信息(底层重写了__len__)。静态图下考虑到
shape[0]
在较多场景下表示batch_size
,因此借助shape_op
动态从Variable获取,取shape[0]作为length -
LoDTensorArray类型
动态图下LoDTensorArray实际就是python的list类型。静态图下LoDTensorArray有单独的array_length方法,借助此api返回length
-
SelectedRows类型
动态图下没有直接暴露出来给用户,如上所述,升级了
shape_op
也支持对此类型调用len
-
python内建类型
返回和动态图一致的结果
Note: 动态图中len返回的均为int类型,转为静态图后,对于variable类型返回的是shape=[1]的Variable
shape中文文档PR:https://github.com/PaddlePaddle/FluidDoc/pull/2176