tcftrees commited on
Commit
9b61800
β€’
1 Parent(s): 29734be

Add application file

Browse files
Files changed (1) hide show
  1. app.py +61 -63
app.py CHANGED
@@ -1,78 +1,76 @@
1
- # # !usr/bin/env python
2
- # # -*- coding:utf-8 -*-
3
-
4
- # '''
5
- # Description :
6
- # Version : 1.0
7
- # Author : Chaofan Tao
8
- # Mail : tcftrees@gmail.com
9
- # Github : https://github.com/sail-sg/scaling-with-vocab
10
- # Date : 2024-08-09 00:25
11
- # Copyright (C) 2024 Chaofan Tao. All rights reserved.
12
- # '''
13
- # import gradio as gr
14
- # from utils import approach1_isoflops, approach2_derivative, approach3_isoloss
15
 
16
 
17
- # def compute_optimal_vocab(Nnv: float,
18
- # flops: float,
19
- # ):
20
 
21
- # if flops is None:
22
- # Vopt_app1 = approach1_isoflops(Nnv)
23
- # Vopt_app2 = approach2_derivative(Nnv)
24
- # Vopt_app3 = approach3_isoloss(Nnv)
25
- # else:
26
- # Vopt_app1, Vopt_app2 = None, None
27
- # Vopt_app3 = approach3_isoloss(Nnv, flops)
 
28
 
29
- # results = f"## The optimal vocabulary size for non-vocabulary parameters {Nnv:1e} is:\nApproach 1: {Vopt_app1}\nApproach 2: {Vopt_app2}Approach 3: {Vopt_app3}"
30
- # return results
31
 
32
 
33
- # with gr.Blocks() as demo:
34
- # with gr.Column():
35
- # gr.Markdown(
36
- # """<img src="https://raw.githubusercontent.com/MrYxJ/calculate-flops.pytorch/main/screenshot/calflops_hf3.png?raw=true" style="float: left;" width="250" height="250"><h1> ⛽️Model(Transformers) FLOPs and Parameter Calculator</h1>
37
- # This tool is used to predict the optimal vocabulary size <h1> given the non-vocabulary parameters $N_{nv}$</h1>.
38
- # We provide 3 ways for prediction:
39
 
40
- # - Approach 1: Build the relationship between studied attributes and FLOPs: Build the relationship between the optimal data points (the points that reach the lowest loss under the same FLOPs budget) and the FLOPs.
41
- # - Approach 2: Derivative-Based Estimation: Fast calculation method using the derivative of FLOPs with respect to the vocabulary size.
42
- # - Approach 3: Parametric Fit of Loss Formula: Design a loss formula that considers the effect of vocabulary size and utilizes the loss to make prediction.
43
 
44
- # Approach 1 and 2 can only be used to compute the optimal vocabulary size when the compute is optimally allocated to non-vocabulary parameters, vocabulary parameters and data jointly.
45
- # Approach 3 will not only consider the case above, but also consider the case when the amount of data does not satisfy the optimal compute allocation, and can calculate the optimal vocabulary size with specified $N_{nv}$ and FLOPs.
46
 
47
- # Thanks for trying 🌟🌟🌟!
48
- # """)
49
 
50
- # with gr.Row():
51
- # Nnv = gr.Textbox(label="Non-vocabulary Parameters", value=7*10**9)
52
- # flops = gr.Textbox(label="FLOPs", placeholder="Optional (e.g. 7.05*10**21)")
53
- # output_text = gr.Textbox(label='output')
54
- # with gr.Row():
55
- # btn = gr.Button("Compute the optimal vocabulary size")
56
 
57
 
58
- # demo = gr.Interface(fn=compute_optimal_vocab, inputs=[Nnv, flops], outputs=output_text)
59
- # btn.click(
60
- # compute_optimal_vocab,
61
- # inputs=[Nnv, flops],
62
- # outputs=output_text
63
- # )
64
- # demo.launch(share=True)
65
 
66
- import gradio as gr
67
- def update(name):
68
- return f"Welcome to Gradio, {name}!"
69
 
70
- with gr.Blocks() as demo:
71
- gr.Markdown("Start typing below and then click **Run** to see the output.")
72
- with gr.Row():
73
- inp = gr.Textbox(placeholder="What is your name?")
74
- out = gr.Textbox()
75
- btn = gr.Button("Run")
76
- btn.click(fn=update, inputs=inp, outputs=out)
77
 
