<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by AHD Co on Medium]]></title>
        <description><![CDATA[Stories by AHD Co on Medium]]></description>
        <link>https://medium.com/@info.ahdsoft?source=rss-3e77b9ef6700------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IjBPs_2gzKrRUZ5I6MBHUg.png</url>
            <title>Stories by AHD Co on Medium</title>
            <link>https://medium.com/@info.ahdsoft?source=rss-3e77b9ef6700------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 07:59:17 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@info.ahdsoft/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Fine-Tuning a Large Language Model for Persian: Our Company’s Journey]]></title>
            <link>https://medium.com/@info.ahdsoft/fine-tuning-a-large-language-model-for-persian-our-companys-journey-20aedcc9c13f?source=rss-3e77b9ef6700------2</link>
            <guid isPermaLink="false">https://medium.com/p/20aedcc9c13f</guid>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[pesian-language-model]]></category>
            <dc:creator><![CDATA[AHD Co]]></dc:creator>
            <pubDate>Thu, 09 Nov 2023 09:26:34 GMT</pubDate>
            <atom:updated>2023-11-12T11:29:59.689Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Fine-Tuning a Large Language Model for Persian" src="https://cdn-images-1.medium.com/max/936/1*l6-gTNNIjoVTiCdfk29rHQ.png" /></figure><h3><strong>Introduction</strong></h3><p>Large Language Models (LLMs) have revolutionized the field of natural language processing, enabling a wide range of applications such as chatbots, language translation, text generation, and more. These models have the ability to understand and generate human-like text, making them incredibly valuable in various domains. However, we have identified a critical gap in the availability of open-source LLMs that perform well on the Persian language. Persian, also known as Farsi, is a widely spoken language with a rich literary history, and it deserves advanced language processing capabilities. To address this gap, our company has undertaken the ambitious task of **fine-tuning LLMs specifically for the Persian language**. In our study, we made efforts to train the first version of the model as quickly as possible. Although the results may not be exceptional, they demonstrate the significantly positive impact of <strong>Fine-Tuning a Large Language Model</strong> and provide promising prospects for the future.</p><h3>LLM Training Pipeline</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*cU0espNGil6_UUUzIvX17A.png" /></figure><p>(ref:<strong> [</strong><a href="https://www.borealisai.com/research-blogs/a-high-level-overview-of-large-language-models/"><strong>borealisai]</strong></a><strong>)</strong></p><p>Training a powerful LLM involves a multi-step process that comprises three main stages: pre-training, <strong>fine-tuning</strong>, and alignment. Pre-training involves exposing the model to massive amounts of data, allowing it to learn the statistical relationships and patterns within the language. Since pre-training a large language model requires significant computational resources, we have decided to focus on <strong>fine-tuning an open-source LLM</strong> that has already been pre-trained. This approach allows us to leverage the knowledge the model has gained from its pre-training and adapt it to our specific use case. In addition, While aligning the LLM’s behavior with human preferences is an important step, we have decided to postpone this phase to a future stage of the project. This decision allows us to focus on fine-tuning the model for our specific task and ensures that we have a solid foundation to build upon before addressing the alignment challenge.</p><h3>Open Source Models and Functionality in Farsi Language</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*eqvO20bL5TMt75e-azGs6w.png" /></figure><p>In our search for the best open-source models for <strong>fine-tuning</strong> in the Persian language, we explored various benchmarks and leaderboards to identify top-performing models. We extensively relied on reputable platforms such as<strong> </strong><a href="https://lmsys.org/blog/2023-05-03-arena/"><strong>chatbot Arena</strong></a>, <a href="https://openrouter.ai/"><strong>openrouter</strong></a>, and <a href="https://tatsu-lab.github.io/alpaca_eval/"><strong>AlpacaEval</strong></a><strong> </strong><a href="https://huggingface.co/spaces/HuggingFaceH4/open_llm_leaderboard"><strong>Leaderboard</strong></a>. These platforms provide valuable insights into the performance and capabilities of different language models.</p><p>Additionally, we actively follow the daily trends and developments on Hugging Face, a renowned platform for natural language processing models. By staying updated with the latest advancements and contributions from the Hugging Face community, we ensure that our selection process is informed by the most recent advancements in the field.</p><p>After narrowing down our list to several potential models, we conducted thorough evaluations of their tokenizers and performance specifically in the Persian language. Tokenization plays a crucial role in processing text, and considering the unique characteristics of the Persian language, it was essential to assess how well each model’s tokenizer handled Persian tokens.</p><p>Among the models we considered, the qwen-7B model stood out due to its more suitable tokenizer for Persian tokens and its significant computational power compared to other models with the same number of parameters. This combination of an effective tokenizer and impressive performance made the qwen-7B model our preferred choice for <strong>fine-tuning</strong> in the Persian language.</p><h3>Fine-tuning Datasets</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*xNvQjx7cwR0wBdxdmk2xrw.png" /></figure><p>In this article, we will explore different methods of creating LLM finetune data for Farsi, the challenges faced, and the promising solution of data synthesis using powerful LLMs.</p><h4><strong>CrowdSourcing Datasets</strong></h4><p>When considering the creation of fine-tune data for Farsi, we encounter several hurdles. Traditional methods like crowdsourcing can be costly and time-consuming, making them impractical for many organizations. Additionally, existing open-source databases, such as OASST1, primarily consist of English data, rendering them unsuitable for direct use in Farsi language <strong>fine-tuning</strong>.</p><h4><strong>ShareGPT</strong></h4><p>Existing chat Datasets, such as ShareGPT, have emerged as valuable resources for studying conversational data. ShareGPT is a dataset comprising chat logs voluntarily shared by users who interacted with ChatGPT publicly. While ShareGPT provides a rich collection of chat conversations, it primarily consists of English chats and lacks substantial Farsi data, making it less suitable for direct use in Farsi language finetuning.</p><h4><strong>Data Synthesis using LLMs</strong></h4><p>Data synthesis offers an intelligent and automated solution to address the dearth of Farsi finetune data. Leveraging powerful LLMs like ChatGPT and GPT-4, we can generate synthetic data tailored to Farsi. The renowned Alpaca dataset serves as a testament to the effectiveness of this method. The Alpaca dataset was meticulously crafted using an innovative approach. Firstly, a seed consisting of 175 different tasks was prepared. Then, by employing ChatGPT, this seed was strengthened, expanded, and augmented with various new tasks and instructions. Through iterative generation, the dataset grew to encompass approximately 50,000 records.</p><p>While the Alpaca dataset showcases the potential of data synthesis, it is not without its limitations. Quality and diversity are key areas that require attention. Researchers have acknowledged these weaknesses, leading to subsequent efforts such as <strong>WizardLM</strong> and <strong>AlpacaClean</strong>, aimed at refining and enhancing the dataset.</p><p>In addition to these efforts, another dataset called <strong>OpenOrca</strong> has emerged in the field of natural language processing. The OpenOrca dataset is a collection of augmented FLAN Collection data. It currently consists of approximately 1 million GPT-4 completions and 3.2 million GPT-3.5 completions. There is another great dataset named <strong>UltraChat</strong>. UltraChat is an extensive open-source dialogue dataset powered by Turbo APIs. It consists of three sectors: Questions about the World, focused on real-world inquiries; Writing and Creation, aiding in creative tasks; and Assistance on Existent Materials, covering rewriting, summarization, and more. The dataset undergoes post-processing for quality assurance and offers valuable resources for training and evaluating conversational AI systems.</p><p>After careful evaluation of the available datasets, we have made the decision to focus solely on the existing datasets for the initial phase, considering the time constraints. However, we have planned to explore additional datasets and data generation methods in subsequent phases. Currently, we are actively engaged in developing an extensive multi-million dataset specifically tailored for <strong>fine-tuning the Large Language Model</strong> (LLM) on the Farsi language. This dataset holds immense potential for enhancing the performance and capabilities of the model. Stay tuned for further updates on our progress and findings.</p><h4><strong>Bactrian-X</strong></h4><p>(ref: <strong>[</strong><a href="https://huggingface.co/datasets/MBZUAI/Bactrian-X"><strong>huggingface</strong></a><strong>]</strong>)</p><p>To expedite the training process of our initial model, we opted to incorporate the Persian section of the Bactrian-X dataset. Bactrian-X is a comprehensive collection of 3.4 million instruction-response pairs in 52 languages. These pairs were generated by translating 67,000 English instructions into 51 different languages using the Google Translate API. Subsequently, the translated instructions were fed to ChatGPT (gpt-3.5-turbo) to obtain natural responses, resulting in an extensive dataset that covers a wide range of languages, including Persian. By leveraging Bactrian-X, we were able to quickly access a diverse and high-quality dataset to kickstart our <strong>fine-tuning</strong> process.</p><h4><strong>OASST1</strong></h4><p>(ref: [<a href="https://huggingface.co/datasets/pourmand1376/OpenAssistant-oasst1-fa"><strong>hugginface</strong></a><strong>]</strong>)</p><p>To ensure our final model excels in chat-based interactions,we incorporated the OpenAssistant Conversations (OASST1) dataset. OASST1 is a human-generated, human-annotated assistant-style conversation corpus consisting of 161,443 messages in 35 different languages. These conversations were carefully annotated with 461,292 quality ratings, resulting in over 10,000 fully annotated conversation trees. The creation of OASST1 involved a global crowd-sourcing effort that engaged over 13,500 volunteers. By incorporating OASST1, we aimed to enhance the conversational abilities of our Persian language model, enabling it to engage in dynamic and context-rich interactions.</p><h4><strong>WebGLM</strong></h4><p>Context-based question-answering (QA) is a vital task, and we recognized the significance of integrating this capability into our Persian language model. To achieve this, we leveraged WebGLM, a web-enhanced question-answering system. WebGLM generates answers using a large language model (LLM) trained specifically for QA tasks. By incorporating a subset of the WebGLM dataset, we infused our Persian language model with the ability to provide context-based answers, further enriching its capabilities.</p><p>The final dataset is nearly 10,000 records.</p><h3>Fine-tuning</h3><p>We conducted a fine-tuning process on a Persian dataset using two A6000 GPUs. During this process, we had to make several choices to optimize our approach:</p><p><strong>Maximum Length</strong></p><p>We evaluated various models, including the qwen model tokenizer, which showed better coverage of Persian tokens compared to other open source and commercial models like ChatGPT. However, the improvement was not significant enough. We needed a larger maximum length to handle long questions and support chat mode with a lengthy history. Considering the time constraint and the trade-off between length and processing time, we decided to set the maximum length to 1024.</p><p><strong>Full Parameter or Adapter-based Fine-tuning</strong></p><p>Due to the limited hardware resources available (GPU memory), full parameter fine-tuning was not feasible for us. FineTune, the 7 billion model, requires over 100 GB of GPU memory, which exceeded our capacity. Consequently, we opted for the use of adapters. Fortunately, we didn’t need to quantize the 7 billion model, and we were able to utilize Laura even with a sequence length of 1024. Initially, we only unfroze the output layers in our implementation. However, considering the significance of token representation in <strong>fine-tuning a new language</strong>, we also decided to unfreeze the embedding layer.</p><p><strong>Architecture and Parallelization Library</strong></p><p>We were fortunate to have access to the transformers library, which offers deepSpeed integration. This allowed us to easily leverage deepspeed’s powerful parallelization capabilities during training. We utilized deepspeed’s zero-2 mode and integrated it into our training process.</p><p>Considering the aforementioned settings and working with the provided data, the <strong>fine-tuning process</strong> took approximately 28 hours to complete.</p><h4>Deployment</h4><p>The deployment of our <strong>fine-tuned Language and Learning Model (LLM)</strong> in Persian was facilitated through the utilization of the <strong>VLLM </strong>library. This powerful library enhances the speed of model inference through the implementation of pagedAttention. By deploying the model as a web service, we were able to seamlessly connect it to the desired user interface (UI) and effortlessly obtain data through continuous streaming. For the preliminary UI design and testing, we incorporated the gradio library, which further streamlined the process.</p><h3>Results</h3><p>In this section, you will observe the outcomes of the <strong>fine-tuned model</strong>. Although there is still a considerable way to go before achieving a fully reliable model, the positive and notable impact of <strong>fine-tuning</strong> on learning comprehension and language generation in Farsi is evident in these examples. Despite the remaining gaps, the results demonstrate the significant progress achieved through <strong>fine-tuning</strong>. The model shows promising potential in understanding and generating Farsi text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*26MFP-EBLHY1PTC6sirh0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*V_itg_Owk9nc_Ez3n0TTYA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*7jcW5VBoHkZWsQAcu0UM1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*oHM4ono-zoKC17ImaE39YQ.png" /></figure><h3><strong>Conclusion</strong></h3><p>In this article, we discussed our organization’s journey in using Large Language Models (LLM) in the Persian language. Although the journey may seem lengthy, it is only a short and initial part of a complex and extensive path. We are still at the beginning of this journey, and there are many more tasks ahead in exploring the world of large-scale models in the Persian language.</p><p>Currently, <a href="https://ahdsoft.ir/"><strong>AHD Company</strong></a> is actively engaged in the second phase of this project. Upon the completion of the second phase, our results and achievements in this path will be published in another article following this one. Stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20aedcc9c13f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>