Buckets:
| """Load the model and render chat prompts the same way for every run.""" | |
| import torch | |
| from transformers import AutoConfig, AutoModelForCausalLM, AutoModelForImageTextToText, AutoTokenizer | |
| DTYPES = {"bfloat16": torch.bfloat16, "float16": torch.float16} | |
| def load(repo_id, revision, dtype_name): | |
| """Model on the GPU in the requested dtype, and its tokenizer with left padding. Fails loudly.""" | |
| if not torch.cuda.is_available(): | |
| raise RuntimeError("no CUDA GPU in this runtime. On Colab, connect to a GPU runtime such as T4") | |
| dtype = DTYPES[dtype_name] | |
| config = AutoConfig.from_pretrained(repo_id, revision=revision) | |
| model_cls = AutoModelForImageTextToText if hasattr(config, "vision_config") else AutoModelForCausalLM | |
| tokenizer = AutoTokenizer.from_pretrained(repo_id, revision=revision) | |
| tokenizer.padding_side = "left" | |
| if tokenizer.pad_token_id is None: | |
| raise RuntimeError(f"{repo_id} tokenizer has no pad token") | |
| model = model_cls.from_pretrained(repo_id, revision=revision, dtype=dtype, device_map="cuda") | |
| model.eval() | |
| loaded = {p.dtype for p in model.parameters()} | |
| if loaded != {dtype}: | |
| raise RuntimeError(f"model parameters loaded as {loaded}, expected {dtype}") | |
| return model, tokenizer | |
| def render(tokenizer, user, system=None): | |
| """Chat prompt with thinking turned off through the template switch.""" | |
| messages = ([{"role": "system", "content": system}] if system else []) + [{"role": "user", "content": user}] | |
| return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True, enable_thinking=False) | |
| def eos_ids(model): | |
| ids = model.generation_config.eos_token_id | |
| return set(ids if isinstance(ids, list) else [ids]) | |
Xet Storage Details
- Size:
- 1.74 kB
- Xet hash:
- f47f9ce25cf304b7a4a970e1641710a55b4ae60d123e7996848af538fa3379ac
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.