Skip to content

Model Interpretation

dnallm.inference.interpret

Classes

DNAInterpret

DNAInterpret(model, tokenizer, config=None)

A class for interpreting DNA language models using Captum.

Usage:

model, tokenizer = load_model_and_tokenizer(...) interpreter = DNAInterpret(model, tokenizer) tokens, scores = interpreter.run_lig( input_seq="ACGT...", target=1, task_type="seq_clf" )

Initialize the interpreter.

Parameters:

Name Type Description Default
model PreTrainedModel

Model with a task head.

required
tokenizer PreTrainedTokenizer

Tokenizer for the model.

required
Source code in dnallm/inference/interpret.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
def __init__(
    self,
    model: PreTrainedModel,
    tokenizer: PreTrainedTokenizer,
    config: dict | None = None,
):
    """
    Initialize the interpreter.

    Args:
        model (PreTrainedModel): Model with a task head.
        tokenizer (PreTrainedTokenizer): Tokenizer for the model.
    """
    self.model = model.eval()
    self.tokenizer = tokenizer
    self.task_config = config["task"]
    self.pred_config = config["inference"]
    self.device = self.pred_config.device
    self.embedding_layer = None
    self.model.to(self.device)

    # Find suitable PAD token ID for baselines
    if hasattr(tokenizer, "pad_token_id"):
        if tokenizer.pad_token_id is not None:
            self.pad_token_id = tokenizer.pad_token_id
        else:
            if hasattr(tokenizer, "eos_token_id"):
                if tokenizer.eos_token_id is not None:
                    print(
                        "Warning: tokenizer.pad_token_id is None. "
                        "Using tokenizer.eos_token_id as pad token "
                        "for baselines."
                    )
                    self.pad_token_id = tokenizer.eos_token_id
                else:
                    print(
                        "Warning: No pad_token_id or eos_token_id "
                        "found. Using 0. This may be incorrect."
                    )
                    self.pad_token_id = 0
    else:
        if hasattr(tokenizer, "pad_token"):
            pad_token = tokenizer.pad_token
            if hasattr(tokenizer, "convert_tokens_to_ids"):
                self.pad_token_id = tokenizer.convert_tokens_to_ids(
                    pad_token
                )
            elif hasattr(tokenizer, "tokenize"):
                self.pad_token_id = tokenizer.tokenize(pad_token)[0]
            elif hasattr(tokenizer, "encode"):
                self.pad_token_id = tokenizer.encode(
                    pad_token, add_special_tokens=False
                )[0]
            else:
                print(
                    "Warning: Cannot determine pad_token_id. "
                    "Using 0. This may be incorrect."
                )
                self.pad_token_id = 0
Functions
batch_interpret
batch_interpret(
    input_seqs,
    method,
    targets,
    token_indices=None,
    target_layers=None,
    max_length=None,
    plot=True,
    **kwargs,
)

Batch interpret multiple sequences.

Parameters:

Name Type Description Default
input_seqs list[str]

List of input DNA sequences.

required
method str

Attribution method name.

required
targets list[int]

List of target class indices for each sequence.

required
token_indices list[int]

Each sequence's token_index list.

None
target_layers list[Module]

Each sequence's target_layer list.

None
max_length int

Max token length for tokenizer.

None
plot bool

Whether to store attributions for plotting.

True
**kwargs Any

Extra args for specific attribution methods.

{}

Returns:

Type Description
list[tuple[list[str], ndarray]]

list[tuple[list[str], np.ndarray]]: List of (tokens list, attribution scores array) tuples.

