super(AutoModelForSentenceEmbedding, self).init() 24 self.model = model ---> 25…
Private research annotation
Sign in to add a private research label or note.
- User
super(AutoModelForSentenceEmbedding, self).init() 24 self.model = model ---> 25 self.model.to("cuda") 26 self.normalize = normalize 27 self.tokenizer = tokenizer
AttributeError: 'str' object has no attribute 'to'
%%writefile /kaggle/working/laserRMT/lib/utils/AutoModelForSentenceEmbedding.py import torch from torch import nn
def get_cosine_embeddings(q1_embs, q2_embs): emb = torch.sum(q1_embs * q2_embs, axis=1) torch.cuda.empty_cache() return emb
def get_loss(cosine_score, labels): return torch.mean( torch.square( labels * (1 - cosine_score) + torch.clamp((1 - labels) * cosine_score, min=0.0) ) )
class AutoModelForSentenceEmbedding(nn.Module): def init(self, model, tokenizer, normalize=True): super(AutoModelForSentenceEmbedding, self).init()
codeself.model = model #self.model.to("cuda") self.normalize = normalize self.tokenizer = tokenizer def forward(self, **kwargs): model_output = self.model(**kwargs) embeddings = self.mean_pooling(model_output, kwargs["attention_mask"]) if self.normalize: embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1) return embeddings def mean_pooling(self, model_output, attention_mask): token_embeddings = model_output[ 0 ] # First element of model_output contains all token embeddings input_mask_expanded = ( attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float() ) return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp( input_mask_expanded.sum(1), min=1e-9 ) def __getattr__(self, name: str): """Forward missing attributes to the wrapped module.""" try: return super().__getattr__(name) # defer to nn.Module's logic except AttributeError: return getattr(self.model, name) - Assistant
- User
289 # Usage 290 model_name = "aloobun/Synch-Qwen1.5-1.8B" --> 291 modifier = ModelModifier(model_name) 293 # %% 294 layer_numbers = list(range(23, -1, -1))
Cell In[5], line 43, in ModelModifier.init(self, model_name, prompt_template, input_length, output_length) 41 self.input_length = input_length 42 self.output_length = output_length ---> 43 self.embeddings_model = AutoModelForSentenceEmbedding( 44 model_name, self.tokenizer 45 )
File /kaggle/working/laserRMT/lib/utils/AutoModelForSentenceEmbedding.py:26, in AutoModelForSentenceEmbedding.init(self, model, tokenizer, normalize) 24 self.model = model 25 if torch.cuda.is_available(): ---> 26 self.model.to("cuda") 27 self.normalize = normalize 28 self.tokenizer = tokenizer
AttributeError: 'str' object has no attribute 'to'
- Assistant