Instructions to use tiny-random/glm-4v with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tiny-random/glm-4v with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="tiny-random/glm-4v") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("tiny-random/glm-4v") model = AutoModelForMultimodalLM.from_pretrained("tiny-random/glm-4v", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.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(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tiny-random/glm-4v with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tiny-random/glm-4v" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tiny-random/glm-4v", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/tiny-random/glm-4v
- SGLang
How to use tiny-random/glm-4v 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 "tiny-random/glm-4v" \ --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": "tiny-random/glm-4v", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "tiny-random/glm-4v" \ --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": "tiny-random/glm-4v", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use tiny-random/glm-4v with Docker Model Runner:
docker model run hf.co/tiny-random/glm-4v
| library_name: transformers | |
| pipeline_tag: image-text-to-text | |
| inference: true | |
| widget: | |
| - text: Hello! | |
| example_title: Hello world | |
| group: Python | |
| base_model: | |
| - THUDM/GLM-4.1V-9B-Thinking | |
| This tiny model is for debugging. It is randomly initialized with the config adapted from [THUDM/GLM-4.1V-9B-Thinking](https://huggingface.co/THUDM/GLM-4.1V-9B-Thinking). | |
| ### Example usage: | |
| ```python | |
| import os | |
| import re | |
| import torch | |
| from transformers import AutoProcessor, Glm4vForConditionalGeneration | |
| model_id = "tiny-random/glm-4v" | |
| messages = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| { | |
| "type": "image", | |
| "url": "https://upload.wikimedia.org/wikipedia/commons/f/fa/Grayscale_8bits_palette_sample_image.png" | |
| }, | |
| { | |
| "type": "text", | |
| "text": "describe this image" | |
| } | |
| ], | |
| } | |
| ] | |
| processor = AutoProcessor.from_pretrained(model_id) | |
| model = Glm4vForConditionalGeneration.from_pretrained( | |
| pretrained_model_name_or_path=model_id, | |
| torch_dtype=torch.bfloat16, | |
| device_map="auto", | |
| ) | |
| inputs = processor.apply_chat_template( | |
| messages, | |
| tokenize=True, | |
| add_generation_prompt=True, | |
| return_dict=True, | |
| return_tensors="pt" | |
| ).to(model.device) | |
| generated_ids = model.generate(**inputs, max_new_tokens=16) | |
| output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False) | |
| print(output_text) | |
| ``` | |
| ### Codes to create this repo: | |
| ```python | |
| import json | |
| from pathlib import Path | |
| import torch | |
| import accelerate | |
| from huggingface_hub import file_exists, hf_hub_download | |
| from transformers import ( | |
| AutoConfig, | |
| AutoModelForCausalLM, | |
| AutoProcessor, | |
| GenerationConfig, | |
| set_seed, | |
| ) | |
| from transformers import AutoProcessor, Glm4vForConditionalGeneration | |
| source_model_id = "THUDM/GLM-4.1V-9B-Thinking" | |
| save_folder = "/tmp/tiny-random/glm-4v" | |
| processor = AutoProcessor.from_pretrained(source_model_id, trust_remote_code=True) | |
| processor.save_pretrained(save_folder) | |
| with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f: | |
| config_json = json.load(f) | |
| config_json['hidden_size'] = 64 | |
| config_json['intermediate_size'] = 128 | |
| config_json['num_attention_heads'] = 2 | |
| config_json['num_hidden_layers'] = 2 | |
| config_json['num_key_value_heads'] = 1 | |
| config_json['tie_word_embeddings'] = True | |
| config_json['vision_config']['hidden_size'] = 64 | |
| config_json['vision_config']['depth'] = 2 | |
| config_json['vision_config']['num_heads'] = 2 | |
| config_json['vision_config']['intermediate_size'] = 128 | |
| config_json['vision_config']['out_hidden_size'] = 64 | |
| config_json['rope_scaling']['mrope_section'] = [2, 2, 4] | |
| with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f: | |
| json.dump(config_json, f, indent=2) | |
| config = AutoConfig.from_pretrained( | |
| save_folder, | |
| trust_remote_code=True, | |
| ) | |
| print(config) | |
| torch.set_default_dtype(torch.bfloat16) | |
| model = Glm4vForConditionalGeneration(config) | |
| torch.set_default_dtype(torch.float32) | |
| if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'): | |
| model.generation_config = GenerationConfig.from_pretrained( | |
| source_model_id, trust_remote_code=True, | |
| ) | |
| set_seed(42) | |
| model = model.cpu() # cpu is more stable for random initialization across machines | |
| with torch.no_grad(): | |
| for name, p in sorted(model.named_parameters()): | |
| torch.nn.init.normal_(p, 0, 0.2) | |
| print(name, p.shape) | |
| model.save_pretrained(save_folder) | |
| print(model) | |
| ``` | |
| ### Printing the model: | |
| ```text | |
| Glm4vForConditionalGeneration( | |
| (model): Glm4vModel( | |
| (visual): Glm4vVisionModel( | |
| (embeddings): Glm4vVisionEmbeddings( | |
| (position_embedding): Embedding(576, 64) | |
| ) | |
| (patch_embed): Glm4vVisionPatchEmbed( | |
| (proj): Conv3d(3, 64, kernel_size=(2, 14, 14), stride=(2, 14, 14)) | |
| ) | |
| (rotary_pos_emb): Glm4vVisionRotaryEmbedding() | |
| (blocks): ModuleList( | |
| (0-1): 2 x Glm4vVisionBlock( | |
| (norm1): Glm4vRMSNorm((64,), eps=1e-05) | |
| (norm2): Glm4vRMSNorm((64,), eps=1e-05) | |
| (attn): Glm4vVisionAttention( | |
| (qkv): Linear(in_features=64, out_features=192, bias=False) | |
| (proj): Linear(in_features=64, out_features=64, bias=False) | |
| ) | |
| (mlp): Glm4VisionMlp( | |
| (gate_proj): Linear(in_features=64, out_features=64, bias=False) | |
| (up_proj): Linear(in_features=64, out_features=64, bias=False) | |
| (down_proj): Linear(in_features=64, out_features=64, bias=False) | |
| (act_fn): SiLU() | |
| ) | |
| ) | |
| ) | |
| (merger): Glm4vVisionPatchMerger( | |
| (proj): Linear(in_features=64, out_features=64, bias=False) | |
| (post_projection_norm): LayerNorm((64,), eps=1e-05, elementwise_affine=True) | |
| (gate_proj): Linear(in_features=64, out_features=128, bias=False) | |
| (up_proj): Linear(in_features=64, out_features=128, bias=False) | |
| (down_proj): Linear(in_features=128, out_features=64, bias=False) | |
| (act1): GELU(approximate='none') | |
| (act_fn): SiLU() | |
| ) | |
| (post_conv_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| (downsample): Conv2d(64, 64, kernel_size=(2, 2), stride=(2, 2)) | |
| (post_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| ) | |
| (language_model): Glm4vTextModel( | |
| (embed_tokens): Embedding(151552, 64, padding_idx=151329) | |
| (layers): ModuleList( | |
| (0-1): 2 x Glm4vTextDecoderLayer( | |
| (self_attn): Glm4vTextAttention( | |
| (q_proj): Linear(in_features=64, out_features=64, bias=True) | |
| (k_proj): Linear(in_features=64, out_features=32, bias=True) | |
| (v_proj): Linear(in_features=64, out_features=32, bias=True) | |
| (o_proj): Linear(in_features=64, out_features=64, bias=False) | |
| ) | |
| (mlp): Glm4vTextMLP( | |
| (gate_up_proj): Linear(in_features=64, out_features=256, bias=False) | |
| (down_proj): Linear(in_features=128, out_features=64, bias=False) | |
| (activation_fn): SiLU() | |
| ) | |
| (input_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| (post_attention_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| (post_self_attn_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| (post_mlp_layernorm): Glm4vRMSNorm((64,), eps=1e-05) | |
| ) | |
| ) | |
| (norm): Glm4vRMSNorm((64,), eps=1e-05) | |
| (rotary_emb): Glm4vTextRotaryEmbedding() | |
| ) | |
| ) | |
| (lm_head): Linear(in_features=64, out_features=151552, bias=False) | |
| ) | |
| ``` |