Module stringclasses 3
values | Name stringlengths 3 15 | MLX Python stringlengths 1.77k 25.6k | MLX Swift stringlengths 3.79k 44k | Notes stringclasses 1
value | Transformers / Diffusers .py (TODO) float64 | Unnamed: 6 float64 | Unnamed: 7 float64 | Unnamed: 8 float64 | Unnamed: 9 float64 | Unnamed: 10 float64 | Unnamed: 11 float64 | Unnamed: 12 float64 | Unnamed: 13 float64 | Unnamed: 14 float64 | Unnamed: 15 float64 | Unnamed: 16 float64 | Unnamed: 17 float64 | Unnamed: 18 float64 | Unnamed: 19 float64 | Unnamed: 20 float64 | Unnamed: 21 float64 | Unnamed: 22 float64 | Unnamed: 23 float64 | Unnamed: 24 float64 | Unnamed: 25 float64 | Unnamed: 26 float64 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
StableDiffusion | StableDiffusion | # Copyright © 2023-2024 Apple Inc.
import time
from typing import Optional, Tuple
import mlx.core as mx
from .model_io import (
_DEFAULT_MODEL,
load_autoencoder,
load_diffusion_config,
load_text_encoder,
load_tokenizer,
load_unet,
)
from .sampler import SimpleEulerAncestralSampler, SimpleEule... | // Copyright © 2024 Apple Inc.
import Foundation
import Hub
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/__init__.py
/// Iterator that produces latent images.
///
/// Created by:
///
/// - ``TextToImageGenerator/generateLatents(parameters:)... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | Tokenizer | # Copyright © 2023 Apple Inc.
import regex
class Tokenizer:
"""A simple port of CLIPTokenizer from https://github.com/huggingface/transformers/ ."""
def __init__(self, bpe_ranks, vocab):
self.bpe_ranks = bpe_ranks
self.vocab = vocab
self.pat = regex.compile(
r"""<\|starto... | // Copyright © 2024 Apple Inc.
import Foundation
struct Bigram: Hashable {
let a: String
let b: String
init(_ s: String) {
let pieces = s.split(separator: " ")
precondition(pieces.count == 2, "BPEPair expected two pieces for '\(s)'")
self.a = String(pieces[0])
self.b = Str... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | Unet | # Copyright © 2023 Apple Inc.
import math
from typing import Optional
import mlx.core as mx
import mlx.nn as nn
from .config import UNetConfig
def upsample_nearest(x, scale: int = 2):
B, H, W, C = x.shape
x = mx.broadcast_to(x[:, :, None, :, None, :], (B, H, scale, W, scale, C))
x = x.reshape(B, H * sc... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/unet.py
func upsampleNearest(_ x: MLXArray, scale: Int = 2) -> MLXArray {
precondition(x.ndim == 4)
let (B, H, W, C) = x.shape4
var x ... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | VAE | # Copyright © 2023 Apple Inc.
import math
from typing import List
import mlx.core as mx
import mlx.nn as nn
from .config import AutoencoderConfig
from .unet import ResnetBlock2D, upsample_nearest
class Attention(nn.Module):
"""A single head unmasked attention for use with the VAE."""
def __init__(self, di... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/vae.py
class Attention: Module, UnaryLayer {
@ModuleInfo(key: "group_norm") public var groupNorm: GroupNorm
@ModuleInfo(key: "query_proj... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | CLIP | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import List, Optional
import mlx.core as mx
import mlx.nn as nn
from .config import CLIPTextModelConfig
_ACTIVATIONS = {"quick_gelu": nn.gelu_fast_approx, "gelu": nn.gelu}
@dataclass
class CLIPOutput:
# The last_hidden_state inde... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/clip.py
struct CLIPOutput {
/// The lastHiddenState indexed at the EOS token and possibly projected if
/// the model has a projection laye... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | Config | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Optional, Tuple
@dataclass
class AutoencoderConfig:
in_channels: int = 3
out_channels: int = 3
latent_channels_out: int = 8
latent_channels_in: int = 4
block_out_channels: Tuple[int] = (128, 256, 512, 512)
... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/config.py
/// Configuration for ``Autoencoder``
struct AutoencoderConfiguration: Codable {
public var inputChannels = 3
public var output... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | Load | # Copyright © 2023-2024 Apple Inc.
import json
from typing import Optional
import mlx.core as mx
from huggingface_hub import hf_hub_download
from mlx.utils import tree_unflatten
from .clip import CLIPTextModel
from .config import AutoencoderConfig, CLIPTextModelConfig, DiffusionConfig, UNetConfig
from .tokenizer imp... | // Copyright © 2024 Apple Inc.
import Foundation
import Hub
import MLX
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/model_io.py
/// Configuration for loading stable diffusion weights.
///
/// These options can be tuned to conserve memory.
public struc... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
StableDiffusion | Sampler | # Copyright © 2023 Apple Inc.
import mlx.core as mx
from .config import DiffusionConfig
def _linspace(a, b, num):
x = mx.arange(0, num) / (num - 1)
return (b - a) * x + a
def _interp(y, x_new):
"""Interpolate the function defined by (arange(0, len(y)), y) at positions x_new."""
x_low = x_new.astyp... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
// port of https://github.com/ml-explore/mlx-examples/blob/main/stable_diffusion/stable_diffusion/sampler.py
/// Interpolate the function defined by `(0 ..< y.count) y)` at positions `xNew`.
func interpolate(y: MLXArray, xNew: MLXArray) -> MLXArray {
le... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Bitnet | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from functools import partial
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .bitlinear_layers import BitLinear
from ... | //
// Bitnet.swift
// mlx-swift-examples
//
// Created by John Mai on 2025/6/12.
//
import Foundation
import MLX
import MLXFast
import MLXLMCommon
import MLXNN
import Tokenizers
// port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/bitnet.py
private func makeBitLinearKernel() -> MLXFast.MLXFastK... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Cohere | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden_size: int = ... | import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/cohere.py
private class Attention: Module {
let args: CohereConfiguration
let scale: Float
@ModuleInfo(key: "q_proj") var wq: Linear
@ModuleInfo(key: "k_pro... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | GLM | # Copyright © 2025 Apple Inc.
from dataclasses import dataclass
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden_size: int
num... | //
// GLM4.swift
// LLM
//
// Created by John Mai on 2025/5/1.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/glm4.py
private class Attention: Module {
let args: GLM4Configuration
let scale: Float
@ModuleInfo(key... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Gemma | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden_size: int
... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
import Tokenizers
// Port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/gemma.py
// Specialized norm for Gemma
private class RMSNorm: Module, UnaryLayer {
let weight: MLXArray
let eps: Flo... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Gemma2 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden_size: int
num_hidden_layers: int
i... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
import Tokenizers
// Port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/gemma2.py
private class Attention: Module {
let args: Gemma2Configuration
let scale: Float
let logitSoftCap: Flo... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Gemma3Text | # Copyright © 2025 Apple Inc.
from dataclasses import dataclass
from functools import partial
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .cache import KVCache, RotatingKVCache
@dataclass
class ... | //
// Gemma3Text.swift
// mlx-swift-examples
//
// Created by Anthony DePasquale on 14.03.2025.
//
// Based on https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/gemma3_text.py
import Foundation
import MLX
import MLXFast
import MLXLLM
import MLXLMCommon
import MLXNN
public struct Gemma3TextCo... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Granite | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import initialize_rope
@dataclass
class ModelArgs(BaseMo... | //
// Granite.swift
// mlx-swift-examples
//
// Created by Sachin Desai on 4/25/25.
//
// Port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/granite.py
import Foundation
import MLX
import MLXLMCommon
import MLXNN
private class Attention: Module {
let args: GraniteConfiguration
let scale:... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | InternLM2 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// Port of https://github.com/maiqingqiang/mlx-examples/blob/main/llms/mlx_lm/models/internlm2.py
private class DynamicNTKScalingRoPE: Module {
let dims: Int
let maxPositionEmbeddings: Int
let traditional: Bool
... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Llama | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import initialize_rope
@dataclass
class ModelArgs(BaseMo... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
import Tokenizers
// port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/llama.py
func computeBaseFrequency(
base: Float, dims: Int, ropeType: String, ropeScaling: [String: StringOrNumber]?
)
... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | MiMo | # Copyright © 2023-2025 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import initialize_rope
@dataclass
class ModelArgs(BaseMo... | //
// MiMo.swift
// LLM
//
// Created by John Mai on 2025/5/3.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
private class Attention: Module {
let args: MiMoConfiguration
let scale: Float
@ModuleInfo(key: "q_proj") var wq: Linear
@ModuleInfo(key: "k_proj") var wk: Linear
@Mod... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | OpenELM | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
... | //
// OpenELM.swift
// LLM
//
// Created by Sachin Desai on 2024/4/27.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
func computeHeads(modelDim: Int, headDim: Int) -> Int {
assert(modelDim % headDim == 0, "modelDim must be divisible by headDim")
return modelDim / headDim
}
func makeDivis... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Phi | # Copyright © 2023-2024 Apple Inc.
import math
from dataclasses import dataclass
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str = "phi"
max_position_embeddings: int = 2... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/phi.py
private class PhiAttention: Module {
let args: PhiConfiguration
let heads: Int
let headDim: Int
@ModuleInfo(key: "q_proj") va... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Phi3 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Tuple, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import SuScaledRoPE
@dataclass
class ModelA... | // Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXLMCommon
import MLXNN
private class Attention: Module {
let args: Phi3Configuration
let scale: Float
let heads: Int
let kvHeads: Int
let headDim: Int
let ropeDim: Int
@ModuleInfo(key: "qkv_proj") var wqkv: Linear
... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | PhiMoE | # Copyright © 2024 Apple Inc.
import math
from dataclasses import dataclass
from typing import Dict, List, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import SuScaledRoPE
from .switch_layers import Swit... | import Foundation
import MLX
import MLXLMCommon
import MLXNN
// Port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/phimoe.py
public struct PhiMoEConfiguration: Codable, Sendable {
var modelType: String = "phimoe"
var vocabularySize: Int = 32064
var hiddenSize: Int = 4096
v... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Qwen2 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import initialize_rope
@dataclass
class ModelArgs(BaseMo... | //
// Qwen2.swift
// LLM
//
// Created by John Mai on 2024/3/3.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/qwen2.py
private class Attention: Module {
let args: Qwen2Configuration
let scale: Float
@... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Qwen3 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .rope_utils import initialize_rope
@dataclass
class ModelArgs(BaseMo... | //
// Qwen3.swift
// LLM
//
// Created by John Mai on 2025/4/28.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/qwen3.py
private class Attention: Module {
let args: Qwen3Configuration
let scale: Float
@ModuleInfo... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Qwen3MoE | # Copyright © 2025 Apple Inc.
from dataclasses import dataclass
from typing import Any, Dict, List, Optional, Union
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
from .switch_layers import SwitchGLU
@dataclass
class ModelArgs(BaseMode... | //
// Qwen3MoE.swift
// LLM
//
// Created by John Mai on 2025/4/30.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-lm/blob/main/mlx_lm/models/qwen3_moe.py
private class Attention: Module {
let args: Qwen3MoEConfiguration
let scale: Float
@... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
LM | Starcoder2 | # Copyright © 2023-2024 Apple Inc.
from dataclasses import dataclass
from typing import Any, Optional
import mlx.core as mx
import mlx.nn as nn
from .base import BaseModelArgs, create_attention_mask, scaled_dot_product_attention
@dataclass
class ModelArgs(BaseModelArgs):
model_type: str
hidden_size: int
... | //
// Starcoder2.swift
// LLM
//
// Created by John Mai on 2024/3/7.
//
import Foundation
import MLX
import MLXLMCommon
import MLXNN
// port of https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/models/starcoder2.py
private class Attention: Module {
let args: Starcoder2Configuration
let scal... | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
VLM | Gemma3 | import glob
import inspect
import json
from dataclasses import dataclass
from pathlib import Path
from typing import List, Optional
import mlx.core as mx
import mlx.nn as nn
import numpy as np
from huggingface_hub import snapshot_download
from .language import LanguageModel, RMSNorm, TextConfig
from .vision import Vi... | import CoreImage
import MLX
import MLXFast
import MLXLMCommon
import MLXNN
import Tokenizers
// Based on https://github.com/Blaizzy/mlx-vlm/tree/main/mlx_vlm/models/gemma3
// MARK: - Text Configuration
public struct Gemma3TextConfiguration: Codable, Sendable {
public let modelType: String
public let hiddenSi... | Python versions at https://github.com/Blaizzy/mlx-vlm usually have multiple files. This one combines the 3 .py files into one cell. Evaluate future format for more VLM examples. Stopping here for now. | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.