Skip to content

A bug for torch.clone() when clone a MKLDNN tensor #20895

@XiaobingSuper

Description

@XiaobingSuper

🐛 Bug

There has a bug when clone a MKLDNN tensor, it seems that this clone share the same memory which is different with CPU tensor's clone .

To Reproduce

Using latest pytorch:
Steps to reproduce the behavior:

import torch
a = torch.randn(2, 3).to_mkldnn()
c = torch.randn(2, 3).to_mkldnn()
b = torch.clone(a)
b.add_(c)
print(a)
print(b)

you can see that tensor a is also changed after change tensor b.

I found the code mkldnn_clone seems wrong, the return tensor share the same memory with input tensor. it shoud write as :

Tensor mkldnn_clone(const Tensor& self) {
  ideep::tensor& src = itensor_from_mkldnn(self);
  ideep::tensor dst;
  ideep::direct_copy::compute<AllocForMKLDNN>(src, dst);
  return new_with_itensor_mkldnn(std::move(dst), self.options());
}

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: mkldnnRelated to Intel IDEEP or oneDNN (a.k.a. mkldnn) integrationtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions