Vokturz commited on
Commit
76398c6
1 Parent(s): 3fe032d
Files changed (1) hide show
  1. src/app.py +109 -0
src/app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from utils import extract_from_url, get_model, calculate_memory
4
+ import plotly.express as px
5
+ import numpy as np
6
+
7
+ st.set_page_config(page_title='Can you run it? LLM GPU check', layout="wide", initial_sidebar_state="expanded")
8
+
9
+ st.title("Can you run it? LLM GPU check")
10
+
11
+ percentage_width_main = 80
12
+ st.markdown(
13
+ f"""<style>
14
+ .appview-container .main .block-container{{
15
+ max-width: {percentage_width_main}%;}}
16
+ </style>
17
+ """,
18
+ unsafe_allow_html=True,
19
+ )
20
+ @st.cache_resource
21
+ def get_gpu_specs():
22
+ return pd.read_csv("data/gpu_specs.csv")
23
+
24
+
25
+
26
+ def get_name(index):
27
+ row = gpu_specs.iloc[index]
28
+ return f"{row['Product Name']} ({row['RAM (GB)']} GB, {row['Year']})"
29
+
30
+ def create_plot(memory_table, y, title, container):
31
+ fig = px.bar(memory_table, x=memory_table.index, y=y, color_continuous_scale="RdBu_r")
32
+ fig.update_layout(yaxis_title="Number of GPUs", title=dict(text=title, font=dict(size=25)))
33
+ fig.update_coloraxes(showscale=False)
34
+
35
+ container.plotly_chart(fig, use_container_width=True)
36
+
37
+ gpu_specs = get_gpu_specs()
38
+
39
+ access_token = st.sidebar.text_input("Access token")
40
+ model_name = st.sidebar.text_input("Model name", value="mistralai/Mistral-7B-v0.1")
41
+ if not model_name:
42
+ st.info("Please enter a model name")
43
+ st.stop()
44
+
45
+
46
+
47
+ model_name = extract_from_url(model_name)
48
+ if model_name not in st.session_state:
49
+ model = get_model(model_name, library="transformers", access_token=access_token)
50
+ st.session_state[model_name] = (model, calculate_memory(model, ["float32", "float16/bfloat16", "int8", "int4"]))
51
+
52
+
53
+ gpu_vendor = st.sidebar.selectbox("GPU Vendor", ["NVIDIA", "AMD", "Intel"])
54
+ year = st.sidebar.selectbox("Filter by Release Year", list(range(2014, 2024))[::-1], index=None)
55
+ gpu_info = gpu_specs[gpu_specs['Vendor'] == gpu_vendor].sort_values('RAM (GB)', ascending=False)
56
+ if year:
57
+ gpu_info = gpu_info[gpu_info['Year'] == year]
58
+
59
+ min_ram = gpu_info['RAM (GB)'].min()
60
+ max_ram = gpu_info['RAM (GB)'].max()
61
+ ram = st.sidebar.slider("Filter by RAM (GB)", min_ram, max_ram, (min_ram, max_ram), step=0.5)
62
+ gpu_info = gpu_info[gpu_info["RAM (GB)"].between(*ram)]
63
+ gpu = st.sidebar.selectbox("GPU", gpu_info['Product Name'].index.tolist(), index=21, format_func=lambda x : gpu_specs.iloc[x]['Product Name'])
64
+
65
+ gpu_spec = gpu_specs.iloc[gpu]
66
+ gpu_spec.name = 'INFO'
67
+
68
+ lora_pct = st.sidebar.slider("LoRa % trainable parameters", 0.1, 100.0, 2.0, step=0.1)
69
+
70
+ st.sidebar.dataframe(gpu_spec.T)
71
+
72
+ memory_table = pd.DataFrame(st.session_state[model_name][1]).set_index('dtype')
73
+ memory_table['LoRA Fine-Tunning (GB)'] = (memory_table["Total Size (GB)"] +
74
+ (memory_table["Parameters (Billion)"]* lora_pct/100 * (16/8)*4)) * 1.2
75
+
76
+ _, col, _ = st.columns([1,3,1])
77
+ with col.expander("Information", expanded=True):
78
+ st.markdown("""- GPU information comes from [TechPowerUp GPU Specs](https://www.techpowerup.com/gpu-specs/)
79
+ - Mainly based on [Model Memory Calculator by hf-accelerate](https://huggingface.co/spaces/hf-accelerate/model-memory-usage)
80
+ using `transformers` library
81
+ - Inference is calculated following [EleutherAI Transformer Math 101](https://blog.eleuther.ai/transformer-math/),
82
+ where is estimated as """)
83
+
84
+ st.latex(r"""\text{Memory}_\text{Inference} \approx \text{Model Size} \times 1.2""")
85
+ st.markdown("""- For LoRa Fine-tunning, I'm asuming a **16-bit** dtype of trainable parameters. The formula (in terms of GB) is""")
86
+ st.latex(r"\text{Memory}_\text{LoRa} \approx \text{Model Size} + \left(\text{ \# trainable Params}_\text{Billions}\times\frac{16}{8} \times 4\right) \times 1.2")
87
+ st.markdown("- You can understand `int4` as models in `GPTQ-4bit`, `AWQ-4bit` or `Q4_0 GGUF/GGML` formats")
88
+
89
+
90
+ _memory_table = memory_table.copy()
91
+ memory_table = memory_table.round(2).T
92
+ _memory_table /= gpu_spec['RAM (GB)']
93
+ _memory_table = _memory_table.apply(np.ceil).astype(int).drop(columns=['Parameters (Billion)', 'Total Size (GB)'])
94
+ _memory_table.columns = ['Inference', 'Full Training Adam', 'LoRa Fine-tuning']
95
+ _memory_table = _memory_table.stack().reset_index()
96
+ _memory_table.columns = ['dtype', 'Variable', 'Number of GPUs']
97
+
98
+ col1, col2 = st.columns([1,1.3])
99
+ with col1:
100
+ st.write(f"#### [{model_name}](https://huggingface.co/{model_name}) ({memory_table.iloc[3,0]:.1f}B)")
101
+ st.write(memory_table.iloc[[0, 1, 2, 4]])
102
+ with col2:
103
+ num_colors= 4
104
+ colors = [px.colors.sequential.RdBu[int(i*(len(px.colors.sequential.RdBu)-1)/(num_colors-1))] for i in range(num_colors)]
105
+ fig = px.bar(_memory_table, x='Variable', y='Number of GPUs', color='dtype', barmode='group', color_discrete_sequence=colors)
106
+ fig.update_layout(title=dict(text=f"Number of GPUs required for<br> {get_name(gpu)}", font=dict(size=25))
107
+ , xaxis_tickfont_size=14, yaxis_tickfont_size=16, yaxis_dtick='1')
108
+ st.plotly_chart(fig, use_container_width=True)
109
+