Download code/vendor-patches.diff from CollinMa/nextgpt-chapter6-report: direct link, hf CLI and curl.
- Browser
- Download file 7.68 kB
-
https://huggingface.co/spaces/CollinMa/nextgpt-chapter6-report/resolve/main/code/vendor-patches.diff
- Command line
-
hf download hf://spaces/CollinMa/nextgpt-chapter6-report/code/vendor-patches.diff
-
curl -L -o vendor-patches.diff https://huggingface.co/spaces/CollinMa/nextgpt-chapter6-report/resolve/main/code/vendor-patches.diff
7.68 kB
| --- a/vendor/nextgpt/code/header.py | |
| +++ b/vendor/nextgpt/code/header.py | |
| import torch | |
| import datetime | |
| import types | |
| -import deepspeed | |
| +try: | |
| + import deepspeed | |
| +except ImportError: | |
| + # DeepSpeed is only required by the upstream training entry points. | |
| + deepspeed = None | |
| from transformers.deepspeed import HfDeepSpeedConfig | |
| import transformers | |
| import numpy as np | |
| from copy import deepcopy | |
| import ipdb | |
| import argparse | |
| -import data | |
| from transformers import LlamaTokenizer, LlamaForCausalLM, LlamaConfig | |
| from torch.nn.utils.rnn import pad_sequence | |
| from peft import LoraConfig, TaskType, get_peft_model | |
| --- a/vendor/nextgpt/code/model/ImageBind/data.py | |
| +++ b/vendor/nextgpt/code/model/ImageBind/data.py | |
| DEFAULT_AUDIO_FRAME_SHIFT_MS = 10 # in milliseconds | |
| -BPE_PATH = "bpe/bpe_simple_vocab_16e6.txt.gz" | |
| +from pathlib import Path | |
| +BPE_PATH = str(Path(__file__).resolve().parent / "bpe" / "bpe_simple_vocab_16e6.txt.gz") | |
| def waveform2melspec(waveform, sample_rate, num_mel_bins, target_length): | |
| --- a/vendor/nextgpt/code/model/__init__.py | |
| +++ b/vendor/nextgpt/code/model/__init__.py | |
| import os.path | |
| from collections import OrderedDict | |
| -from .agent import DeepSpeedAgent | |
| from .anyToImageVideoAudio import NextGPTModel | |
| import torch | |
| def load_model(args): | |
| + from .agent import DeepSpeedAgent | |
| agent_name = args['models'][args['model']]['agent_name'] | |
| model_name = args['models'][args['model']]['model_name'] | |
| model = globals()[model_name](**args) | |
| - agent = globals()[agent_name](model, args) | |
| + agent = {"DeepSpeedAgent": DeepSpeedAgent}[agent_name](model, args) | |
| return agent | |
| --- a/vendor/nextgpt/code/model/anyToImageVideoAudio.py | |
| +++ b/vendor/nextgpt/code/model/anyToImageVideoAudio.py | |
| class StoppingCriteriaSub(StoppingCriteria): | |
| - def __init__(self, stops: List = None, encounters: int = 1): | |
| + def __init__(self, stops: List = None, encounters: int = 1, tokenizer=None): | |
| super().__init__() | |
| self.stops = stops | |
| self.ENCOUNTERS = encounters | |
| + self.tokenizer = tokenizer | |
| def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor): | |
| - stop_count = 0 | |
| - for stop in self.stops: | |
| - _stop = torch.tensor(stop).to(input_ids[0].device) | |
| - indices = torch.where(_stop[0] == input_ids) | |
| - for i in indices: | |
| - if len(i) > 0: | |
| - if torch.all(input_ids[0][i:i + len(_stop)] == _stop): | |
| - stop_count += 1 | |
| - if stop_count >= self.ENCOUNTERS: | |
| - return True | |
| + for stop in self.stops or []: | |
| + token = torch.tensor(stop,device=input_ids.device) | |
| + if input_ids.shape[1]>=len(stop) and torch.equal(input_ids[0,-len(stop):],token): | |
| + return True | |
| + if self.tokenizer is not None: | |
| + return '\n###' in self.tokenizer.decode(input_ids[0,-16:],skip_special_tokens=True) | |
| return False | |
| for name, param in self.visual_encoder.named_parameters(): | |
| param.requires_grad = False | |
| self.visual_encoder.eval() | |
| + if self.args.get('low_vram'): | |
| + self.visual_encoder.half() | |
| print('Visual encoder initialized.') | |
| self.vicuna_ckpt_path = os.path.join(self.args['pretrained_ckpt_path'], 'vicuna_ckpt', | |
| self.args['vicuna_version']) | |
| print(f'Initializing language decoder from {self.vicuna_ckpt_path} ...') | |
| - self.llama_model = LlamaForCausalLM.from_pretrained(self.vicuna_ckpt_path) | |
| + self.llama_model = LlamaForCausalLM.from_pretrained( | |
| + self.vicuna_ckpt_path, torch_dtype=torch.float16, low_cpu_mem_usage=True) | |
| if self.args.get('freeze_lm'): | |
| print("Freezing the LLaMa ...") | |
| for param in self.llama_model.parameters(): | |
| video_output_embedding: output embeddings for synthesizing video | |
| audio_output_embedding: output embeddings for synthesizing audio | |
| """ | |
| - stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=inputs['stops_id'], encounters=1)]) | |
| + stopping_criteria = StoppingCriteriaList([StoppingCriteriaSub(stops=inputs['stops_id'], encounters=1, | |
| + tokenizer=self.llama_tokenizer)]) | |
| outputs = self.llama_model.generate( | |
| + input_ids=torch.full((input_embeds.shape[0], 1), self.llama_tokenizer.bos_token_id, | |
| + dtype=torch.long, device=input_embeds.device), | |
| inputs_embeds=input_embeds, | |
| max_new_tokens=inputs['max_tgt_len'], | |
| top_p=inputs['top_p'], | |
| stopping_criteria=stopping_criteria, | |
| output_hidden_states=True, | |
| return_dict_in_generate=True, | |
| - output_attentions=True | |
| + output_attentions=False | |
| ) | |
| output_embeddings = [] | |
| """ | |
| last_ret_idx = 0 | |
| return_outputs = [] | |
| - generation_model = StableDiffusionPipeline.from_pretrained(self.sd_ckpt_path, torch_dtype=torch.float16).to( | |
| - "cuda") | |
| + source = self.args['resolve_decoder']('image') if self.args.get('resolve_decoder') else self.sd_ckpt_path | |
| + generation_model = StableDiffusionPipeline.from_pretrained(source, torch_dtype=torch.float16) | |
| + if self.args.get('low_vram'): | |
| + generation_model.enable_attention_slicing() | |
| + generation_model.vae.enable_slicing() | |
| + generation_model.enable_sequential_cpu_offload() | |
| + else: | |
| + generation_model.to("cuda") | |
| for gen_idx in all_gen_idx: | |
| assert generated_ids[0, | |
| gen_idx:gen_idx + self.args['num_gen_img_tokens']].cpu().detach().numpy().tolist() == self.args[ | |
| """ | |
| return_outputs = [] | |
| last_ret_idx = 0 | |
| - generation_model = TextToVideoSDPipeline.from_pretrained(self.vd_ckpt_path, torch_dtype=torch.float16).to( | |
| - "cuda") | |
| + source = self.args['resolve_decoder']('video') if self.args.get('resolve_decoder') else self.vd_ckpt_path | |
| + generation_model = TextToVideoSDPipeline.from_pretrained(source, torch_dtype=torch.float16) | |
| + if self.args.get('low_vram'): | |
| + generation_model.enable_attention_slicing() | |
| + generation_model.vae.enable_slicing() | |
| + generation_model.enable_sequential_cpu_offload() | |
| + else: | |
| + generation_model.to("cuda") | |
| for gen_idx in all_gen_idx: | |
| assert generated_ids[0, | |
| gen_idx:gen_idx + self.args['num_gen_video_tokens']].cpu().detach().numpy().tolist() == \ | |
| """ | |
| return_outputs = [] | |
| last_ret_idx = 0 | |
| - generation_model = AudioLDMPipeline.from_pretrained(self.ad_ckpt_path, torch_dtype=torch.float16).to("cuda") | |
| + source = self.args['resolve_decoder']('audio') if self.args.get('resolve_decoder') else self.ad_ckpt_path | |
| + generation_model = AudioLDMPipeline.from_pretrained(source, torch_dtype=torch.float16) | |
| + if self.args.get('low_vram'): | |
| + generation_model.enable_sequential_cpu_offload() | |
| + else: | |
| + generation_model.to("cuda") | |
| for gen_idx in all_gen_idx: | |
| assert generated_ids[0, | |
| gen_idx:gen_idx + self.args['num_gen_audio_tokens']].cpu().detach().numpy().tolist() == \ | |