Browse Source

fix pnnx PermissionError (#4801)

tags/20230816
Zhenjia Guo GitHub 3 years ago
parent
commit
d9e45ec703
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 8 deletions
  1. +2
    -4
      tools/pnnx/README.md
  2. +2
    -4
      tools/pnnx/src/ir.cpp

+ 2
- 4
tools/pnnx/README.md View File

@@ -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)


+ 2
- 4
tools/pnnx/src/ir.cpp View File

@@ -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");


Loading…
Cancel
Save