|
--- |
|
language: |
|
- bn |
|
license: apache-2.0 |
|
tags: |
|
- text-generation-inference |
|
- transformers |
|
- unsloth |
|
- llama |
|
- trl |
|
base_model: unsloth/gemma-2-9b-it-bnb-4bit |
|
inference: false |
|
--- |
|
|
|
# Gemma-2 Bangla 4 bit |
|
|
|
<div align="center"> |
|
<img src="https://cdn-uploads.huggingface.co/production/uploads/65ca6f0098a46a56261ac3ac/k4MH-skEIgS_zHXyAxtXv.jpeg" width="300"/> |
|
|
|
</div> |
|
|
|
- **Developed by:** KillerShoaib |
|
- **License:** apache-2.0 |
|
- **Finetuned from model :** unsloth/gemma-2-9b-it-bnb-4bit |
|
- **Datset used for fine-tuning :** iamshnoo/alpaca-cleaned-bengali |
|
|
|
|
|
# 4-bit Quantization |
|
**This is 4-bit quantization of Gemma-2 9b model.** |
|
|
|
|
|
# Gemma-2 Bangla Different Formats |
|
|
|
- `LoRA Adapters only` - [**KillerShoaib/gemma-2-9b-bangla-lora**](https://huggingface.co/KillerShoaib/gemma-2-9b-bangla-lora) |
|
- `16-bit model` - [**KillerShoaib/gemma-2-9b-bangla-16bit**](https://huggingface.co/KillerShoaib/gemma-2-9b-bangla-16bit) |
|
|
|
# Model Details |
|
|
|
**Gemma 2 9 billion** model was finetuned using **unsloth** package on a **cleaned Bangla alpaca** dataset. The model was finetune using **LoRA** finetuning technique. The model is finetuned for **1 epoch** on a single T4 GPU. |
|
|
|
|
|
# Pros & Cons of the Model |
|
|
|
## Pros |
|
|
|
- **The model can comprehend the Bangla language, including its semantic nuances** |
|
- **Given context model can answer the question based on the context** |
|
|
|
## Cons |
|
- **Model is unable to do creative or complex work. i.e: creating a poem or solving a math problem in Bangla** |
|
- **Since the size of the dataset was small, the model lacks lot of general knowledge in Bangla** |
|
|
|
## Llama 3 8b Bangla Vs Gemma 2 9b Bangla |
|
- **Overall both performace were similar, the pros and cons both were same, both the model struggle with longer context query and reasoning, but able to answer question if the context is given** |
|
|
|
# Some Examples from the model: |
|
|
|
- **Example 1:** |
|
```python |
|
Question: স্সুস্থ থাকার তিনটি উপায় বলুন |
|
Model Generated Answer: ১. স্বাস্থ্যকর খাদ্য গ্রহণ করুনঃ ফল, সবজি, পূর্ণ শস্য, চর্বিযুক্ত প্রোটিন এবং স্বাস্থ্যকর ফ্যাট সমৃদ্ধ একটি ভারসাম্যপূর্ণ খাদ্য গ্রহণ করা স্বাস্থ্যকর থাকার একটি গুরুত্বপূর্ণ উপায়। এটি আপনার শরীরকে প্রয়োজনীয় পুষ্টি সরবরাহ করে এবং আপনাকে সুস্থ থাকতে সহায়তা করে। ২. নিয়মিত ব্যায়াম করুনঃ নিয়মিত শারীরিক ক্রিয়াকলাপ আপনার সামগ্রিক স্বাস্থ্যের উন্নতি করতে পারে, আপনার হৃদরোগের স্বাস্থ্য উন্নত করতে পারে, আপনার মানসিক স্বাস্থ্য উন্নত করতে পারে এবং আপনাকে সুস্থ থাকতে সহায়তা করতে পারে। ৩. পর্যাপ্ত ঘুম পানঃ পর্যাপ্ত ঘুম আপনার শরীরকে পুনরুদ্ধার করতে এবং আপনার সামগ্রিক স্বাস্থ্যের উন্নতি করতে সহায়তা করে। প্রতি রাতে ৭-৮ ঘন্টা ঘুম পাওয়া সুস্থ থাকার জন্য গুরুত্বপূর্ণ |
|
``` |
|
|
|
- **Example 2:** |
|
```python |
|
Question: নিচের আলোচনা দেখে বাংলাদেশের রাজধানীর নাম বের করো। |
|
Context: বাংলাদেশের রাজধানী ঢাকায় তার নামে একটি এলাকার নামকরন করা হয়েছে শেরেবাংলা নগর (পূর্ববর্তী আইয়ুবনগর ও তারও পূর্বে মনিপুর) ,২০১৪ বাংলাদেশ স্থানীয় সময় শুক্রবার বেলা তিনটায় রাজধানীর ইউনাইটেড হাসপাতালে চিকিৎসাধীন অবস্থায় মারা যান বেবী মওদুদ। মৃত্যুর সময় তার বয়স হয়েছিল ৬৬ বছর।বাংলাদেশ জাতীয় জাদুঘর বাংলাদেশের রাজধানী ঢাকা শহরে অবস্থিত দেশের প্রধান জাদুঘর। এটি ২০, মার্চ, ১৯১৩ খ্রিস্টাব্দে প্রতিষ্ঠিত হয়, এবং ৭ আগস্ট। |
|
Model Generated Answer: বাংলাদেশের রাজধানী ঢাকা। |
|
``` |
|
- **Example 3:** |
|
```python |
|
Question: বিশ্বের সবচেয়ে বিখ্যাত চিত্রশিল্পী কে? |
|
Model Generated Answer: বিশ্বের সবচেয়ে বিখ্যাত চিত্রশিল্পী হিসেবে লেওনার্দো দা ভিঞ্চি সবচেয়ে বেশি পরিচিত। তিনি রেনেসাঁ যুগের একজন বিশিষ্ট চিত্রশিল্পী, উদ্ভাবক, বিজ্ঞানী এবং প্রকৌশলী ছিলেন। তাঁর সবচেয়ে বিখ্যাত কাজগুলির মধ্যে মোনালিসা এবং দ্য লাস্ট সাপার অন্তর্ভুক্ত রয়েছে। |
|
``` |
|
|
|
|
|
# Run The Model |
|
|
|
## Download the right dependency for Unsloth |
|
|
|
**Google Colab** |
|
|
|
```python |
|
%%capture |
|
# Installs Unsloth, Xformers (Flash Attention) and all other packages! |
|
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git" |
|
|
|
# We have to check which Torch version for Xformers (2.3 -> 0.0.27) |
|
from torch import __version__; from packaging.version import Version as V |
|
xformers = "xformers==0.0.27" if V(__version__) < V("2.4.0") else "xformers" |
|
!pip install --no-deps {xformers} trl peft accelerate bitsandbytes triton |
|
``` |
|
|
|
## FastLanguageModel from unsloth for 2x faster inference |
|
|
|
```python |
|
|
|
from unsloth import FastLanguageModel |
|
model, tokenizer = FastLanguageModel.from_pretrained( |
|
model_name = "KillerShoaib/gemma-2-9b-bangla-4bit", # or KillerShoaib/gemma-2-9b-bangla-4bit / KillerShoaib/gemma-2-9b-bangla-16bit |
|
max_seq_length = 2048, |
|
dtype = None, |
|
load_in_4bit = True, |
|
) |
|
FastLanguageModel.for_inference(model) |
|
|
|
# alpaca_prompt for the model |
|
alpaca_prompt = """Below is an instruction in bangla that describes a task, paired with an input also in bangla that provides further context. Write a response in bangla that appropriately completes the request. |
|
|
|
### Instruction: |
|
{} |
|
|
|
### Input: |
|
{} |
|
|
|
### Response: |
|
{}""" |
|
|
|
# input with instruction and input |
|
inputs = tokenizer( |
|
[ |
|
alpaca_prompt.format( |
|
"সুস্থ থাকার তিনটি উপায় বলুন", # instruction |
|
"", # input |
|
"", # output - leave this blank for generation! |
|
) |
|
], return_tensors = "pt").to("cuda") |
|
|
|
# generating the output and decoding it |
|
outputs = model.generate(**inputs, max_new_tokens = 2048, use_cache = True) |
|
tokenizer.batch_decode(outputs) |
|
``` |
|
|
|
## AutoModelForCausalLM from Hugginface |
|
|
|
```python |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
|
model_name = "KillerShoaib/gemma-2-9b-bangla-4bit" # or KillerShoaib/gemma-2-9b-bangla-16bit |
|
tokenizer_name = model_name |
|
|
|
# Load tokenizer |
|
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name) |
|
# Load model |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
# Text prompt to start generation |
|
alpaca_prompt = """Below is an instruction in bangla that describes a task, paired with an input also in bangla that provides further context. Write a response in bangla that appropriately completes the request. |
|
|
|
### Instruction: |
|
{} |
|
|
|
### Input: |
|
{} |
|
|
|
### Response: |
|
{}""" |
|
|
|
# Encode the prompt text |
|
inputs = tokenizer( |
|
[ |
|
alpaca_prompt.format( |
|
"বিশ্বের সবচেয়ে বিখ্যাত চিত্রশিল্পী কে?", # instruction |
|
"", # input |
|
"", # output - leave this blank for generation! |
|
) |
|
], return_tensors = "pt").to("cuda") |
|
|
|
# output |
|
outputs = model.generate(**inputs, max_new_tokens = 1024, use_cache = True) |
|
|
|
tokenizer.batch_decode(outputs) |
|
``` |
|
|
|
|
|
# Inference Script & Github Repo |
|
|
|
- `Google Colab` - [**Gemma-2 9b Bangla Inference Script**](https://colab.research.google.com/drive/13urCM6hQ2zE401uwu4laS9czi0K6qFUz?usp=sharing) |
|
- `Github Repo` - [**Llama-3 Bangla**](https://github.com/KillerShoaib/Llama-3-Bangla) |
|
|
|
**‼️ Github Repo** shows how to finetune any unsloth model using incremental training. For **Gemma 2 9b** model finetuning I've used the exact **same logic** that was used for **Llama 3 8b** model. **Remember to change the dependency based on the Unsloth notebook example. ‼️** |