Skip to content

Replace bare excepts with specific exception types and add explicit weights_only to torch.load#373

Open
LiudengZhang wants to merge 1 commit into
helicalAI:mainfrom
LiudengZhang:fix/code-quality-bare-except-weights-only
Open

Replace bare excepts with specific exception types and add explicit weights_only to torch.load#373
LiudengZhang wants to merge 1 commit into
helicalAI:mainfrom
LiudengZhang:fix/code-quality-bare-except-weights-only

Conversation

@LiudengZhang
Copy link
Copy Markdown
Contributor

Summary

  • Replace 9 bare except: clauses with specific exception types (ImportError, AttributeError, KeyError, Exception) across 8 files — bare except: catches KeyboardInterrupt and SystemExit, which can mask real problems
  • Replace 2 raise Exception(...) with specific types (KeyError, ValueError)
  • Add explicit weights_only=True to 3 torch.load() calls that load state dicts (security best practice — prevents arbitrary code execution from untrusted checkpoints)
  • Add explicit weights_only=False with comments to 2 torch.load() calls that load pickled gene embedding dicts (these need unpickling)
  • Skipped vendored code (model_dir/, minimal_llm_foundry/)

Test plan

  • Verify no bare except: remains in helical-native code
  • Verify all torch.load() calls have explicit weights_only
  • Run existing test suite to confirm no regressions

🤖 Generated with Claude Code

…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/)
@dmiv-helical
Copy link
Copy Markdown
Contributor

Thank you for improving helical, @LiudengZhang !
Do you mind retargeting this PR to the main branch?

@LiudengZhang LiudengZhang changed the base branch from release to main May 6, 2026 19:41
@LiudengZhang
Copy link
Copy Markdown
Contributor Author

Done, retargeted to main. Let me know if anything else is needed.

Copy link
Copy Markdown
Contributor

@dmiv-helical dmiv-helical left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broaden to except (ImportError, OSError, RuntimeError): for non-CUDA environments.

from .caduceus_config import CaduceusConfig
from .fine_tuning_model import CaduceusFineTuningModel
except:
except ImportError:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broaden to except (ImportError, OSError, RuntimeError): for non-CUDA environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants