Skip to content

Conversation

@HydrogenSulfate
Copy link
Contributor

@HydrogenSulfate HydrogenSulfate commented Apr 23, 2024

PR Category

Inference

PR Types

Bug fixes

Description

Pcard-75624

Keep output order of exported inference model same with dynamic model.

import paddle



class Model(paddle.nn.Layer):
    def __init__(self):
        super().__init__()
        self.u = paddle.to_tensor(1.0)
        self.v = paddle.to_tensor(2.0)
        self.w = paddle.to_tensor(3.0)
        self.p = paddle.to_tensor(4.0)

    def forward(self):
        return {
            "u": self.u,
            "v": self.v,
            "w": self.w,
            "p": self.p,
        }


model = Model()

dy_output = model()
# print(list(dy_output.values()))

st_model = paddle.jit.to_static(model)
st_output = st_model()
# print(list(st_output.values()))

paddle.jit.save(st_model, "dy2st")
jit_load_model = paddle.jit.load("dy2st")
jit_output = jit_load_model()
print(list(jit_output)) # <--- here

before:

[Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       4.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       2.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       3.)]

after:

[Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       1.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       2.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       3.), Tensor(shape=[], dtype=float32, place=Place(gpu:0), stop_gradient=True,
       4.)]

@paddle-bot
Copy link

paddle-bot bot commented Apr 23, 2024

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

return sorted(dict_.keys())
return list(dict_.keys())
except TypeError:
raise TypeError("nest only supports dicts with sortable keys.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数是不是可以删了,下面 160 行直接调用 list(iterable.keys()) 就好,160 行上面的注释也可以删了,_sequence_like 同样

另外可以按照注释里所说的情况加个单测看看 map_structurepack_sequence_as 等 API 是否还符合预期,这个 flatten 也测一下吧

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个函数是不是可以删了,下面 160 行直接调用 list(iterable.keys()) 就好,160 行上面的注释也可以删了,_sequence_like 同样

另外可以按照注释里所说的情况加个单测看看 map_structurepack_sequence_as 等 API 是否还符合预期,这个 flatten 也测一下吧

已添加单测

Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以单独测一下 pack_sequence_asmap_structure 吧,这俩我更担心会有问题,很多地方都在使用这俩函数

# ordered and plain dicts (e.g., flattening a dict but using a
# corresponding `OrderedDict` to pack it back).
for key in _sorted(iterable):
# NOTE: Keep order unchanged as python dict is ordered since python3.7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3.6+ 吧?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已修改

@HydrogenSulfate HydrogenSulfate force-pushed the keep_flatten_order branch 2 times, most recently from e3eaab5 to 4dde7fc Compare April 24, 2024 07:01
Copy link
Member

@SigureMo SigureMo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMeow 🐾

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

Successfully merging this pull request may close these issues.

2 participants