testbot commited on
Commit
1fc2558
β€’
1 Parent(s): 60074e0

try in docker

Browse files
Files changed (6) hide show
  1. .dockerignore +2 -0
  2. .gitignore +142 -0
  3. Dockerfile +44 -0
  4. README.md +2 -3
  5. app.py +77 -0
  6. convert.py +36 -0
.dockerignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__
2
+ bloomz.cpp
.gitignore ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib64/
18
+ parts/
19
+ sdist/
20
+ var/
21
+ wheels/
22
+ pip-wheel-metadata/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+
53
+ # Translations
54
+ *.mo
55
+ *.pot
56
+
57
+ # Django stuff:
58
+ *.log
59
+ local_settings.py
60
+ db.sqlite3
61
+ db.sqlite3-journal
62
+
63
+ # Flask stuff:
64
+ instance/
65
+ .webassets-cache
66
+
67
+ # Scrapy stuff:
68
+ .scrapy
69
+
70
+ # Sphinx documentation
71
+ docs/_build/
72
+
73
+ # PyBuilder
74
+ target/
75
+
76
+ # Jupyter Notebook
77
+ .ipynb_checkpoints
78
+
79
+ # IPython
80
+ profile_default/
81
+ ipython_config.py
82
+
83
+ # pyenv
84
+ .python-version
85
+
86
+ # pipenv
87
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
88
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
89
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
90
+ # install all needed dependencies.
91
+ #Pipfile.lock
92
+
93
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow
94
+ __pypackages__/
95
+
96
+ # Celery stuff
97
+ celerybeat-schedule
98
+ celerybeat.pid
99
+
100
+ # SageMath parsed files
101
+ *.sage.py
102
+
103
+ # Environments
104
+ .env
105
+ .venv
106
+ .venv*
107
+ env/
108
+ venv/
109
+ ENV/
110
+ env.bak/
111
+ venv.bak/
112
+ .venv*
113
+
114
+ # Spyder project settings
115
+ .spyderproject
116
+ .spyproject
117
+
118
+ # Rope project settings
119
+ .ropeproject
120
+
121
+ # mkdocs documentation
122
+ /site
123
+
124
+ # mypy
125
+ .mypy_cache/
126
+ .dmypy.json
127
+ dmypy.json
128
+
129
+ # Pyre type checker
130
+ .pyre/
131
+ .vscode/
132
+ .idea/
133
+
134
+ .DS_Store
135
+
136
+ # Ruff
137
+ .ruff_cache
138
+
139
+ # Spell checker config
140
+ cspell.json
141
+
142
+ bloomz.cpp
Dockerfile ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # See https://hub.docker.com/r/nikolaik/python-nodejs
2
+ # https://github.com/nikolaik/docker-python-nodejs
3
+ # Default user is 'pn' with uid 1000, gid 1000
4
+ FROM python:3.10
5
+
6
+ RUN apt-get update && apt-get install -y \
7
+ git \
8
+ cmake \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ # User
12
+ RUN useradd -m -u 1000 user
13
+ USER user
14
+ ENV HOME=/home/user \
15
+ PATH=/home/user/.local/bin:$PATH
16
+ WORKDIR /home/user/app
17
+
18
+ # Install Python deps
19
+ RUN pip install --no-cache-dir pip && \
20
+ pip install --no-cache-dir \
21
+ torch \
22
+ numpy \
23
+ transformers
24
+
25
+ # Prepare bloomz.cpp
26
+ RUN git clone https://github.com/NouamaneTazi/bloomz.cpp@4fc96cbf2e2c257eaca1cd7b7ed8e31741e672ee
27
+
28
+ WORKDIR $HOME/app/bloomz.cpp
29
+
30
+ RUN make
31
+
32
+ # Add files
33
+ COPY . /home/user/app
34
+
35
+ ENV PYTHONPATH=$HOME/app \
36
+ PYTHONUNBUFFERED=1 \
37
+ GRADIO_ALLOW_FLAGGING=never \
38
+ GRADIO_NUM_PORTS=1 \
39
+ GRADIO_SERVER_NAME=0.0.0.0 \
40
+ GRADIO_THEME=huggingface \
41
+ SYSTEM=spaces
42
+
43
+ # Run script
44
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -3,9 +3,8 @@ title: Bloom.cpp Converter
3
  emoji: πŸ‘
4
  colorFrom: green
5
  colorTo: pink
6
- sdk: gradio
7
- sdk_version: 3.21.0
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
 
3
  emoji: πŸ‘
4
  colorFrom: green
5
  colorTo: pink
6
+ sdk: docker
7
+ app_port: 7860
 
8
  pinned: false
