xai-org/x-algorithm · error · ValueError

The value of top_feedforward specified ({top_feedforward}) d

Error message

The value of top_feedforward specified ({top_feedforward}) does not match that in the checkpoint for {clip_model_type} ({config['top_feedforward']}).

What it means

When loading a fine-tuned checkpoint, Model.__init__ compares the top_feedforward hyperparameter you pass to the value stored in the checkpoint's config; a mismatch means the caller's model architecture would differ from the trained one, so weight loading would be invalid and it raises.

Source

Thrown at clip/model.py:123

        self._device = "cuda:0" if self.use_gpu else "cpu"
        if self.use_gpu:
            self._prepare_model_for_gpu()

        self.clip_model.logit_scale.to(self._device)

        if clip_model_type in self.TWITTER_MODELS:
            map_location = None if self.use_gpu else torch.device("cpu")
            local_model_path = io_utils.maybe_download_file(
                self.TWITTER_MODELS[clip_model_type]
            )
            print(f"loading model from: {local_model_path}")
            checkpoint_contents = torch.load(
                local_model_path, map_location=map_location
            )

            config = checkpoint_contents["config"]
            if config["top_feedforward"] != top_feedforward:
                raise ValueError(
                    f"The value of top_feedforward specified ({top_feedforward}) does not match that in the "
                    f"checkpoint for {clip_model_type} ({config['top_feedforward']})."
                )

            if self._multi_gpu:
                image_state_dict = checkpoint_contents[
                    "image_encoder_state_dict_multi_gpu"
                ]
                text_state_dict = checkpoint_contents[
                    "text_encoder_state_dict_multi_gpu"
                ]
            else:
                image_state_dict = checkpoint_contents["image_encoder_state_dict"]
                text_state_dict = checkpoint_contents["text_encoder_state_dict"]

            self.image_encoder.load_state_dict(image_state_dict, strict=True)
            self.text_encoder.load_state_dict(text_state_dict, strict=True)

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Open the checkpoint's config dict and read config['top_feedforward'], then pass exactly that value
  2. Or do not override top_feedforward; let the code take the checkpoint's value
  3. If you intentionally changed the architecture, retrain and export a new checkpoint
  4. Keep a single source of truth for hyperparameters alongside checkpoints

Example fix

# before
Model(clip_model_type='ViT-B/32-finetuned', top_feedforward=4096)
# after
Model(clip_model_type='ViT-B/32-finetuned', top_feedforward=1024)  # from checkpoint config
Defensive patterns

Strategy: validation

Validate before calling

ckpt = torch.load(local_model_path, map_location='cpu')
expected_ff = ckpt['config']['top_feedforward']
assert top_feedforward == expected_ff, f"checkpoint has {expected_ff}"

Try / catch

try:
    Model(clip_model_type=t, top_feedforward=ff)
except ValueError as e:
    if 'top_feedforward' in str(e):
        ff = torch.load(path)['config']['top_feedforward']
        Model(clip_model_type=t, top_feedforward=ff)
    else:
        raise

Prevention

When it happens

Trigger: Instantiating Model with top_feedforward=2048 while the checkpoint for that clip_model_type was trained with top_feedforward=1024; changing the hyperparameter in config without retraining; loading someone else's checkpoint with your own defaults.

Common situations: Hyperparameter tuning changed top_feedforward but old checkpoints are still loaded; shared checkpoint files across experiments with different heads; defaults in code drifted from the exported config.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/a1c6c8ffa0b99812. Report an issue: GitHub.