Skip to content

fix: convert string values to enum types in update_config#1021

Open
Mr-Neutr0n wants to merge 1 commit intometa-llama:mainfrom
Mr-Neutr0n:fix/fsdp-sharding-strategy-enum-conversion
Open

fix: convert string values to enum types in update_config#1021
Mr-Neutr0n wants to merge 1 commit intometa-llama:mainfrom
Mr-Neutr0n:fix/fsdp-sharding-strategy-enum-conversion

Conversation

@Mr-Neutr0n
Copy link

Summary

Fix KeyError when passing enum values like sharding_strategy as strings from the command line.

Problem

When running with --fsdp_config.sharding_strategy "FULL_SHARD", the string value is not converted to ShardingStrategy enum, causing:

KeyError: 'FULL_SHARD'
# at torch/distributed/fsdp/_init_utils.py
# SHARDING_STRATEGY_MAP[state.sharding_strategy]

PyTorch's FSDP expects a ShardingStrategy enum value, not a string.

Solution

Added _convert_to_field_type() helper function that:

  1. Uses get_type_hints() to get the expected field type from the dataclass
  2. If the expected type is an Enum and the value is a string, converts it using EnumClass[value]
  3. Returns the original value if conversion fails or is not needed

This allows users to pass enum values as strings from the command line:

--fsdp_config.sharding_strategy FULL_SHARD
--fsdp_config.sharding_strategy HYBRID_SHARD
--fsdp_config.checkpoint_type SHARDED_STATE_DICT

The fix also works for checkpoint_type (StateDictType enum) and any future enum fields.

Fixes #809

When passing enum values like sharding_strategy from the command line
as strings (e.g., --fsdp_config.sharding_strategy="FULL_SHARD"),
the string was not converted to the ShardingStrategy enum, causing
a KeyError in PyTorch's FSDP initialization.

Added _convert_to_field_type() helper that:
1. Uses get_type_hints() to get the expected field type
2. Converts string values to enum types if the expected type is an Enum

This allows users to pass enum values as strings from the command line
and have them automatically converted to the correct enum type.

Fixes meta-llama#809
@meta-cla meta-cla bot added the cla signed label Feb 8, 2026
@Mr-Neutr0n
Copy link
Author

Friendly bump! Let me know if there's anything I should update or improve to help move this forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FSDP sharding_strategy parameter fails with KeyError when passed as string

1 participant