Buckets:

mishig's picture
|
download
raw
8.98 kB
# Pruna
[Pruna](https://github.com/PrunaAI/pruna) هو إطار عمل لتحسين النماذج يقدم طرق تحسين متنوعة - مثل التكميم، والتقليم، والتخزين المؤقت، والتجميع - لتسريع الاستدلال وتقليل استهلاك الذاكرة. تُعرض نظرة عامة على طرق التحسين أدناه.
| تقنية | الوصف | السرعة | الذاكرة | الجودة |
|--------------|-----------------------------------------------------------------------------------------------|:-----:|:------:|:-------:|
| `batcher` | تجمع المدخلات المتعددة معًا لمعالجتها في وقت واحد، مما يحسّن الكفاءة الحسابية ويقلل من وقت المعالجة. | ✅ | ❌ | ➖ |
| `cacher` | يخزّن النتائج الوسيطة للعمليات الحسابية لتسريع العمليات اللاحقة. | ✅ | ➖ | ➖ |
| `compiler` | يُحسّن النموذج باستخدام تعليمات مخصصة للأجهزة المحددة. | ✅ | ➖ | ➖ |
| `distiller` | يُدرّب نموذجًا أصغر وأبسط لمحاكاة نموذج أكبر وأكثر تعقيدًا. | ✅ | ✅ | ❌ |
| `quantizer` | يقلل من دقة الأوزان والتفعيلات، مما يخفض متطلبات الذاكرة. | ✅ | ✅ | ❌ |
| `pruner` | يزيل الاتصالات والعصبونات الأقل أهمية أو المتكررة، مما يؤدي إلى شبكة أكثر ندرة وكفاءة. | ✅ | ✅ | ❌ |
| `recoverer` | يعيد أداء النموذج بعد الضغط. | ➖ | ➖ | ✅ |
| `factorizer` | يقوم Factorization بدمج عدة عمليات ضرب مصفوفات صغيرة في عملية واحدة كبيرة مدمجة. | ✅ | ➖ | ➖ |
| `enhancer` | يعزز مخرجات النموذج من خلال تطبيق خوارزميات ما بعد المعالجة مثل إزالة الضوضاء أو رفع الدقة. | ❌ | - | ✅ |
✅ (يحسّن)، ➖ (نفس الشيء تقريبًا)، ❌ (يُضعف)
استكشف مجموعة طرق التحسين الكاملة في [توثيق Pruna](https://docs.pruna.ai/en/stable/docs_pruna/user_manual/configure.html#configure-algorithms).
## التثبيت
قم بتثبيت Pruna باستخدام الأمر التالي.
```bash
pip install pruna
```
## تحسين نماذج Diffusers
تدعم نماذج Diffusers مجموعة واسعة من خوارزميات التحسين كما هو موضح أدناه.
يُحسّن المثال أدناه [black-forest-labs/FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) باستخدام مزيج من خوارزميات factorizer وcompiler وcacher. يُسرّع هذا المزيج الاستدلال حتى 4.2 أضعاف ويقلل ذروة استخدام ذاكرة GPU من 34.7GB إلى 28.0GB، مع الحفاظ على جودة المخرجات شبه المتطابقة.
> [!TIP]
> راجع وثائق [تحسين Pruna](https://docs.pruna.ai/en/stable/docs_pruna/user_manual/configure.html) لمعرفة المزيد حول تقنيات التحسين المستخدمة في هذا المثال.
ابدأ بتعريف `SmashConfig` بخوارزميات التحسين المراد استخدامها. لتحسين النموذج، قم بتغليف خط الأنابيب و`SmashConfig` باستخدام `smash` ثم استخدم خط الأنابيب بشكل طبيعي للاستدلال.
```python
import torch
from diffusers import FluxPipeline
from pruna import PrunaModel, SmashConfig, smash
# load the model
# Try segmind/Segmind-Vega or black-forest-labs/FLUX.1-schnell with a small GPU memory
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
dtype=torch.bfloat16
).to("cuda") # or "mps", "xpu", "cpu"
# define the configuration
smash_config = SmashConfig()
smash_config["factorizer"] = "qkv_diffusers"
smash_config["compiler"] = "torch_compile"
smash_config["torch_compile_target"] = "module_list"
smash_config["cacher"] = "fora"
smash_config["fora_interval"] = 2
# for the best results in terms of speed you can add these configs
# however they will increase your warmup time from 1.5 min to 10 min
# smash_config["torch_compile_mode"] = "max-autotune-no-cudagraphs"
# smash_config["quantizer"] = "torchao"
# smash_config["torchao_quant_type"] = "fp8dq"
# smash_config["torchao_excluded_modules"] = "norm+embedding"
# optimize the model
smashed_pipe = smash(pipe, smash_config)
# run the model
smashed_pipe("a knitted purple prune").images[0]
```
بعد التحسين، يمكننا مشاركة النموذج المحسّن وتحميله باستخدام Hugging Face Hub.
```python
# save the model
smashed_pipe.save_to_hub("<username>/FLUX.1-dev-smashed")
# load the model
smashed_pipe = PrunaModel.from_hub("<username>/FLUX.1-dev-smashed")
```
## تقييم نماذج Diffusers ومعايرتها
توفّر Pruna الوكيل [EvaluationAgent](https://docs.pruna.ai/en/stable/docs_pruna/user_manual/evaluate.html) لتقييم جودة نماذجك المُحسَّنة.
يمكننا تحديد المقاييس التي نهتم بها، مثل الوقت الكلي ومعدل الإنتاجية، بالإضافة إلى مجموعة البيانات التي سنقيّم عليها. كما يمكننا تعريف نموذج ومروره إلى `EvaluationAgent`.
يمكننا تحميل نموذج محسّن وتقييمه باستخدام `EvaluationAgent` ثم تمريره إلى `Task`.
```python
import torch
from diffusers import FluxPipeline
from pruna import PrunaModel
from pruna.data.pruna_datamodule import PrunaDataModule
from pruna.evaluation.evaluation_agent import EvaluationAgent
from pruna.evaluation.metrics import (
ThroughputMetric,
TorchMetricWrapper,
TotalTimeMetric,
)
from pruna.evaluation.task import Task
# define the device
device = "cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu"
# load the model
# Try PrunaAI/Segmind-Vega-smashed or PrunaAI/FLUX.1-dev-smashed with a small GPU memory
smashed_pipe = PrunaModel.from_hub("PrunaAI/FLUX.1-dev-smashed")
# Define the metrics
metrics = [
TotalTimeMetric(n_iterations=20, n_warmup_iterations=5),
ThroughputMetric(n_iterations=20, n_warmup_iterations=5),
TorchMetricWrapper("clip"),
]
# Define the datamodule
datamodule = PrunaDataModule.from_string("LAION256")
datamodule.limit_datasets(10)
# Define the task and evaluation agent
task = Task(metrics, datamodule=datamodule, device=device)
eval_agent = EvaluationAgent(task)
# Evaluate smashed model and offload it to CPU
smashed_pipe.move_to_device(device)
smashed_pipe_results = eval_agent.evaluate(smashed_pipe)
smashed_pipe.move_to_device("cpu")
```
بدلاً من مقارنة النموذج المُحسَّن بالنموذج الأساسي، يمكنك أيضًا تقييم نموذج `diffusers` المستقل. وهذا مفيد إذا كنت ترغب في تقييم أداء النموذج دون التحسين. يمكننا القيام بذلك باستخدام غلاف `PrunaModel` وتشغيل `EvaluationAgent` عليه.
```python
import torch
from diffusers import FluxPipeline
from pruna import PrunaModel
# load the model
# Try PrunaAI/Segmind-Vega-smashed or PrunaAI/FLUX.1-dev-smashed with a small GPU memory
pipe = FluxPipeline.from_pretrained(
"black-forest-labs/FLUX.1-dev",
dtype=torch.bfloat16
).to("cpu")
wrapped_pipe = PrunaModel(model=pipe)
```
الآن وقد اطلعت على كيفية تحسين نماذجك وتقييمها، يمكنك البدء في استخدام Pruna لتحسين نماذجك الخاصة. لحسن الحظ، لدينا العديد من الأمثلة لمساعدتك على البدء.
> [!TIP]
> لمزيد من التفاصيل حول إجراء اختبارات الأداء على Flux، اطّلع على منشور المدونة [إعلان FLUX-Juiced: أسرع نقطة نهاية لتوليد الصور (أسرع بـ 2.6 مرة)!](https://huggingface.co/blog/PrunaAI/flux-fastest-image-generation-endpoint) ومساحة [InferBench](https://huggingface.co/spaces/PrunaAI/InferBench).
## مرجع
- [Pruna](https://github.com/PrunaAI/pruna)
- تحسين [Pruna](https://docs.pruna.ai/en/stable/docs_pruna/user_manual/configure.html#configure-algorithms)
- [تقييم Pruna](https://docs.pruna.ai/en/stable/docs_pruna/user_manual/evaluate.html)
- [دروس Pruna](https://docs.pruna.ai/en/stable/docs_pruna/tutorials/index.html)

Xet Storage Details

Size:
8.98 kB
·
Xet hash:
13dac85639e9d17c8c9bde837e8e57aa8293597c41a06536ae597c3467f10550

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.