Skip to content

OOM while load the whole model #141

Description

@Milesld

Thanks for your great work.

I meet an OOM error while load the model. After check the code, it seems can be fixed in this file: https://github.com/nvidia-cosmos/cosmos-predict2.5/blob/main/cosmos_predict2/_src/predict2/utils/model_loader.py#L366
Here .cuda() was applied first before apply_fsdp, becasuse of the big model, it cause OOM. Can I change the .cuda to end after apply_fsdp? I change the code and it seems work fine, but I have no idea is this ok...The code shows below:
`
def create_model_from_consolidated_checkpoint_with_fsdp(config: Config) -> ImaginaireModel:

"""
Instantiate a model, load weights from a consolidated checkpoint, and initialize FSDP if required.

Args:
    config: The configuration object for the experiment.

Returns:
    model: The loaded and (optionally) FSDP-wrapped model.
"""
# To avoid DTensor issues, load the model from a consolidated checkpoint in Tensor format before applying FSDP.
fsdp_shard_size = config.model.config.fsdp_shard_size
config.model.config.fsdp_shard_size = 1  # Set to 1 to disable FSDP during model instantiation.
# model = instantiate(config.model).cuda()
model = instantiate(config.model)
# DCP checkpointer does not support loading from a consolidated checkpoint, so we support it here.
model = load_model_state_dict_from_checkpoint(
    model=model,
    config=config,
    s3_checkpoint_dir=config.checkpoint.load_path,
    load_ema_to_reg=config.checkpoint.load_ema_to_reg,
)
# If FSDP is enabled, apply FSDP to the model.
if fsdp_shard_size > 1:
    config.model.config.fsdp_shard_size = fsdp_shard_size
    fsdp_device_mesh = hsdp_device_mesh(
        sharding_group_size=fsdp_shard_size,
    )
    if hasattr(model, "apply_fsdp") and callable(model.apply_fsdp):
        model.apply_fsdp(fsdp_device_mesh)
    else:
        raise AttributeError(
            "Model does not implement 'apply_fsdp'. Please implement this method to enable FSDP after consolidated checkpoint loading."
        )
model = model.cuda()

return model

`

And another question, is the loss value between 0~0.5 (I just trained for about 100 steps)? And there is no downward trend, just up and down between

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions