<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Micael Costa — Software Engineer</title>
    <subtitle>Portfolio of Micael Costa — Software Engineering student at IPVC, building close to the hardware and exploring AI&#x2F;ML.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://micaelcosta.dev/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://micaelcosta.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-03-04T00:00:00+00:00</updated>
    <id>https://micaelcosta.dev/atom.xml</id>
    <entry xml:lang="en">
        <title>Bare-Metal Gemma 2</title>
        <published>2026-03-04T00:00:00+00:00</published>
        <updated>2026-03-04T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://micaelcosta.dev/project/bare-metal-gemma2/"/>
        <id>https://micaelcosta.dev/project/bare-metal-gemma2/</id>
        
        <content type="html" xml:base="https://micaelcosta.dev/project/bare-metal-gemma2/">&lt;p&gt;This is an implementation of the Gemma 2 (2B) inference engine in &lt;strong&gt;Rust&lt;&#x2F;strong&gt;. I might provide &lt;strong&gt;C&lt;&#x2F;strong&gt;, &lt;strong&gt;Zig&lt;&#x2F;strong&gt;, or &lt;strong&gt;Nim&lt;&#x2F;strong&gt; implementations in the future for fun.&lt;&#x2F;p&gt;
&lt;p&gt;I tried to make the code as simple and readable as possible, but my Rust skills are not perfect. I ran into too many issues with the borrow checker and ended up using &lt;code&gt;unsafe&lt;&#x2F;code&gt; to map the weight tensors directly to memory. If you have suggestions for doing this without &lt;code&gt;unsafe&lt;&#x2F;code&gt;, please open an issue or submit a pull request.&lt;&#x2F;p&gt;
&lt;p&gt;This project was inspired by Karpathy’s llama2.c. He showed that the math behind transformer inference can be implemented without heavy frameworks — newer models are a bit more complex but still quite doable. You can explore &lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;karpathy&#x2F;llama2.c&quot;&gt;llama2.c on GitHub&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;feel-the-power-of-1-token-s&quot;&gt;Feel the power of 1 token&#x2F;s&lt;&#x2F;h2&gt;
&lt;p&gt;To try it out, clone the repo and run the following commands:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;git&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; clone https:&#x2F;&#x2F;github.com&#x2F;md3f4ltm&#x2F;bare-metal-gemma2.git&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; bare-metal-gemma2&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Download and export the weights from Hugging Face using the &lt;code&gt;export.py&lt;&#x2F;code&gt; script. You will need to create a Hugging Face account and accept the model license to download the weights: https:&#x2F;&#x2F;huggingface.co&#x2F;google&#x2F;gemma-2-2b-it&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;python&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; export.py&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you don’t want to use the &lt;code&gt;export.py&lt;&#x2F;code&gt; script, I also uploaded the exported weights and tokenizer to hugging face.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;wget&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; https:&#x2F;&#x2F;huggingface.co&#x2F;d3falt-dev&#x2F;gemma2-2b-bin&#x2F;resolve&#x2F;main&#x2F;gemma2_2b.bin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;wget&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; https:&#x2F;&#x2F;huggingface.co&#x2F;d3falt-dev&#x2F;gemma2-2b-bin&#x2F;resolve&#x2F;main&#x2F;tokenizer.json&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Compile the Rust code and run the inference:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;cargo&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; run&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; --release&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I include a language-agnostic mathematical breakdown of the model (written by an LLM) in the repository.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;md3f4ltm&#x2F;bare-metal-gemma2&#x2F;blob&#x2F;main&#x2F;math.md&quot;&gt;Mathematical breakdown&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;references&quot;&gt;References&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2408.00118&quot;&gt;Google Gemma 2 Technical Report&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;huggingface&#x2F;transformers&#x2F;blob&#x2F;main&#x2F;src&#x2F;transformers&#x2F;models&#x2F;gemma2&#x2F;modeling_gemma2.py&quot;&gt;Hugging Face Gemma 2 Modeling&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;noopener noreferrer external&quot; target=&quot;_blank&quot; href=&quot;https:&#x2F;&#x2F;blog.eleuther.ai&#x2F;rotary-embeddings&#x2F;&quot;&gt;Rotary Embeddings (RoPE) Explained&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;dependencies&quot;&gt;Dependencies&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;rust&quot;&gt;Rust&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;memmap2&lt;&#x2F;code&gt; — for memory-mapping the weights file.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;tokenizers&lt;&#x2F;code&gt; — for tokenization (it’s possible to implement your own; an inefficient one is not too hard to do).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;hr &#x2F;&gt;
&lt;p&gt;Have fun!&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Circuit Logic Simulator</title>
        <published>2023-05-15T00:00:00+00:00</published>
        <updated>2023-05-15T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://micaelcosta.dev/project/circuit-logic/"/>
        <id>https://micaelcosta.dev/project/circuit-logic/</id>
        
        <content type="html" xml:base="https://micaelcosta.dev/project/circuit-logic/">&lt;h2 id=&quot;overview&quot;&gt;Overview&lt;&#x2F;h2&gt;