9
  ---
10
 
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+
3
+ import gradio as gr
4
+ from huggingface_hub import HfApi, Repository
5
+ from huggingface_hub.utils import RepositoryNotFoundError
6
+
7
+ from convert import convert
8
+
9
+ REPO_PATH = Path("bloomz.cpp")
10
+
11
+ repo = Repository(local_dir="bloomz.cpp", clone_from="https://github.com/NouamaneTazi/bloomz.cpp")
12
+
13
+
14
+ def run(token: str, model_id: str, precision: str, quantization: bool) -> str:
15
+ if token == "" or model_id == "":
16
+ return """
17
+ ### Invalid input 🐞
18
+
19
+ Please fill a token and model_id.
20
+ """
21
+
22
+ api = HfApi(token=token)
23
+ try:
24
+ # TODO: make a PR to bloomz.cpp to be able to pass a token
25
+ api.model_info(repo_id=model_id, token=False) # only public repos are accessible
26
+ except RepositoryNotFoundError:
27
+ return f"""
28
+ ### Error 😒😒😒
29
+
30
+ Repository {model_id} not found. Only public models are convertible at the moment.
31
+ """
32
+
33
+ try:
34
+ model_path = convert(model_id=model_id, precision=precision, quantization=quantization)
35
+ print("[commit_info]", model_path)
36
+
37
+ return f"""
38
+ ### Success πŸ”₯
39
+ """
40
+ return f"""
41
+ ### Success πŸ”₯
42
+ Yay! This model was successfully converted and a PR was open using your token, here:
43
+ # [{commit_info.pr_url}]({commit_info.pr_url})
44
+ """
45
+ except Exception as e:
46
+ return f"""
47
+ ### Error 😒😒😒
48
+
49
+ {e}
50
+ """
51
+
52
+
53
+ DESCRIPTION = """
54
+ The steps are the following:
55
+ - Paste a read-access token from hf.co/settings/tokens. Read access is enough given that we will open a PR against the source repo.
56
+ - Input a model id from the Hub
57
+ - Click "Submit"
58
+ - That's it! You'll get feedback if it works or not, and if it worked, you'll get the URL of the opened PR πŸ”₯
59
+ ⚠️ For now only `pytorch_model.bin` files are supported but we'll extend in the future.
60
+ """
61
+
62
+ demo = gr.Interface(
63
+ title="Convert any BLOOM-like model to be compatible with bloomz.cpp",
64
+ description=DESCRIPTION,
65
+ allow_flagging="never",
66
+ article="Check out the [bloomz.cpp](https://github.com/NouamaneTazi/bloomz.cpp) repo on GitHub",
67
+ inputs=[
68
+ gr.Text(max_lines=1, label="your hf_token"),
69
+ gr.Text(max_lines=1, label="model_id (e.g.: bigscience/bloomz-7b1)"),
70
+ gr.Radio(choices=["FP16", "FP32"], label="Precision", value="FP16"),
71
+ gr.Checkbox(value=False, label="4-bits quantization"),
72
+ ],
73
+ outputs=[gr.Markdown(label="output")],
74
+ fn=run,
75
+ ).queue()
76
+
77
+ demo.launch()
convert.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from subprocess import run
3
+
4
+
5
+ def convert(model_id:str, precision:str, quantization:bool)->Path:
6
+ # Conversion
7
+ cmd = [
8
+ "python",
9
+ "./bloomz.cpp/convert-hf-to-ggml.py",
10
+ model_id, # bigscience/bloomz-560m
11
+ "./bloomz.cpp/models",
12
+ ]
13
+ if precision == "FP32":
14
+ cmd.append("--use-fp32")
15
+ run(cmd, check=True)
16
+
17
+ # Model file should exist
18
+ f_suffix = "f32" if precision == "FP32" else "f16"
19
+ _, model_name = model_id.split("/")
20
+ model_path = Path(f"./bloomz.cpp/models/ggml-model-{model_name}-{f_suffix}.bin")
21
+ assert model_path.is_file()
22
+
23
+ # Quantization
24
+ if quantization:
25
+ cmd = [
26
+ "./bloomz.cpp/quantize",
27
+ f"./bloomz.cpp/models/ggml-model-{model_name}-{f_suffix}.bin",
28
+ f"./bloomz.cpp/models/ggml-model-{model_name}-{f_suffix}-q4_0.bin",
29
+ "2",
30
+ ]
31
+ run(cmd, check=True)
32
+ model_path = Path(f"./bloomz.cpp/models/ggml-model-{model_name}-{f_suffix}.bin")
33
+ assert model_path.is_file()
34
+
35
+ # Return
36
+ return model_path