-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
417 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import time | ||
|
||
import torch | ||
|
||
from tllm.engine import MyLlamaForCausalLM | ||
from tllm.generate.decode_utils import DecodeUtils | ||
from tllm.generate.token_utils import TokenizerUtils | ||
from tllm.protocol import ChatCompletionRequest | ||
from tllm.rpc.manager import RPCManager | ||
|
||
if __name__ == "__main__": | ||
BASE_PATH = "/Users/lujianghu/Documents/" | ||
model_path = os.path.join(BASE_PATH, "Llama-3.2-1B-Instruct") | ||
weight_path = os.path.join(model_path, "master_weight.pt") | ||
|
||
url_list = ["localhost:25001"] | ||
server = RPCManager(url_list) | ||
model = MyLlamaForCausalLM.from_pretrained(model_path, weight_path, server) | ||
tok = TokenizerUtils(model_path) | ||
|
||
request = ChatCompletionRequest( | ||
messages=[{"role": "user", "content": "Hello, how are you?"}], max_tokens=20, do_sample=False | ||
) | ||
input_id_list = tok.preprocess(messages=request.messages).input_ids | ||
|
||
input_ids = torch.tensor(input_id_list).unsqueeze(0) | ||
print("input_ids: ", input_ids) | ||
|
||
s1 = time.time() | ||
output = model.generate( | ||
input_ids, max_new_tokens=request.max_tokens, do_sample=request.do_sample, sampler=DecodeUtils("greedy") | ||
) | ||
print(output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/bin/bash | ||
MASTER_PORT=29501 | ||
GRPC_PORT=25001 | ||
BASE_PATH=/Users/lujianghu/Documents/ | ||
MODE_SIZE=$1 | ||
TP=$2 | ||
|
||
|
||
if [ $MODE_SIZE == "1" ]; then | ||
MODEL_PATH=$BASE_PATH/Llama-3.2-1B-Instruct | ||
START_LAYER_IDX=0 | ||
END_LAYER_IDX=16 | ||
elif [ $MODE_SIZE == "3" ]; then | ||
MODEL_PATH=$BASE_PATH/Llama-3.2-3B-Instruct | ||
START_LAYER_IDX=0 | ||
END_LAYER_IDX=28 | ||
elif [ $MODE_SIZE == "8" ]; then | ||
MODEL_PATH=$BASE_PATH/Meta-Llama-3-8B-Instruct | ||
START_LAYER_IDX=0 | ||
END_LAYER_IDX=32 | ||
elif [ $MODE_SIZE == "70" ]; then | ||
MODEL_PATH=$BASE_PATH/Meta-Llama-3-70B-Instruct | ||
else | ||
echo "Invalid mode size" | ||
exit 1 | ||
fi | ||
|
||
export OMP_NUM_THREADS=8; | ||
export PYTHONPATH="./":$PYTHONPATH; | ||
|
||
torchrun --nproc_per_node=$TP --master_port=$MASTER_PORT tllm/rpc/client.py --port=$GRPC_PORT --start_layer_idx=$START_LAYER_IDX --end_layer_idx=$END_LAYER_IDX --model_path $MODEL_PATH |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import List | ||
|
||
import torch | ||
|
||
|
||
def merge_mask(mask_list: List[torch.Tensor], total_length: int) -> torch.Tensor: | ||
combined_mask = torch.zeros((total_length, total_length), dtype=torch.bool) | ||
|
||
start_index = 0 | ||
for mask in mask_list: | ||
combined_mask[start_index : start_index + mask.size(0), start_index : start_index + mask.size(1)] = mask | ||
start_index += mask.size(0) | ||
|
||
combined_attn_bias = torch.zeros(total_length, total_length, dtype=torch.float) | ||
combined_attn_bias.masked_fill_(combined_mask.logical_not(), float("-inf")) | ||
return combined_attn_bias | ||
|
||
|
||
def build_mask(mask: torch.Tensor) -> torch.Tensor: | ||
attn_bias = torch.zeros(mask.size(0), mask.size(1), dtype=torch.float) | ||
attn_bias.masked_fill_(mask.logical_not(), float("-inf")) | ||
return attn_bias | ||
|
||
|
||
def build_qkv(bs, num_heads, seq_len, head_dim): | ||
query = torch.randn(bs, num_heads, seq_len, head_dim) | ||
key = torch.randn(bs, num_heads, seq_len, head_dim) | ||
value = torch.randn(bs, num_heads, seq_len, head_dim) | ||
return query, key, value | ||
|
||
|
||
if __name__ == "__main__": | ||
seq_len1, seq_len2 = 3, 4 | ||
temp_mask = torch.ones(seq_len1, seq_len1, dtype=torch.bool).tril(diagonal=0) | ||
temp_mask2 = torch.ones(seq_len2, seq_len2, dtype=torch.bool).tril(diagonal=0) | ||
|
||
combined_attn_bias = merge_mask([temp_mask, temp_mask2], seq_len1 + seq_len2) | ||
|
||
# bs, num_heads, seq_len, head_dim | ||
query1, key1, value1 = build_qkv(1, 2, seq_len1, 4) | ||
base_out1 = torch.nn.functional.scaled_dot_product_attention(query1, key1, value1, is_causal=True) | ||
|
||
query2, key2, value2 = build_qkv(1, 2, seq_len2, 4) | ||
base_out2 = torch.nn.functional.scaled_dot_product_attention(query2, key2, value2, is_causal=True) | ||
|
||
query = torch.cat([query1, query2], dim=-2) | ||
key = torch.cat([key1, key2], dim=-2) | ||
value = torch.cat([value1, value2], dim=-2) | ||
out = torch.nn.functional.scaled_dot_product_attention(query, key, value, attn_mask=combined_attn_bias) | ||
out1, out2 = torch.split(out, [seq_len1, seq_len2], dim=-2) | ||
|
||
print("torch.allclose(base_out1, out1)", torch.allclose(base_out1, out1)) | ||
print("torch.allclose(base_out2, out2)", torch.allclose(base_out2, out2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Tuple | ||
|
||
import torch | ||
from transformers.models.llama.modeling_llama import LlamaRotaryEmbedding, apply_rotary_pos_emb | ||
|
||
|
||
def build_qkv(bs, num_heads, seq_len, head_dim): | ||
query = torch.randn(bs, num_heads, seq_len, head_dim) | ||
key = torch.randn(bs, num_heads, seq_len, head_dim) | ||
value = torch.randn(bs, num_heads, seq_len, head_dim) | ||
return query, key, value | ||
|
||
|
||
if __name__ == "__main__": | ||
seq_len1, seq_len2 = 3, 4 | ||
head_dim = 4 | ||
query1, key1, value1 = build_qkv(1, 2, seq_len1, head_dim) | ||
query2, key2, value2 = build_qkv(1, 2, seq_len2, head_dim) | ||
position_ids1 = torch.arange(seq_len1).unsqueeze(0) | ||
position_ids2 = torch.arange(seq_len2).unsqueeze(0) | ||
|
||
rotary_emb = LlamaRotaryEmbedding( | ||
head_dim, | ||
max_position_embeddings=4096, | ||
base=10000, | ||
) | ||
cos1, sin1 = rotary_emb(value1, seq_len=seq_len1) | ||
cos2, sin2 = rotary_emb(value2, seq_len=seq_len2) | ||
|
||
query1_out, key1_out = apply_rotary_pos_emb(query1, key1, cos1, sin1, position_ids1) | ||
query2_out, key2_out = apply_rotary_pos_emb(query2, key2, cos2, sin2, position_ids2) | ||
|
||
large_cos1, large_sin1 = rotary_emb(value1, seq_len=seq_len1 * 10) | ||
large_query1, large_key1 = apply_rotary_pos_emb(query1, key1, large_cos1, large_sin1, position_ids1) | ||
|
||
print("test large seq len", torch.allclose(large_query1, query1_out)) | ||
|
||
# merge | ||
query = torch.cat([query1, query2], dim=-2) | ||
key = torch.cat([key1, key2], dim=-2) | ||
position_ids = torch.cat([position_ids1, position_ids2], dim=-1) | ||
cos, sin = rotary_emb(value1, seq_len=max(seq_len1, seq_len2)) | ||
query_out, key_out = apply_rotary_pos_emb(query, key, cos, sin, position_ids) | ||
print("test merge rope for query", torch.allclose(query_out, torch.cat([query1_out, query2_out], dim=-2))) | ||
print("test merge rope for key", torch.allclose(key_out, torch.cat([key1_out, key2_out], dim=-2))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ exclude = ''' | |
| .venv | ||
| build | ||
| dist | ||
| .pyi | ||
| schemas_pb2.py | ||
| schemas_pb2_grpc.py | ||
)/ | ||
''' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
transformers==4.36.2 | ||
transformers==4.45.1 | ||
torch | ||
grpcio==1.66.2 | ||
grpcio-tools==1.66.2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.