• About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
Wednesday, October 8, 2025
mGrowTech
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions
No Result
View All Result
mGrowTech
No Result
View All Result
Home Al, Analytics and Automation

Mixture of Experts Architecture in Transformer Models

Josh by Josh
July 20, 2025
in Al, Analytics and Automation
0
Mixture of Experts Architecture in Transformer Models
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


import torch

import torch.nn as nn

import torch.nn.functional as F

 

class Expert(nn.Module):

    def __init__(self, dim, intermediate_dim):

        super().__init__()

        self.gate_proj = nn.Linear(dim, intermediate_dim)

        self.up_proj = nn.Linear(dim, intermediate_dim)

        self.down_proj = nn.Linear(intermediate_dim, dim)

        self.act = nn.SiLU()

 

    def forward(self, x):

        gate = self.gate_proj(x)

        up = self.up_proj(x)

        swish = self.act(gate)

        output = self.down_proj(swish * up)

        return output

 

class MoELayer(nn.Module):

    def __init__(self, dim, intermediate_dim, num_experts, top_k=2):

        super().__init__()

        self.num_experts = num_experts

        self.top_k = top_k

        self.dim = dim

        # Create expert networks

        self.experts = nn.ModuleList([

            Expert(dim, intermediate_dim) for _ in range(num_experts)

        ])

        self.router = nn.Linear(dim, num_experts)

 

    def forward(self, hidden_states):

        batch_size, seq_len, hidden_dim = hidden_states.shape

        

        # Reshape for expert processing, the compute routing probabilities

        hidden_states_reshaped = hidden_states.view(–1, hidden_dim)

        router_logits = self.router(hidden_states_reshaped)  # (batch_size * seq_len, num_experts)

        routing_probs = F.softmax(router_logits, dim=–1)

 

        # Select top-k experts, and scale the probabilities to sum to 1

        # output shape: (batch_size * seq_len, k)

        top_k_probs, top_k_indices = torch.topk(routing_probs, self.top_k, dim=–1)

        top_k_probs = top_k_probs / top_k_probs.sum(dim=–1, keepdim=True)

 

        # Process through selected experts

        output = []

        for i in range(self.top_k):

            expert_idx = top_k_indices[:, i]

            expert_probs = top_k_probs[:, i]

            # Process each vector in the batch and sequence with the selected expert

            expert_output = torch.stack([

                self.experts[j](hidden_states_reshaped[j])

                for j in expert_idx

            ], dim=0)

            # Weighted sum by routing probability

            output.sum(expert_probs.unsqueeze(–1) * expert_output)

 

        # Reshape back to original shape

        output = sum(output).view(batch_size, seq_len, hidden_dim)

        return output

 

class MoETransformerLayer(nn.Module):

    def __init__(self, dim, intermediate_dim, num_experts, top_k=2, num_heads=8):

        super().__init__()

        self.attention = nn.MultiheadAttention(dim, num_heads, batch_first=True)

        self.moe = MoELayer(dim, intermediate_dim, num_experts, top_k)

        self.norm1 = nn.RMSNorm(dim)

        self.norm2 = nn.RMSNorm(dim)

 

    def forward(self, x):

        # Attention sublayer

        input_x = x

        x = self.norm1(x)

        attn_output, _ = self.attention(x, x, x)

        input_x = input_x + attn_output

 

        # MoE sublayer

        x = self.norm2(input_x)

        moe_output = self.moe(x)

        return input_x + moe_output



Source_link

READ ALSO

Ai Flirt Chat Generator With Photos

Fighting for the health of the planet with AI | MIT News

Related Posts

Ai Flirt Chat Generator With Photos
Al, Analytics and Automation

Ai Flirt Chat Generator With Photos

October 8, 2025
Fighting for the health of the planet with AI | MIT News
Al, Analytics and Automation

Fighting for the health of the planet with AI | MIT News

October 8, 2025
Building a Human Handoff Interface for AI-Powered Insurance Agent Using Parlant and Streamlit
Al, Analytics and Automation

Building a Human Handoff Interface for AI-Powered Insurance Agent Using Parlant and Streamlit

October 7, 2025
How OpenAI’s Sora 2 Is Transforming Toy Design into Moving Dreams
Al, Analytics and Automation

How OpenAI’s Sora 2 Is Transforming Toy Design into Moving Dreams

October 7, 2025
Printable aluminum alloy sets strength records, may enable lighter aircraft parts | MIT News
Al, Analytics and Automation

Printable aluminum alloy sets strength records, may enable lighter aircraft parts | MIT News

October 7, 2025
Google DeepMind Introduces CodeMender: A New AI Agent that Uses Gemini Deep Think to Automatically Patch Critical Software Vulnerabilities
Al, Analytics and Automation

Google DeepMind Introduces CodeMender: A New AI Agent that Uses Gemini Deep Think to Automatically Patch Critical Software Vulnerabilities

October 7, 2025
Next Post
At Least 750 US Hospitals Faced Disruptions During Last Year’s CrowdStrike Outage, Study Finds

At Least 750 US Hospitals Faced Disruptions During Last Year’s CrowdStrike Outage, Study Finds

POPULAR NEWS

Communication Effectiveness Skills For Business Leaders

Communication Effectiveness Skills For Business Leaders

June 10, 2025
15 Trending Songs on TikTok in 2025 (+ How to Use Them)

15 Trending Songs on TikTok in 2025 (+ How to Use Them)

June 18, 2025
Trump ends trade talks with Canada over a digital services tax

Trump ends trade talks with Canada over a digital services tax

June 28, 2025
App Development Cost in Singapore: Pricing Breakdown & Insights

App Development Cost in Singapore: Pricing Breakdown & Insights

June 22, 2025
7 Best EOR Platforms for Software Companies in 2025

7 Best EOR Platforms for Software Companies in 2025

June 21, 2025

EDITOR'S PICK

My 9 Favorite Pickleball Paddles From 3 Years of Testing (2025)

My 9 Favorite Pickleball Paddles From 3 Years of Testing (2025)

July 21, 2025
Experience Design and Creative Process Benchmark Report

Experience Design and Creative Process Benchmark Report

September 3, 2025

The 4 pillars of meaningful AI usage

May 28, 2025
Why Your Business Needs One

Why Your Business Needs One

September 21, 2025

About

We bring you the best Premium WordPress Themes that perfect for news, magazine, personal blog, etc. Check our landing page for details.

Follow us

Categories

  • Account Based Marketing
  • Ad Management
  • Al, Analytics and Automation
  • Brand Management
  • Channel Marketing
  • Digital Marketing
  • Direct Marketing
  • Event Management
  • Google Marketing
  • Marketing Attribution and Consulting
  • Marketing Automation
  • Mobile Marketing
  • PR Solutions
  • Social Media Management
  • Technology And Software
  • Uncategorized

Recent Posts

  • Gemini CLI extensions let you customize your command line
  • Features & Pricing Comparison Guide
  • How To Create Engaging Content For Ski Resort Social Media Channels
  • Pinterest Board Strategy: How to Use Boards Effectively
  • About Us
  • Disclaimer
  • Contact Us
  • Privacy Policy
No Result
View All Result
  • Technology And Software
    • Account Based Marketing
    • Channel Marketing
    • Marketing Automation
      • Al, Analytics and Automation
      • Ad Management
  • Digital Marketing
    • Social Media Management
    • Google Marketing
  • Direct Marketing
    • Brand Management
    • Marketing Attribution and Consulting
  • Mobile Marketing
  • Event Management
  • PR Solutions

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?