mmgp 3.3.1__py3-none-any.whl → 3.6.11__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,197 +1,195 @@
1
- Metadata-Version: 2.4
2
- Name: mmgp
3
- Version: 3.3.1
4
- Summary: Memory Management for the GPU Poor
5
- Author-email: deepbeepmeep <deepbeepmeep@yahoo.com>
6
- License: GNU GENERAL PUBLIC LICENSE
7
- Version 3, 29 June 2007
8
- Requires-Python: >=3.10
9
- Description-Content-Type: text/markdown
10
- License-File: LICENSE.md
11
- Requires-Dist: torch>=2.1.0
12
- Requires-Dist: optimum-quanto
13
- Requires-Dist: accelerate
14
- Requires-Dist: safetensors
15
- Requires-Dist: psutil
16
- Dynamic: license-file
17
-
18
-
19
- <p align="center">
20
- <H2>Memory Management 3.3.1 for the GPU Poor by DeepBeepMeep</H2>
21
- </p>
22
-
23
-
24
- This module contains multiples optimisations so that models such as Flux (and derived), Mochi, CogView, HunyuanVideo, ... can run smoothly on a 12 to 24 GB GPU limited card.
25
- This a replacement for the accelerate library that should in theory manage offloading, but doesn't work properly with models that are loaded / unloaded several
26
- times in a pipe (eg VAE).
27
-
28
- Requirements:
29
- - VRAM: minimum 6 GB, recommended 24 GB (RTX 3090/ RTX 4090)
30
- - RAM: minimum 24 GB, recommended 48 GB
31
-
32
- This module features 5 profiles in order to able to run the model at a decent speed on a low end consumer config (24 GB of RAM and 6 VRAM) and to run it at a very good speed (if not the best) on a high end consumer config (48 GB of RAM and 24 GB of VRAM).\
33
- These RAM requirements are for Linux systems. Due to different memory management Windows will require an extra 16 GB of RAM to run the corresponding profile.
34
-
35
- Each profile may use a combination of the following:
36
- - Low RAM consumption (thanks to a rewritten safetensors library) that allows low RAM on the fly quantization
37
- - Smart automated loading / unloading of models in the GPU to avoid unloading models that may be needed again soon
38
- - Smart slicing of models to reduce memory occupied by models in the VRAM
39
- - Ability to pin models to reserved RAM to accelerate transfers to VRAM
40
- - Async transfers to VRAM to avoid a pause when loading a new slice of a model
41
- - Automated on the fly quantization or ability to load pre quantized models
42
- - Pretrained Lora support with low RAM requirements
43
- - Support for pytorch compilation on Linux and WSL (supported on pure Windows but requires a complex Triton Installation).
44
-
45
- ## Sample applications that use mmgp
46
- It is recommended to have a look at these applications to see how mmgp was implemented in each of them:
47
- - Wan2GP: https://github.com/deepbeepmeep/Wan2GP :\
48
- An excellent text to video and image to video generator by Alibaba
49
-
50
- - Hunyuan3D-2GP: https://github.com/deepbeepmeep/Hunyuan3D-2GP :\
51
- A great image to 3D and text to 3D tool by the Tencent team. Thanks to mmgp it can run with less than 6 GB of VRAM
52
-
53
- - HuanyuanVideoGP: https://github.com/deepbeepmeep/HunyuanVideoGP :\
54
- One of the best open source Text to Video generator
55
-
56
- - FluxFillGP: https://github.com/deepbeepmeep/FluxFillGP :\
57
- One of the best inpainting / outpainting tools based on Flux that can run with less than 12 GB of VRAM.
58
-
59
- - Cosmos1GP: https://github.com/deepbeepmeep/Cosmos1GP :\
60
- This application include two models: a text to world generator and a image / video to world (probably the best open source image to video generator).
61
-
62
- - OminiControlGP: https://github.com/deepbeepmeep/OminiControlGP :\
63
- A Flux derived application very powerful that can be used to transfer an object of your choice in a prompted scene. With mmgp you can run it with only 6 GB of VRAM.
64
-
65
- - YuE GP: https://github.com/deepbeepmeep/YuEGP :\
66
- A great song generator (instruments + singer's voice) based on prompted Lyrics and a genre description. Thanks to mmgp you can run it with less than 10 GB of VRAM without waiting forever.
67
-
68
- ## Installation
69
- First you need to install the module in your current project with:
70
- ```shell
71
- pip install mmgp
72
- ```
73
-
74
-
75
- ## Usage
76
-
77
- It is almost plug and play and just needs to be invoked from the main app just after the model pipeline has been created.
78
- 1) First make sure that the pipeline explictly loads the models in the CPU device, for instance:
79
- ```
80
- pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to("cpu")
81
- ```
82
-
83
- 2) Once every potential Lora has been loaded and merged, add the following lines for a quick setup:
84
- ```
85
- from mmgp import offload, profile_type
86
- offload.profile(pipe, profile_type.HighRAM_LowVRAM_Fast)
87
- ```
88
-
89
- You can choose between 5 profiles depending on your hardware:
90
- - HighRAM_HighVRAM (1): at least 48 GB of RAM and 24 GB of VRAM : the fastest well suited for a RTX 3090 / RTX 4090 but consumes much more VRAM, adapted for fast shorter video or small batches of pictures
91
- - HighRAM_LowVRAM (2): at least 48 GB of RAM and 12 GB of VRAM : a bit slower, better suited for RTX 3070/3080/4070/4080 or for RTX 3090 / RTX 4090 with large pictures batches or long videos
92
- - LowRAM_HighVRAM (3): at least 32 GB of RAM and 24 GB of VRAM : adapted for RTX 3090 / RTX 4090 with limited RAM but at the cost of VRAM (shorter videos / fewer images)
93
- - LowRAM_LowVRAM (4): at least 32 GB of RAM and 12 GB of VRAM : if you have little VRAM or want to generate longer videos / more images
94
- - VerylowRAM_LowVRAM (5): at least 24 GB of RAM and 10 GB of VRAM : if you don't have much it won't be fast but maybe it will work
95
-
96
- Profile 2 (High RAM) and 4 (Low RAM) are the most recommended profiles since they are versatile (support for long videos for a slight performance cost).\
97
- If you use Flux derived applciation profile 1 and 3 will offer much faster generation times.
98
- In any case, a safe approach is to start from profile 5 (default profile) and then go down progressively to profile 4 and then to profile 2 as long as the app remains responsive or doesn't trigger any out of memory error.
99
-
100
- By default the model named 'transformer' will be quantized to 8 bits for all profiles. If you don't want that you may specify the optional parameter *quantizeTransformer = False*.
101
-
102
- Every parameter set automatically by a profile can be overridden with one or multiple parameters accepted by *offload.all* (see below):
103
- ```
104
- from mmgp import offload, profile_type
105
- offload.profile(pipe, profile_type.HighRAM_LowVRAM, budgets = 1000)
106
- ```
107
- If you want to know which parameter are set by one specific profile you can use the parameter *verboseLevel=2*
108
-
109
- **It is highly recommended to put the *from mmgp import offload, profile_type* at the top of your main python file (that is as the first import) so that all the existing safetensors calls are redirected to mmpg.**
110
-
111
-
112
- ## Alternatively you may want to create your own profile with specific parameters:
113
-
114
- For example:
115
- ```
116
- from mmgp import offload
117
- offload.all(pipe, pinnedMemory=True, ExtraModelsToQuantize = ["text_encoder_2"] )
118
- ```
119
- - pinnedMemory: Boolean (for all models) or List of models ids to pin to RAM. Every model pinned to RAM will load much faster (up to 2 times) but this requires more RAM
120
- - quantizeTransformer: boolean by default True. The 'transformer' model in the pipe contains usually the video or image generator is by defaut; quantized on the fly by default to 8 bits. If you want to save time on disk and reduce the loading time, you may want to load directly a prequantized model. If you don't want to quantize the image generator, you need to set the option *quantizeTransformer* to *False* to turn off on the fly quantization.
121
- - extraModelsToQuantize: list of additional modelids of models to quantize on the fly. If the corresponding model is already quantized, this option will be ignored.
122
- - budgets: either a number in mega bytes, (for all models, if 0 unlimited budget) a string that is perecentage of the total VRAM or a dictionary that maps model ids to mega bytes : define the approximate budget in mega bytes that is allocated in VRAM for a model. Try not to allocate all the available VRAM so that the rest can be used to process the data. To define the default value in the dictionary, you may add entry named "*".
123
- The smaller this number, the more VRAM left for image data / longer video but also the slower because there will be lots of loading / unloading between the RAM and the VRAM. If model is too big to fit in a budget, it will be broken down in multiples parts that will be unloaded / loaded consequently. The speed of low budget can be increased (up to 2 times) by turning on the options pinnedMemory and asyncTransfers.
124
- - workingVRAM: either a number in mega bytes, a string that is perecentage of the total VRAM or a dictionary that maps a model ids to a number in mega bytes that corresponds to a minimum amount of VRAM that should be left for the data processed by the model. This number will prevail if it is in conflict with a too high budget defined for the same model.
125
- - asyncTransfers: boolean, load to the GPU the next model part while the current part is being processed. This requires twice the budget if any is defined. This may increase speed by 20% (mostly visible on fast modern GPUs).
126
- - verboseLevel: number between 0 and 2 (1 by default), provides various level of feedback of the different processes
127
- - compile: list of model ids to compile, may accelerate up x2 depending on the type of GPU. It makes sense to compile only the model that is frequently used such as the "transformer" model in the case of video or image generation. Compilation requires Triton to be installed. Triton is available out of the box on Linux or WSL but requires to be installed with Windows: https://github.com/woct0rdho/triton-windows
128
- - coTenantsMap: a dictionary that maps a model id to a list of other models with which it accepts to share the VRAM at the same time. This is useful to avoid unefficient loading / unloading when two models processes are interleaved. For instance *coTenantsMap = { "text_encoder_2": ["text_encoder"] }* , here when *text_encoder_2* is loaded it won't unload *text_encoder*. Please note that the reverse is not true as these maps by design are not symetrical to allow tailored workflows. If you need to have as well *text_encoder* that won't unload *text_encoder_2* if it is already loaded *coTenantsMap = { "text_encoder_2": ["text_encoder"], "text_encoder": ["text_encoder_2"] }*
129
-
130
- If you are short on RAM and plan to work with quantized models, it is recommended to load pre-quantized models direclty rather than using on the fly quantization, it will be faster and consume slightly less RAM.
131
-
132
- ## Going further
133
-
134
- The module includes several tools to package a light version of your favorite video / image generator:
135
- - *extract_models(string prefix, obj to explore)*\
136
- This tool will try to detect for you models that are embedded in a pipeline or in some custom class. It will save you time by building a pipe dictionary required by *offload.all* or "offload.profile*. The prefix correponds to the text that will appear before the name of each model in the dictionary.
137
-
138
- - *load_loras_into_model(model, lora_path, lora_multi, activate_all_loras = True)*\
139
- Load in a model a list of Lora described by a list of path *lora_path* and a list of *weights coefficients*.
140
- The Lora file must be in the *diffusers* format. This function works also on non diffusers models. However if there is already an official Lora support for a model it is recommended to use the official diffusers functions. By default all the load loras will be activated or they can be activated later using *activate_loras*.
141
-
142
- -*activate_loras(model, lora_nos, lora_multi = None )*\
143
- Activate the loras whose nos are in the list of nos. Every lora that is not this list and that was activated previously will be disactivated.
144
-
145
- - *save_model(model, file_path, do_quantize = False, quantizationType = qint8 )*\
146
- Save tensors of a model already loaded in memory in a safetensor format (much faster to reload). You can save it in a quantized format (default qint8 quantization recommended).
147
- The resulting safetensor file will contain extra fields in its metadata such as the quantization map and its configuration, so you will be able to move the file around without files such as *config.json* or *file_map.json*.
148
- You will need *load_model_data* or *fast_load_transformers_model* to read the file again . You may also load it using the default *safetensor* librar however you will need to provide in the same directory any complementary file that are usually requested (for instance *config.json*)
149
-
150
- - *load_model_data(model, file_path: str, do_quantize = False, quantizationType = qint8, pinToRAM = False, partialPin = False)*\
151
- Load the tensors data of a model in RAM of a model already initialized with no data. Detect and handle quantized models saved previously with *save_model*.A model can also be quantized on the fly while being loaded. The model which is loaded can be pinned to RAM while it is loaded, this is more RAM efficient than pinning tensors later using *offline.all* or *offline.profile*
152
-
153
- - *fast_load_transformers_model(model_path: str, do_quantize = False, quantizationType = qint8, pinToRAM = False, partialPin = False)*\
154
- Initialize (build the model hierarchy in memory) and fast load the corresponding tensors of a 'transformers' or 'diffusers' library model.
155
- The advantages over the original *from_pretrained* method is that a full model can fit into a single file with a filename of your choosing (thefore you can have multiple 'transformers' versions of the same model in the same directory) and prequantized models are processed in a transparent way.
156
- Last but not least, you can also on the fly pin to RAM the whole model or the most important part of it (partialPin = True) in a more efficient way (faster and requires less RAM) than if you did through *offload.all* or *offload.profile*.
157
-
158
-
159
- The typical workflow wil be:
160
- 1) temporarly insert the *save_model* function just after a model has been fully loaded to save a copy of the model / quantized model.
161
- 2) replace the full initalizing / loading logic with *fast_load_transformers_model* (if there is a *from_pretrained* call to a transformers object) or only the tensor loading functions (*torch.load_model_file* and *torch.load_state_dict*) with *load_model_data after* the initializing logic.
162
-
163
- ## Special cases
164
- Sometime there isn't an explicit pipe object as each submodel is loaded separately in the main app. If this is the case, you may try to use *extract_models* or create a dictionary that manually maps all the models.\
165
- For instance :
166
-
167
-
168
- - for flux derived models:
169
- ```
170
- pipe = { "text_encoder": clip, "text_encoder_2": t5, "transformer": model, "vae":ae }
171
- ```
172
- - for mochi:
173
- ```
174
- pipe = { "text_encoder": self.text_encoder, "transformer": self.dit, "vae":self.decoder }
175
- ```
176
-
177
-
178
- Please note it is recommended to have always one model whose Id is 'transformer' so that you can leverage predefined profiles. The 'transformer' corresponds to the main image / video model which usually needs to be quantized (this is done on the fly by default when loading the model).
179
-
180
- Be careful, lots of models use the T5 XXL as a text encoder. However, quite often their corresponding pipeline configurations point at the official Google T5 XXL repository
181
- where there is a huge 40GB model to download and load. It is cumbersorme as it is a 32 bits model and contains the decoder part of T5 that is not used.
182
- I suggest you use instead one of the 16 bits encoder only version available around, for instance:
183
- ```
184
- text_encoder_2 = T5EncoderModel.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="text_encoder_2", torch_dtype=torch.float16)
185
- ```
186
-
187
- Sometime just providing the pipe won't be sufficient as you will need to change the content of the core model:
188
- - For instance you may need to disable an existing CPU offload logic that already exists (such as manual calls to move tensors between cuda and the cpu)
189
- - mmpg to tries to fake the device as being "cuda" but sometimes some code won't be fooled and it will create tensors in the cpu device and this may cause some issues.
190
-
191
- You are free to use my module for non commercial use as long you give me proper credits. You may contact me on twitter @deepbeepmeep
192
-
193
- Thanks to
194
- ---------
195
- - Huggingface / accelerate for the hooking examples
196
- - Huggingface / quanto for their very useful quantizer
197
- - gau-nernst for his Pinnig RAM samples
1
+ Metadata-Version: 2.4
2
+ Name: mmgp
3
+ Version: 3.6.11
4
+ Summary: Memory Management for the GPU Poor
5
+ Author-email: deepbeepmeep <deepbeepmeep@yahoo.com>
6
+ Requires-Python: >=3.10
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE.md
9
+ Requires-Dist: torch>=2.1.0
10
+ Requires-Dist: optimum-quanto
11
+ Requires-Dist: accelerate
12
+ Requires-Dist: safetensors
13
+ Requires-Dist: psutil
14
+ Dynamic: license-file
15
+
16
+
17
+ <p align="center">
18
+ <H2>Memory Management 3.6.11 for the GPU Poor by DeepBeepMeep</H2>
19
+ </p>
20
+
21
+
22
+ This module contains multiples optimisations so that models such as Flux (and derived), Mochi, CogView, HunyuanVideo, ... can run smoothly on a 12 to 24 GB GPU limited card.
23
+ This a replacement for the accelerate library that should in theory manage offloading, but doesn't work properly with models that are loaded / unloaded several
24
+ times in a pipe (eg VAE).
25
+
26
+ Requirements:
27
+ - VRAM: minimum 6 GB, recommended 24 GB (RTX 3090/ RTX 4090)
28
+ - RAM: minimum 24 GB, recommended 48 GB
29
+
30
+ This module features 5 profiles in order to able to run the model at a decent speed on a low end consumer config (24 GB of RAM and 6 VRAM) and to run it at a very good speed (if not the best) on a high end consumer config (48 GB of RAM and 24 GB of VRAM).\
31
+ These RAM requirements are for Linux systems. Due to different memory management Windows will require an extra 16 GB of RAM to run the corresponding profile.
32
+
33
+ Each profile may use a combination of the following:
34
+ - Low RAM consumption (thanks to a rewritten safetensors library) that allows low RAM on the fly quantization
35
+ - Smart automated loading / unloading of models in the GPU to avoid unloading models that may be needed again soon
36
+ - Smart slicing of models to reduce memory occupied by models in the VRAM
37
+ - Ability to pin models to reserved RAM to accelerate transfers to VRAM
38
+ - Async transfers to VRAM to avoid a pause when loading a new slice of a model
39
+ - Automated on the fly quantization or ability to load pre quantized models
40
+ - Pretrained Lora support with low RAM requirements
41
+ - Support for pytorch compilation on Linux and WSL (supported on pure Windows but requires a complex Triton Installation).
42
+
43
+ ## Sample applications that use mmgp
44
+ It is recommended to have a look at these applications to see how mmgp was implemented in each of them:
45
+ - Wan2GP: https://github.com/deepbeepmeep/Wan2GP :\
46
+ An excellent text to video and image to video generator that supports the best Open Source Video Architectures: Wan, Hunyuan and LTX Video
47
+
48
+ - Hunyuan3D-2GP: https://github.com/deepbeepmeep/Hunyuan3D-2GP :\
49
+ A great image to 3D and text to 3D tool by the Tencent team. Thanks to mmgp it can run with less than 6 GB of VRAM
50
+
51
+ - HuanyuanVideoGP: https://github.com/deepbeepmeep/HunyuanVideoGP :\
52
+ One of the best open source Text to Video generator
53
+
54
+ - FluxFillGP: https://github.com/deepbeepmeep/FluxFillGP :\
55
+ One of the best inpainting / outpainting tools based on Flux that can run with less than 12 GB of VRAM.
56
+
57
+ - Cosmos1GP: https://github.com/deepbeepmeep/Cosmos1GP :\
58
+ This application include two models: a text to world generator and a image / video to world (probably the best open source image to video generator).
59
+
60
+ - OminiControlGP: https://github.com/deepbeepmeep/OminiControlGP :\
61
+ A Flux derived application very powerful that can be used to transfer an object of your choice in a prompted scene. With mmgp you can run it with only 6 GB of VRAM.
62
+
63
+ - YuE GP: https://github.com/deepbeepmeep/YuEGP :\
64
+ A great song generator (instruments + singer's voice) based on prompted Lyrics and a genre description. Thanks to mmgp you can run it with less than 10 GB of VRAM without waiting forever.
65
+
66
+ ## Installation
67
+ First you need to install the module in your current project with:
68
+ ```shell
69
+ pip install mmgp
70
+ ```
71
+
72
+
73
+ ## Usage
74
+
75
+ It is almost plug and play and just needs to be invoked from the main app just after the model pipeline has been created.
76
+ 1) First make sure that the pipeline explictly loads the models in the CPU device, for instance:
77
+ ```
78
+ pipe = FluxPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=torch.bfloat16).to("cpu")
79
+ ```
80
+
81
+ 2) Once every potential Lora has been loaded and merged, add the following lines for a quick setup:
82
+ ```
83
+ from mmgp import offload, profile_type
84
+ offload.profile(pipe, profile_type.HighRAM_LowVRAM_Fast)
85
+ ```
86
+
87
+ You can choose between 5 profiles depending on your hardware:
88
+ - HighRAM_HighVRAM (1): at least 48 GB of RAM and 24 GB of VRAM : the fastest well suited for a RTX 3090 / RTX 4090 but consumes much more VRAM, adapted for fast shorter video or small batches of pictures
89
+ - HighRAM_LowVRAM (2): at least 48 GB of RAM and 12 GB of VRAM : a bit slower, better suited for RTX 3070/3080/4070/4080 or for RTX 3090 / RTX 4090 with large pictures batches or long videos
90
+ - LowRAM_HighVRAM (3): at least 32 GB of RAM and 24 GB of VRAM : adapted for RTX 3090 / RTX 4090 with limited RAM but at the cost of VRAM (shorter videos / fewer images)
91
+ - LowRAM_LowVRAM (4): at least 32 GB of RAM and 12 GB of VRAM : if you have little VRAM or want to generate longer videos / more images
92
+ - VerylowRAM_LowVRAM (5): at least 24 GB of RAM and 10 GB of VRAM : if you don't have much it won't be fast but maybe it will work
93
+
94
+ Profile 2 (High RAM) and 4 (Low RAM) are the most recommended profiles since they are versatile (support for long videos for a slight performance cost).\
95
+ If you use Flux derived applciation profile 1 and 3 will offer much faster generation times.
96
+ In any case, a safe approach is to start from profile 5 (default profile) and then go down progressively to profile 4 and then to profile 2 as long as the app remains responsive or doesn't trigger any out of memory error.
97
+
98
+ By default the model named 'transformer' will be quantized to 8 bits for all profiles. If you don't want that you may specify the optional parameter *quantizeTransformer = False*.
99
+
100
+ Every parameter set automatically by a profile can be overridden with one or multiple parameters accepted by *offload.all* (see below):
101
+ ```
102
+ from mmgp import offload, profile_type
103
+ offload.profile(pipe, profile_type.HighRAM_LowVRAM, budgets = 1000)
104
+ ```
105
+ If you want to know which parameter are set by one specific profile you can use the parameter *verboseLevel=2*
106
+
107
+ **It is highly recommended to put the *from mmgp import offload, profile_type* at the top of your main python file (that is as the first import) so that all the existing safetensors calls are redirected to mmpg.**
108
+
109
+
110
+ ## Alternatively you may want to create your own profile with specific parameters:
111
+
112
+ For example:
113
+ ```
114
+ from mmgp import offload
115
+ offload.all(pipe, pinnedMemory=True, ExtraModelsToQuantize = ["text_encoder_2"] )
116
+ ```
117
+ - pinnedMemory: Boolean (for all models) or List of models ids to pin to RAM. Every model pinned to RAM will load much faster (up to 2 times) but this requires more RAM
118
+ - quantizeTransformer: boolean by default True. The 'transformer' model in the pipe contains usually the video or image generator is by defaut; quantized on the fly by default to 8 bits. If you want to save time on disk and reduce the loading time, you may want to load directly a prequantized model. If you don't want to quantize the image generator, you need to set the option *quantizeTransformer* to *False* to turn off on the fly quantization.
119
+ - extraModelsToQuantize: list of additional modelids of models to quantize on the fly. If the corresponding model is already quantized, this option will be ignored.
120
+ - budgets: either a number in mega bytes, (for all models, if 0 unlimited budget) a string that is perecentage of the total VRAM or a dictionary that maps model ids to mega bytes : define the approximate budget in mega bytes that is allocated in VRAM for a model. Try not to allocate all the available VRAM so that the rest can be used to process the data. To define the default value in the dictionary, you may add entry named "*".
121
+ The smaller this number, the more VRAM left for image data / longer video but also the slower because there will be lots of loading / unloading between the RAM and the VRAM. If model is too big to fit in a budget, it will be broken down in multiples parts that will be unloaded / loaded consequently. The speed of low budget can be increased (up to 2 times) by turning on the options pinnedMemory and asyncTransfers.
122
+ - workingVRAM: either a number in mega bytes, a string that is perecentage of the total VRAM or a dictionary that maps a model ids to a number in mega bytes that corresponds to a minimum amount of VRAM that should be left for the data processed by the model. This number will prevail if it is in conflict with a too high budget defined for the same model.
123
+ - asyncTransfers: boolean, load to the GPU the next model part while the current part is being processed. This requires twice the budget if any is defined. This may increase speed by 20% (mostly visible on fast modern GPUs).
124
+ - verboseLevel: number between 0 and 2 (1 by default), provides various level of feedback of the different processes
125
+ - compile: list of model ids to compile, may accelerate up x2 depending on the type of GPU. It makes sense to compile only the model that is frequently used such as the "transformer" model in the case of video or image generation. Compilation requires Triton to be installed. Triton is available out of the box on Linux or WSL but requires to be installed with Windows: https://github.com/woct0rdho/triton-windows
126
+ - coTenantsMap: a dictionary that maps a model id to a list of other models with which it accepts to share the VRAM at the same time. This is useful to avoid unefficient loading / unloading when two models processes are interleaved. For instance *coTenantsMap = { "text_encoder_2": ["text_encoder"] }* , here when *text_encoder_2* is loaded it won't unload *text_encoder*. Please note that the reverse is not true as these maps by design are not symetrical to allow tailored workflows. If you need to have as well *text_encoder* that won't unload *text_encoder_2* if it is already loaded *coTenantsMap = { "text_encoder_2": ["text_encoder"], "text_encoder": ["text_encoder_2"] }*
127
+
128
+ If you are short on RAM and plan to work with quantized models, it is recommended to load pre-quantized models direclty rather than using on the fly quantization, it will be faster and consume slightly less RAM.
129
+
130
+ ## Going further
131
+
132
+ The module includes several tools to package a light version of your favorite video / image generator:
133
+ - *extract_models(string prefix, obj to explore)*\
134
+ This tool will try to detect for you models that are embedded in a pipeline or in some custom class. It will save you time by building a pipe dictionary required by *offload.all* or "offload.profile*. The prefix correponds to the text that will appear before the name of each model in the dictionary.
135
+
136
+ - *load_loras_into_model(model, lora_path, lora_multi, activate_all_loras = True)*\
137
+ Load in a model a list of Lora described by a list of path *lora_path* and a list of *weights coefficients*.
138
+ The Lora file must be in the *diffusers* format. This function works also on non diffusers models. However if there is already an official Lora support for a model it is recommended to use the official diffusers functions. By default all the load loras will be activated or they can be activated later using *activate_loras*.
139
+
140
+ -*activate_loras(model, lora_nos, lora_multi = None )*\
141
+ Activate the loras whose nos are in the list of nos. Every lora that is not this list and that was activated previously will be disactivated.
142
+
143
+ - *save_model(model, file_path, do_quantize = False, quantizationType = qint8 )*\
144
+ Save tensors of a model already loaded in memory in a safetensor format (much faster to reload). You can save it in a quantized format (default qint8 quantization recommended).
145
+ The resulting safetensor file will contain extra fields in its metadata such as the quantization map and its configuration, so you will be able to move the file around without files such as *config.json* or *file_map.json*.
146
+ You will need *load_model_data* or *fast_load_transformers_model* to read the file again . You may also load it using the default *safetensor* librar however you will need to provide in the same directory any complementary file that are usually requested (for instance *config.json*)
147
+
148
+ - *load_model_data(model, file_path: str, do_quantize = False, quantizationType = qint8, pinToRAM = False, partialPin = False)*\
149
+ Load the tensors data of a model in RAM of a model already initialized with no data. Detect and handle quantized models saved previously with *save_model*.A model can also be quantized on the fly while being loaded. The model which is loaded can be pinned to RAM while it is loaded, this is more RAM efficient than pinning tensors later using *offline.all* or *offline.profile*
150
+
151
+ - *fast_load_transformers_model(model_path: str, do_quantize = False, quantizationType = qint8, pinToRAM = False, partialPin = False)*\
152
+ Initialize (build the model hierarchy in memory) and fast load the corresponding tensors of a 'transformers' or 'diffusers' library model.
153
+ The advantages over the original *from_pretrained* method is that a full model can fit into a single file with a filename of your choosing (thefore you can have multiple 'transformers' versions of the same model in the same directory) and prequantized models are processed in a transparent way.
154
+ Last but not least, you can also on the fly pin to RAM the whole model or the most important part of it (partialPin = True) in a more efficient way (faster and requires less RAM) than if you did through *offload.all* or *offload.profile*.
155
+
156
+
157
+ The typical workflow wil be:
158
+ 1) temporarly insert the *save_model* function just after a model has been fully loaded to save a copy of the model / quantized model.
159
+ 2) replace the full initalizing / loading logic with *fast_load_transformers_model* (if there is a *from_pretrained* call to a transformers object) or only the tensor loading functions (*torch.load_model_file* and *torch.load_state_dict*) with *load_model_data after* the initializing logic.
160
+
161
+ ## Special cases
162
+ Sometime there isn't an explicit pipe object as each submodel is loaded separately in the main app. If this is the case, you may try to use *extract_models* or create a dictionary that manually maps all the models.\
163
+ For instance :
164
+
165
+
166
+ - for flux derived models:
167
+ ```
168
+ pipe = { "text_encoder": clip, "text_encoder_2": t5, "transformer": model, "vae":ae }
169
+ ```
170
+ - for mochi:
171
+ ```
172
+ pipe = { "text_encoder": self.text_encoder, "transformer": self.dit, "vae":self.decoder }
173
+ ```
174
+
175
+
176
+ Please note it is recommended to have always one model whose Id is 'transformer' so that you can leverage predefined profiles. The 'transformer' corresponds to the main image / video model which usually needs to be quantized (this is done on the fly by default when loading the model).
177
+
178
+ Be careful, lots of models use the T5 XXL as a text encoder. However, quite often their corresponding pipeline configurations point at the official Google T5 XXL repository
179
+ where there is a huge 40GB model to download and load. It is cumbersorme as it is a 32 bits model and contains the decoder part of T5 that is not used.
180
+ I suggest you use instead one of the 16 bits encoder only version available around, for instance:
181
+ ```
182
+ text_encoder_2 = T5EncoderModel.from_pretrained("black-forest-labs/FLUX.1-dev", subfolder="text_encoder_2", torch_dtype=torch.float16)
183
+ ```
184
+
185
+ Sometime just providing the pipe won't be sufficient as you will need to change the content of the core model:
186
+ - For instance you may need to disable an existing CPU offload logic that already exists (such as manual calls to move tensors between cuda and the cpu)
187
+ - mmpg to tries to fake the device as being "cuda" but sometimes some code won't be fooled and it will create tensors in the cpu device and this may cause some issues.
188
+
189
+ You are free to use my module for non commercial use as long you give me proper credits. You may contact me on twitter @deepbeepmeep
190
+
191
+ Thanks to
192
+ ---------
193
+ - Huggingface / accelerate for the hooking examples
194
+ - Huggingface / quanto for their very useful quantizer
195
+ - gau-nernst for his Pinnig RAM samples
@@ -0,0 +1,14 @@
1
+ __init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ mmgp/__init__.py,sha256=A9qBwyQMd1M7vshSTOBnFGP1MQvS2hXmTcTCMUcmyzE,509
3
+ mmgp/fp8_quanto_bridge.py,sha256=vTKnzWKe88MgMl2z0gzcu7EGTVHM7c2Wg95dmqMU3vM,26356
4
+ mmgp/fp8_quanto_bridge_old.py,sha256=VtUaD6wzo7Yn9vGY0LMtbhwt6KMWRpSWLc65bU_sfZU,21155
5
+ mmgp/offload.py,sha256=emZtKU6Fq_3d39EIplkJ6c4pd5PNIPwLqsD74qOixkk,160548
6
+ mmgp/quant_router.py,sha256=WGh9C0eRV4EPC1eAEbUjrfxwfieqC6lgMnGNYdeUEjg,16515
7
+ mmgp/quanto_int8_cuda.py,sha256=LD5pTtM-bNgKseI1wnHp8JxeUl2Q4uSL8TbFYT1Jg5s,3258
8
+ mmgp/quanto_int8_inject.py,sha256=MyegEMZvUmc2iOmaRC7_zTlbvqlfKaN0xDoOGTBkAzY,11038
9
+ mmgp/safetensors2.py,sha256=vPaM8rGjrJosj_5WAYe9Xgr2_oGKmeB-8bLOANhA2aQ,19935
10
+ mmgp-3.6.11.dist-info/licenses/LICENSE.md,sha256=HjzvY2grdtdduZclbZ46B2M-XpT4MDCxFub5ZwTWq2g,93
11
+ mmgp-3.6.11.dist-info/METADATA,sha256=wwOcRwAyxkjiXvhWvgpdVGBWp7QcLJQQ7Zl_eMzCyig,16311
12
+ mmgp-3.6.11.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
13
+ mmgp-3.6.11.dist-info/top_level.txt,sha256=waGaepj2qVfnS2yAOkaMu4r9mJaVjGbEi6AwOUogU_U,14
14
+ mmgp-3.6.11.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (77.0.3)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,9 +0,0 @@
1
- __init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- mmgp/__init__.py,sha256=A9qBwyQMd1M7vshSTOBnFGP1MQvS2hXmTcTCMUcmyzE,509
3
- mmgp/offload.py,sha256=hG-gMFeHsRkjaPan_lwiTsQOctkXylJMiWhyL3KvGQA,106337
4
- mmgp/safetensors2.py,sha256=DCdlRH3769CTyraAmWAB3b0XrVua7z6ygQ-OyKgJN6A,16453
5
- mmgp-3.3.1.dist-info/licenses/LICENSE.md,sha256=HjzvY2grdtdduZclbZ46B2M-XpT4MDCxFub5ZwTWq2g,93
6
- mmgp-3.3.1.dist-info/METADATA,sha256=SF0kLwi8zGHF1F53ZxFDZq5bDCWE39l-A24tYeyyhHo,16153
7
- mmgp-3.3.1.dist-info/WHEEL,sha256=1tXe9gY0PYatrMPMDd6jXqjfpz_B-Wqm32CPfRC58XU,91
8
- mmgp-3.3.1.dist-info/top_level.txt,sha256=waGaepj2qVfnS2yAOkaMu4r9mJaVjGbEi6AwOUogU_U,14
9
- mmgp-3.3.1.dist-info/RECORD,,