Download objectrelator/model/builder.py from YuqianFu/ObjectRelator: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/YuqianFu/ObjectRelator/resolve/main/objectrelator/model/builder.py
- Command line
-
hf download hf://YuqianFu/ObjectRelator/objectrelator/model/builder.py
-
curl -L -o builder.py https://huggingface.co/YuqianFu/ObjectRelator/resolve/main/objectrelator/model/builder.py
2.84 kB
| # Copyright 2023 Haotian Liu | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import os | |
| import warnings | |
| import shutil | |
| from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig, BitsAndBytesConfig | |
| import torch | |
| from objectrelator.model import * | |
| from objectrelator.constants import DEFAULT_IMAGE_PATCH_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN | |
| from objectrelator.train.train_datasets import get_mask_config | |
| from objectrelator.model.language_model.llava_phi import PSALM, PSALMForDAVISEval, ObjectRelator | |
| def load_pretrained_model(model_path, model_base, model_name, model_args, mask_config='./objectrelator/mask_config/maskformer2_swin_base_384_bs16_50ep.yaml', load_8bit=False, load_4bit=False, device_map="auto", device="cuda"): | |
| kwargs = {"device_map": 'cpu'} | |
| if load_8bit: | |
| kwargs['load_in_8bit'] = True | |
| elif load_4bit: | |
| kwargs['load_in_4bit'] = True | |
| kwargs['quantization_config'] = BitsAndBytesConfig( | |
| load_in_4bit=True, | |
| bnb_4bit_compute_dtype=torch.float16, | |
| bnb_4bit_use_double_quant=True, | |
| bnb_4bit_quant_type='nf4' | |
| ) | |
| else: | |
| kwargs['torch_dtype'] = torch.float16 | |
| model_map = { | |
| 'psalm': PSALM, | |
| 'psalm_video': PSALMForDAVISEval, | |
| 'ObjectRelator': ObjectRelator | |
| } | |
| mask_cfg = get_mask_config(mask_config) | |
| mask_cfg.MODEL.MASK_FORMER.SEG_TASK = model_args.seg_task if hasattr(model_args, 'seg_task') else 'instance' | |
| tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=True) | |
| if model_name not in model_map: | |
| raise ValueError(f"Model {model_name} is not supported. Supported models are: {list(model_map.keys())}") | |
| model_map_name = model_name #debug | |
| print(f'current model is {model_map_name}') | |
| model = model_map[model_map_name].from_pretrained(model_path, mask_decoder_cfg=mask_cfg, **kwargs) | |
| vision_tower = model.get_vision_tower() | |
| vision_tower.to(device=device) | |
| image_processor = vision_tower.image_processor | |
| if hasattr(model.config, "max_sequence_length"): | |
| context_len = model.config.max_sequence_length | |
| else: | |
| context_len = 2048 | |
| return tokenizer, model, image_processor, context_len | |