Replace bare excepts with specific exception types and add explicit weights_only to torch.load#373
Conversation
…eights_only to torch.load - Replace 9 bare `except:` clauses with specific types (ImportError, AttributeError, KeyError, Exception) - Replace 2 `raise Exception(...)` with specific types (KeyError, ValueError) - Add explicit `weights_only=True` to 3 torch.load calls loading state dicts - Add explicit `weights_only=False` to 2 torch.load calls loading pickled gene embedding dicts - Skip vendored code (model_dir/, minimal_llm_foundry/)
|
Thank you for improving |
|
Done, retargeted to |
dmiv-helical
left a comment
There was a problem hiding this comment.
Thank you Liudeng, and apologies for the long turnover.
Please address the few minor comments.
| scratch_dict[key] = pretrained_dict[key_loaded] | ||
| except: | ||
| raise Exception("key mismatch in the state dicts!") | ||
| except KeyError: |
There was a problem hiding this comment.
Let's keep the original error's context by capturing it as
except KeyError as e: to then raise with the contents of e.
| ) | ||
| # If file is split, download and join parts | ||
| except: | ||
| except huggingface_hub.utils.EntryNotFoundError: |
There was a problem hiding this comment.
Let's keep it to huggingface_hub.errors.EntryNotFoundError to comply with the convention used elsewhere in this file.
| from .evo_2_config import Evo2Config | ||
| from .model import Evo2 | ||
| except: | ||
| except ImportError: |
There was a problem hiding this comment.
Broaden to except (ImportError, OSError, RuntimeError): for non-CUDA environments.
| from .caduceus_config import CaduceusConfig | ||
| from .fine_tuning_model import CaduceusFineTuningModel | ||
| except: | ||
| except ImportError: |
There was a problem hiding this comment.
Broaden to except (ImportError, OSError, RuntimeError): for non-CUDA environments.
| from vortex.model.model import StripedHyena | ||
| from vortex.model.utils import dotdict, load_checkpoint | ||
| except: | ||
| except ImportError: |
There was a problem hiding this comment.
Broaden to except (ImportError, OSError, RuntimeError): for non-CUDA environments.
Summary
except:clauses with specific exception types (ImportError,AttributeError,KeyError,Exception) across 8 files — bareexcept:catchesKeyboardInterruptandSystemExit, which can mask real problemsraise Exception(...)with specific types (KeyError,ValueError)weights_only=Trueto 3torch.load()calls that load state dicts (security best practice — prevents arbitrary code execution from untrusted checkpoints)weights_only=Falsewith comments to 2torch.load()calls that load pickled gene embedding dicts (these need unpickling)model_dir/,minimal_llm_foundry/)Test plan
except:remains in helical-native codetorch.load()calls have explicitweights_only🤖 Generated with Claude Code