Instructions to use xiaohang07/MatterGPT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use xiaohang07/MatterGPT with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="xiaohang07/MatterGPT")# pip install -U transformers accelerate # Load model directly from transformers import GPT model = GPT.from_pretrained("xiaohang07/MatterGPT", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use xiaohang07/MatterGPT with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "xiaohang07/MatterGPT" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xiaohang07/MatterGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/xiaohang07/MatterGPT
- SGLang
How to use xiaohang07/MatterGPT with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "xiaohang07/MatterGPT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xiaohang07/MatterGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "xiaohang07/MatterGPT" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xiaohang07/MatterGPT", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use xiaohang07/MatterGPT with Docker Model Runner:
docker model run hf.co/xiaohang07/MatterGPT
Download mattergpt_wrapper.py from xiaohang07/MatterGPT: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/xiaohang07/MatterGPT/resolve/main/mattergpt_wrapper.py
- Command line
-
hf download hf://xiaohang07/MatterGPT/mattergpt_wrapper.py
-
curl -L -o mattergpt_wrapper.py https://huggingface.co/xiaohang07/MatterGPT/resolve/main/mattergpt_wrapper.py
2.47 kB
| import torch | |
| from torch import nn | |
| from transformers import PreTrainedModel, PretrainedConfig | |
| from model import GPT, GPTConfig # Import your original model and config classes | |
| import json | |
| class CustomGPTConfig(PretrainedConfig): | |
| model_type = "gpt" | |
| def __init__(self, **kwargs): | |
| super().__init__(**kwargs) | |
| for key, value in kwargs.items(): | |
| setattr(self, key, value) | |
| class MatterGPTWrapper(PreTrainedModel): | |
| config_class = CustomGPTConfig | |
| base_model_prefix = "gpt" | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.model = GPT(GPTConfig(**config.__dict__)) | |
| def forward(self, input_ids, attention_mask=None, labels=None, prop=None): | |
| return self.model(input_ids, targets=labels, prop=prop) | |
| def generate(self, input_ids, prop, max_length, num_return_sequences=1, **kwargs): | |
| steps = max_length - input_ids.shape[1] | |
| return self.model.sample(input_ids, steps, prop=prop, **kwargs) | |
| def from_pretrained(cls, pretrained_model_path, *model_args, **kwargs): | |
| config_file = f"{pretrained_model_path}/config.json" | |
| with open(config_file, 'r') as f: | |
| config_dict = json.load(f) | |
| config = CustomGPTConfig(**config_dict) | |
| model = cls(config) | |
| state_dict = torch.load(f"{pretrained_model_path}/pytorch_model.pt", map_location="cpu") | |
| model.model.load_state_dict(state_dict) | |
| return model | |
| def save_pretrained(self, save_directory): | |
| self.config.save_pretrained(save_directory) | |
| torch.save(self.model.state_dict(), f"{save_directory}/pytorch_model.pt") | |
| class SimpleTokenizer: | |
| def __init__(self, vocab_file): | |
| with open(vocab_file, 'r') as f: | |
| self.vocab = f.read().splitlines() | |
| self.vocab = sorted(set(self.vocab + ['<', '>'])) | |
| self.stoi = {ch: i for i, ch in enumerate(self.vocab)} | |
| self.itos = {i: ch for i, ch in enumerate(self.vocab)} | |
| def encode(self, text): | |
| return [self.stoi[token] for token in text.split()] | |
| def decode(self, ids): | |
| return " ".join([self.itos[int(i)] for i in ids if i in self.itos]).replace("<", "").strip() | |
| def __call__(self, text, return_tensors=None): | |
| encoded = self.encode(text) | |
| if return_tensors == 'pt': | |
| import torch | |
| return {'input_ids': torch.tensor([encoded])} | |
| return {'input_ids': [encoded]} |