|
| 1 | +import torch |
| 2 | +from dbrx.neuron_modeling_dbrx import ( |
| 3 | + NeuronDbrxConfig, |
| 4 | + NeuronDbrxForCausalLM, |
| 5 | + NeuronDbrxModel, |
| 6 | +) |
| 7 | +from runner import InferenceRunner |
| 8 | +from transformers import AutoTokenizer |
| 9 | + |
| 10 | +from neuronx_distributed.parallel_layers.checkpointing import _invoke_preshard_hook |
| 11 | + |
| 12 | + |
| 13 | +class DbrxRunner(InferenceRunner): |
| 14 | + def load_hf_model(self): |
| 15 | + config = NeuronDbrxConfig.from_pretrained(self.model_path) |
| 16 | + return NeuronDbrxForCausalLM.load_hf_model(self.model_path, config) |
| 17 | + |
| 18 | + def load_neuron_model_on_cpu(self, max_prompt_length, sequence_length, batch_size, **kwargs): |
| 19 | + # On CPU we can only run tensor parallelism with degree 1 |
| 20 | + config = self.get_config_for_nxd( |
| 21 | + batch_size, |
| 22 | + 1, |
| 23 | + max_prompt_length=max_prompt_length, |
| 24 | + sequence_length=sequence_length, |
| 25 | + enable_bucketing=False, |
| 26 | + **kwargs) |
| 27 | + config.torch_dtype = torch.float32 |
| 28 | + |
| 29 | + self.init_ditributed_env() |
| 30 | + neuron_model = NeuronDbrxModel(config) |
| 31 | + |
| 32 | + state_dict = NeuronDbrxForCausalLM.get_state_dict(self.model_path, config) |
| 33 | + |
| 34 | + _invoke_preshard_hook(neuron_model, state_dict) |
| 35 | + |
| 36 | + neuron_model.load_state_dict(state_dict, strict=False) |
| 37 | + |
| 38 | + if config.torch_dtype == torch.bfloat16: |
| 39 | + neuron_model.bfloat16() |
| 40 | + |
| 41 | + model = NeuronDbrxForCausalLM(None, config) |
| 42 | + model.context_encoding_model.model = neuron_model |
| 43 | + model.token_generation_model.model = neuron_model |
| 44 | + return model |
| 45 | + |
| 46 | + def load_neuron_model(self, traced_model_path): |
| 47 | + config = NeuronDbrxConfig.from_pretrained(traced_model_path) |
| 48 | + model = NeuronDbrxForCausalLM.from_pretrained("", config) |
| 49 | + |
| 50 | + model.load(traced_model_path) |
| 51 | + if config.torch_dtype == torch.bfloat16: |
| 52 | + model.bfloat16() |
| 53 | + |
| 54 | + return model |
| 55 | + |
| 56 | + def load_tokenizer(self, padding_side=None): |
| 57 | + tokenizer = AutoTokenizer.from_pretrained(self.tokenizer_path) |
| 58 | + tokenizer.pad_token = tokenizer.unk_token |
| 59 | + tokenizer.padding_side = padding_side if padding_side else self.get_padding_side() |
| 60 | + return tokenizer |
| 61 | + |
| 62 | + def get_config_cls(self): |
| 63 | + return NeuronDbrxConfig |
| 64 | + |
| 65 | + def get_model_cls(self): |
| 66 | + return NeuronDbrxForCausalLM |
| 67 | + |
| 68 | + def get_padding_side(self): |
| 69 | + return "right" |
| 70 | + |
| 71 | + def get_default_hf_generation_config_kwargs(self): |
| 72 | + config = super().get_default_hf_generation_config_kwargs() |
| 73 | + config['pad_token_id'] = 0 |
| 74 | + |
| 75 | + return config |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + DbrxRunner.cmd_execute() |
0 commit comments