nn

    Neural network training library for MoonBit

    neural-network
    tensor
    moonbit
    Download zip
    Author
    Version
    0.1.4
    License
    Apache-2.0
    Last updated
    2 days ago
    Downloads
    91

    #mizchi/nn

    MoonBit-based GPT-2 training demo and tensor utilities.

    このリポジトリは、mizchi の勉強用に作っている実験的な POC(MoonBit 製の機械学習フレームワーク)です。 動作・互換性・性能についての保証はありません。

    #Scope

    This repository is now focused on:

    • mizchi/nn/tensor (tensor + transformer training kernels)
    • mizchi/nn/transformer-bench (GPT-2 style language-model training demo)

    JS / Node / Playwright related assets were removed.

    #Quick Commands

    The native build needs Rust and the pinned wgpu-native submodule. Linux also needs OpenBLAS and LAPACK development packages. macOS uses Accelerate.

    just # check + test (native) just fmt # format code just check # type check (native) just test # run tests (native) just info # generate mbti files (native) just run --steps 1 --warmup 0 --repeat 1 --batch-size 1 --seq-len 8 --d-model 16 --heads 2 --layers 1 --d-ff 32

    #GPT-2 Training Demo

    Run directly:

    moon run --target native src/transformer-bench -- --steps 40 --warmup 5

    #OpenWebText Sharding

    just openwebtext-shard \ --input-dir ~/data/lm/openwebtext/plain_text \ --output-dir ~/data/lm/openwebtext_gpt2 \ --tokens-per-shard 8388608

    #NumPy Comparison Bench (Reference)

    numbt と NumPy の比較ベンチ(既存の参考値):

    Operationnumbt (ms)NumPy (ms)Notes
    vec_add<0.001<0.001Element-wise
    vec_dot<0.001<0.001BLAS sdot
    vec_exp<0.0010.001vForce
    vec_sort<0.0010.006vDSP_vsort
    mat_matmul (128x784 @ 784x128)~0.020.019BLAS sgemm
    fmat_inv (100x100)<0.0010.067LAPACK sgetrf/sgetri
    fmat_svd (100x100)<0.001-LAPACK sgesvd
    fmat_cholesky (100x100)<0.001-LAPACK spotrf
    fmat_qr (100x100)<0.001-LAPACK sgeqrf

    NumPy 側ベンチは以下で実行できます:

    uv run bench/numpy_bench.py

    #License

    Apache License 2.0 (LICENSE)

    BufferUsage

    type BufferUsage = Int

    A signed 32-bit integer.

    ShaderStageFlags

    type ShaderStageFlags = Int

    A signed 32-bit integer.

    TextureUsage

    type TextureUsage = Int

    A signed 32-bit integer.

    Adapter

    #external
    pub type Adapter

    BindGroup

    #external
    pub type BindGroup

    BindGroupDescriptor

    pub struct BindGroupDescriptor {
    layout : BindGroupLayout
    entries : Array[BindGroupEntry]
    label : String?
    }

    BindGroupEntry

    pub struct BindGroupEntry {
    binding : Int
    resource : BindingResource
    }

    BindGroupLayout

    #external
    pub type BindGroupLayout

    BindGroupLayoutDescriptor

    pub struct BindGroupLayoutDescriptor {
    entries : Array[BindGroupLayoutEntry]
    label : String?
    } derive(Eq, Show)

    BindGroupLayoutEntry

    pub struct BindGroupLayoutEntry {
    binding : Int
    visibility : Int
    ty : BindingType
    } derive(Eq, Show)

    BindingResource

    pub enum BindingResource {
    Buffer(Buffer, Int, Int)
    Sampler(Sampler)
    TextureView(TextureView)
    }

    BindingType

    pub enum BindingType {
    UniformBuffer
    StorageBuffer
    ReadOnlyStorageBuffer
    Sampler
    Texture
    StorageTexture
    } derive(Eq, Show)

    Buffer

    #external
    pub type Buffer

    BufferDescriptor

    pub struct BufferDescriptor {
    size : Int
    usage : Int
    mapped_at_creation : Bool
    label : String?
    } derive(Eq, Show)

    ColorTargetState

    pub struct ColorTargetState {
    format : TextureFormat
    } derive(Eq, Show)

    CommandBuffer

    #external
    pub type CommandBuffer

    CommandEncoder

    #external
    pub type CommandEncoder

    CommandEncoderDescriptor

    pub struct CommandEncoderDescriptor {
    label : String?
    } derive(Eq, Show)

    ComputePipeline

    #external
    pub type ComputePipeline

    ComputePipelineDescriptor

    pub struct ComputePipelineDescriptor {
    layout : PipelineLayout
    compute : ProgrammableStage
    label : String?
    }

    Device

    #external
    pub type Device

    DeviceDescriptor

    pub struct DeviceDescriptor {
    required_features : Array[Feature]
    required_limits : Limits
    label : String?
    } derive(Eq, Show)

    Extent3d

    pub struct Extent3d {
    width : Int
    height : Int
    depth_or_array_layers : Int
    } derive(Eq, Show)

    Feature

    pub enum Feature {
    ShaderF16
    TimestampQuery
    TextureCompressionBC
    TextureCompressionETC2
    TextureCompressionASTC
    Float32Filterable
    Other(String)
    } derive(Eq, Show)

    FilterMode

    pub enum FilterMode {
    Nearest
    Linear
    } derive(Eq, Show)

    FragmentState

    pub struct FragmentState {
    shader_module : ShaderModule
    entry_point : String
    targets : Array[ColorTargetState]
    }

    Instance

    #external
    pub type Instance

    Limits

    pub struct Limits {
    max_texture_dimension_2d : Int
    max_storage_buffer_binding_size : Int
    max_uniform_buffer_binding_size : Int
    max_bind_groups : Int
    max_compute_workgroup_size_x : Int
    max_compute_workgroup_size_y : Int
    max_compute_workgroup_size_z : Int
    max_compute_workgroups_per_dimension : Int
    } derive(Eq, Show)

    PipelineLayout

    #external
    pub type PipelineLayout

    PipelineLayoutDescriptor

    pub struct PipelineLayoutDescriptor {
    bind_group_layouts : Array[BindGroupLayout]
    label : String?
    }

    PowerPreference

    pub enum PowerPreference {
    Default
    LowPower
    HighPerformance
    } derive(Eq, Show)

    PrimitiveState

    pub struct PrimitiveState {
    topology : PrimitiveTopology
    } derive(Eq, Show)

    PrimitiveTopology

    pub enum PrimitiveTopology {
    PointList
    LineList
    LineStrip
    TriangleList
    TriangleStrip
    } derive(Eq, Show)

    ProgrammableStage

    pub struct ProgrammableStage {
    shader_module : ShaderModule
    entry_point : String
    }

    Queue

    #external
    pub type Queue

    RenderPipeline

    #external
    pub type RenderPipeline

    RenderPipelineDescriptor

    pub struct RenderPipelineDescriptor {
    layout : PipelineLayout
    vertex : VertexState
    fragment : FragmentState
    primitive : PrimitiveState
    label : String?
    }

    RequestAdapterOptions

    pub struct RequestAdapterOptions {
    power_preference : PowerPreference
    force_fallback_adapter : Bool
    } derive(Eq, Show)

    Sampler

    #external
    pub type Sampler

    SamplerDescriptor

    pub struct SamplerDescriptor {
    mag_filter : FilterMode
    min_filter : FilterMode
    mipmap_filter : FilterMode
    label : String?
    } derive(Eq, Show)

    ShaderModule

    #external
    pub type ShaderModule

    ShaderModuleDescriptor

    pub struct ShaderModuleDescriptor {
    code : String
    label : String?
    } derive(Eq, Show)

    Texture

    #external
    pub type Texture

    TextureDescriptor

    pub struct TextureDescriptor {
    size : Extent3d
    mip_level_count : Int
    sample_count : Int
    dimension : TextureDimension
    format : TextureFormat
    usage : Int
    label : String?
    } derive(Eq, Show)

    TextureDimension

    pub enum TextureDimension {
    D1
    D2
    D3
    } derive(Eq, Show)

    TextureFormat

    pub enum TextureFormat {
    Rgba8Unorm
    Bgra8Unorm
    Rgba16Float
    R32Float
    R32Uint
    } derive(Eq, Show)

    TextureView

    #external
    pub type TextureView

    VertexState

    pub struct VertexState {
    shader_module : ShaderModule
    entry_point : String
    }

    WgpuError

    pub enum WgpuError {
    NotImplemented
    NotSupported
    Validation(String)
    } derive(Eq, Show)

    bind_group_descriptor

    fn bind_group_descriptor(layout : BindGroupLayout, entries : Array[BindGroupEntry], label : String?) -> BindGroupDescriptor

    bind_group_entry

    fn bind_group_entry(binding : Int, resource : BindingResource) -> BindGroupEntry

    bind_group_layout_descriptor

    fn bind_group_layout_descriptor(entries : Array[BindGroupLayoutEntry], label : String?) -> BindGroupLayoutDescriptor

    bind_group_layout_entry

    fn bind_group_layout_entry(binding : Int, visibility : Int, ty : BindingType) -> BindGroupLayoutEntry

    binding_resource_buffer

    fn binding_resource_buffer(buffer : Buffer, offset : Int, size : Int) -> BindingResource

    binding_resource_sampler

    fn binding_resource_sampler(sampler : Sampler) -> BindingResource

    binding_resource_texture_view

    fn binding_resource_texture_view(view : TextureView) -> BindingResource

    binding_type_read_only_storage_buffer

    let binding_type_read_only_storage_buffer : BindingType

    binding_type_sampler

    let binding_type_sampler : BindingType

    binding_type_storage_buffer

    let binding_type_storage_buffer : BindingType

    binding_type_texture

    let binding_type_texture : BindingType

    binding_type_uniform_buffer

    let binding_type_uniform_buffer : BindingType

    buffer_descriptor

    fn buffer_descriptor(size : Int, usage : Int, mapped_at_creation : Bool, label : String?) -> BufferDescriptor

    buffer_usage_copy_dst

    let buffer_usage_copy_dst : Int

    buffer_usage_copy_src

    let buffer_usage_copy_src : Int

    buffer_usage_index

    let buffer_usage_index : Int

    buffer_usage_indirect

    let buffer_usage_indirect : Int

    buffer_usage_map_read

    let buffer_usage_map_read : Int

    buffer_usage_map_write

    let buffer_usage_map_write : Int

    buffer_usage_or

    fn buffer_usage_or(a : Int, b : Int) -> Int

    buffer_usage_query_resolve

    let buffer_usage_query_resolve : Int

    buffer_usage_storage

    let buffer_usage_storage : Int

    buffer_usage_uniform

    let buffer_usage_uniform : Int

    buffer_usage_vertex

    let buffer_usage_vertex : Int

    command_encoder_finish

    fn command_encoder_finish(_encoder : CommandEncoder) -> Result[CommandBuffer, WgpuError]

    compute_pipeline_descriptor

    fn compute_pipeline_descriptor(layout : PipelineLayout, compute : ProgrammableStage, label : String?) -> ComputePipelineDescriptor

    device_create_bind_group

    fn device_create_bind_group(device : Device, desc : BindGroupDescriptor) -> Result[BindGroup, WgpuError]

    device_create_bind_group_layout

    fn device_create_bind_group_layout(device : Device, desc : BindGroupLayoutDescriptor) -> Result[BindGroupLayout, WgpuError]

    device_create_buffer

    fn device_create_buffer(device : Device, desc : BufferDescriptor) -> Result[Buffer, WgpuError]

    device_create_command_encoder

    fn device_create_command_encoder(_device : Device, _desc : CommandEncoderDescriptor) -> Result[CommandEncoder, WgpuError]

    device_create_compute_pipeline

    fn device_create_compute_pipeline(device : Device, desc : ComputePipelineDescriptor) -> Result[ComputePipeline, WgpuError]

    device_create_pipeline_layout

    fn device_create_pipeline_layout(device : Device, desc : PipelineLayoutDescriptor) -> Result[PipelineLayout, WgpuError]

    device_create_render_pipeline

    fn device_create_render_pipeline(_device : Device, _desc : RenderPipelineDescriptor) -> Result[RenderPipeline, WgpuError]

    device_create_sampler

    fn device_create_sampler(_device : Device, _desc : SamplerDescriptor) -> Result[Sampler, WgpuError]

    device_create_shader_module

    fn device_create_shader_module(device : Device, desc : ShaderModuleDescriptor) -> Result[ShaderModule, WgpuError]

    device_create_texture

    fn device_create_texture(_device : Device, _desc : TextureDescriptor) -> Result[Texture, WgpuError]

    device_descriptor_default

    fn device_descriptor_default() -> DeviceDescriptor

    device_dispatch_compute

    fn device_dispatch_compute(device : Device, pipeline : ComputePipeline, bind_group : BindGroup, dispatch_x : Int) -> Result[Unit, WgpuError]

    device_get_queue

    fn device_get_queue(device : Device) -> Result[Queue, WgpuError]

    device_poll

    fn device_poll(device : Device, wait : Bool) -> Unit

    device_read_buffer_bytes

    fn device_read_buffer_bytes(device : Device, buffer : Buffer, size : Int) -> Result[Bytes, WgpuError]

    dispatch_batch_add

    fn dispatch_batch_add(pipeline : ComputePipeline, bind_group : BindGroup, dispatch_x : Int) -> Unit

    dispatch_batch_begin

    fn dispatch_batch_begin() -> Unit

    dispatch_batch_submit

    fn dispatch_batch_submit(device : Device) -> Unit

    error_not_implemented

    fn error_not_implemented() -> WgpuError

    error_not_supported

    fn error_not_supported() -> WgpuError

    error_validation

    fn error_validation(message : String) -> WgpuError

    extent_2d

    fn extent_2d(width : Int, height : Int) -> Extent3d

    feature_other

    fn feature_other(name : String) -> Feature

    filter_mode_linear

    let filter_mode_linear : FilterMode

    filter_mode_nearest

    let filter_mode_nearest : FilterMode

    is_supported

    fn is_supported() -> Bool

    limits_default

    fn limits_default() -> Limits

    pipeline_layout_descriptor

    fn pipeline_layout_descriptor(bind_group_layouts : Array[BindGroupLayout], label : String?) -> PipelineLayoutDescriptor

    power_preference_default

    let power_preference_default : PowerPreference

    power_preference_high_performance

    let power_preference_high_performance : PowerPreference

    power_preference_low_power

    let power_preference_low_power : PowerPreference

    primitive_topology_triangle_list

    let primitive_topology_triangle_list : PrimitiveTopology

    programmable_stage

    fn programmable_stage(shader_module : ShaderModule, entry_point : String) -> ProgrammableStage

    queue_submit

    fn queue_submit(_queue : Queue, _commands : Array[CommandBuffer]) -> Result[Unit, WgpuError]

    queue_write_buffer

    fn queue_write_buffer(queue : Queue, buffer : Buffer, offset : Int, data : Array[Byte]) -> Result[Unit, WgpuError]

    request_adapter

    async fn request_adapter(options : RequestAdapterOptions) -> Result[Adapter, WgpuError]

    request_adapter_options_default

    fn request_adapter_options_default() -> RequestAdapterOptions

    request_device

    async fn request_device(adapter : Adapter, desc : DeviceDescriptor) -> Result[Device, WgpuError]

    shader_module_descriptor

    fn shader_module_descriptor(code : String, label : String?) -> ShaderModuleDescriptor

    shader_stage_compute

    let shader_stage_compute : Int

    shader_stage_fragment

    let shader_stage_fragment : Int

    shader_stage_or

    fn shader_stage_or(a : Int, b : Int) -> Int

    shader_stage_vertex

    let shader_stage_vertex : Int

    texture_create_view

    fn texture_create_view(_texture : Texture) -> Result[TextureView, WgpuError]

    texture_dimension_2d

    let texture_dimension_2d : TextureDimension

    texture_format_bgra8_unorm

    let texture_format_bgra8_unorm : TextureFormat

    texture_format_rgba8_unorm

    let texture_format_rgba8_unorm : TextureFormat

    texture_usage_copy_dst

    let texture_usage_copy_dst : Int

    texture_usage_copy_src

    let texture_usage_copy_src : Int

    texture_usage_or

    fn texture_usage_or(a : Int, b : Int) -> Int

    texture_usage_render_attachment

    let texture_usage_render_attachment : Int

    texture_usage_storage_binding

    let texture_usage_storage_binding : Int

    texture_usage_texture_binding

    let texture_usage_texture_binding : Int