Skip to content

convert_hf_to_gguf.py incorrectly falls back to num_hidden_layers, causing KeyError on ChatGLM3 (num_layers present) #17358

@shadowmmu

Description

@shadowmmu

Description

Some newer model configurations (such as ChatGLM3) no longer include the field "num_hidden_layers".
Instead, they provide "num_layers".

The current converter code contains the following line:

self.gguf_writer.add_block_count(
    self.hparams.get("num_layers", self.hparams["num_hidden_layers"])
)

This leads to a KeyError even if "num_layers" is present and "num_hidden_layers" is not present in the model config.


Questions

  1. Is the absence of "num_hidden_layers" in newer models expected?
    It appears that recent architectures rely on "num_layers" instead.

  2. Was the original line intended to assume "num_hidden_layers" always exists?
    If not, the KeyError suggests that the converter logic may be outdated for modern model configs.

  3. Should the converter be updated to support both fields safely rather than assuming "num_hidden_layers" is always present?


Cause of the Error

The line:

self.hparams["num_hidden_layers"]

directly indexes into the dict.
If "num_hidden_layers" is missing, Python raises:

KeyError: 'num_hidden_layers'

This happens even when "num_layers" is present, because the dict lookup is only used when "num_layers" is missing from the get() call.


Suggested Fix

To avoid failing when "num_hidden_layers" is absent, the line could be updated to a safer fallback:

self.gguf_writer.add_block_count(
    self.hparams.get("num_layers", self.hparams.get("num_hidden_layers", 0)))

This ensures:

  • "num_layers" is used when available
  • "num_hidden_layers" is used if "num_layers" is missing
  • a fallback value (e.g., 0) prevents a crash

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions