ReFT
zhengxuanzenwu commited on
Commit
ed89a08
·
verified ·
1 Parent(s): 6390521

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +27 -0
README.md CHANGED
@@ -21,8 +21,35 @@ It is a single dictionary of subspaces for 16K concepts and serves as a drop-in
21
  # 3. How can I use these dictionaries straight away?
22
 
23
  ```python
 
24
  import pyvene as pv
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ```
27
 
28
  # 4. Point of Contact
 
21
  # 3. How can I use these dictionaries straight away?
22
 
23
  ```python
24
+ from huggingface_hub import hf_hub_download
25
  import pyvene as pv
26
 
27
+ # Create an intervention.
28
+ class Encoder(pv.CollectIntervention):
29
+ """An intervention that reads concept latent from streams"""
30
+ def __init__(self, **kwargs):
31
+ super().__init__(**kwargs, keep_last_dim=True)
32
+ self.proj = torch.nn.Linear(
33
+ self.embed_dim, kwargs["latent_dim"], bias=False)
34
+ def forward(self, base, source=None, subspaces=None):
35
+ return torch.relu(self.proj(base))
36
+
37
+ # Loading weights
38
+ path_to_params = hf_hub_download(repo_id="pyvene/gemma-reft-2b-it-res", filename="l20/weight.pt")
39
+ encoder = Encoder(embed_dim=params.shape[0], latent_dim=params.shape[1])
40
+ encoder.proj.weight.data = params.float()
41
+
42
+ # Mount the loaded intervention.
43
+ pv_model = pv.IntervenableModel({
44
+ "component": f"model.layers[20].output",
45
+ "intervention": encoder}, model=model)
46
+
47
+ # use pv_model just as other torch model, and you can collect subspace latent.
48
+ prompt = "Would you be able to travel through time using a wormhole?"
49
+ input_ids = torch.tensor([tokenizer.apply_chat_template(
50
+ [{"role": "user", "content": prompt}], tokenize=True, add_generation_prompt=True)]).cuda()
51
+ acts = pv_model.forward(
52
+ {"input_ids": input_ids}, return_dict=True).collected_activations[0]
53
  ```
54
 
55
  # 4. Point of Contact