Text Generation
Transformers
Safetensors
English
Chinese
llama
conversational
text-generation-inference
Instructions to use ekojs/internlm2-7b with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ekojs/internlm2-7b with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="ekojs/internlm2-7b") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("ekojs/internlm2-7b") model = AutoModelForCausalLM.from_pretrained("ekojs/internlm2-7b", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use ekojs/internlm2-7b with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ekojs/internlm2-7b" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ekojs/internlm2-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/ekojs/internlm2-7b
- SGLang
How to use ekojs/internlm2-7b 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 "ekojs/internlm2-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ekojs/internlm2-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "ekojs/internlm2-7b" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ekojs/internlm2-7b", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use ekojs/internlm2-7b with Docker Model Runner:
docker model run hf.co/ekojs/internlm2-7b
| #!/usr/bin/env python3 | |
| # 1/17/2024 | |
| # Charles O. Goddard | |
| """Convert internlm2 weights to Llama format.""" | |
| import json | |
| import os | |
| import einops | |
| import tqdm | |
| from mergekit.io import LazyTensorLoader, TensorWriter | |
| from mergekit.common import ModelReference | |
| from transformers import LlamaTokenizer | |
| MODEL_IN = "internlm/internlm2-20b" | |
| OUT_PATH = "./internlm2-20b-llama" | |
| model_ref = ModelReference.parse(MODEL_IN) | |
| cfg = model_ref.config(trust_remote_code=True) | |
| head_dim = cfg.hidden_size // cfg.num_attention_heads | |
| num_key_value_groups = cfg.num_attention_heads // cfg.num_key_value_heads | |
| loader = LazyTensorLoader(model_ref.tensor_index(), lazy_unpickle=True) | |
| writer = TensorWriter(OUT_PATH) | |
| SIMPLE_REPLACEMENTS = { | |
| "feed_forward.w1": "mlp.gate_proj", | |
| "feed_forward.w2": "mlp.down_proj", | |
| "feed_forward.w3": "mlp.up_proj", | |
| "attention.wo": "self_attn.o_proj", | |
| "ffn_norm": "post_attention_layernorm", | |
| "attention_norm": "input_layernorm", | |
| "tok_embeddings": "embed_tokens", | |
| "output.weight": "lm_head.weight", | |
| } | |
| for tensor_name in tqdm.tqdm(loader.index.tensor_paths): | |
| tensor = loader.get_tensor(tensor_name) | |
| if "attention.wqkv" in tensor_name: | |
| # make me think about tensor shapes will you >:( | |
| # ((cfg.num_attention_heads + 2 * cfg.num_key_value_heads) * head_dim, cfg.hidden_size) x (batch_sz, sq_len, cfg.hidden_size) | |
| # -> (batch_sz, sq_len, (cfg.num_attention_heads + 2 * cfg.num_key_value_heads) * head_dim) | |
| # qkv_states = rearrange( | |
| # qkv_states, | |
| # "b q (h gs d) -> b q h gs d", | |
| # gs=2 + self.num_key_value_groups, | |
| # d=self.head_dim, | |
| # ) | |
| # ->(batch_sz, sq_len, h, 2 + self.num_key_value_groups, head_dim) | |
| qkv_vecs = einops.rearrange( | |
| tensor, "(h gs d) z -> h gs d z", gs=2 + num_key_value_groups, d=head_dim | |
| ) | |
| q_proj = ( | |
| qkv_vecs[:, :num_key_value_groups, ...] | |
| .reshape(-1, cfg.hidden_size) | |
| .contiguous() | |
| ) | |
| k_proj = qkv_vecs[:, -2, ...].reshape(-1, cfg.hidden_size).contiguous() | |
| v_proj = qkv_vecs[:, -1, ...].reshape(-1, cfg.hidden_size).contiguous() | |
| assert k_proj.shape == v_proj.shape | |
| writer.save_tensor( | |
| tensor_name.replace("attention.wqkv", "self_attn.q_proj"), | |
| q_proj, | |
| clone=True, | |
| ) | |
| writer.save_tensor( | |
| tensor_name.replace("attention.wqkv", "self_attn.k_proj"), | |
| k_proj, | |
| clone=True, | |
| ) | |
| writer.save_tensor( | |
| tensor_name.replace("attention.wqkv", "self_attn.v_proj"), | |
| v_proj, | |
| clone=True, | |
| ) | |
| continue | |
| out_name = tensor_name | |
| for pattern, sub in SIMPLE_REPLACEMENTS.items(): | |
| if pattern in out_name: | |
| out_name = out_name.replace(pattern, sub) | |
| writer.save_tensor(out_name, tensor) | |
| writer.finalize() | |
| cfg_dict = json.loads(cfg.to_json_string()) | |
| del cfg_dict["auto_map"] | |
| cfg_dict["architectures"] = ["LlamaForCausalLM"] | |
| cfg_dict["model_type"] = "llama" | |
| if "rope_scaling" in cfg_dict and cfg_dict["rope_scaling"]["factor"] == 1.0: | |
| del cfg_dict["rope_scaling"] | |
| with open(os.path.join(OUT_PATH, "config.json"), "w", encoding="utf-8") as fp: | |
| json.dump(cfg_dict, fp, indent=2) | |
| # InternLMTokenizer differences: | |
| # 1. clean_up_tokenization() hardcoded to always be called | |
| # 2. might prepend a space to some tokens that LlamaTokenizer doesn't if they're the first token | |
| # 1 is easy to fix, 2... is not important | |
| tok = LlamaTokenizer.from_pretrained(MODEL_IN, trust_remote_code=False, legacy=True) | |
| tok.clean_up_tokenization_spaces = True | |
| tok.save_pretrained(OUT_PATH) | |