Skip to content

feat: Add support for deepseek recipes #5011

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

Merged
merged 3 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/sagemaker/modules/train/sm_recipes/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,27 @@ def _register_custom_resolvers():
OmegaConf.register_new_resolver("add", lambda *numbers: sum(numbers))


def _get_trainining_recipe_gpu_model_name_and_script(model_type: str):
"""Get the model base name and script for the training recipe."""

model_type_to_script = {
"llama_v3": ("llama", "llama_pretrain.py"),
"mistral": ("mistral", "mistral_pretrain.py"),
"mixtral": ("mixtral", "mixtral_pretrain.py"),
"deepseek": ("deepseek", "deepseek_pretrain.py"),
}
Comment on lines +131 to +136
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we remove this redundancy and hardcode it somewhere ?


for key in model_type_to_script.keys():
if model_type.startswith(key):
model_type = key
break

if model_type not in model_type_to_script:
raise ValueError(f"Model type {model_type} not supported")

return model_type_to_script[model_type][0], model_type_to_script[model_type][1]


def _configure_gpu_args(
training_recipes_cfg: Dict[str, Any],
region_name: str,
Expand All @@ -140,24 +161,16 @@ def _configure_gpu_args(
)
_run_clone_command_silent(adapter_repo, recipe_train_dir.name)

model_type_to_entry = {
"llama_v3": ("llama", "llama_pretrain.py"),
"mistral": ("mistral", "mistral_pretrain.py"),
"mixtral": ("mixtral", "mixtral_pretrain.py"),
}

if "model" not in recipe:
raise ValueError("Supplied recipe does not contain required field model.")
if "model_type" not in recipe["model"]:
raise ValueError("Supplied recipe does not contain required field model_type.")
model_type = recipe["model"]["model_type"]
if model_type not in model_type_to_entry:
raise ValueError(f"Model type {model_type} not supported")

source_code.source_dir = os.path.join(
recipe_train_dir.name, "examples", model_type_to_entry[model_type][0]
)
source_code.entry_script = model_type_to_entry[model_type][1]
model_base_name, script = _get_trainining_recipe_gpu_model_name_and_script(model_type)

source_code.source_dir = os.path.join(recipe_train_dir.name, "examples", model_base_name)
source_code.entry_script = script

gpu_image_cfg = training_recipes_cfg.get("gpu_image")
if isinstance(gpu_image_cfg, str):
Expand Down
7 changes: 7 additions & 0 deletions src/sagemaker/pytorch/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,20 @@ def _get_training_recipe_gpu_script(code_dir, recipe, source_dir):
"llama_v3": ("llama", "llama_pretrain.py"),
"mistral": ("mistral", "mistral_pretrain.py"),
"mixtral": ("mixtral", "mixtral_pretrain.py"),
"deepseek": ("deepseek", "deepseek_pretrain.py"),
}

if "model" not in recipe:
raise ValueError("Supplied recipe does not contain required field model.")
if "model_type" not in recipe["model"]:
raise ValueError("Supplied recipe does not contain required field model_type.")
model_type = recipe["model"]["model_type"]

for key in model_type_to_script.keys():
if model_type.startswith(key):
model_type = key
break

if model_type not in model_type_to_script:
raise ValueError(f"Model type {model_type} not supported")

Expand Down
35 changes: 35 additions & 0 deletions tests/unit/sagemaker/modules/train/sm_recipes/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_load_recipes_cfg,
_configure_gpu_args,
_configure_trainium_args,
_get_trainining_recipe_gpu_model_name_and_script,
)
from sagemaker.modules.utils import _run_clone_command_silent
from sagemaker.modules.configs import Compute
Expand Down Expand Up @@ -178,3 +179,37 @@ def test_get_args_from_recipe_compute(
assert mock_gpu_args.call_count == 0
assert mock_trainium_args.call_count == 0
assert args is None

@pytest.mark.parametrize(
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks Erick! Can you copy this test case/change to tests/unit/test_pytorch.py as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some for pytorch as well

"test_case",
[
{
"model_type": "llama_v3",
"script": "llama_pretrain.py",
"model_base_name": "llama_v3",
},
{
"model_type": "mistral",
"script": "mistral_pretrain.py",
"model_base_name": "mistral",
},
{
"model_type": "deepseek_llamav3",
Copy link
Contributor

Choose a reason for hiding this comment

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

to be extra safe can we align on deepseek_r1_llama_v3 and deepseek_r1_qwen_v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would these have different training scripts? Or you mean just add a test case for

Copy link
Contributor

@rohithn1 rohithn1 Jan 31, 2025

Choose a reason for hiding this comment

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

no im just saying instead deepseek_llamav3 lets do deepseek_r1_llama_v3

I think it will be easier to extend support to the v3 and native r1 model from deepseek if we use deepseek_r1_llama_v3 over deepseek_llamav3

"script": "deepseek_pretrain.py",
"model_base_name": "deepseek",
},
{
"model_type": "deepseek_qwenv2",
"script": "deepseek_pretrain.py",
"model_base_name": "deepseek",
},
],
)
def test_get_trainining_recipe_gpu_model_name_and_script(test_case):
model_type = test_case["model_type"]
script = test_case["script"]
model_base_name, script = _get_trainining_recipe_gpu_model_name_and_script(
model_type, script
)
assert model_base_name == test_case["model_base_name"]
assert script == test_case["script"]
Loading