Skip to content
This repository was archived by the owner on Mar 20, 2026. It is now read-only.

Commit 4d2efae

Browse files
Weiyi Zhengfacebook-github-bot
authored andcommitted
support manifold in average_checkpoint.py
Summary: use PathManager to support averaging checkpoints. Reviewed By: myleott Differential Revision: D20725346 fbshipit-source-id: 44b91f8652826da72c82087f8fbab7ae7d179423
1 parent d37fdee commit 4d2efae

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

‎scripts/average_checkpoints.py‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import os
1111
import re
1212

13+
from fairseq.file_io import PathManager
14+
1315

1416
def average_checkpoints(inputs):
1517
"""Loads checkpoints from inputs and returns a model with averaged weights.
@@ -27,13 +29,14 @@ def average_checkpoints(inputs):
2729
new_state = None
2830
num_models = len(inputs)
2931

30-
for f in inputs:
31-
state = torch.load(
32-
f,
33-
map_location=(
34-
lambda s, _: torch.serialization.default_restore_location(s, 'cpu')
35-
),
36-
)
32+
for fpath in inputs:
33+
with PathManager.open(fpath, 'rb') as f:
34+
state = torch.load(
35+
f,
36+
map_location=(
37+
lambda s, _: torch.serialization.default_restore_location(s, 'cpu')
38+
),
39+
)
3740
# Copies over the settings from the first checkpoint
3841
if new_state is None:
3942
new_state = state
@@ -74,7 +77,7 @@ def last_n_checkpoints(paths, n, update_based, upper_bound=None):
7477
pt_regexp = re.compile(r'checkpoint_\d+_(\d+)\.pt')
7578
else:
7679
pt_regexp = re.compile(r'checkpoint(\d+)\.pt')
77-
files = os.listdir(path)
80+
files = PathManager.ls(path)
7881

7982
entries = []
8083
for f in files:
@@ -135,7 +138,8 @@ def main():
135138
print('averaging checkpoints: ', args.inputs)
136139

137140
new_state = average_checkpoints(args.inputs)
138-
torch.save(new_state, args.output)
141+
with PathManager.open(args.output, 'wb') as f:
142+
torch.save(new_state, f)
139143
print('Finished writing averaged checkpoint to {}.'.format(args.output))
140144

141145

0 commit comments

Comments
 (0)