method_name
stringlengths
3
45
method_body
stringlengths
9
6.25k
full_code
stringlengths
35
7.02k
docstring
stringlengths
18
4.7k
load_weights
stacked_params_mapping = [('gate_up_proj', 'gate_proj', 0), ('gate_up_proj', 'up_proj', 1)] params_dict = dict(self.named_parameters()) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if 'rotary_emb.inv_freq' in name: continue if name =...
def load_weights(self, model_name_or_path: str, cache_dir: Optional[str]= None, load_format: str='auto', revision: Optional[str]=None): stacked_params_mapping = [('gate_up_proj', 'gate_proj', 0), ( 'gate_up_proj', 'up_proj', 1)] params_dict = dict(self.named_parameters()) for name, loaded_weight...
null
get_min_capability
return 70
def get_min_capability(self) ->int: return 70
null
in_wsl
return 'microsoft' in ' '.join(uname()).lower()
def in_wsl() ->bool: return 'microsoft' in ' '.join(uname()).lower()
null
convert_pyslice_to_tensor
"""convert PySafeSlice object from safetensors to torch.Tensor PySafeSlice object supports indexing, which is done before loading the actual tensor and can reduce the amount of memory being read into the memory. However, it does not support more advanced functionalities like `.view()` or `.t()`. Theref...
def convert_pyslice_to_tensor(x: Any) ->torch.Tensor: """convert PySafeSlice object from safetensors to torch.Tensor PySafeSlice object supports indexing, which is done before loading the actual tensor and can reduce the amount of memory being read into the memory. However, it does not support more adv...
convert PySafeSlice object from safetensors to torch.Tensor PySafeSlice object supports indexing, which is done before loading the actual tensor and can reduce the amount of memory being read into the memory. However, it does not support more advanced functionalities like `.view()` or `.t()`. Therefore, if we need to ...
_forward
"""PyTorch-native implementation equivalent to forward().""" d = x.shape[-1] // 2 return F.silu(x[..., :d]) * x[..., d:]
def _forward(self, x: torch.Tensor) ->torch.Tensor: """PyTorch-native implementation equivalent to forward().""" d = x.shape[-1] // 2 return F.silu(x[..., :d]) * x[..., d:]
PyTorch-native implementation equivalent to forward().
__init__
self.parent_seq_id = parent_seq_id self.output_token = output_token self.logprobs = logprobs
def __init__(self, parent_seq_id: int, output_token: int, logprobs: Dict[ int, float]) ->None: self.parent_seq_id = parent_seq_id self.output_token = output_token self.logprobs = logprobs
null
_convert_id_to_token
"""Converts an index (integer) in a token (str) using the vocab.""" token = self.sp_model.IdToPiece(index) return token
def _convert_id_to_token(self, index): """Converts an index (integer) in a token (str) using the vocab.""" token = self.sp_model.IdToPiece(index) return token
Converts an index (integer) in a token (str) using the vocab.
__init__
"""The MPT configuration class. Args: d_model (int): The size of the embedding dimension of the model. n_heads (int): The number of attention heads. n_layers (int): The number of layers in the model. expansion_ratio (int): The ratio of the up/down scale in the ffn...
def __init__(self, d_model: int=2048, n_heads: int=16, n_layers: int=24, expansion_ratio: int=4, max_seq_len: int=2048, vocab_size: int=50368, resid_pdrop: float=0.0, emb_pdrop: float=0.0, learned_pos_emb: bool= True, attn_config: Dict=attn_config_defaults, ffn_config: Dict= ffn_config_defaults, init_de...
The MPT configuration class. Args: d_model (int): The size of the embedding dimension of the model. n_heads (int): The number of attention heads. n_layers (int): The number of layers in the model. expansion_ratio (int): The ratio of the up/down scale in the ffn. max_seq_len (int): The maximum sequen...
__init__
super().__init__(vocab_size=vocab_size) self.fake_logits = fake_logits
def __init__(self, vocab_size: int, fake_logits: torch.Tensor): super().__init__(vocab_size=vocab_size) self.fake_logits = fake_logits
null
vocab_range_from_per_partition_vocab_size
index_f = rank * per_partition_vocab_size index_l = index_f + per_partition_vocab_size return index_f, index_l
def vocab_range_from_per_partition_vocab_size(per_partition_vocab_size: int, rank: int) ->Sequence[int]: index_f = rank * per_partition_vocab_size index_l = index_f + per_partition_vocab_size return index_f, index_l
null
__repr__
return f'SequenceGroup(request_id={self.request_id}, sampling_params={self.sampling_params}, num_seqs={len(self.seqs_dict)})'
def __repr__(self) ->str: return ( f'SequenceGroup(request_id={self.request_id}, sampling_params={self.sampling_params}, num_seqs={len(self.seqs_dict)})' )
null
apply_weights
qweight = weights['qweight'] out_shape = x.shape[:-1] + (qweight.shape[-1],) reshaped_x = x.reshape(-1, x.shape[-1]) if weights['exllama_state'] == ExllamaState.UNINITIALIZED: if self.quant_config.desc_act: weights['g_idx'] = torch.argsort(weights['g_idx']).to(torch.int) else: weights['g_idx'] =...
def apply_weights(self, weights: Dict[str, Any], x: torch.Tensor, bias: Optional[torch.Tensor]=None) ->torch.Tensor: qweight = weights['qweight'] out_shape = x.shape[:-1] + (qweight.shape[-1],) reshaped_x = x.reshape(-1, x.shape[-1]) if weights['exllama_state'] == ExllamaState.UNINITIALIZED: ...
null
forward
qkv, _ = self.W_pack(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) if self.postion_embedding != 'ALIBI': q, k = self.rotary_emb(positions, q, k) k_cache, v_cache = kv_cache attn_output = self.attn(q, k, v, k_cache, v_cache, input_metadata) output, _ = self.o_proj(attn_output) return output
def forward(self, positions: torch.Tensor, hidden_states: torch.Tensor, kv_cache: KVCache, input_metadata: InputMetadata) ->torch.Tensor: qkv, _ = self.W_pack(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) if self.postion_embedding != 'ALIBI': q, k = self.rotary_emb(positions, q, k) k_...
null
__init__
self.request_id = request_id self.is_prompt = is_prompt self.seq_data = seq_data self.sampling_params = sampling_params self.block_tables = block_tables
def __init__(self, request_id: str, is_prompt: bool, seq_data: Dict[int, SequenceData], sampling_params: SamplingParams, block_tables: Dict[int, List[int]]) ->None: self.request_id = request_id self.is_prompt = is_prompt self.seq_data = seq_data self.sampling_params = sampling_params self.bl...
null
post_http_request
headers = {'User-Agent': 'Test Client'} pload = {'prompt': prompt, 'n': n, 'use_beam_search': True, 'temperature': 0.0, 'max_tokens': 16, 'stream': stream} response = requests.post(api_url, headers=headers, json=pload, stream=True) return response
def post_http_request(prompt: str, api_url: str, n: int=1, stream: bool=False ) ->requests.Response: headers = {'User-Agent': 'Test Client'} pload = {'prompt': prompt, 'n': n, 'use_beam_search': True, 'temperature': 0.0, 'max_tokens': 16, 'stream': stream} response = requests.post(api_url, heade...
null
load_weights
params_dict = dict(self.named_parameters()) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if 'rotary_emb.inv_freq' in name: continue if name.endswith('.bias') and name not in params_dict: continue param = params_dict[name] ...
def load_weights(self, model_name_or_path: str, cache_dir: Optional[str]= None, load_format: str='auto', revision: Optional[str]=None): params_dict = dict(self.named_parameters()) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if '...
null
get_torch_arch_list
env_arch_list = os.environ.get('TORCH_CUDA_ARCH_LIST', None) if env_arch_list is None: return set() torch_arch_list = set(env_arch_list.replace(' ', ';').split(';')) if not torch_arch_list: return set() valid_archs = NVIDIA_SUPPORTED_ARCHS.union({(s + '+PTX') for s in NVIDIA_SUPPORTED_ARCHS}) arch_list = to...
def get_torch_arch_list() ->Set[str]: env_arch_list = os.environ.get('TORCH_CUDA_ARCH_LIST', None) if env_arch_list is None: return set() torch_arch_list = set(env_arch_list.replace(' ', ';').split(';')) if not torch_arch_list: return set() valid_archs = NVIDIA_SUPPORTED_ARCHS.union(...
null
__init__
super().__init__() self.config = config self.linear_method = linear_method self.model = OPTModel(config, linear_method) self.lm_head_weight = self.model.decoder.embed_tokens.weight self.sampler = Sampler(config.vocab_size)
def __init__(self, config, linear_method: Optional[LinearMethodBase]=None): super().__init__() self.config = config self.linear_method = linear_method self.model = OPTModel(config, linear_method) self.lm_head_weight = self.model.decoder.embed_tokens.weight self.sampler = Sampler(config.vocab_siz...
null
run_hf
assert not use_beam_search llm = AutoModelForCausalLM.from_pretrained(model, torch_dtype=torch.float16, trust_remote_code=trust_remote_code) if llm.config.model_type == 'llama': tokenizer.pad_token = tokenizer.eos_token llm = llm.cuda() pbar = tqdm(total=len(requests)) start = time.perf_counter() batch: List[st...
def run_hf(requests: List[Tuple[str, int, int]], model: str, tokenizer: PreTrainedTokenizerBase, n: int, use_beam_search: bool, max_batch_size: int, trust_remote_code: bool) ->float: assert not use_beam_search llm = AutoModelForCausalLM.from_pretrained(model, torch_dtype=torch. float16, trust_re...
null
forward
hidden_states = self.model(input_ids, positions, kv_caches, input_metadata) return hidden_states
def forward(self, input_ids: torch.Tensor, positions: torch.Tensor, kv_caches: List[KVCache], input_metadata: InputMetadata) ->torch.Tensor: hidden_states = self.model(input_ids, positions, kv_caches, input_metadata) return hidden_states
null
_prepare_prompt
assert len(seq_group_metadata_list) > 0 input_tokens: List[List[int]] = [] input_positions: List[List[int]] = [] slot_mapping: List[List[int]] = [] prompt_lens: List[int] = [] for seq_group_metadata in seq_group_metadata_list: assert seq_group_metadata.is_prompt seq_ids = list(seq_group_metadata.seq_data.keys()...
def _prepare_prompt(self, seq_group_metadata_list: List[SequenceGroupMetadata] ) ->Tuple[torch.Tensor, torch.Tensor, InputMetadata, List[int]]: assert len(seq_group_metadata_list) > 0 input_tokens: List[List[int]] = [] input_positions: List[List[int]] = [] slot_mapping: List[List[int]] = [] prom...
null
get_config_filenames
return ['quant_config.json']
@staticmethod def get_config_filenames() ->List[str]: return ['quant_config.json']
null
_paged_attention
output = torch.empty_like(query) block_size = value_cache.shape[3] num_seqs, num_heads, head_size = query.shape max_num_partitions = (input_metadata.max_context_len + _PARTITION_SIZE - 1 ) // _PARTITION_SIZE use_v1 = input_metadata.max_context_len <= 8192 and (max_num_partitions == 1 or num_seqs * num_heads > ...
def _paged_attention(query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, input_metadata: InputMetadata, num_kv_heads: int, scale: float, alibi_slopes: Optional[torch.Tensor]) ->torch.Tensor: output = torch.empty_like(query) block_size = value_cache.shape[3] num_seqs, num_heads, ...
null
_apply_logits_processors
logits_row_idx = 0 found_logits_processors = False for seq_ids, sampling_params in sampling_metadata.seq_groups: logits_processors = sampling_params.logits_processors if logits_processors: found_logits_processors = True for seq_id in seq_ids: logits_row = logits[logits_row_idx] ...
def _apply_logits_processors(logits: torch.Tensor, sampling_metadata: SamplingMetadata) ->torch.Tensor: logits_row_idx = 0 found_logits_processors = False for seq_ids, sampling_params in sampling_metadata.seq_groups: logits_processors = sampling_params.logits_processors if logits_process...
null
forward
qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) key_cache, value_cache = kv_cache attn_output = self.attn(q, k, v, key_cache, value_cache, input_metadata) output, _ = self.out_proj(attn_output) return output
def forward(self, hidden_states: torch.Tensor, kv_cache: KVCache, input_metadata: InputMetadata) ->torch.Tensor: qkv, _ = self.qkv_proj(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) key_cache, value_cache = kv_cache attn_output = self.attn(q, k, v, key_cache, value_cache, input_metadata) ...
null
__init__
super().__init__() self.config = config self.linear_method = linear_method self.model = LlamaModel(config, linear_method) self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size) self.sampler = Sampler(config.vocab_size)
def __init__(self, config: LlamaConfig, linear_method: Optional[ LinearMethodBase]=None) ->None: super().__init__() self.config = config self.linear_method = linear_method self.model = LlamaModel(config, linear_method) self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size) self...
null
record_metrics
gauge_avg_prompt_throughput.set(labels, avg_prompt_throughput) gauge_avg_generation_throughput.set(labels, avg_generation_throughput) gauge_scheduler_running.set(labels, scheduler_running) gauge_scheduler_swapped.set(labels, scheduler_swapped) gauge_scheduler_waiting.set(labels, scheduler_waiting) gauge_gpu_cache_usage...
def record_metrics(avg_prompt_throughput: float, avg_generation_throughput: float, scheduler_running: int, scheduler_swapped: int, scheduler_waiting: int, gpu_cache_usage: float, cpu_cache_usage: float): gauge_avg_prompt_throughput.set(labels, avg_prompt_throughput) gauge_avg_generation_throughput.set(l...
null
get_config_filenames
"""List of filenames to search for in the model directory.""" raise NotImplementedError
@staticmethod @abstractmethod def get_config_filenames() ->List[str]: """List of filenames to search for in the model directory.""" raise NotImplementedError
List of filenames to search for in the model directory.
get_num_empty_slots
return self.block_size - self.num_tokens
def get_num_empty_slots(self) ->int: return self.block_size - self.num_tokens
null
get_beam_search_score
"""Calculate the beam search score with length penalty. Adapted from https://github.com/huggingface/transformers/blob/ccb92be23def445f2afdea94c31286f84b89eb5b/src/transformers/generation/beam_search.py#L938 """ if seq_len is None: seq_len = self.get_len() if eos_token_id is not None an...
def get_beam_search_score(self, length_penalty: float=0.0, seq_len: Optional[int]=None, eos_token_id: Optional[int]=None) ->float: """Calculate the beam search score with length penalty. Adapted from https://github.com/huggingface/transformers/blob/ccb92be23def445f2afdea94c31286f84b89eb5b/src/...
Calculate the beam search score with length penalty. Adapted from https://github.com/huggingface/transformers/blob/ccb92be23def445f2afdea94c31286f84b89eb5b/src/transformers/generation/beam_search.py#L938
__init__
self.model = model self.tokenizer = tokenizer self.tokenizer_mode = tokenizer_mode self.trust_remote_code = trust_remote_code self.download_dir = download_dir self.load_format = load_format self.seed = seed self.revision = revision self.tokenizer_revision = tokenizer_revision self.quantization = quantization self.enfor...
def __init__(self, model: str, tokenizer: str, tokenizer_mode: str, trust_remote_code: bool, download_dir: Optional[str], load_format: str, dtype: Union[str, torch.dtype], seed: int, revision: Optional[str]=None, tokenizer_revision: Optional[str]=None, max_model_len: Optional[int]= None, quantization: O...
null
forward
bias = self.bias if not self.skip_bias_add else None output = self.linear_method.apply_weights(self.linear_weights, x, bias) output_bias = self.bias if self.skip_bias_add else None return output, output_bias
def forward(self, x: torch.Tensor) ->torch.Tensor: bias = self.bias if not self.skip_bias_add else None output = self.linear_method.apply_weights(self.linear_weights, x, bias) output_bias = self.bias if self.skip_bias_add else None return output, output_bias
null
_degroup_weight
hidden_size = self.config.hidden_size head_size = self.config.hidden_size // self.config.num_attention_heads target_num_kv_heads = self.config.num_key_value_heads num_kv_heads = loaded_weight.shape[0] // head_size n_repeats = target_num_kv_heads / num_kv_heads assert n_repeats == int(n_repeats) n_repeats = int(n_repeat...
def _degroup_weight(self, loaded_weight: torch.Tensor) ->torch.Tensor: hidden_size = self.config.hidden_size head_size = self.config.hidden_size // self.config.num_attention_heads target_num_kv_heads = self.config.num_key_value_heads num_kv_heads = loaded_weight.shape[0] // head_size n_repeats = tar...
null
forward
qkv, _ = self.c_attn(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) key_cache, value_cache = kv_cache attn_output = self.attn(q, k, v, key_cache, value_cache, input_metadata) attn_output, _ = self.c_proj(attn_output) return attn_output
def forward(self, hidden_states: torch.Tensor, kv_cache: KVCache, input_metadata: InputMetadata) ->torch.Tensor: qkv, _ = self.c_attn(hidden_states) q, k, v = qkv.chunk(chunks=3, dim=-1) key_cache, value_cache = kv_cache attn_output = self.attn(q, k, v, key_cache, value_cache, input_metadata) at...
null
get_cache_block_size
head_size = model_config.get_head_size() num_heads = model_config.get_num_kv_heads(parallel_config) num_layers = model_config.get_num_layers(parallel_config) key_cache_block = block_size * num_heads * head_size value_cache_block = key_cache_block total = num_layers * (key_cache_block + value_cache_block) dtype_size = _...
@staticmethod def get_cache_block_size(block_size: int, model_config: ModelConfig, parallel_config: ParallelConfig) ->int: head_size = model_config.get_head_size() num_heads = model_config.get_num_kv_heads(parallel_config) num_layers = model_config.get_num_layers(parallel_config) key_cache_block = b...
null
_get_alibi_slopes
closest_power_of_2 = 2 ** math.floor(math.log2(total_num_heads)) base = torch.tensor(2 ** -2 ** -(math.log2(closest_power_of_2) - 3), dtype= torch.float32) powers = torch.arange(1, 1 + closest_power_of_2, dtype=torch.int32) slopes = torch.pow(base, powers) if closest_power_of_2 != total_num_heads: extra_base = ...
def _get_alibi_slopes(total_num_heads: int) ->torch.Tensor: closest_power_of_2 = 2 ** math.floor(math.log2(total_num_heads)) base = torch.tensor(2 ** -2 ** -(math.log2(closest_power_of_2) - 3), dtype=torch.float32) powers = torch.arange(1, 1 + closest_power_of_2, dtype=torch.int32) slopes = torc...
null
sample
next_tokens = self.sampler(self.lm_head_weight, hidden_states, sampling_metadata) return next_tokens
def sample(self, hidden_states: torch.Tensor, sampling_metadata: SamplingMetadata) ->Optional[SamplerOutput]: next_tokens = self.sampler(self.lm_head_weight, hidden_states, sampling_metadata) return next_tokens
null
allocate
seq = seq_group.get_seqs(status=SequenceStatus.WAITING)[0] block_table: BlockTable = [] for logical_idx in range(len(seq.logical_token_blocks)): if (self.block_sliding_window is not None and logical_idx >= self. block_sliding_window): block = block_table[logical_idx % self.block_sliding_window] ...
def allocate(self, seq_group: SequenceGroup) ->None: seq = seq_group.get_seqs(status=SequenceStatus.WAITING)[0] block_table: BlockTable = [] for logical_idx in range(len(seq.logical_token_blocks)): if (self.block_sliding_window is not None and logical_idx >= self. block_sliding_window): ...
null
load_weights
params_dict = dict(self.named_parameters()) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if ('attention.bias' in name or 'attention.masked_bias' in name or 'rotary_emb.inv_freq' in name): continue param = params_dict[name] i...
def load_weights(self, model_name_or_path: str, cache_dir: Optional[str]= None, load_format: str='auto', revision: Optional[str]=None): params_dict = dict(self.named_parameters()) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if (...
null
reset
self.counter = 0
def reset(self) ->None: self.counter = 0
null
__init__
super().__init__() self.ln = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_epsilon) self.mixer = PhiAttention(config, linear_method) self.mlp = PhiMLP(config, linear_method)
def __init__(self, config: PretrainedConfig, linear_method: Optional[ LinearMethodBase]=None): super().__init__() self.ln = nn.LayerNorm(config.hidden_size, eps=config.layer_norm_epsilon) self.mixer = PhiAttention(config, linear_method) self.mlp = PhiMLP(config, linear_method)
null
__init__
super().__init__() self.gate_up_proj = MergedColumnParallelLinear(hidden_size, [ intermediate_size] * 2, bias=False, linear_method=linear_method) self.down_proj = RowParallelLinear(intermediate_size, hidden_size, bias= False, linear_method=linear_method) if hidden_act != 'silu': raise ValueError( f'...
def __init__(self, hidden_size: int, intermediate_size: int, hidden_act: str, linear_method: Optional[LinearMethodBase]=None) ->None: super().__init__() self.gate_up_proj = MergedColumnParallelLinear(hidden_size, [ intermediate_size] * 2, bias=False, linear_method=linear_method) self.down_proj =...
null
_compute_inv_freq
pos_freqs = self.base ** (torch.arange(0, self.rotary_dim, 2, dtype=torch. float, device='cuda') / self.rotary_dim) inv_freq_extrapolation = 1.0 / pos_freqs inv_freq_interpolation = 1.0 / (scaling_factor * pos_freqs) low, high = _yarn_find_correction_range(self.beta_fast, self.beta_slow, self.rotary_dim, self.b...
def _compute_inv_freq(self, scaling_factor: float) ->torch.Tensor: pos_freqs = self.base ** (torch.arange(0, self.rotary_dim, 2, dtype= torch.float, device='cuda') / self.rotary_dim) inv_freq_extrapolation = 1.0 / pos_freqs inv_freq_interpolation = 1.0 / (scaling_factor * pos_freqs) low, high = ...
null
broadcast
"""Broadcast the input tensor.""" world_size = torch.distributed.get_world_size() assert 0 <= src < world_size, f'Invalid src rank ({src})' if world_size == 1: return input_ torch.distributed.broadcast(input_, src=src) return input_
def broadcast(input_, src=0): """Broadcast the input tensor.""" world_size = torch.distributed.get_world_size() assert 0 <= src < world_size, f'Invalid src rank ({src})' if world_size == 1: return input_ torch.distributed.broadcast(input_, src=src) return input_
Broadcast the input tensor.
tensor_model_parallel_all_gather
"""All-gather the input tensor across model parallel group.""" world_size = get_tensor_model_parallel_world_size() if world_size == 1: return input_ assert -input_.dim() <= dim < input_.dim( ), f'Invalid dim ({dim}) for input tensor with shape {input_.size()}' if dim < 0: dim += input_.dim() input_size = in...
def tensor_model_parallel_all_gather(input_, dim=-1): """All-gather the input tensor across model parallel group.""" world_size = get_tensor_model_parallel_world_size() if world_size == 1: return input_ assert -input_.dim() <= dim < input_.dim( ), f'Invalid dim ({dim}) for input tensor w...
All-gather the input tensor across model parallel group.
_forward
"""PyTorch-native implementation equivalent to forward().""" c = math.sqrt(2.0 / math.pi) return 0.5 * x * (1.0 + torch.tanh(c * (x + 0.044715 * torch.pow(x, 3.0))))
def _forward(self, x: torch.Tensor) ->torch.Tensor: """PyTorch-native implementation equivalent to forward().""" c = math.sqrt(2.0 / math.pi) return 0.5 * x * (1.0 + torch.tanh(c * (x + 0.044715 * torch.pow(x, 3.0))))
PyTorch-native implementation equivalent to forward().
is_running
return self.background_loop is not None and not self.background_loop.done()
@property def is_running(self) ->bool: return self.background_loop is not None and not self.background_loop.done()
null
is_finished
return all(seq.is_finished() for seq in self.get_seqs())
def is_finished(self) ->bool: return all(seq.is_finished() for seq in self.get_seqs())
null
__init__
super().__init__(*args, **kwargs) self._num_aborts = 0
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._num_aborts = 0
null
get_num_unfinished_requests
"""Gets the number of unfinished requests.""" return self.scheduler.get_num_unfinished_seq_groups()
def get_num_unfinished_requests(self) ->int: """Gets the number of unfinished requests.""" return self.scheduler.get_num_unfinished_seq_groups()
Gets the number of unfinished requests.
sample
next_tokens = self.sampler(self.lm_head_weight, hidden_states, sampling_metadata) return next_tokens
def sample(self, hidden_states: torch.Tensor, sampling_metadata: SamplingMetadata) ->Optional[SamplerOutput]: next_tokens = self.sampler(self.lm_head_weight, hidden_states, sampling_metadata) return next_tokens
null
__init__
super().__init__() self.config = config self.linear_method = linear_method self.transformer = BloomModel(config, linear_method) self.lm_head_weight = self.transformer.word_embeddings.weight self.sampler = Sampler(config.vocab_size)
def __init__(self, config: BloomConfig, linear_method: Optional[ LinearMethodBase]=None): super().__init__() self.config = config self.linear_method = linear_method self.transformer = BloomModel(config, linear_method) self.lm_head_weight = self.transformer.word_embeddings.weight self.sampler...
null
test_health_endpoint
response = client.get('/health') assert response.status_code == 200
def test_health_endpoint(): response = client.get('/health') assert response.status_code == 200
null
get_config_filenames
return ['quant_config.json', 'quantize_config.json']
@staticmethod def get_config_filenames() ->List[str]: return ['quant_config.json', 'quantize_config.json']
null
init_cache_engine
self.cache_config = cache_config self.cache_engine = CacheEngine(self.cache_config, self.model_config, self. parallel_config) self.cache_events = self.cache_engine.events self.gpu_cache = self.cache_engine.gpu_cache self.model_runner.set_block_size(self.cache_engine.block_size)
def init_cache_engine(self, cache_config: CacheConfig) ->None: self.cache_config = cache_config self.cache_engine = CacheEngine(self.cache_config, self.model_config, self.parallel_config) self.cache_events = self.cache_engine.events self.gpu_cache = self.cache_engine.gpu_cache self.model_run...
null
__init__
super().__init__() self.config = config self.linear_method = linear_method self.transformer = QWenModel(config, linear_method) self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size) self.sampler = Sampler(config.vocab_size)
def __init__(self, config: QWenConfig, linear_method: Optional[ LinearMethodBase]=None): super().__init__() self.config = config self.linear_method = linear_method self.transformer = QWenModel(config, linear_method) self.lm_head = ParallelLMHead(config.vocab_size, config.hidden_size) self.sa...
null
get_max_num_running_seqs
"""The maximum number of sequences running in parallel in the remaining lifetime of the request.""" if self.sampling_params.use_beam_search: return self.sampling_params.best_of else: if self.sampling_params.best_of > self.num_seqs(): return self.sampling_params.best_of return self.num_unfini...
def get_max_num_running_seqs(self) ->int: """The maximum number of sequences running in parallel in the remaining lifetime of the request.""" if self.sampling_params.use_beam_search: return self.sampling_params.best_of else: if self.sampling_params.best_of > self.num_seqs(): ...
The maximum number of sequences running in parallel in the remaining lifetime of the request.
_decode_sequence
"""Decodes the new token for a sequence.""" new_tokens, new_output_text, prefix_offset, read_offset = ( detokenize_incrementally(self.tokenizer, all_input_ids=seq. get_token_ids(), prev_tokens=seq.tokens, prefix_offset=seq. prefix_offset, read_offset=seq.read_offset, skip_special_tokens=prms. skip_speci...
def _decode_sequence(self, seq: Sequence, prms: SamplingParams) ->None: """Decodes the new token for a sequence.""" new_tokens, new_output_text, prefix_offset, read_offset = ( detokenize_incrementally(self.tokenizer, all_input_ids=seq. get_token_ids(), prev_tokens=seq.tokens, prefix_offset=seq. ...
Decodes the new token for a sequence.
get_token_ids
return self.token_ids[:self.num_tokens]
def get_token_ids(self) ->List[int]: return self.token_ids[:self.num_tokens]
null
load_weights
params_dict = dict(self.named_parameters(remove_duplicate=False)) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if name == 'lm_head.weight': continue if not name.startswith('transformer.'): name = 'transformer.' + name param =...
def load_weights(self, model_name_or_path: str, cache_dir: Optional[str]= None, load_format: str='auto', revision: Optional[str]=None): params_dict = dict(self.named_parameters(remove_duplicate=False)) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, r...
null
get_name
"""Name of the quantization method.""" raise NotImplementedError
@abstractmethod def get_name(self) ->str: """Name of the quantization method.""" raise NotImplementedError
Name of the quantization method.
get_requirements
"""Get Python package dependencies from requirements.txt.""" if _is_hip(): with open(get_path('requirements-rocm.txt')) as f: requirements = f.read().strip().split('\n') else: with open(get_path('requirements.txt')) as f: requirements = f.read().strip().split('\n') return requirements
def get_requirements() ->List[str]: """Get Python package dependencies from requirements.txt.""" if _is_hip(): with open(get_path('requirements-rocm.txt')) as f: requirements = f.read().strip().split('\n') else: with open(get_path('requirements.txt')) as f: requiremen...
Get Python package dependencies from requirements.txt.
load_weights
params_dict = dict(self.named_parameters(remove_duplicate=False)) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, revision): if name.endswith('.bias') and name not in params_dict: continue param = params_dict[name] weight_loader = getattr(param, '...
def load_weights(self, model_name_or_path: str, cache_dir: Optional[str]= None, load_format: str='auto', revision: Optional[str]=None): params_dict = dict(self.named_parameters(remove_duplicate=False)) for name, loaded_weight in hf_model_weights_iterator(model_name_or_path, cache_dir, load_format, r...
null
__init__
self.vocab_size = vocab_size self.hidden_size = hidden_size self.intermediate_size = intermediate_size self.num_hidden_layers = num_hidden_layers self.num_attention_heads = num_attention_heads self.emb_dropout_prob = emb_dropout_prob self.attn_dropout_prob = attn_dropout_prob self.layer_norm_epsilon = layer_norm_epsilo...
def __init__(self, vocab_size=151936, hidden_size=4096, num_hidden_layers= 32, num_attention_heads=32, emb_dropout_prob=0.0, attn_dropout_prob=0.0, layer_norm_epsilon=1e-06, initializer_range=0.02, max_position_embeddings=8192, scale_attn_weights=True, use_cache=True, bf16=False, fp16=False, fp32=False,...
null
clear
self.flag = False
def clear(self): self.flag = False
null
__init__
super().__init__() self.hidden_size = config.hidden_size rope_theta = getattr(config, 'rope_theta', 10000) rope_scaling = getattr(config, 'rope_scaling', None) max_position_embeddings = getattr(config, 'max_position_embeddings', 8192) self.self_attn = LlamaAttention(hidden_size=self.hidden_size, num_heads= config.n...
def __init__(self, config: LlamaConfig, linear_method: Optional[ LinearMethodBase]=None) ->None: super().__init__() self.hidden_size = config.hidden_size rope_theta = getattr(config, 'rope_theta', 10000) rope_scaling = getattr(config, 'rope_scaling', None) max_position_embeddings = getattr(confi...
null
_is_cuda
return torch.version.cuda is not None
def _is_cuda() ->bool: return torch.version.cuda is not None
null
test_beam_search_single_input
hf_model = hf_runner(model, dtype=dtype) hf_outputs = hf_model.generate_beam_search(example_prompts, beam_width, max_tokens) del hf_model vllm_model = vllm_runner(model, dtype=dtype) vllm_outputs = vllm_model.generate_beam_search(example_prompts, beam_width, max_tokens) del vllm_model for i in range(len(example...
@pytest.mark.parametrize('model', MODELS) @pytest.mark.parametrize('dtype', ['half']) @pytest.mark.parametrize('max_tokens', MAX_TOKENS) @pytest.mark.parametrize('beam_width', BEAM_WIDTHS) def test_beam_search_single_input(hf_runner, vllm_runner, example_prompts, model: str, dtype: str, max_tokens: int, beam_width:...
null
forward
if residual is None: residual = hidden_states hidden_states = self.ln_1(hidden_states) else: hidden_states, residual = self.ln_1(hidden_states, residual) hidden_states = self.attn(positions=positions, hidden_states=hidden_states, kv_cache=kv_cache, input_metadata=input_metadata) hidden_states, residual ...
def forward(self, positions: torch.Tensor, hidden_states: torch.Tensor, kv_cache: KVCache, input_metadata: InputMetadata, residual: Optional[ torch.Tensor]) ->Tuple[torch.Tensor, torch.Tensor]: if residual is None: residual = hidden_states hidden_states = self.ln_1(hidden_states) else: ...
null
forward
hidden_states = self.embed_tokens(input_ids) residual = None for i in range(len(self.layers)): layer = self.layers[i] hidden_states, residual = layer(positions, hidden_states, kv_caches[i], input_metadata, residual) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states
def forward(self, input_ids: torch.Tensor, positions: torch.Tensor, kv_caches: List[KVCache], input_metadata: InputMetadata) ->torch.Tensor: hidden_states = self.embed_tokens(input_ids) residual = None for i in range(len(self.layers)): layer = self.layers[i] hidden_states, residual = lay...
null
get_last_token_id
return self.data.get_last_token_id()
def get_last_token_id(self) ->int: return self.data.get_last_token_id()
null
stop_generating
self.request_id = None
def stop_generating(self): self.request_id = None
null
_rotate_gptj
x1 = x[..., ::2] x2 = x[..., 1::2] x = torch.stack((-x2, x1), dim=-1) return x.flatten(-2)
def _rotate_gptj(x: torch.Tensor) ->torch.Tensor: x1 = x[..., ::2] x2 = x[..., 1::2] x = torch.stack((-x2, x1), dim=-1) return x.flatten(-2)
null
apply_weights
"""Apply the weights to the input tensor.""" raise NotImplementedError
@abstractmethod def apply_weights(self, weights: Dict[str, torch.Tensor], x: torch.Tensor, bias: Optional[torch.Tensor]=None) ->torch.Tensor: """Apply the weights to the input tensor.""" raise NotImplementedError
Apply the weights to the input tensor.
__init__
super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden_size) self.layers = nn.ModuleList([LlamaDecoderLayer(config, linear_method) for _ in range(config.num_hidden_layers)]) s...
def __init__(self, config: LlamaConfig, linear_method: Optional[ LinearMethodBase]=None) ->None: super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden...
null
__init__
super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden_size) self.layers = nn.ModuleList([MistralDecoderLayer(config, linear_method) for _ in range(config.num_hidden_layers)])...
def __init__(self, config: MistralConfig, linear_method: Optional[ LinearMethodBase]=None) ->None: super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidd...
null
swap_out
self._swap(self.gpu_cache, self.cpu_cache, src_to_dst)
def swap_out(self, src_to_dst: Dict[int, int]) ->None: self._swap(self.gpu_cache, self.cpu_cache, src_to_dst)
null
forward
for i in range(self.num_layers): layer = self.layers[i] hidden_states = layer(hidden_states=hidden_states, position_ids= position_ids, kv_cache=kv_caches[i], input_metadata=input_metadata) if self.post_layer_norm: hidden_states = self.final_layernorm(hidden_states) return hidden_states
def forward(self, hidden_states: torch.Tensor, position_ids: torch.Tensor, kv_caches: List[KVCache], input_metadata: InputMetadata) ->torch.Tensor: for i in range(self.num_layers): layer = self.layers[i] hidden_states = layer(hidden_states=hidden_states, position_ids= position_ids, k...
null
load_model_cls
if model_arch not in _MODELS: return None if is_hip(): if model_arch in _ROCM_UNSUPPORTED_MODELS: raise ValueError( f'Model architecture {model_arch} is not supported by ROCm for now.' ) if model_arch in _ROCM_PARTIALLY_SUPPORTED_MODELS: logger.warning( f'...
@staticmethod def load_model_cls(model_arch: str) ->Optional[Type[nn.Module]]: if model_arch not in _MODELS: return None if is_hip(): if model_arch in _ROCM_UNSUPPORTED_MODELS: raise ValueError( f'Model architecture {model_arch} is not supported by ROCm for now.' ...
null
finish
self._queue.put_nowait(StopIteration) self._finished = True
def finish(self) ->None: self._queue.put_nowait(StopIteration) self._finished = True
null
__init__
super().__init__() self.d_model = config.d_model self.total_num_heads = config.n_heads self.head_dim = self.d_model // self.total_num_heads self.clip_qkv = config.attn_config['clip_qkv'] self.qk_ln = config.attn_config['qk_ln'] self.alibi_bias_max = config.attn_config['alibi_bias_max'] if 'kv_n_heads' in config.attn_co...
def __init__(self, config: MPTConfig, linear_method: Optional[ LinearMethodBase]=None): super().__init__() self.d_model = config.d_model self.total_num_heads = config.n_heads self.head_dim = self.d_model // self.total_num_heads self.clip_qkv = config.attn_config['clip_qkv'] self.qk_ln = conf...
null
get_pipeline_model_parallel_first_rank
"""Return the global rank of the first process in the pipeline for the current tensor parallel group""" assert _PIPELINE_GLOBAL_RANKS is not None, 'Pipeline parallel group is not initialized' return _PIPELINE_GLOBAL_RANKS[0]
def get_pipeline_model_parallel_first_rank(): """Return the global rank of the first process in the pipeline for the current tensor parallel group""" assert _PIPELINE_GLOBAL_RANKS is not None, 'Pipeline parallel group is not initialized' return _PIPELINE_GLOBAL_RANKS[0]
Return the global rank of the first process in the pipeline for the current tensor parallel group
_preempt_by_swap
self._swap_out(seq_group, blocks_to_swap_out) self.swapped.append(seq_group)
def _preempt_by_swap(self, seq_group: SequenceGroup, blocks_to_swap_out: Dict[int, int]) ->None: self._swap_out(seq_group, blocks_to_swap_out) self.swapped.append(seq_group)
null
forward
residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_output = self.attn(hidden_states=hidden_states, kv_cache=kv_cache, input_metadata=input_metadata) hidden_states = attn_output + residual residual = hidden_states hidden_states = self.ln_2(hidden_states) feed_forward_hidden_states = self.mlp(hidd...
def forward(self, hidden_states: torch.Tensor, kv_cache: KVCache, input_metadata: InputMetadata) ->torch.Tensor: residual = hidden_states hidden_states = self.ln_1(hidden_states) attn_output = self.attn(hidden_states=hidden_states, kv_cache=kv_cache, input_metadata=input_metadata) hidden_sta...
null
_verify_args
self.model_config.verify_with_parallel_config(self.parallel_config) self.cache_config.verify_with_parallel_config(self.parallel_config)
def _verify_args(self) ->None: self.model_config.verify_with_parallel_config(self.parallel_config) self.cache_config.verify_with_parallel_config(self.parallel_config)
null
__setstate__
self.__dict__ = d self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file)
def __setstate__(self, d): self.__dict__ = d self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs) self.sp_model.Load(self.vocab_file)
null
_yarn_get_mscale
if scale <= 1: return 1.0 return 0.1 * math.log(scale) + 1.0
def _yarn_get_mscale(scale: float=1) ->float: if scale <= 1: return 1.0 return 0.1 * math.log(scale) + 1.0
null
test_prepare_prompt
model_runner = ModelRunner(None, None, None) model_runner.set_block_size(16) batch_size = random.randint(1, 256) prompt_lens = [] seq_group_metadata_list = [] for i in range(batch_size): prompt_len = i % (model_runner.block_size - 1) + 1 prompt_lens.append(prompt_len) seq_data = list(range(prompt_len)) ...
def test_prepare_prompt(): model_runner = ModelRunner(None, None, None) model_runner.set_block_size(16) batch_size = random.randint(1, 256) prompt_lens = [] seq_group_metadata_list = [] for i in range(batch_size): prompt_len = i % (model_runner.block_size - 1) + 1 prompt_lens.app...
null
__init__
super().__init__(config, 'ROPE', linear_method)
def __init__(self, config, linear_method: Optional[LinearMethodBase]=None): super().__init__(config, 'ROPE', linear_method)
null
get_model
model_class = _get_model_architecture(model_config.hf_config) linear_method = None if model_config.quantization is not None: quant_config = get_quant_config(model_config.quantization, model_config .model, model_config.hf_config, model_config.download_dir) capability = torch.cuda.get_device_capability() ...
def get_model(model_config: ModelConfig) ->nn.Module: model_class = _get_model_architecture(model_config.hf_config) linear_method = None if model_config.quantization is not None: quant_config = get_quant_config(model_config.quantization, model_config.model, model_config.hf_config, model_...
null
vllm_runner
return VllmRunner
@pytest.fixture def vllm_runner(): return VllmRunner
null
forward
gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x
def forward(self, x): gate_up, _ = self.gate_up_proj(x) x = self.act_fn(gate_up) x, _ = self.down_proj(x) return x
null
forward
hidden_states = self.embed_tokens(input_ids) residual = None for i in range(len(self.layers)): layer = self.layers[i] hidden_states, residual = layer(positions, hidden_states, kv_caches[i], input_metadata, residual) hidden_states, _ = self.norm(hidden_states, residual) return hidden_states
def forward(self, input_ids: torch.Tensor, positions: torch.Tensor, kv_caches: List[KVCache], input_metadata: InputMetadata) ->torch.Tensor: hidden_states = self.embed_tokens(input_ids) residual = None for i in range(len(self.layers)): layer = self.layers[i] hidden_states, residual = lay...
null
__init__
super().__init__() hidden_size = config.d_model self.norm_1 = nn.LayerNorm(hidden_size) self.attn = MPTAttention(config, linear_method) self.norm_2 = nn.LayerNorm(hidden_size) self.ffn = MPTMLP(config, linear_method)
def __init__(self, config: MPTConfig, linear_method: Optional[ LinearMethodBase]=None): super().__init__() hidden_size = config.d_model self.norm_1 = nn.LayerNorm(hidden_size) self.attn = MPTAttention(config, linear_method) self.norm_2 = nn.LayerNorm(hidden_size) self.ffn = MPTMLP(config, li...
null
_verify_args
if self.gpu_memory_utilization > 1.0: raise ValueError( f'GPU memory utilization must be less than 1.0. Got {self.gpu_memory_utilization}.' )
def _verify_args(self) ->None: if self.gpu_memory_utilization > 1.0: raise ValueError( f'GPU memory utilization must be less than 1.0. Got {self.gpu_memory_utilization}.' )
null
_prepare_sample
seq_groups: List[Tuple[List[int], SamplingParams]] = [] selected_token_indices: List[int] = [] selected_token_start_idx = 0 categorized_sample_indices = {t: [] for t in SamplingType} categorized_sample_indices_start_idx = 0 max_prompt_len = max(prompt_lens) if prompt_lens else 1 for i, seq_group_metadata in enumerate(s...
def _prepare_sample(self, seq_group_metadata_list: List[ SequenceGroupMetadata], prompt_lens: List[int]) ->SamplingMetadata: seq_groups: List[Tuple[List[int], SamplingParams]] = [] selected_token_indices: List[int] = [] selected_token_start_idx = 0 categorized_sample_indices = {t: [] for t in Sampli...
null
get_tensor_model_parallel_rank
"""Return my rank for the tensor model parallel group.""" return torch.distributed.get_rank(group=get_tensor_model_parallel_group())
def get_tensor_model_parallel_rank(): """Return my rank for the tensor model parallel group.""" return torch.distributed.get_rank(group=get_tensor_model_parallel_group())
Return my rank for the tensor model parallel group.
__init__
super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden_size) self.layers = nn.ModuleList([YiDecoderLayer(config, linear_method) for _ in range(config.num_hidden_layers)]) self...
def __init__(self, config: YiConfig, linear_method: Optional[ LinearMethodBase]=None) ->None: super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden_si...
null
__init__
super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, config. hidden_size) self.layers = nn.ModuleList([BaiChuanDecoderLayer(config, position_embedding, linear_method) for _ in range(config...
def __init__(self, config: BaiChuanConfig, position_embedding: str, linear_method: Optional[LinearMethodBase]=None): super().__init__() self.config = config self.padding_idx = config.pad_token_id self.vocab_size = config.vocab_size self.embed_tokens = VocabParallelEmbedding(config.vocab_size, co...
null
__init__
super().__init__() self.input_size = input_size self.output_size = output_size self.skip_bias_add = skip_bias_add if params_dtype is None: params_dtype = torch.get_default_dtype() self.params_dtype = params_dtype if linear_method is None: linear_method = UnquantizedLinearMethod() self.linear_method = linear_met...
def __init__(self, input_size: int, output_size: int, bias: bool=True, skip_bias_add: bool=False, params_dtype: Optional[torch.dtype]=None, linear_method: Optional[LinearMethodBase]=None): super().__init__() self.input_size = input_size self.output_size = output_size self.skip_bias_add = skip_bi...
null