78
- demo.launch()
 
1
+ '''
2
+ Description :
3
+ Version : 1.0
4
+ Author : Chaofan Tao
5
6
+ Github : https://github.com/sail-sg/scaling-with-vocab
7
+ Date : 2024-08-09 00:25
8
+ Copyright (C) 2024 Chaofan Tao. All rights reserved.
9
+ '''
10
+ import gradio as gr
11
+ from utils import approach1_isoflops, approach2_derivative, approach3_isoloss
 
 
 
12
 
13
 
14
+ def compute_optimal_vocab(Nnv: float,
15
+ flops: float,
16
+ ):
17
 
18
+ # if flops is None:
19
+ # Vopt_app1 = approach1_isoflops(Nnv)
20
+ # Vopt_app2 = approach2_derivative(Nnv)
21
+ # Vopt_app3 = approach3_isoloss(Nnv)
22
+ # else:
23
+ # Vopt_app1, Vopt_app2 = None, None
24
+ # Vopt_app3 = approach3_isoloss(Nnv, flops)
25
+ Vopt_app1, Vopt_app2, Vopt_app3=1,2,3
26
 
27
+ results = f"## The optimal vocabulary size for non-vocabulary parameters {Nnv:1e} is:\nApproach 1: {Vopt_app1}\nApproach 2: {Vopt_app2}Approach 3: {Vopt_app3}"
28
+ return results
29
 
30
 
31
+ with gr.Blocks() as demo:
32
+ with gr.Column():
33
+ gr.Markdown(
34
+ """<img src="https://raw.githubusercontent.com/MrYxJ/calculate-flops.pytorch/main/screenshot/calflops_hf3.png?raw=true" style="float: left;" width="250" height="250"><h1> ⛽️Model(Transformers) FLOPs and Parameter Calculator</h1>
35
+ This tool is used to predict the optimal vocabulary size <h1> given the non-vocabulary parameters $N_{nv}$</h1>.
36
+ We provide 3 ways for prediction:
37
 
38
+ - Approach 1: Build the relationship between studied attributes and FLOPs: Build the relationship between the optimal data points (the points that reach the lowest loss under the same FLOPs budget) and the FLOPs.
39
+ - Approach 2: Derivative-Based Estimation: Fast calculation method using the derivative of FLOPs with respect to the vocabulary size.
40
+ - Approach 3: Parametric Fit of Loss Formula: Design a loss formula that considers the effect of vocabulary size and utilizes the loss to make prediction.
41
 
42
+ Approach 1 and 2 can only be used to compute the optimal vocabulary size when the compute is optimally allocated to non-vocabulary parameters, vocabulary parameters and data jointly.
43
+ Approach 3 will not only consider the case above, but also consider the case when the amount of data does not satisfy the optimal compute allocation, and can calculate the optimal vocabulary size with specified $N_{nv}$ and FLOPs.
44
 
45
+ Thanks for trying 🌟🌟🌟!
46
+ """)
47
 
48
+ with gr.Row():
49
+ Nnv = gr.Textbox(label="Non-vocabulary Parameters", value=7*10**9)
50
+ flops = gr.Textbox(label="FLOPs", placeholder="Optional (e.g. 7.05*10**21)")
51
+ output_text = gr.Textbox()
52
+ with gr.Row():
53
+ btn = gr.Button("Compute the optimal vocabulary size")
54
 
55
 
56
+ demo = gr.Interface(fn=compute_optimal_vocab, inputs=[Nnv, flops], outputs=output_text)
57
+ btn.click(
58
+ compute_optimal_vocab,
59
+ inputs=[Nnv, flops],
60
+ outputs=output_text
61
+ )
62
+ demo.launch()
63
 
64
+ # import gradio as gr
65
+ # def update(name):
66
+ # return f"Welcome to Gradio, {name}!"
67
 
68
+ # with gr.Blocks() as demo:
69
+ # gr.Markdown("Start typing below and then click **Run** to see the output.")
70
+ # with gr.Row():
71
+ # inp = gr.Textbox(placeholder="What is your name?")
72
+ # out = gr.Textbox()
73
+ # btn = gr.Button("Run")
74
+ # btn.click(fn=update, inputs=inp, outputs=out)
75
 
76
+ # demo.launch()