&lt;p&gt;The Circuit Logic Simulator allows users to create, modify, and analyze digital logic circuits. It provides several commands for validating circuits, generating truth tables, and animating circuit behavior.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;main-features&quot;&gt;Main Features&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;circuit-validation&quot;&gt;Circuit Validation&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;VALIDATE&lt;&#x2F;code&gt; command checks your circuit for:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Cycles (logic loops)&lt;&#x2F;li&gt;
&lt;li&gt;Isolated components (without inputs or outputs)&lt;&#x2F;li&gt;
&lt;li&gt;Other potential issues&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;truth-table-generation&quot;&gt;Truth Table Generation&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;TRUTHTABLE&lt;&#x2F;code&gt; command automatically:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Identifies all Switch inputs&lt;&#x2F;li&gt;
&lt;li&gt;Identifies all LED outputs&lt;&#x2F;li&gt;
&lt;li&gt;Calculates output states for each input combination&lt;&#x2F;li&gt;
&lt;li&gt;Displays or saves results&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;circuit-animation&quot;&gt;Circuit Animation&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;ANIMATE&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Cycles through all possible input combinations&lt;&#x2F;li&gt;
&lt;li&gt;Updates the circuit state&lt;&#x2F;li&gt;
&lt;li&gt;Visually displays the results with a configurable delay&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;basic-commands&quot;&gt;Basic Commands&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;adding-components&quot;&gt;Adding Components&lt;&#x2F;h3&gt;
&lt;p&gt;Add various logic gates:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g1:AND@100,100 &amp;quot;AND Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g2:OR@200,100 &amp;quot;OR Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g3:NOT@300,100 &amp;quot;NOT Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g4:NAND@400,100 &amp;quot;NAND Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g5:NOR@500,100 &amp;quot;NOR Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ADD g6:XOR@600,100 &amp;quot;XOR Gate&amp;quot;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;moving-components&quot;&gt;Moving Components&lt;&#x2F;h3&gt;
&lt;p&gt;Move components to new positions:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;MOVE g1 150,150;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;removing-components&quot;&gt;Removing Components&lt;&#x2F;h3&gt;
&lt;p&gt;Remove unwanted components:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;REMOVE g2;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h3 id=&quot;listing-components&quot;&gt;Listing Components&lt;&#x2F;h3&gt;
&lt;p&gt;View all components in your circuit:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;LIST;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or save the list to a file:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;LIST components.txt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;circuit-logic&#x2F;list.png&quot; alt=&quot;Component listing example&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;&quot;&gt;Advanced Features&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;-1&quot;&gt;Circuit Validation Example&lt;&#x2F;h3&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;VALIDATE;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will report any issues with your circuit, such as:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Logic loops that might cause oscillation&lt;&#x2F;li&gt;
&lt;li&gt;Components with no connections&lt;&#x2F;li&gt;
&lt;li&gt;Incomplete circuits&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;-2&quot;&gt;Truth Table Generation&lt;&#x2F;h3&gt;
&lt;p&gt;Generate and display a truth table:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;TRUTHTABLE;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Save the table to a file:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;TRUTHTABLE results.txt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;circuit-logic&#x2F;truth.png&quot; alt=&quot;Truth table example&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;-3&quot;&gt;Circuit Animation&lt;&#x2F;h3&gt;
&lt;p&gt;Animate with default delay (500ms):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ANIMATE;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Animate with custom delay (1 second):&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ANIMATE 1000;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;circuit-logic&#x2F;animate.webp&quot; alt=&quot;Circuit animation&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;-4&quot;&gt;Example Circuit&lt;&#x2F;h2&gt;
&lt;p&gt;Here’s an example of a simple circuit created with the simulator:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;circuit-logic&#x2F;normal.png&quot; alt=&quot;Example circuit&quot; loading=&quot;lazy&quot; decoding=&quot;async&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;-5&quot;&gt;Tips and Best Practices&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;Always validate your circuit before generating truth tables or animations&lt;&#x2F;li&gt;
&lt;li&gt;Use meaningful component names for better readability&lt;&#x2F;li&gt;
&lt;li&gt;Position components in a logical flow (inputs on left, outputs on right)&lt;&#x2F;li&gt;
&lt;li&gt;Save complex circuits to files for future reference&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;-6&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;The Circuit Logic Simulator provides powerful tools for digital circuit design and analysis. Use the commands outlined in this guide to create, validate, and test your digital logic designs.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
