deepspeedai / deepspeedai/DeepSpeed

[BUG] RuntimeError: weight should have at least three dimensions

Open
#7,518 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug training
Dominant language
Python
Stars
43.1k
Forks
5k
Avg merge
4d 15h
Merged PRs (30d)
112

Description

Describe the bug
I'm now using deepspeed to initialize a new torch module which is not in the original huggingface model.
at first it pop out the error similar to https://github.com/deepspeedai/DeepSpeed/issues/5326, then i used the method in it and resolve the weight-import-error
but now i face this error, i believe it's caused by deepspeed

my code is presented below:

class RemoteClipLoader:
    def __init__(self, model_name="RN50", ckpt_path=None, dim=0, device="cuda"):
        self.model, _, _ = open_clip.create_model_and_transforms(model_name)
        self.device = device
        self.model.to(self.device).eval()

        self.mlp = nn.Linear(
            self.model.visual.attnpool.c_proj.out_features if hasattr(self.model.visual, "attnpool") else 512,
            dim
        ).to(self.device)

        if ckpt_path is not None:
            state_dict = torch.load(ckpt_path, map_location="cpu")
            self._load_state_dict(self.model, state_dict)

    def _load_state_dict(self, module_to_load, state_dict, start_prefix=""):
        metadata = getattr(state_dict, "_metadata", None)
        state_dict = state_dict.copy()
        if metadata is not None:
            state_dict._metadata = metadata
        error_msgs = []
        def load(module, state_dict, prefix=""):
            local_metadata = {} if metadata is None else metadata.get(prefix[:-1], {})
            args = (state_dict, prefix, local_metadata, True, [], [], error_msgs)
            if any(key.startswith(prefix) for key in state_dict):
                named_parameters = dict(module.named_parameters(prefix=prefix[:-1], recurse=False))
                params_to_gather = [named_parameters[k] for k in state_dict.keys() if k in named_parameters]
                if params_to_gather:
                    with deepspeed.zero.GatheredParameters(params_to_gather, modifier_rank=0):
                        rank = dist.get_rank() if dist.is_initialized() else 0
                        if rank == 0:
                            module._load_from_state_dict(*args)
                else:
                    module._load_from_state_dict(*args)
            else:
                module._load_from_state_dict(*args)
            for name, child in module._modules.items():
                if child is not None:
                    load(child, state_dict, prefix + name + ".")
        load(module_to_load, state_dict, start_prefix)
        if error_msgs:
            raise RuntimeError(
                f"Error(s) in loading state_dict for {module_to_load.__class__.__name__}:\n\t" +
                "\n\t".join(error_msgs)
            )
        print("RemoteCLIP checkpoint loaded successfully!")

    @torch.no_grad()
    def encode_image(self, x):
        if x.ndim == 3:
            B, N, feat = x.shape  # (B, 256, 1176)
            p = int(math.sqrt(feat // (2 * 3)))  # 14
            x = x.view(B * N, 2, 3, p, p)        # (B*256, 2, 3, 14, 14)
            x = x[:, 0]                   
            x = torch.nn.functional.interpolate(x, size=(224,224), mode="bilinear")
        elif x.ndim == 4 and x.shape[1] == 3:
            B = x.size(0)
            patches = x.unfold(2,14,14).unfold(3,14,14)  # (B,3,16,16,14,14)
            patches = patches.permute(0,2,3,1,4,5).reshape(B,256,3,14,14)
            x = patches.reshape(B*256,3,14,14)
            x = torch.nn.functional.interpolate(x, size=(224,224), mode="bilinear")
        else:
            raise ValueError(f"Unsupported input shape {tuple(x.shape)}")

        x = x.to(self.device)
        feat = self.model.encode_image(x)   # (B*256, d_model)
        out = self.mlp(feat)                # (B*256, dim)

        return out.view(-1, out.size(-1))   # [256, dim]

    def __call__(self, x):
        return self.encode_image(x)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided RemoteClipLoader example, especially _load_state_dict and the deepspeed.zero.GatheredParameters block, and capture the full traceback, dependency versions, and a minimal reproducible case. Compare the failing parameter shape with the module state_dict during loading; done means identifying whether DeepSpeed or the custom loading path causes the dimensionality error and documenting or fixing that path.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
distributed-systems, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.