Skip to content

pth name is garbled when there is Chinese character #98918

@feiyuhuahuo

Description

@feiyuhuahuo

🐛 Describe the bug

I'm trying to use torch.save to save a model and the model name is in Chinese. But the output file name is garbled.
Code:

# -*- coding:utf-8 -*-
import os
import torch
import torch.nn as nn


class FakeModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Conv2d(10, 10, 3)

    def forward(self, x):
        return x


if __name__ == '__main__':
    model = FakeModel()
    model.eval()
    torch.save(model.state_dict(), '网络.pth')

The output file name is:
image
If there is a Chinese parent folder, an error will occur. The related code:

import os
import torch
import torch.nn as nn


class FakeModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Conv2d(10, 10, 3)

    def forward(self, x):
        return x


if __name__ == '__main__':
    model = FakeModel()
    model.eval()

    os.makedirs('测试路径', exist_ok=True)
    torch.save(model.state_dict(), '测试路径/网络啊.pth')

This will get the error:

Traceback (most recent call last):
  File "E:\SBKJ_inspection\ttt.py", line 22, in <module>
    torch.save(model.state_dict(), '测试路径/网络啊.pth')
  File "C:\Users\feiyuhuahuo\python_base\lib\site-packages\torch\serialization.py", line 440, in save
    with _open_zipfile_writer(f) as opened_zipfile:
  File "C:\Users\feiyuhuahuo\python_base\lib\site-packages\torch\serialization.py", line 315, in _open_zipfile_writer
    return container(name_or_buffer)
  File "C:\Users\feiyuhuahuo\python_base\lib\site-packages\torch\serialization.py", line 288, in __init__
    super().__init__(torch._C.PyTorchFileWriter(str(name)))
RuntimeError: Parent directory 测试路径 does not exist.

I tested torch version==1.13.1 and 2.0.0. They all have the bug. But version 1.11 works well.

Versions

PyTorch version: 2.0.0+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Microsoft Windows 10 家庭中文版
GCC version: (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0
Clang version: Could not collect
CMake version: version 3.20.4
Libc version: N/A

Python version: 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] (64-bit runtime)
Python platform: Windows-10-10.0.19044-SP0
Is CUDA available: True
CUDA runtime version: 11.5.119
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 Super with Max-Q Design
Nvidia driver version: 528.49
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture=9
CurrentClockSpeed=2396
DeviceID=CPU0
Family=207
L2CacheSize=2048
L2CacheSpeed=
Manufacturer=GenuineIntel
MaxClockSpeed=3096
Name=Intel(R) Core(TM) i9-10980HK CPU @ 2.40GHz
ProcessorType=3
Revision=

Versions of relevant libraries:
[pip3] numpy==1.21.4
[pip3] torch==2.0.0+cu117
[conda] Could not collect

cc @mruberry

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issuemodule: serializationIssues related to serialization (e.g., via pickle, or otherwise) of PyTorch objectstriagedThis 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