Skip to content

Commit c8dc707

Browse files
suofacebook-github-bot
authored andcommitted
avoid multiple writes to files on export (#21186)
Summary: Pull Request resolved: #21186 ghimport-source-id: 2f62fed Differential Revision: D15581527 Pulled By: suo fbshipit-source-id: b1150cfa47d8df6f217f048c742a5ba9fa7f7935
1 parent 4c19421 commit c8dc707

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

test/test_jit.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,8 @@ def copy_structure_and_params(m):
365365

366366
# crack open the zip format to get at the main module code
367367
archive = zipfile.ZipFile(buffer)
368+
# check that we have no duplicate names
369+
self.assertEqual(len(set(archive.namelist())), len(archive.namelist()))
368370
main_module = archive.open('archive/code/archive.py')
369371
main_module_code = ""
370372
for line in main_module:

torch/csrc/jit/export.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,16 @@ void ScriptModuleSerializer::writeLibs(torch::ModelDef* model_def) {
607607

608608
// Write out the files. We still have to do this in converted_classes_ order,
609609
// to maintain dependency order.
610+
std::unordered_set<std::string> written_files;
610611
for (const auto& item : converted_classes_) {
611612
const ClassTypePtr& class_type = item.key();
612613
const std::string filename =
613614
ImportExportHelpers::qualifierToPath(class_type->qualifier());
615+
if (written_files.count(filename)) {
616+
continue;
617+
}
618+
written_files.insert(filename);
619+
614620
const std::string& src = fileToSrc.at(filename).str();
615621

616622
std::ostringstream lib_stream;

0 commit comments

Comments
 (0)