Text Generation
Transformers
Safetensors
English
CoDA
feature-extraction
text diffusion model
language model
code generation
conversational
custom_code
Instructions to use Salesforce/CoDA-v0-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Salesforce/CoDA-v0-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Salesforce/CoDA-v0-Instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Salesforce/CoDA-v0-Instruct", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Salesforce/CoDA-v0-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Salesforce/CoDA-v0-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Salesforce/CoDA-v0-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/Salesforce/CoDA-v0-Instruct
- SGLang
How to use Salesforce/CoDA-v0-Instruct 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 "Salesforce/CoDA-v0-Instruct" \ --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": "Salesforce/CoDA-v0-Instruct", "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 "Salesforce/CoDA-v0-Instruct" \ --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": "Salesforce/CoDA-v0-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use Salesforce/CoDA-v0-Instruct with Docker Model Runner:
docker model run hf.co/Salesforce/CoDA-v0-Instruct
| import math | |
| from typing import Any | |
| import torch | |
| from torch import nn | |
| from torch.nn.functional import scaled_dot_product_attention | |
| from torch.nn.attention import SDPBackend, sdpa_kernel | |
| from .model_config import CoDAConfig | |
| def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: | |
| """ | |
| This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, | |
| num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) | |
| """ | |
| batch, num_key_value_heads, slen, head_dim = hidden_states.shape | |
| if n_rep == 1: | |
| return hidden_states | |
| hidden_states = hidden_states[:, :, None, :, :].expand( | |
| batch, num_key_value_heads, n_rep, slen, head_dim | |
| ) | |
| return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) | |
| class AttentionModule(nn.Module): | |
| def __init__(self, config: CoDAConfig, kernel_config: dict[str, Any] | None = None): | |
| super().__init__() | |
| self.config = config | |
| self.kernel_config = kernel_config | |
| self.partition_spec = None | |
| def forward( | |
| self, | |
| query_states: torch.Tensor, | |
| key_states: torch.Tensor, | |
| value_states: torch.Tensor, | |
| attention_mask: torch.Tensor | None = None, | |
| ): | |
| """GPU-optimized PyTorch implementation""" | |
| if self.config.attention_kernel != "splash_attention": | |
| num_key_value_groups = ( | |
| self.config.num_attention_heads // self.config.num_key_value_heads | |
| ) | |
| key_states = repeat_kv(key_states, num_key_value_groups) | |
| value_states = repeat_kv(value_states, num_key_value_groups) | |
| bsz, num_heads, q_len, head_dim = query_states.size() | |
| head_dim = value_states.shape[-1] | |
| kv_seq_len = key_states.shape[-2] | |
| # Use SDPA with appropriate backend | |
| match self.config.attention_kernel: | |
| case "splash_attention": | |
| raise NotImplementedError( | |
| "Splash Attention is not supported in GPU environment" | |
| ) | |
| case "flash_attention": | |
| # Try to use flash attention backend, fallback to default if not available | |
| with sdpa_kernel(SDPBackend.FLASH_ATTENTION): | |
| attn_output = scaled_dot_product_attention( | |
| query_states, | |
| key_states, | |
| value_states, | |
| dropout_p=( | |
| self.config.attention_dropout if self.training else 0.0 | |
| ), | |
| is_causal=False, # weiran: causal=False for bi-directional attention | |
| ) | |
| case _: | |
| # Default implementation - use math backend for compatibility | |
| with sdpa_kernel(SDPBackend.MATH): | |
| attn_output = scaled_dot_product_attention( | |
| query_states, | |
| key_states, | |
| value_states, | |
| dropout_p=( | |
| self.config.attention_dropout if self.training else 0.0 | |
| ), | |
| is_causal=False, # weiran: causal=False for bi-directional attention | |
| ) | |
| if attn_output.size() != (bsz, num_heads, q_len, head_dim): | |
| raise ValueError( | |
| f"`attn_output` should be of size {(bsz, num_heads, q_len, head_dim)}, but is" | |
| f" {attn_output.size()}" | |
| ) | |
| return attn_output | |