Error in calculating cosine similarity in perturber_utils and solutions

#287
by davidliwei - opened

Hi, I encountered an error when running the examples provided (in silico perturbation), during the "isp_perturb_all". The error message is pasted below. When I look into this error, it seems that the problem comes from the different tensor shapes at cos_sim_shift function (Line 581, 592).

I changed Line 584 from
state_embs_dict[state].to("cuda"), # required to move to cuda here
to
state_embs_dict[state].to("cuda").unsqueeze(0), # required to move to cuda here

And it seems to solve the problem (btw, I have to change cuda to cpu in source code as I tested in a CPU environment). However I don't know if the same error happens to others, so I didn't send a push request.


IndexError Traceback (most recent call last)
Cell In[20], line 1
----> 1 isp.isp_perturb_all(
2 wl_model, filtered_input_data, zlayer_to_quant, "insilico_out/out_perturbed_wl"
3 )

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/geneformer/in_silico_perturber.py:785, in InSilicoPerturber.isp_perturb_all(self, model, filtered_input_data, layer_to_quant, output_path_prefix)
778 original_cell_emb = pu.compute_nonpadded_cell_embedding(
779 full_original_emb, "mean_pool"
780 )
781 perturbation_cell_emb = pu.compute_nonpadded_cell_embedding(
782 full_perturbation_emb, "mean_pool"
783 )
--> 785 cell_cos_sims = pu.quant_cos_sims(
786 perturbation_cell_emb,
787 original_cell_emb,
788 self.cell_states_to_model,
789 self.state_embs_dict,
790 emb_mode="cell",
791 )
793 if self.emb_mode == "cell_and_gene":
794 # remove perturbed index for gene list
795 perturbed_gene_dict = {
796 gene: gene_list[:i] + gene_list[i + 1 :]
797 for i, gene in enumerate(gene_list)
798 }

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/geneformer/perturber_utils.py:584, in quant_cos_sims(perturbation_emb, original_emb, cell_states_to_model, state_embs_dict, emb_mode)
582 cos_sims = dict(zip(possible_states, [[] for _ in range(len(possible_states))]))
583 for state in possible_states:
--> 584 cos_sims[state] = cos_sim_shift(
585 original_emb,
586 perturbation_emb,
587 #state_embs_dict[state].to("cuda"), # required to move to cuda here
588 state_embs_dict[state].to("cpu"), # required to move to cuda here
589 cos,
590 )
592 return cos_sims

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/geneformer/perturber_utils.py:597, in cos_sim_shift(original_emb, perturbed_emb, end_emb, cos)
596 def cos_sim_shift(original_emb, perturbed_emb, end_emb, cos):
--> 597 origin_v_end = cos(original_emb, end_emb)
598 perturb_v_end = cos(perturbed_emb, end_emb)
600 return perturb_v_end - origin_v_end

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/nn/modules/module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/nn/modules/module.py:1527, in Module._call_impl(self, *args, **kwargs)
1522 # If we don't have any hooks, we want to skip the rest of the logic in
1523 # this function, and just call forward.
1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1529 try:
1530 result = None

File ~/miniconda3/envs/pytorch/lib/python3.11/site-packages/torch/nn/modules/distance.py:87, in CosineSimilarity.forward(self, x1, x2)
86 def forward(self, x1: Tensor, x2: Tensor) -> Tensor:
---> 87 return F.cosine_similarity(x1, x2, self.dim, self.eps)

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

Thank you for letting us know! Could you confirm the exact arguments you used so that we can reproduce the error to test the solution? Is line 588 the only one you changed in the source code?

Hi, I just tested it in the GPU environment and the code runs without error. Therefore it may be related to the CPU environment, or changes that I made (I have to change cuda to cpu in many places of the source code). Thanks again!

davidliwei changed discussion status to closed
This comment has been hidden

Sign up or log in to comment