Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor checkpoint conversion script for improved readability and efficiency #633

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tdas3001
Copy link

  • Replaced os.path.join with Pathlib for improved readability and efficiency
  • Used Path.glob() instead of glob(os.path.join(...)) for file operations
  • Simplified directory creation with Path.mkdir(parents=True, exist_ok=True)
  • Refactored parameter splitting logic to remove unnecessary loops and redundant conditions
  • Used direct indexing for expert assignment instead of iterating over all model parallel indices
  • Replaced .narrow() with Python slicing for improved readability
  • Added an assertion to ensure mp > 0 before proceeding
  • Reorganized key mapping logic for better readability and maintainability
  • Optimized name modifications using a single chained .replace() operation
  • Ensured that only necessary operations are performed when processing model parameters

Comment on lines +80 to +89
if "experts" in name and "shared_experts" not in name:
idx = int(name.split(".")[-3])
target_index = idx // n_local_experts
if target_index < mp:
state_dicts[target_index][name] = param
elif dim is not None:
assert param.size(dim) % mp == 0, f"Dimension {dim} must be divisible by {mp}"
shard_size = param.size(dim) // mp
for i in range(mp):
state_dicts[i][name] = param[:, i * shard_size : (i + 1) * shard_size] if dim == 1 else param[i * shard_size : (i + 1) * shard_size]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add comments here for better clarity.

@@ -43,46 +44,55 @@ def main(hf_ckpt_path, save_path, n_experts, mp):
Returns:
None
"""
assert mp > 0, "Model parallelism (mp) must be greater than 0"

torch.set_num_threads(8)
n_local_experts = n_experts // mp
state_dicts = [{} for _ in range(mp)]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment to define state_dicts variable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants