Skip to content

A ~9M parameter LLM that talks like a small fish.

Python

Fix inference.py: use abspath for config.json lookup

Arman Hossaina9bfe4491e08
diff --git a/guppylm/inference.py b/guppylm/inference.py
index e1d4884..2790ca4 100644
--- a/guppylm/inference.py
+++ b/guppylm/inference.py
@@ -16,23 +16,26 @@ class GuppyInference:
self.device = torch.device(device)
self.tokenizer = Tokenizer.from_file(tokenizer_path)
+ import os
ckpt = torch.load(checkpoint_path, map_location=self.device, weights_only=False)
- # Support both HF standard (state_dict only) and legacy (full checkpoint) format
- if "model_state_dict" in ckpt:
- # Legacy format: {config, model_state_dict, ...}
+ # Detect format: legacy checkpoint has "model_state_dict" key,
+ # HF standard is just the state_dict (first key is a weight like "tok_emb.weight")
+ is_legacy = isinstance(ckpt, dict) and "model_state_dict" in ckpt
+
+ if is_legacy:
valid_fields = {f.name for f in GuppyConfig.__dataclass_fields__.values()}
self.config = GuppyConfig(**{k: v for k, v in ckpt["config"].items() if k in valid_fields})
state_dict = ckpt["model_state_dict"]
else:
- # HF standard format: state_dict only, config from separate file
- import os
- config_path = os.path.join(os.path.dirname(checkpoint_path), "config.json")
+ # HF standard: load config from config.json next to the model file
+ config_dir = os.path.dirname(os.path.abspath(checkpoint_path))
+ config_path = os.path.join(config_dir, "config.json")
with open(config_path) as f:
cfg = json.load(f)
self.config = GuppyConfig(
vocab_size=cfg["vocab_size"],
- max_seq_len=cfg["max_position_embeddings"],
+ max_seq_len=cfg.get("max_position_embeddings", 128),
d_model=cfg["hidden_size"],
n_layers=cfg["num_hidden_layers"],
n_heads=cfg["num_attention_heads"],
diff --git a/train_guppylm.ipynb b/train_guppylm.ipynb
index 3dc619f..6c37dbe 100644
--- a/train_guppylm.ipynb
+++ b/train_guppylm.ipynb
@@ -532,23 +532,26 @@
" self.device = torch.device(device)\n",
" self.tokenizer = Tokenizer.from_file(tokenizer_path)\n",
"\n",
+ " import os\n",
" ckpt = torch.load(checkpoint_path, map_location=self.device, weights_only=False)\n",
"\n",
- " # Support both HF standard (state_dict only) and legacy (full checkpoint) format\n",
- " if \"model_state_dict\" in ckpt:\n",
- " # Legacy format: {config, model_state_dict, ...}\n",
+ " # Detect format: legacy checkpoint has \"model_state_dict\" key,\n",
+ " # HF standard is just the state_dict (first key is a weight like \"tok_emb.weight\")\n",
+ " is_legacy = isinstance(ckpt, dict) and \"model_state_dict\" in ckpt\n",
+ "\n",
+ " if is_legacy:\n",
" valid_fields = {f.name for f in GuppyConfig.__dataclass_fields__.values()}\n",
" self.config = GuppyConfig(**{k: v for k, v in ckpt[\"config\"].items() if k in valid_fields})\n",
" state_dict = ckpt[\"model_state_dict\"]\n",
" else:\n",
- " # HF standard format: state_dict only, config from separate file\n",
- " import os\n",
- " config_path = os.path.join(os.path.dirname(checkpoint_path), \"config.json\")\n",
+ " # HF standard: load config from config.json next to the model file\n",
+ " config_dir = os.path.dirname(os.path.abspath(checkpoint_path))\n",
+ " config_path = os.path.join(config_dir, \"config.json\")\n",
" with open(config_path) as f:\n",
" cfg = json.load(f)\n",
" self.config = GuppyConfig(\n",
" vocab_size=cfg[\"vocab_size\"],\n",
- " max_seq_len=cfg[\"max_position_embeddings\"],\n",
+ " max_seq_len=cfg.get(\"max_position_embeddings\", 128),\n",
" d_model=cfg[\"hidden_size\"],\n",
" n_layers=cfg[\"num_hidden_layers\"],\n",
" n_heads=cfg[\"num_attention_heads\"],\n",
diff --git a/use_guppylm.ipynb b/use_guppylm.ipynb
index 6bec640..8ee2051 100644
--- a/use_guppylm.ipynb
+++ b/use_guppylm.ipynb
@@ -278,23 +278,26 @@
" self.device = torch.device(device)\n",
" self.tokenizer = Tokenizer.from_file(tokenizer_path)\n",
"\n",
+ " import os\n",
" ckpt = torch.load(checkpoint_path, map_location=self.device, weights_only=False)\n",
"\n",
- " # Support both HF standard (state_dict only) and legacy (full checkpoint) format\n",
- " if \"model_state_dict\" in ckpt:\n",
- " # Legacy format: {config, model_state_dict, ...}\n",
+ " # Detect format: legacy checkpoint has \"model_state_dict\" key,\n",
+ " # HF standard is just the state_dict (first key is a weight like \"tok_emb.weight\")\n",
+ " is_legacy = isinstance(ckpt, dict) and \"model_state_dict\" in ckpt\n",
+ "\n",
+ " if is_legacy:\n",
" valid_fields = {f.name for f in GuppyConfig.__dataclass_fields__.values()}\n",
" self.config = GuppyConfig(**{k: v for k, v in ckpt[\"config\"].items() if k in valid_fields})\n",
" state_dict = ckpt[\"model_state_dict\"]\n",
" else:\n",
- " # HF standard format: state_dict only, config from separate file\n",
- " import os\n",
- " config_path = os.path.join(os.path.dirname(checkpoint_path), \"config.json\")\n",
+ " # HF standard: load config from config.json next to the model file\n",
+ " config_dir = os.path.dirname(os.path.abspath(checkpoint_path))\n",
+ " config_path = os.path.join(config_dir, \"config.json\")\n",
" with open(config_path) as f:\n",
" cfg = json.load(f)\n",
" self.config = GuppyConfig(\n",
" vocab_size=cfg[\"vocab_size\"],\n",
- " max_seq_len=cfg[\"max_position_embeddings\"],\n",
+ " max_seq_len=cfg.get(\"max_position_embeddings\", 128),\n",
" d_model=cfg[\"hidden_size\"],\n",
" n_layers=cfg[\"num_hidden_layers\"],\n",
" n_heads=cfg[\"num_attention_heads\"],\n",