DEBUG: """ import torch from transformers import AutoModelForCausalLM, AutoTokenizer…
- User
DEBUG:
""" import torch from transformers import AutoModelForCausalLM, AutoTokenizer from sklearn.cluster import KMeans, AgglomerativeClustering from typing import List, Dict, Union
def load_model( model_name: str = 'dicta-il/dictalm2.0-AWQ', device: str = 'cuda', offload_folder: str = None ): """ Load the DIctaLM2.0 model and tokenizer for embedding extraction, ensuring the tokenizer has a pad token defined. Supports offloading and attention slicing to reduce peak GPU. """ tokenizer = AutoTokenizer.from_pretrained(model_name) if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token
code# Load in 4bit with offload and quantization model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype=torch.bfloat16, # load_in_4bit=True, device_map='auto', offload_folder=offload_folder, offload_state_dict=True, ) model.resize_token_embeddings(len(tokenizer)) model.eval() # Reduce memory by slicing attention and enabling checkpointing try: model.enable_attention_slicing() except Exception: pass try: model.gradient_checkpointing_enable() except Exception: pass return model, tokenizerdef get_embeddings( texts: List[str], model: AutoModelForCausalLM, tokenizer: AutoTokenizer, device: str = 'cuda', max_length: int = None, batch_size: int = 8 ) -> torch.Tensor: """ Compute sentence embeddings in small batches to avoid OOM. """ if max_length is None: max_length = getattr(tokenizer, 'model_max_length', 512) if max_length > 1024: max_length = 512
code - Assistant
- User
OutOfMemoryError Traceback (most recent call last) /tmp/ipython-input-001-(459)848-6823.py in <cell line: 0>() 4 for k in range(5, 9): 5 print(f"\n~~ Clusters for k={k} ~~\n") ----> 6 clusters = cluster_texts(samples, max_length=512, method='kmeans', n_clusters=k, device='cuda', batch_size=1) 7 # save a pickle and a text file of the results 8 with open(f'clusters_{k}.pkl', 'wb') as f:
7 frames /usr/local/lib/python3.11/dist-packages/torch/nn/modules/module.py in convert(t) 1327 memory_format=convert_to_format, 1328 ) -> 1329 return t.to( 1330 device, 1331 dtype if t.is_floating_point() or t.is_complex() else None,
OutOfMemoryError: CUDA out of memory. Tried to allocate 260.00 MiB. GPU 0 has a total capacity of 22.16 GiB of which 155.38 MiB is free. Process 3310 has 22.00 GiB memory in use. Of the allocated memory 21.49 GiB is allocated by PyTorch, and 296.49 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
- Assistant