stanfordnlp / pyreft

ReFT: Representation Finetuning for Language Models
https://arxiv.org/abs/2404.03592
Apache License 2.0
1.12k stars 93 forks source link

[P0] compreft.ipynb error = KeyError: 'subspaces' #72

Closed RonanKMcGovern closed 4 months ago

RonanKMcGovern commented 5 months ago

When running the script it appears that result['subspaces'] has not been initialized meaning that it cannot be appended to in:

result["subspaces"].append(_subspaces)

On manually fixing that, it appears there's an issue permuting the subspaces:

subspaces=inputs["subspaces"].permute(1, 0, 2).tolist() if "subspaces" in inputs else None

because running the training results in:

     75 def compute_loss(
     76     self,
     77     intervenable: pv.IntervenableModel,
   (...)
     80 ):
     81     # run intervened forward pass
     82     _, cf_outputs = intervenable(
     83         {
     84             "input_ids": inputs["input_ids"],
     85             "attention_mask": inputs["attention_mask"]
     86         },
     87         unit_locations={"sources->base": (
     88             None,
     89             inputs["intervention_locations"].permute(1, 0, 2).tolist()
     90         )},
     91         labels=inputs["labels"],
---> 92         subspaces=inputs["subspaces"].permute(1, 0, 2).tolist() if "subspaces" in inputs else None
     93     )
     94     # return
     95     return (cf_outputs.loss, cf_outputs) if return_outputs else cf_outputs.loss

RuntimeError: permute(sparse_coo): number of dimensions in the tensor input does not match the length of the desired ordering of dimensions i.e. input.dim() = 2 is not equal to len(dims) = 3
frankaging commented 5 months ago

@RonanKMcGovern thanks for the question.

Could you print the inputs["subspaces"] dimension, before passing into the call in line 82 above? it should be a 3d tensor.

Before permutation, the first shape should be the batch size, after permutation, the first shape should be 1 if you are doing a single intervention.

frankaging commented 5 months ago

i will update the tutorial - it is currently incompatible with the new infra code.

frankaging commented 5 months ago

@RonanKMcGovern i checked in the fix: https://github.com/stanfordnlp/pyreft/pull/75

(i haven't rerun the notebook to verify tho, but please let me know if it works for your examples.) If it works, feel free to close this issue! ty!

p.s. please install with top of the tree after git pull: pip install .

frankaging commented 4 months ago

marking this issue as closed for now --- if the problem still exists, feel free to reopen or open a new issue.

chris-aeviator commented 2 months ago

yes I think I'm still running into this

`File ~/micromamba/envs/trtf/lib/python3.9/site-packages/pyreft/dataset.py:161, in ReftDataset.init(self, task, data_path, tokenizer, data_split, dataset, seed, max_n_example, kwargs) 159 for i, data_item in enumerate(tqdm(self.task_dataset)): 160 tokenized, last_position = self.tokenize(data_item) --> 161 tokenized = self.compute_intervention_and_subspaces(i, data_item, tokenized, last_position, kwargs) 162 self.result.append(tokenized)

File ~/micromamba/envs/trtf/lib/python3.9/site-packages/pyreft/dataset.py:251, in ReftDataset.compute_intervention_and_subspaces(self, id, data_item, result, last_position, *kwargs) 249 # we now assume each task has a constant subspaces 250 _subspaces = [data_item["subspaces"]] num_interventions --> 251 result["subspaces"] = _subspaces 253 return result

KeyError: 'subspaces'`

EDIT: oh sorry, wrong issue?

EDIT2: nevermind, restarting the notebook somehow fixed this