-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change Llama2 from the Turbine implementation to the Sharktank one #2170
Draft
gpetters-amd
wants to merge
1
commit into
nod-ai:main
Choose a base branch
from
gpetters-amd:sharktank
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
from turbine_models.custom_models import stateless_llama | ||
from shark_turbine.aot import * | ||
from sharktank.models.llama.llama import LlamaModelConfig, PagedLlamaModelV1 | ||
import sharktank | ||
import huggingface_hub | ||
|
||
# from turbine_models.custom_models import stateless_llama | ||
from turbine_models.model_runner import vmfbRunner | ||
from turbine_models.gen_external_params.gen_external_params import gen_external_params | ||
import time | ||
|
@@ -19,23 +24,23 @@ | |
|
||
llm_model_map = { | ||
"meta-llama/Llama-2-7b-chat-hf": { | ||
"initializer": stateless_llama.export_transformer_model, | ||
# "initializer": stateless_llama.export_transformer_model, | ||
"hf_model_name": "meta-llama/Llama-2-7b-chat-hf", | ||
"compile_flags": ["--iree-opt-const-expr-hoisting=False"], | ||
"stop_token": 2, | ||
"max_tokens": 4096, | ||
"system_prompt": """<s>[INST] <<SYS>>Be concise. You are a helpful, respectful and honest assistant. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>>""", | ||
}, | ||
"Trelis/Llama-2-7b-chat-hf-function-calling-v2": { | ||
"initializer": stateless_llama.export_transformer_model, | ||
# "initializer": stateless_llama.export_transformer_model, | ||
"hf_model_name": "Trelis/Llama-2-7b-chat-hf-function-calling-v2", | ||
"compile_flags": ["--iree-opt-const-expr-hoisting=False"], | ||
"stop_token": 2, | ||
"max_tokens": 4096, | ||
"system_prompt": """<s>[INST] <<SYS>>Be concise. You are a helpful, respectful and honest assistant. If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information. <</SYS>>""", | ||
}, | ||
"TinyPixel/small-llama2": { | ||
"initializer": stateless_llama.export_transformer_model, | ||
# "initializer": stateless_llama.export_transformer_model, | ||
"hf_model_name": "TinyPixel/small-llama2", | ||
"compile_flags": ["--iree-opt-const-expr-hoisting=True"], | ||
"stop_token": 2, | ||
|
@@ -130,13 +135,18 @@ def __init__( | |
print( | ||
f"External weight file {self.external_weight_file} does not exist. Generating..." | ||
) | ||
gen_external_params( | ||
hf_model_name=self.hf_model_name, | ||
quantization=self.quantization, | ||
weight_path=self.external_weight_file, | ||
hf_auth_token=hf_auth_token, | ||
precision=self.precision, | ||
# gen_external_params( | ||
# hf_model_name=self.hf_model_name, | ||
# quantization=self.quantization, | ||
# weight_path=self.external_weight_file, | ||
# hf_auth_token=hf_auth_token, | ||
# precision=self.precision, | ||
# ) | ||
cache_dir = os.path.join(".", str(self.hf_model_name).replace("/", "_")) | ||
huggingface_hub.snapshot_download( | ||
repo_id=self.hf_model_name, cache_dir=cache_dir | ||
) | ||
# TODO: Convert to gguf, delete cache | ||
else: | ||
print( | ||
f"External weight file {self.external_weight_file} found for {self.vmfb_name}" | ||
|
@@ -161,20 +171,39 @@ def __init__( | |
use_auth_token=hf_auth_token, | ||
) | ||
elif not os.path.exists(self.tempfile_name): | ||
self.torch_ir, self.tokenizer = llm_model_map[self.hf_model_name][ | ||
"initializer" | ||
]( | ||
self.hf_model_name, | ||
hf_auth_token, | ||
compile_to="torch", | ||
external_weights=external_weights, | ||
precision=self.precision, | ||
quantization=self.quantization, | ||
streaming_llm=self.streaming_llm, | ||
decomp_attn=True, | ||
# self.torch_ir, self.tokenizer = llm_model_map[self.hf_model_name][ | ||
# "initializer" | ||
# ]( | ||
# self.hf_model_name, | ||
# hf_auth_token, | ||
# compile_to="torch", | ||
# external_weights=external_weights, | ||
# precision=self.precision, | ||
# quantization=self.quantization, | ||
# streaming_llm=self.streaming_llm, | ||
# decomp_attn=True, | ||
# ) | ||
|
||
dataset = sharktank.types.Dataset.load( | ||
self.external_weight_file, file_type="gguf" | ||
) | ||
hp = sharktank.layers.configs.LlamaHParams.from_gguf_props( | ||
dataset.properties | ||
) | ||
with open(self.tempfile_name, "w+") as f: | ||
f.write(self.torch_ir) | ||
llama_config = sharktank.models.llama.llama.LlamaModelConfig(hp) | ||
llama_config.use_hf = False | ||
llama_config.static_tables = ( | ||
False # Rely on the compiler for hoisting tables. | ||
) | ||
llama_config.kv_cache_type = "direct" # if args.bs == [1] else "paged" | ||
model = PagedLlamaModelV1(dataset.root_theta, llama_config) | ||
|
||
fxb = FxProgramsBuilder(model) | ||
self.torch_ir = export(fxb) | ||
self.torch_ir.save_mlir(self.tempfile_name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure why, but this is producing an empty module. Any idea what I'm missing? |
||
|
||
# with open(self.tempfile_name, "w+") as f: | ||
# f.write(self.torch_ir) | ||
del self.torch_ir | ||
gc.collect() | ||
self.compile() | ||
|
@@ -413,7 +442,7 @@ def llm_chat_api(InputData: dict): | |
hf_auth_token=cmd_opts.hf_auth_token, | ||
device=device, | ||
quantization=cmd_opts.quantization, | ||
external_weights="safetensors", | ||
external_weights="gguf", | ||
use_system_prompt=True, | ||
streaming_llm=False, | ||
) | ||
|
@@ -467,7 +496,7 @@ def llm_chat_api(InputData: dict): | |
"Trelis/Llama-2-7b-chat-hf-function-calling-v2", | ||
hf_auth_token=None, | ||
device="cpu-task", | ||
external_weights="safetensors", | ||
external_weights="gguf", | ||
) | ||
|
||
print("model loaded") | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way that sharktank recommends for generating the .gguf file is to use a CLI tool from llama.cpp. Is that still the best way to extract that, or do we have a way to do it using sharktank?