Source code in dnallm/inference/interpret.py
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
def batch_interpret(
    self,
    input_seqs: list[str],
    method: str,
    targets: list[int],
    token_indices: list[int] | None = None,
    target_layers: list[nn.Module] | None = None,
    max_length: int | None = None,
    plot: bool = True,
    **kwargs: Any,
) -> list[tuple[list[str], np.ndarray]]:
    """
    Batch interpret multiple sequences.

    Args:
        input_seqs (list[str]): List of input DNA sequences.
        method (str): Attribution method name.
        targets (list[int]): List of target class indices for each
            sequence.
        token_indices (list[int], optional): Each sequence's token_index
            list.
        target_layers (list[nn.Module], optional): Each sequence's
            target_layer list.
        max_length (int, optional): Max token length for tokenizer.
        plot (bool): Whether to store attributions for plotting.
        **kwargs (Any): Extra args for specific attribution methods.

    Returns:
        list[tuple[list[str], np.ndarray]]: List of (tokens list,
            attribution scores array) tuples.
    """
    results = []
    for i, seq in enumerate(input_seqs):
        token_index = None
        if token_indices is not None:
            token_index = token_indices[i]
        target_layer = None
        if target_layers is not None:
            target_layer = target_layers[i]
        tokens, scores = self.interpret(
            input_seq=seq,
            method=method,
            target=targets[i],
            token_index=token_index,
            target_layer=target_layer,
            max_length=max_length,
            **kwargs,
        )
        results.append((tokens, scores))
    if plot:
        self.attributions = results
    else:
        self.attributions = None

    return results
interpret
interpret(
    input_seq,
    method,
    target,
    token_index=None,
    target_layer=None,
    max_length=None,
    plot=True,
    **kwargs,
)

A unified interpretation interface that runs the specified attribution method.

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
method str

Attribution method name. Supported: 'lig', 'deeplift', 'gradshap', 'occlusion', 'feature_ablation', 'layer_conductance', 'noise_tunnel'.

required
target int | str

Target for attribution.

required
token_index int

For 'token_cls'/'causal_lm' token position to explain.

None
target_layer Module

For 'layer_conductance', internal layer to analyze.

None
max_length int

Max token length for tokenizer.

None
plot bool

Whether to store attributions for plotting.

True
**kwargs Any

Extra args for specific attribution methods.

{}

Returns:

Type Description
tuple[list[str], ndarray]

Tuple[List[str], np.ndarray]: tokens list, attribution scores array

