load_model_from_checkpoint drops num_out/signal_in_channels for models whose __init__ takes **kwargs

Aperta Adatta ai principianti
#479 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
74/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Inizia da model_loading._instantiate_model, intorno alle righe 252–266, e verifica come vengono filtrati i parametri del costruttore quando un modello accetta **kwargs; confronta con ConvLSTMRemoraBase.__init__ in models/conv_lstm_remora.py. Aggiungi un test di regressione che carichi e salvi nuovamente un checkpoint con un valore non predefinito di num_out o signal_in_channels. Il lavoro è completato quando questi valori di configurazione vengono mantenuti durante il caricamento e il modello carica il checkpoint senza incompatibilità nelle dimensioni dei tensori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

load_model_from_checkpoint cannot reload a checkpoint whose constructor parameters differ from the model class's defaults, when that class's __init__ accepts **kwargs. _instantiate_model resolves the valid parameter set from the parent class in that case, so class-specific parameters recorded in config.json — num_out, signal_in_channels — are filtered out and the model is rebuilt with defaults.

Reproduction

Train ConvLSTMRemoraBase with --loss bce (gives num_out=1) and --signal-mode both (gives signal_in_channels=2), then evaluate it:

RuntimeError: Error(s) in loading state_dict for ConvLSTMRemoraBase:
	size mismatch for signal_branch.layers.0.weight: copying a param with shape
	    torch.Size([4, 2, 5]) from checkpoint, the shape in current model is torch.Size([4, 1, 5]).
	size mismatch for fc.weight: copying a param with shape torch.Size([1, 64]) ... is torch.Size([2, 64]).
	size mismatch for fc.bias: copying a param with shape torch.Size([1]) ... is torch.Size([2]).

The checkpoint's own config.json records num_out: 1 and the training run clearly used a 2-channel signal, so the information needed to rebuild it correctly is present and is being discarded.

Observed on leech 0.14.1 across three seeds: every ConvLSTMRemoraBase arm failed this way, while every ConvLSTMRemora arm — same module, same training command, same corpus — evaluated normally.

Cause

model_loading._instantiate_model (around lines 252-266):

sig = inspect.signature(model_class)
has_var_keyword = any(p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values())
if has_var_keyword:
    # Constructor accepts **kwargs — resolve params from the parent class
    for base in model_class.__mro__[1:]:
        ...
        valid_params = set(base_sig.parameters.keys()) - {"self"}
        break

ConvLSTMRemoraBase.__init__ ends in **kwargs (models/conv_lstm_remora.py:140), so valid_params comes from BaseModel, which does not declare num_out or signal_in_channels. ConvLSTMRemora has no **kwargs in its __init__ and therefore keeps its own signature — which is why the two behave differently.

The asymmetry is easy to miss because the failure is silent at train time and only appears when something later tries to load the checkpoint.

Suggested fix

Union the class's own named parameters with the parent's, rather than replacing them:

valid_params = {p for p in sig.parameters if p != "self"} | parent_params

Anything in config.json that the constructor accepts should survive the filter regardless of where in the MRO it is declared. A regression test that round-trips a checkpoint whose num_out differs from the class default would pin it.

Happy to send a PR.

Lingua principale
Python
Stelle
0
Fork
0
Merge medio
11h 17m
PR unite (30g)
23

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rnabioco/leech

Tutte le issue di rnabioco/leech

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.