Skip to content

Commit

Permalink
fix pnnx PermissionError (#4801)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenjiaguo authored Jun 14, 2023
1 parent 4a78b6d commit d9e45ec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
6 changes: 2 additions & 4 deletions tools/pnnx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,11 +346,9 @@ class Model(nn.Module):
return nn.Parameter(self.load_pnnx_bin_as_tensor(archive, key, shape, dtype))

def load_pnnx_bin_as_tensor(self, archive, key, shape, dtype):
_, tmppath = tempfile.mkstemp()
tmpf = open(tmppath, 'wb')
with archive.open(key) as keyfile:
fd, tmppath = tempfile.mkstemp()
with os.fdopen(fd, 'wb') as tmpf, archive.open(key) as keyfile:
tmpf.write(keyfile.read())
tmpf.close()
m = np.memmap(tmppath, dtype=dtype, mode='r', shape=shape).copy()
os.remove(tmppath)
return torch.from_numpy(m)
Expand Down
6 changes: 2 additions & 4 deletions tools/pnnx/src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1785,11 +1785,9 @@ int Graph::python(const std::string& pypath, const std::string& pnnxbinpath)
fprintf(pyfp, " return nn.Parameter(self.load_pnnx_bin_as_tensor(archive, key, shape, dtype), requires_grad)\n");
fprintf(pyfp, "\n");
fprintf(pyfp, " def load_pnnx_bin_as_tensor(self, archive, key, shape, dtype):\n");
fprintf(pyfp, " _, tmppath = tempfile.mkstemp()\n");
fprintf(pyfp, " tmpf = open(tmppath, 'wb')\n");
fprintf(pyfp, " with archive.open(key) as keyfile:\n");
fprintf(pyfp, " fd, tmppath = tempfile.mkstemp()\n");
fprintf(pyfp, " with os.fdopen(fd, 'wb') as tmpf, archive.open(key) as keyfile:\n");
fprintf(pyfp, " tmpf.write(keyfile.read())\n");
fprintf(pyfp, " tmpf.close()\n");
fprintf(pyfp, " m = np.memmap(tmppath, dtype=dtype, mode='r', shape=shape).copy()\n");
fprintf(pyfp, " os.remove(tmppath)\n");
fprintf(pyfp, " return torch.from_numpy(m)\n");
Expand Down

0 comments on commit d9e45ec

Please sign in to comment.