<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ammar Hassona</title>
    <description>The latest articles on DEV Community by Ammar Hassona (@ammarhassona).</description>
    <link>https://dev.to/ammarhassona</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3983908%2F55ee96fb-f549-4d6b-a6b2-e1f1ad0316a9.png</url>
      <title>DEV Community: Ammar Hassona</title>
      <link>https://dev.to/ammarhassona</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ammarhassona"/>
    <language>en</language>
    <item>
      <title>I was fine-tuning a language model on Arabic. The loss was perfect. It spoke Chinese.</title>
      <dc:creator>Ammar Hassona</dc:creator>
      <pubDate>Sun, 14 Jun 2026 13:11:48 +0000</pubDate>
      <link>https://dev.to/ammarhassona/i-was-fine-tuning-a-language-model-on-arabic-the-loss-was-perfect-it-spoke-chinese-5ln</link>
      <guid>https://dev.to/ammarhassona/i-was-fine-tuning-a-language-model-on-arabic-the-loss-was-perfect-it-spoke-chinese-5ln</guid>
      <description>&lt;p&gt;Repo: &lt;a href="https://github.com/AmmarHassona/trainsafe" rel="noopener noreferrer"&gt;github.com/AmmarHassona/trainsafe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I was working on fine-tuning an open-source small language model (SLM) on Arabic using DPO. I had the data, the pipeline, and everything set up for training. I was fairly confident that this training run would improve the model and align it further to what I wanted. I started the training and let it run until it finished. When I came back to test the checkpoint, it was speaking Chinese.&lt;/p&gt;

&lt;p&gt;Loss only tells you the model is learning &lt;em&gt;something&lt;/em&gt; — not what it's actually &lt;em&gt;learning&lt;/em&gt;. By the time training finished, I had wasted my time and my compute with nothing useful to show for it. If only there was something to tell me if training was actually going well before it was too late.&lt;/p&gt;

&lt;p&gt;This is when I began looking at tools that could help me solve this issue. Nothing existed that did exactly what I needed, so I built it myself. I built trainsafe to plug into any HuggingFace or TRL training pipeline with two lines of code. It runs alongside your training and checks whether the model's outputs are still behaving correctly at every eval checkpoint — catching issues like language drift, output collapse, and repetition loops before the run finishes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;Install with pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;trainsafe

&lt;span class="c"&gt;# with language drift detection&lt;/span&gt;
pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="s2"&gt;"trainsafe[language]"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add it to your training script with no other changes needed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;trainsafe&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TrainSafeCallback&lt;/span&gt;

&lt;span class="n"&gt;trainer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SFTTrainer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="bp"&gt;...&lt;/span&gt;
    &lt;span class="n"&gt;callbacks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;TrainSafeCallback&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;trainer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;train&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What it checks
&lt;/h2&gt;

&lt;p&gt;At every eval checkpoint, trainsafe generates a small sample of outputs and runs five checks automatically:&lt;/p&gt;

&lt;p&gt;Language — detects if the model switches output language mid-training. This is exactly what would have caught my situation.&lt;/p&gt;

&lt;p&gt;Length — catches output collapse (model suddenly generating much shorter text) or runaway growth. Compares against a rolling baseline so legitimate learning doesn't trigger false alarms.&lt;/p&gt;

&lt;p&gt;Repetition — flags n-gram loops inside individual outputs, the classic "the the the the" failure mode.&lt;/p&gt;

&lt;p&gt;Echo — flags outputs that are mostly a copy of the prompt rather than an actual response.&lt;/p&gt;

&lt;p&gt;Format — detects if a model trained to output JSON starts producing plain text, or vice versa.&lt;/p&gt;

&lt;p&gt;All five run with zero configuration. If the overall health score drops below the warning threshold, you get a warning. If it drops below the stop threshold, training stops and trainsafe points you at the last healthy checkpoint.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it looks like
&lt;/h2&gt;

&lt;p&gt;Healthy run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[TrainSafe @ step 5] ✅ Language consistent (en)
[TrainSafe @ step 5] ✅ Output length normal (avg 62 words)
[TrainSafe @ step 5] ✅ No repetition detected
[TrainSafe @ step 5] ✅ No prompt echoing
[TrainSafe @ step 5] ✅ Format consistent (plain)
[TrainSafe @ step 5] Overall health: 1.00
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When something goes wrong&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[TrainSafe @ step 600] 🚨 Language drift — expected ar, got zh
[TrainSafe @ step 600] 🚨 Output length collapsed (avg 3 words vs baseline 87)
[TrainSafe @ step 600] ⚠️  Repetition detected in 3/5 outputs
[TrainSafe @ step 600] Overall health: 0.20
&amp;gt;&amp;gt;&amp;gt; TrainSafe stopped training. Recommended checkpoint: step 400.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Custom probes
&lt;/h2&gt;

&lt;p&gt;If you have a specific capability you can't afford to lose, you can define fixed prompts and expected behaviors in a YAML file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;probes&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;prompt&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;مرحبا،&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;كيف&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;يمكنني&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;مساعدتك؟"&lt;/span&gt;
    &lt;span class="na"&gt;checks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;ar&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;min_length&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;not_contains&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;&amp;lt;|im_start|&amp;gt;"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;###"&lt;/span&gt;&lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These run at every checkpoint alongside the automatic checks.&lt;/p&gt;

&lt;p&gt;trainsafe is MIT licensed, early stage, and feedback is very welcome. If you've hit a similar problem during fine-tuning I'd love to hear about it in the comments.&lt;/p&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/AmmarHassona/trainsafe" rel="noopener noreferrer"&gt;github.com/AmmarHassona/trainsafe&lt;/a&gt;&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>llm</category>
      <category>opensource</category>
      <category>python</category>
    </item>
  </channel>
</rss>