Source code in dnallm/inference/interpret.py
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
def interpret(
    self,
    input_seq: str,
    method: str,
    target: int | str,
    token_index: int | None = None,
    target_layer: nn.Module | None = None,
    max_length: int | None = None,
    plot: bool = True,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    A unified interpretation interface that runs
    the specified attribution method.

    Args:
        input_seq (str): Input DNA sequence.
        method (str): Attribution method name.
            Supported: 'lig', 'deeplift', 'gradshap',
            'occlusion', 'feature_ablation',
            'layer_conductance', 'noise_tunnel'.
        target (int | str): Target for attribution.
        token_index (int, optional): For 'token_cls'/'causal_lm'
            token position to explain.
        target_layer (nn.Module, optional): For 'layer_conductance',
            internal layer to analyze.
        max_length (int, optional): Max token length for tokenizer.
        plot (bool): Whether to store attributions for plotting.
        **kwargs (Any): Extra args for specific attribution methods.

    Returns:
        Tuple[List[str], np.ndarray]: tokens list, attribution scores array
    """
    method = method.lower()
    if method == "lig":
        tokens, attr_scores = self.run_lig(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    elif method == "deeplift":
        tokens, attr_scores = self.run_deeplift(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    elif method == "gradshap":
        tokens, attr_scores = self.run_gradshap(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    elif method == "occlusion":
        tokens, attr_scores = self.run_occlusion(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    elif method == "feature_ablation":
        tokens, attr_scores = self.run_feature_ablation(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    elif method == "layer_conductance":
        if target_layer is None:
            raise ValueError(
                "`target_layer` must be provided for "
                "`layer_conductance` method."
            )
        tokens, attr_scores = self.run_layer_conductance(
            input_seq,
            target,
            target_layer,
            token_index,
            max_length=max_length,
            **kwargs,
        )
    elif method == "noise_tunnel":
        tokens, attr_scores = self.run_noise_tunnel(
            input_seq, target, token_index, max_length=max_length, **kwargs
        )
    else:
        raise ValueError(
            f"Unknown method: {method}. "
            "Supported methods: 'lig', 'deeplift', "
            "'gradshap', 'occlusion', 'feature_ablation', "
            "'layer_conductance', 'noise_tunnel'."
        )
    if plot:
        self.attributions = (tokens, attr_scores)
    else:
        self.attributions = None

    return tokens, attr_scores
plot_attributions
plot_attributions(plot_type='token', **kwargs)

Plot the attributions using specified plot type.

Parameters:

Name Type Description Default
plot_type str

Plot type, 'token' (default), 'line', or 'multi'.

'token'
**kwargs

Extra parameters for specific plotting functions.

{}
Source code in dnallm/inference/interpret.py
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
def plot_attributions(self, plot_type: str = "token", **kwargs):
    """
    Plot the attributions using specified plot type.

    Args:
        plot_type (str): Plot type, 'token' (default), 'line', or 'multi'.
        **kwargs: Extra parameters for specific plotting functions.
    """
    if self.attributions is None:
        raise RuntimeError(
            "No attributions found. "
            "Please run `interpret` or `batch_interpret` "
            "with `plot=True` first."
        )

    plot_type = plot_type.lower()
    if isinstance(self.attributions, list):
        if plot_type != "multi":
            print(
                "Warning: Multiple attributions found, "
                "falling back to 'multi' plot."
            )
        # Multiple sequences' attributions
        plot = plot_attributions_multi(self.attributions, **kwargs)
    elif plot_type == "token":
        # Single sequence's token attribution plot
        tokens, scores = self.attributions
        plot = plot_attributions_token(tokens, scores, **kwargs)
    elif plot_type == "line":
        # Single sequence's line attribution plot
        tokens, scores = self.attributions
        plot = plot_attributions_line(tokens, scores, **kwargs)
    elif plot_type == "multi":
        # Multiple sequences' attributions
        plot = plot_attributions_multi(self.attributions, **kwargs)
    else:
        raise ValueError(
            f"Unknown plot_type: {plot_type}. "
            "Supported: 'token', 'line', 'multi'."
        )
    return plot
run_deeplift
run_deeplift(
    input_seq,
    target,
    token_index=None,
    embedding_layer=None,
    max_length=None,
    **kwargs,
)

Run DeepLIFT for attribution to Embedding layer.

Source code in dnallm/inference/interpret.py
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
def run_deeplift(
    self,
    input_seq: str,
    target: int | str,
    token_index: int | None = None,
    embedding_layer: nn.Module | None = None,
    max_length: int | None = None,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run DeepLIFT for attribution to Embedding layer.
    """
    wrapper = _CaptumWrapperInputIDs(self.model)
    if embedding_layer is None:
        embedding_layer = self._find_embedding_layer()

    # Use LayerDeepLift
    ldl = LayerDeepLift(wrapper, embedding_layer)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baseline_input_ids = self._get_pad_baseline(input_ids)
    captum_target = self._format_captum_target(target, token_index)

    attributions = ldl.attribute(
        inputs=input_ids,
        baselines=baseline_input_ids,
        target=captum_target,
        additional_forward_args=(attention_mask,),
        **kwargs,
    )

    attr_scores = (
        attributions.sum(dim=-1).squeeze(0).cpu().detach().numpy()
    )

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_feature_ablation
run_feature_ablation(
    input_seq,
    target,
    token_index=None,
    max_length=None,
    **kwargs,
)

Run Feature Ablation (perturbation-based method).

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
target int | str

Target for attribution.

required
token_index int

Token position to explain.

None
max_length int

Max token length for tokenizer.

None
**kwargs Any

Additional arguments for FeatureAblation.attribute.

{}

Returns:

Type Description
tuple[list[str], ndarray]

Tuple[List[str], np.ndarray]: tokens list, attribution scores array

Source code in dnallm/inference/interpret.py
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
def run_feature_ablation(
    self,
    input_seq: str,
    target: int | str,
    token_index: int | None = None,
    max_length: int | None = None,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run Feature Ablation (perturbation-based method).

    Args:
        input_seq (str): Input DNA sequence.
        target (int | str): Target for attribution.
        token_index (int, optional): Token position to explain.
        max_length (int, optional): Max token length for tokenizer.
        **kwargs (Any): Additional arguments for FeatureAblation.attribute.

    Returns:
        Tuple[List[str], np.ndarray]: tokens list, attribution scores array
    """
    # 1. Use wrapper that accepts input_ids
    wrapper = _CaptumWrapperInputIDs(self.model)
    ablation = FeatureAblation(wrapper)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 2. Prepare inputs and baselines
    # (FeatureAblation requires a tensor baseline)
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baselines = self._get_pad_baseline(input_ids)

    # 3. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 4. FeatureAblation requires feature_mask to define features
    # (default one feature per token)
    # Shape: (batch_size, num_features) -> (1, seq_len)
    feature_mask = (
        torch.arange(input_ids.shape[1]).unsqueeze(0).to(self.device)
    )

    # 5. Compute attributions
    attributions = ablation.attribute(
        inputs=input_ids,
        target=captum_target,
        additional_forward_args=(attention_mask,),
        baselines=baselines,
        feature_mask=feature_mask,
        **kwargs,
    )

    # 6. Process results
    # Shape: (batch, seq_len) -> (seq_len)
    attr_scores = attributions.squeeze(0).cpu().detach().numpy()

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_gradshap
run_gradshap(
    input_seq,
    target,
    token_index=None,
    max_length=None,
    n_samples=5,
    **kwargs,
)

Run GradientSHAP at the Embedding layer. Attention: GradientSHAP can be slow.

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
target int | str

Target for attribution.

required
token_index int

Token position to explain.

None
max_length int

Max token length for tokenizer.

None
n_samples int

Number of samples to draw from the baseline.

5
**kwargs Any

Additional arguments for GradientShap.attribute.

{}
Source code in dnallm/inference/interpret.py
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
def run_gradshap(
    self,
    input_seq: str,
    target: int | str,
    token_index: int | None = None,
    max_length: int | None = None,
    n_samples: int = 5,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run GradientSHAP at the Embedding layer.
    Attention: GradientSHAP can be slow.

    Args:
        input_seq (str): Input DNA sequence.
        target (int | str): Target for attribution.
        token_index (int, optional): Token position to explain.
        max_length (int, optional): Max token length for tokenizer.
        n_samples (int): Number of samples to draw from the baseline.
        **kwargs (Any): Additional arguments for GradientShap.attribute.
    """
    # 1. Use wrapper that accepts inputs_embeds
    wrapper = _CaptumWrapperInputEmbeds(self.model)

    # 2. Use basic GradientShap (non-layer version)
    gs = GradientShap(wrapper)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 3. Prepare Embeddings (the same as LayerConductance)
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baseline_input_ids = self._get_pad_baseline(input_ids)
    embedding_layer = self._find_embedding_layer()
    with torch.no_grad():
        inputs_embeds = embedding_layer(input_ids)
        baseline_embeds = embedding_layer(baseline_input_ids)

    # 4. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 5. Call attribute at the embeds level
    attributions = gs.attribute(
        inputs=inputs_embeds,  # Pass in embeds
        baselines=baseline_embeds,  # Pass in baseline embeds
        n_samples=n_samples,
        target=captum_target,
        additional_forward_args=(attention_mask,),
        **kwargs,
    )

    # 6. Process results (shape: (batch, seq_len, embed_dim) -> (seq_len))
    attr_scores = (
        attributions.sum(dim=-1).squeeze(0).cpu().detach().numpy()
    )

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_layer_conductance
run_layer_conductance(
    input_seq,
    target,
    target_layer,
    token_index=None,
    max_length=None,
    **kwargs,
)

Run Layer Conductance for attribution to an internal layer.

Important: 1. This method assumes self.model.forward supports 'inputs_embeds'. 2. You must manually pass in 'target_layer'.

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
target int | str

Target for attribution.

required
target_layer Module

Internal layer to analyze. For example: model.bert.encoder.layer[-1].

required
token_index int

Token position to explain.

None
max_length int

Max token length for tokenizer.

None
**kwargs Any

Additional arguments for LayerConductance.attribute.

{}

Returns:

Type Description
tuple[list[str], ndarray]

Tuple[List[str], np.ndarray]: tokens list, attribution scores array

Source code in dnallm/inference/interpret.py
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
def run_layer_conductance(
    self,
    input_seq: str,
    target: int | str,
    target_layer: nn.Module,
    token_index: int | None = None,
    max_length: int | None = None,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run Layer Conductance for attribution to an internal layer.

    Important:
    1. This method assumes self.model.forward supports 'inputs_embeds'.
    2. You must manually pass in 'target_layer'.

    Args:
        input_seq (str): Input DNA sequence.
        target (int | str): Target for attribution.
        target_layer (nn.Module): Internal layer to analyze.
            For example: `model.bert.encoder.layer[-1]`.
        token_index (int, optional): Token position to explain.
        max_length (int, optional): Max token length for tokenizer.
        **kwargs (Any): Additional arguments for
            LayerConductance.attribute.

    Returns:
        Tuple[List[str], np.ndarray]: tokens list, attribution scores array
    """
    # 1. Use wrapper that accepts inputs_embeds
    wrapper = _CaptumWrapperInputEmbeds(self.model)
    lc = LayerConductance(wrapper, target_layer)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 2. Prepare inputs (input_ids)
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baseline_input_ids = self._get_pad_baseline(input_ids)

    # 3. Manually convert IDs to Embeddings
    embedding_layer = self._find_embedding_layer()
    with torch.no_grad():
        inputs_embeds = embedding_layer(input_ids)
        baseline_embeds = embedding_layer(baseline_input_ids)

    # 4. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 5. Compute attributions (inputs are embeds)
    attributions_tuple = lc.attribute(
        inputs=inputs_embeds,  # <--- Pass in embeds
        baselines=baseline_embeds,  # <--- Pass in embeds
        target=captum_target,
        additional_forward_args=(attention_mask,),
        **kwargs,
    )

    # 6. Process results
    if isinstance(attributions_tuple, tuple):
        attributions = attributions_tuple[0]
    else:
        attributions = attributions_tuple
    # Shape: (batch, seq_len, hidden_dim) -> (seq_len)
    attr_scores = attributions.sum(dim=-1).squeeze(0)
    attr_scores = attr_scores.cpu().detach().numpy()

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_lig
run_lig(
    input_seq,
    target,
    token_index=None,
    embedding_layer=None,
    max_length=None,
    **kwargs,
)

Run Layer Integrated Gradients (LIG) for attribution to the Embedding layer. This is the most recommended method for k-mer importance analysis.

Parameters:

Name Type Description Default
input_seq str

input DNA sequence.

required
target int

Target for attribution. - For 'seq_cls': target index (e.g., 1). - for 'token_cls': target type index (e.g., 2 for 'Promoter'). - For 'causal_lm': Target Token ID (e.g., 8 for 'G').

required
task_type str

Task type

required
token_index int

for 'token_cls'/'causal_lm' Token position to explain.

None
embedding_layer Module

Specific Embedding layer. Auto-detected if None.

None
max_length int

Max token length for tokenizer.

None
**kwargs Any

Extra arguments for captum.attr.LayerIntegratedGradients.attribute (for example: internal_batch_size=4).

{}

Returns:

Type Description
tuple[list[str], ndarray]

Tuple[List[str], np.ndarray]: tokens list, attribution scores array

Source code in dnallm/inference/interpret.py
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
def run_lig(
    self,
    input_seq: str,
    target: int | str,
    token_index: int | None = None,
    embedding_layer: nn.Module | None = None,
    max_length: int | None = None,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run Layer Integrated Gradients (LIG) for
    attribution to the Embedding layer.
    This is the most recommended method for k-mer importance analysis.

    Args:
        input_seq (str): input DNA sequence.
        target (int): Target for attribution.
                      - For 'seq_cls': target index (e.g., 1).
                      - for 'token_cls': target type index
                            (e.g., 2 for 'Promoter').
                      - For 'causal_lm': Target *Token ID*
                             (e.g., 8 for 'G').
        task_type (str): Task type
        token_index (int, optional): for 'token_cls'/'causal_lm'
            Token position to explain.
        embedding_layer (nn.Module, optional): Specific Embedding layer.
            Auto-detected if None.
        max_length (int): Max token length for tokenizer.
        **kwargs (Any): Extra arguments for
            captum.attr.LayerIntegratedGradients.attribute
            (for example: internal_batch_size=4).

    Returns:
        Tuple[List[str], np.ndarray]: tokens list, attribution scores array
    """
    # 1. Use wrapper that accepts input_ids
    wrapper = _CaptumWrapperInputIDs(self.model)

    # 2. Find or use specified Embedding layer
    if embedding_layer is None:
        embedding_layer = self._find_embedding_layer()

    lig = LayerIntegratedGradients(wrapper, embedding_layer)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 3. Prepare inputs and baselines
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baseline_input_ids = self._get_pad_baseline(input_ids)

    # 4. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 5. Compute attributions
    attributions = lig.attribute(
        inputs=input_ids,
        baselines=baseline_input_ids,
        target=captum_target,
        additional_forward_args=(attention_mask,),
        **kwargs,
    )

    # 6. Process results
    # Shape: (batch, seq_len, embed_dim) -> (seq_len)
    attr_scores = attributions.sum(dim=-1).squeeze(0)
    attr_scores = attr_scores.cpu().detach().numpy()

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_noise_tunnel
run_noise_tunnel(
    input_seq,
    target,
    base_method,
    token_index=None,
    max_length=None,
    nt_type="smoothgrad",
    nt_samples=5,
    nt_stdevs=0.1,
    **kwargs,
)

Run NoiseTunnel (e.g., SmoothGrad) for smoother attributions. This will run at the 'inputs_embeds' level to avoid type conflicts with 'nn.Embedding'.

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
target int | str

Target for attribution.

required
base_method str

The base attribution method to use. Supported: 'lig' (IntegratedGradients), 'deeplift' (DeepLift), 'gradshap' (GradientShap).

required
token_index int

Token position to explain.

None
max_length int

Max token length for tokenizer.

None
nt_type str

'smoothgrad' (default), 'smoothgrad_sq' or 'vargrad'.

'smoothgrad'
nt_samples int

The number of noise samples.

5
nt_stdevs float

The standard deviation of the noise.

0.1
**kwargs Any

Additional arguments for NoiseTunnel.

{}
Source code in dnallm/inference/interpret.py
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
def run_noise_tunnel(
    self,
    input_seq: str,
    target: int | str,
    base_method: str,
    token_index: int | None = None,
    max_length: int | None = None,
    nt_type: str = "smoothgrad",
    nt_samples: int = 5,
    nt_stdevs: float = 0.1,
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run NoiseTunnel (e.g., SmoothGrad) for smoother attributions.
    This will run at the 'inputs_embeds' level to avoid
    type conflicts with 'nn.Embedding'.

    Args:
        input_seq (str): Input DNA sequence.
        target (int | str): Target for attribution.
        base_method (str): The base attribution method to use.
            Supported: 'lig' (IntegratedGradients), 'deeplift' (DeepLift),
            'gradshap' (GradientShap).
        token_index (int, optional): Token position to explain.
        max_length (int, optional): Max token length for tokenizer.
        nt_type (str): 'smoothgrad' (default), 'smoothgrad_sq'
            or 'vargrad'.
        nt_samples (int): The number of noise samples.
        nt_stdevs (float): The standard deviation of the noise.
        **kwargs (Any): Additional arguments for NoiseTunnel.
    """
    print(
        f"Running NoiseTunnel ({nt_type}) "
        f"with base method: {base_method}..."
    )

    # 1. Use wrapper that accepts inputs_embeds
    wrapper = _CaptumWrapperInputEmbeds(self.model)

    # 2. Select base attribution method (non-Layer version)
    if base_method.lower() == "lig":
        attr_method = IntegratedGradients(wrapper)
    elif base_method.lower() == "deeplift":
        attr_method = DeepLift(wrapper)
    elif base_method.lower() == "gradshap":
        attr_method = GradientShap(wrapper)
    else:
        raise ValueError(
            f"Unknown base_method: {base_method}. "
            "Supported: 'lig', 'deeplift', 'gradshap'"
        )

    # 3. Wrap with NoiseTunnel
    nt = NoiseTunnel(attr_method)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 4. Prepare Embeddings (same as LayerConductance)
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )
    baseline_input_ids = self._get_pad_baseline(input_ids)
    embedding_layer = self._find_embedding_layer()
    with torch.no_grad():
        inputs_embeds = embedding_layer(input_ids)
        baseline_embeds = embedding_layer(baseline_input_ids)

    # 5. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 6. Prepare attribution parameters
    attr_kwargs = {
        "target": captum_target,
        "additional_forward_args": (attention_mask,),
        "nt_type": nt_type,
        "nt_samples": nt_samples,
        "stdevs": nt_stdevs,
        **kwargs,
    }

    # 7. Call attribute method
    # GradientShap need n_samples and baselines (distribution)
    if base_method.lower() == "gradshap":
        attributions = nt.attribute(
            inputs=inputs_embeds,
            baselines=baseline_embeds,
            n_samples=nt_samples,  # gradshap own n_samples
            **attr_kwargs,
        )
    else:  # LIG or DeepLIFT
        attributions = nt.attribute(
            inputs=inputs_embeds, baselines=baseline_embeds, **attr_kwargs
        )

    # 8. Process results
    # Shape: (batch, seq_len, hidden_dim) -> (seq_len)
    attr_scores = (
        attributions.sum(dim=-1).squeeze(0).cpu().detach().numpy()
    )

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores
run_occlusion
run_occlusion(
    input_seq,
    target,
    token_index=None,
    max_length=None,
    sliding_window_shapes=(1,),
    **kwargs,
)

Run Occlusion (perturbation-based method). Attention: This method can be very slow.

Parameters:

Name Type Description Default
input_seq str

Input DNA sequence.

required
target int | str

Target for attribution.

required
token_index int

Token position to explain.

None
max_length int

Max token length for tokenizer.

None
sliding_window_shapes tuple[int]

Size of the occlusion window. (1,) means occluding 1 token at a time.

(1,)
**kwargs Any

Additional arguments for Occlusion.attribute.

{}

Returns:

Type Description
tuple[list[str], ndarray]

Tuple[List[str], np.ndarray]: tokens list, attribution scores array

Source code in dnallm/inference/interpret.py
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
def run_occlusion(
    self,
    input_seq: str,
    target: int | str,
    token_index: int | None = None,
    max_length: int | None = None,
    sliding_window_shapes: tuple[int] = (1,),
    **kwargs: Any,
) -> tuple[list[str], np.ndarray]:
    """
    Run Occlusion (perturbation-based method).
    Attention: This method can be very slow.

    Args:
        input_seq (str): Input DNA sequence.
        target (int | str): Target for attribution.
        token_index (int, optional): Token position to explain.
        max_length (int, optional): Max token length for tokenizer.
        sliding_window_shapes (tuple[int]): Size of the occlusion window.
            (1,) means occluding 1 token at a time.
        **kwargs (Any): Additional arguments for Occlusion.attribute.

    Returns:
        Tuple[List[str], np.ndarray]: tokens list, attribution scores array
    """
    # 1. Use wrapper that accepts input_ids
    wrapper = _CaptumWrapperInputIDs(self.model)
    occlusion = Occlusion(wrapper)

    # Get max token length from config if not provided
    if max_length is None:
        max_length = self.pred_config.max_length

    # 2. Prepare inputs
    input_ids, attention_mask = self._get_input_tensors(
        input_seq, max_length
    )

    # 3. Occlusion baseline is a single PAD token ID (scalar)
    baselines = self.pad_token_id

    # 4. Format Captum target
    captum_target = self._format_captum_target(target, token_index)

    # 5. Compute attributions
    attributions = occlusion.attribute(
        inputs=input_ids,
        sliding_window_shapes=sliding_window_shapes,
        target=captum_target,
        additional_forward_args=(attention_mask,),
        baselines=baselines,
        **kwargs,
    )

    # 6. Process results
    # Shape: (batch, seq_len) -> (seq_len)
    attr_scores = attributions.squeeze(0).cpu().detach().numpy()

    tokens = self._ids_to_tokens(
        token_ids=input_ids.squeeze(0), input_seq=input_seq
    )
    return tokens, attr_scores

Functions