File size: 3,034 Bytes
e20929a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff0c2b6
e20929a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea95cc8
 
 
e20929a
 
 
 
 
 
 
 
 
 
ff0c2b6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# !usr/bin/env python
# -*- coding:utf-8 -*-

'''
 Description  : 
 Version      : 1.0
 Author       : Chaofan Tao
 Mail         : tcftrees@gmail.com
 Github       : https://github.com/sail-sg/scaling-with-vocab
 Date         : 2024-08-09 00:25
 Copyright (C) 2024 Chaofan Tao. All rights reserved.
'''
import gradio as gr
from utils import approach1_isoflops, approach2_derivative, approach3_isoloss


def compute_optimal_vocab(Nnv: float,
                        flops: float, 
                        ):
    
    if flops is None:
        Vopt_app1 = approach1_isoflops(Nnv)
        Vopt_app2 = approach2_derivative(Nnv)
        Vopt_app3 = approach3_isoloss(Nnv) 
    else:
        Vopt_app1,  Vopt_app2 = None, None
        Vopt_app3 = approach3_isoloss(Nnv, flops) 
    
    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}"
    return results


with gr.Blocks() as demo:
    with gr.Column():
        gr.Markdown(
            """<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>
    This tool is used to predict the optimal vocabulary size <h1> given the non-vocabulary parameters $N_{nv}$</h1>.
    We provide 3 ways for prediction:

     - 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.
     - Approach 2: Derivative-Based Estimation: Fast calculation method using the derivative of FLOPs with respect to the vocabulary size.
     - 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.
    
    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.
    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.

    Thanks for trying 🌟🌟🌟!
    """)
        
        with gr.Row():
            Nnv = gr.Textbox(label="Non-vocabulary Parameters", value=7*10**9)
            flops = gr.Textbox(label="FLOPs", placeholder="Optional (e.g. 7.05*10**21)")
            output_text = gr.Textbox(label='output')
        with gr.Row():
            btn = gr.Button("Compute the optimal vocabulary size")

  
        demo = gr.Interface(fn=compute_optimal_vocab, inputs=[Nnv, flops], outputs=output_text)
        btn.click(
                compute_optimal_vocab,
                inputs=[Nnv, flops],
                outputs=output_text
            )
demo.launch(share=True)