Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Minor] Update dependency #185

Merged
merged 10 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions pyvene/models/intervenable_base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json, logging, torch, types
import nnsight
import numpy as np
from collections import OrderedDict
from typing import List, Optional, Tuple, Union, Dict, Any
Expand Down Expand Up @@ -27,6 +26,12 @@
from transformers.utils import ModelOutput
from tqdm import tqdm, trange

try:
import nnsight
except:
print("nnsight is not detected. Please install via 'pip install nnsight' for nnsight backend.")


@dataclass
class IntervenableModelOutput(ModelOutput):
"""
Expand Down Expand Up @@ -226,7 +231,7 @@ def __init__(self, config, model, backend, **kwargs):
# cached swapped activations (hot)
self.hot_activations = {}

self.aux_loss = []
self.full_intervention_outputs = []

# temp fields should not be accessed outside
self._batched_setter_activation_select = {}
Expand Down Expand Up @@ -1558,16 +1563,17 @@ def hook_callback(model, args, kwargs, output=None):
else:
if not isinstance(self.interventions[key][0], types.FunctionType):
if intervention.is_source_constant:
intervened_representation = do_intervention(
raw_intervened_representation = do_intervention(
selected_output,
None,
intervention,
subspaces[key_i] if subspaces is not None else None,
)
if isinstance(intervened_representation, InterventionOutput):
if intervened_representation.loss is not None:
self.aux_loss.append(intervened_representation.loss)
intervened_representation = intervened_representation.output
if isinstance(raw_intervened_representation, InterventionOutput):
self.full_intervention_outputs.append(raw_intervened_representation)
intervened_representation = raw_intervened_representation.output
else:
intervened_representation = raw_intervened_representation
else:
intervened_representation = do_intervention(
selected_output,
Expand Down Expand Up @@ -1866,7 +1872,7 @@ def forward(
if sources is not None and not isinstance(sources, list):
sources = [sources]

self.aux_loss.clear()
self.full_intervention_outputs.clear()

self._cleanup_states()

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ numpy>=1.23.5
fsspec>=2023.6.0
accelerate>=0.29.1
sentencepiece>=0.1.96
nnsight>=0.1.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name="pyvene",
version="0.1.4",
version="0.1.5",
description="Use Activation Intervention to Interpret Causal Mechanism of Model",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
Loading