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

[BUG] i64数据经pnnx存档后,无法在forward里正确输出 #5880

Open
Baiyuetribe opened this issue Jan 18, 2025 · 2 comments
Open

[BUG] i64数据经pnnx存档后,无法在forward里正确输出 #5880

Baiyuetribe opened this issue Jan 18, 2025 · 2 comments

Comments

@Baiyuetribe
Copy link
Contributor

复现步骤

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
    def forward(self, x):
        i32 = torch.tensor([2, 1, 3, 4], dtype=torch.int32)
        i64 = torch.tensor([2, 1, 3, 4], dtype=torch.int64)
        return i32, i64

pnnx demo.pt inputshape=[32]
经过上述转换,ncnn.param内容为

7767517
2 2
MemoryData               pnnx_fold_11             0 1 out0 0=4
MemoryData               pnnx_fold_22             0 1 out1 0=4

ncnn.bin文件内容为

Image

最终,在cpp中提取时,i64数据输出错误:
int|int32_t ---> [2,0,1,0]
int64_t ---> [2,1,2,0]

故障来源

torch.gather实现上,Python强制要求i64类型,然后经pnnx后,ncnn无法正确输出int结果

@Baiyuetribe
Copy link
Contributor Author

#include "net.h"

int main()
{
    auto net = ncnn::Net();
    net.load_param("demo.ncnn.param");
    net.load_model("demo.ncnn.bin");
    ncnn::Extractor ex = net.create_extractor();

    ncnn::Mat out0, out1;
    ex.extract("out0", out0);
    ex.extract("out1", out1);
    int* p = out0;
    printf("out0 = %d,%d,%d,%d\n", p[0], p[1], p[2], p[3]);
    int* p1 = out1;
    printf("out1 = %d,%d,%d,%d\n", p1[0], p1[1], p1[2], p1[3]);
    return 0;
}

输出:

out0 = 2,1,3,4 # i32正确输出
out1 = 2,0,1,0 # i64 错误输出

结论,pnnx存档时,i64应该强制转i32存档。如果不作为Memorydata存档,那么输入i64也要做一次i32转换

@Baiyuetribe Baiyuetribe mentioned this issue Jan 18, 2025
19 tasks
@Baiyuetribe
Copy link
Contributor Author

fix: #5881

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

1 participant