Run this model with HF Jobs
Run replicate/megablocks on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/replicate/megablocks. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/replicate/megablocks on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use replicate/megablocks with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/megablocks with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/megablocks", version=1) - Notebooks
- Google Colab
- Kaggle
drbh commited on
Commit ·
484fde0
1
Parent(s): 6756875
fix: prefer relative imports
Browse filesFiles changed (31)
- build.toml-1
- torch-ext/megablocks/grouped_gemm/backend.py+2-1
- torch-ext/megablocks/layers/arguments.py+2-1
- torch-ext/megablocks/layers/common.py+1-1
- torch-ext/megablocks/layers/dmlp_registry.py+2-2
- torch-ext/megablocks/layers/dmoe.py+8-5
- torch-ext/megablocks/layers/glu.py+16-5
- torch-ext/megablocks/layers/memory_test.py+2-1
- torch-ext/megablocks/layers/mlp.py+9-5
- torch-ext/megablocks/layers/moe.py+50-18
- torch-ext/megablocks/layers/mpu.py+2-1
- torch-ext/megablocks/layers/router.py+4-2
- torch-ext/megablocks/layers/sharedexpert_registry.py+4-2
- torch-ext/megablocks/ops/__init__.py+14-14
- torch-ext/megablocks/ops/all_to_all_benchmark.py+5-2
- torch-ext/megablocks/ops/binned_gather.py+1-1
- torch-ext/megablocks/ops/binned_scatter.py+1-1
- torch-ext/megablocks/ops/cumsum.py+1-1
- torch-ext/megablocks/ops/gather.py+1-1
- torch-ext/megablocks/ops/histogram.py+1-1
- torch-ext/megablocks/ops/histogram_benchmark.py+1-1
- torch-ext/megablocks/ops/matmul_benchmark.py+1-1
- torch-ext/megablocks/ops/padded_gather.py+1-1
- torch-ext/megablocks/ops/padded_scatter.py+1-1
- torch-ext/megablocks/ops/padded_scatter_benchmark.py+1-1
- torch-ext/megablocks/ops/permute_benchmark.py+1-1
- torch-ext/megablocks/ops/replicate.py+1-1
- torch-ext/megablocks/ops/scatter.py+1-1
- torch-ext/megablocks/ops/sort.py+1-1
- torch-ext/megablocks/ops/sort_benchmark.py+1-1
- torch-ext/megablocks/ops/topology.py+1-1
22 unmodified lines2324252626272822 unmodified lines "10.1", "11.8", "12.0" # "12.4"]depends = ["torch", "cutlass_3_8"]src = [9 unmodified lines1011121313141516179 unmodified lines# We import the backend operations from the megablocks package as# grouped_gemm is vendored in megablocks in this repository.# from ... import _ops as backendfrom megablocks._ops import ops as backend # type: ignore# from megablocks._ops import ops as backend # type: ignorefrom .._ops import ops as backend # type: ignore
def _allocate_output(a, b, batch_sizes, trans_a, trans_b): assert not (trans_a and trans_b)8 unmodified lines910111212131415168 unmodified linesimport torch.distributed as distimport torch.nn.functional as F
import megablocks.grouped_gemm_util as grouped_gemm# import megablocks.grouped_gemm_util as grouped_gemmfrom .. import grouped_gemm_util as grouped_gemm
# Type annotation for in-place Tensor initialization function.InitFn = Union[Callable[[torch.Tensor], None], partial[torch.Tensor]]2 unmodified lines345667892 unmodified lines
import torch
from megablocks.layers.arguments import Argumentsfrom .arguments import Arguments
def dtype(args: Arguments):2 unmodified lines345676789102 unmodified lines
from typing import Union
from megablocks.layers import glu, mlpfrom megablocks.layers.arguments import Argumentsfrom . import glu, mlpfrom .arguments import Arguments
MlpType = Union[mlp.SparseMLP, glu.SparseGLU]
5 unmodified lines6789101112139101112131415161718195 unmodified linesimport torchfrom stk import Matrix
import megablocks.ops as ops# from megablocks.ops import opsfrom megablocks.layers import common, dmlp_registry, moe, mpufrom megablocks.layers.arguments import Arguments
# import megablocks.ops as ops# # from megablocks.ops import ops# from megablocks.layers import common, dmlp_registry, moe, mpu# from megablocks.layers.arguments import Arguments
from .. import opsfrom . import common, dmlp_registry, moe, mpufrom .arguments import Arguments
def promote_scalar(x): return x.view(1) if not len(x.size()) else x3 unmodified lines4567891011789101112131415161718192021222324253 unmodified linesimport stk.opsimport torch
from megablocks import grouped_gemm_util as ggfrom megablocks.layers import common, mpufrom megablocks.layers.activation_fn import act_fnfrom megablocks.layers.arguments import Argumentsfrom megablocks.layers.mlp import (# from megablocks import grouped_gemm_util as gg# from megablocks.layers import common, mpu# from megablocks.layers.activation_fn import act_fn# from megablocks.layers.arguments import Arguments# from megablocks.layers.mlp import (# SharedMLP,# SparseMLP,# create_dmoe_expert_weights,# resolve_dtensor,# )
from .. import grouped_gemm_util as ggfrom . import common, mpufrom .activation_fn import act_fnfrom .arguments import Argumentsfrom .mlp import ( SharedMLP, SparseMLP, create_dmoe_expert_weights,5 unmodified lines67899101112135 unmodified linesimport torchimport torch.distributed as dist
from megablocks.layers import arguments, dmoe# from megablocks.layers import arguments, dmoefrom . import arguments, dmoe
_TESTS = ((8, 2048, 4096, 4096, 32, 4),)
8 unmodified lines9101112131415161213141516171819202122238 unmodified linesimport torchfrom packaging import version
from megablocks import grouped_gemm_util as ggfrom megablocks.layers import common, gelu, mpufrom megablocks.layers.activation_fn import act_fnfrom megablocks.layers.arguments import DEFAULT_ACTIVATION_FN, Arguments, InitFn
# from megablocks import grouped_gemm_util as gg# from megablocks.layers import common, gelu, mpu# from megablocks.layers.activation_fn import act_fn# from megablocks.layers.arguments import DEFAULT_ACTIVATION_FN, Arguments, InitFn
from .. import grouped_gemm_util as ggfrom . import common, gelu, mpufrom .activation_fn import act_fnfrom .arguments import DEFAULT_ACTIVATION_FN, Arguments, InitFn
class ScaleGradient(torch.autograd.Function):
5 unmodified lines678910111291011121314151617181920212223242526272829303132142 unmodified lines1751761771611781791801811823 unmodified lines18618718817118919019119217419319419519619717 unmodified lines2152162171982182192202212222 unmodified lines22522622720722822923023123223353 unmodified lines28728828926729029129229329414 unmodified lines30931031128831231331431531625 unmodified lines34234334432034534634734834912 unmodified lines3623633643393653663673683698 unmodified lines3783793803543813823833843853863873883613893903913923933 unmodified lines39739839937140040140240340441 unmodified lines4464474484194494504514524224534544554564575 unmodified linesimport torchimport torch.distributed as dist
import megablocks.ops as opsfrom megablocks.layers import common, mlp, mpu, router, sharedexpert_registryfrom megablocks.layers.all_to_all import all_to_allfrom megablocks.layers.arguments import Arguments# import megablocks.ops as ops# from megablocks.layers import common, mlp, mpu, router, sharedexpert_registry# from megablocks.layers.all_to_all import all_to_all# from megablocks.layers.arguments import Arguments
from ..ops import ( sort, histogram, inclusive_cumsum, exclusive_cumsum, binned_gather, binned_scatter, gather, scatter, repeat, replicate,)
from . import common, mlp, mpu, router, sharedexpert_registryfrom .arguments import Argumentsfrom .all_to_all import all_to_all
_LOAD_BALANCING_LOSS = []
142 unmodified lines # prior? Could we place the `torch.max` operation to return # 32-bit expert indices? top_expert = top_expert.int() output = ops.sort(top_expert, self.sort_end_bit) # output = ops.sort(top_expert, self.sort_end_bit) output = sort(top_expert, self.sort_end_bit) assert output is not None bin_ids, indices = output
3 unmodified lines # TODO(tgale): Does the sorted data produce a more favorable # data distribution for histogram? Or is the op parallelism # worth more? tokens_per_expert = ops.histogram(top_expert, self.num_experts) # tokens_per_expert = ops.histogram(top_expert, self.num_experts) tokens_per_expert = histogram(top_expert, self.num_experts)
# Calculate the bin bounds for the sorted tokens. bins = ops.inclusive_cumsum(tokens_per_expert, 0) # bins = ops.inclusive_cumsum(tokens_per_expert, 0) bins = inclusive_cumsum(tokens_per_expert, 0) assert bins is not None bins = bins.view(1) if not len(bins.size()) else bins
17 unmodified lines ): # Route the tokens for MoE computation. x = x.view(-1, x.shape[-1]) output = ops.binned_gather(x, indices, bins, expert_capacity, top_k) # output = ops.binned_gather(x, indices, bins, expert_capacity, top_k) output = binned_gather(x, indices, bins, expert_capacity, top_k) assert output is not None x = output
2 unmodified lines x = self.mlp(x)
# Un-route the data for the MoE output. return ops.binned_scatter(x, indices, expert_weights, bins, top_k) # return ops.binned_scatter(x, indices, expert_weights, bins, top_k) return binned_scatter(x, indices, expert_weights, bins, top_k)
def forward_once(self, x: torch.Tensor, expert_weights: torch.Tensor, top_experts: torch.Tensor): # x: [sl, bs, hs]53 unmodified lines # If we're sharding the experts along the hidden dimension # multiple devices own parts of the same sets of experts. # Replicate the token counts so every device gets the counts. repeated_tokens_per_expert = ops.repeat( # repeated_tokens_per_expert = ops.repeat( repeated_tokens_per_expert = repeat( tokens_per_expert, (mpu.hidden_sharding_degree(self.args),), )14 unmodified lines # This view updates the shape of the tensor from [sl, bs, hs] to # [sl * bs, hs] prior to the permutation. x = x.view(-1, x.shape[-1]) output = ops.gather(x, indices, bin_ids, bins, self.top_k) # output = ops.gather(x, indices, bin_ids, bins, self.top_k) output = gather(x, indices, bin_ids, bins, self.top_k) assert output is not None x = output
25 unmodified lines # get all of the tokens assigned to them. # # TODO(tgale): Fuse this into the prior, local permutation. x = ops.repeat(x, (mpu.hidden_sharding_degree(self.args), 1)) # x = ops.repeat(x, (mpu.hidden_sharding_degree(self.args), 1)) x = repeat(x, (mpu.hidden_sharding_degree(self.args), 1))
# Start the cross-device permutation asynchronously so we can # overlap communication with computation.12 unmodified lines # for expert computation we'll do one more local permutation. The # rest of this torch.no_grad() scope sets up the indices and bins # for this permutation. replicate_bins = ops.inclusive_cumsum( # replicate_bins = ops.inclusive_cumsum( replicate_bins = inclusive_cumsum( parallel_tokens_per_expert.flatten(), 0, )8 unmodified lines ), mpu.experts_per_rank(self.args), ) parallel_top_expert = ops.replicate( # parallel_top_expert = ops.replicate( parallel_top_expert = replicate( parallel_top_expert.unsqueeze(dim=0), replicate_bins, tokens_received, ).flatten()
# TODO(tgale): The sort_end_bit here can be reduced. parallel_bin_ids, parallel_indices = ops.sort( # parallel_bin_ids, parallel_indices = ops.sort( parallel_bin_ids, parallel_indices = sort( parallel_top_expert, self.sort_end_bit, )3 unmodified lines dim=0, dtype=torch.int, ) parallel_bins = ops.inclusive_cumsum(parallel_tokens_per_expert, 0) # parallel_bins = ops.inclusive_cumsum(parallel_tokens_per_expert, 0) parallel_bins = inclusive_cumsum(parallel_tokens_per_expert, 0) parallel_bins = (parallel_bins.view(1) if not len(parallel_bins.size()) else parallel_bins)
# If expert_capacity is set to zero, set the number of tokens41 unmodified lines -1, self.args.hidden_size, ) x = ops.sum(x.view(shape), dim=0) # x = ops.sum(x.view(shape), dim=0) x = x.view(shape).sum(dim=0)
# Un-permute locally to setup for the next series of operations. x = ops.scatter(x, indices, bin_ids, expert_weights, bins, self.top_k) # x = ops.scatter(x, indices, bin_ids, expert_weights, bins, self.top_k) x = scatter(x, indices, bin_ids, expert_weights, bins, self.top_k) return x, tokens_per_expert.flatten()
def forward(self, x: torch.Tensor, scores: torch.Tensor, expert_weights: torch.Tensor, top_experts: torch.Tensor):5 unmodified lines67899101112135 unmodified linesimport torchimport torch.distributed as dist
from megablocks.layers.arguments import Arguments# from megablocks.layers.arguments import Argumentsfrom .arguments import Arguments
class MoeParam(torch.Tensor):3 unmodified lines45678789101112133 unmodified lines
import torch
from megablocks.layers import commonfrom megablocks.layers.arguments import Arguments# from megablocks.layers import common# from megablocks.layers.arguments import Argumentsfrom . import commonfrom .arguments import Arguments
_ROUTER_LOGITS = []
2 unmodified lines3456767891011122 unmodified lines
from typing import Union
from megablocks.layers import glu, mlpfrom megablocks.layers.arguments import Arguments# from megablocks.layers import glu, mlp# from megablocks.layers.arguments import Argumentsfrom . import glu, mlpfrom .arguments import Arguments
_REGISTRY = { 'mlp': mlp.SharedMLP,12345678910111213141516174567891011121314151617181920# Copyright 2024 Databricks# SPDX-License-Identifier: Apache-2.0
from megablocks.ops.binned_gather import binned_gatherfrom megablocks.ops.binned_scatter import binned_scatterfrom megablocks.ops.cumsum import exclusive_cumsum, inclusive_cumsumfrom megablocks.ops.gather import gatherfrom megablocks.ops.histogram import histogramfrom megablocks.ops.padded_gather import padded_gatherfrom megablocks.ops.padded_scatter import padded_scatterfrom megablocks.ops.repeat import repeatfrom megablocks.ops.replicate import replicatefrom megablocks.ops.round_up import round_upfrom megablocks.ops.scatter import scatterfrom megablocks.ops.sort import sortfrom megablocks.ops.sum import sumfrom megablocks.ops.topology import topologyfrom .binned_gather import binned_gatherfrom .binned_scatter import binned_scatterfrom .cumsum import exclusive_cumsum, inclusive_cumsumfrom .gather import gatherfrom .histogram import histogramfrom .padded_gather import padded_gatherfrom .padded_scatter import padded_scatterfrom .repeat import repeatfrom .replicate import replicatefrom .round_up import round_upfrom .scatter import scatterfrom .sort import sortfrom .sum import sumfrom .topology import topology
__all__ = [ 'binned_gather',3 unmodified lines4567878910111213143 unmodified linesimport torchimport torch.distributed as dist
from megablocks import benchmark_utilfrom megablocks.layers.all_to_all import all_to_all# from megablocks import benchmark_util# from megablocks.layers.all_to_all import all_to_all
from .. import benchmark_utilfrom ..layers.all_to_all import all_to_all
_ALL_TO_ALL_BENCHMARK = ( (8, 1024),4 unmodified lines56788910114 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for binned_gather kernel.4 unmodified lines56788910114 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for binned_scatter kernel.10 unmodified lines111213141415161710 unmodified lines# instructions for building the c++ operations.try: # import megablocks_ops as ops # type: ignore from megablocks._ops import ops # type: ignore from .._ops import ops # type: ignoreexcept ModuleNotFoundError as e: raise ModuleNotFoundError("No module named 'megablocks_ops'.") from e
4 unmodified lines56788910114 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for gather kernel.9 unmodified lines10111213131415169 unmodified lines# Wrap this in a try-block with better error message and# instructions for building the c++ operations.try: from megablocks._ops import ops # type: ignore from .._ops import ops # type: ignoreexcept ModuleNotFoundError as e: raise ModuleNotFoundError("No module named 'megablocks_ops'.") from e
6 unmodified lines78910101112136 unmodified linesimport torchfrom absl.testing import parameterized
from megablocks import opsfrom .. import ops
_HISTOGRAM_TESTS = ( (16384, torch.int32, 2),6 unmodified lines78910101112136 unmodified linesimport torchfrom absl.testing import parameterized
from megablocks import benchmark_util, opsfrom .. import benchmark_util, ops
# Calling tensor.t() calls tensor.transpose(0, 1) which calls4 unmodified lines56788910114 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for padded_gather kernel.4 unmodified lines56788910114 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for padded_scatter kernel.5 unmodified lines678991011125 unmodified linesimport torchfrom absl.testing import parameterized
from megablocks import benchmark_util, opsfrom .. import benchmark_util, ops
_PADDED_SCATTER_BENCHMARK = ( # dMoE-Medium, 8-way EMP.5 unmodified lines678991011125 unmodified linesimport torchfrom absl.testing import parameterized
from megablocks import benchmark_util, opsfrom .. import benchmark_util, ops
_PERMUTE_TESTS = ( (16384, 768, 2),9 unmodified lines10111213131415169 unmodified lines# Wrap this in a try-block with better error message and# instructions for building the c++ operations.try: from megablocks._ops import ops # type: ignore from .._ops import ops # type: ignoreexcept ModuleNotFoundError as e: raise ModuleNotFoundError("No module named 'megablocks_ops'.") from e
5 unmodified lines678991011125 unmodified linesimport torchfrom stk.backend.autocast import custom_bwd, custom_fwd
from megablocks.backend import kernelsfrom ..backend import kernels
# Autograd wrapper for scatter kernel.9 unmodified lines10111213131415169 unmodified lines# Wrap this in a try-block with better error message and# instructions for building the c++ operations.try: from megablocks._ops import ops # type: ignore from .._ops import ops # type: ignoreexcept ModuleNotFoundError as e: raise ModuleNotFoundError("No module named 'megablocks_ops'.") from e
6 unmodified lines78910101112136 unmodified linesimport torchfrom absl.testing import parameterized
from megablocks import opsfrom .. import ops
_SORT_TESTS = ( (16384, torch.int32, None),9 unmodified lines10111213131415169 unmodified lines# Wrap this in a try-block with better error message and# instructions for building the c++ operations.try: from megablocks._ops import ops # type: ignore from .._ops import ops # type: ignoreexcept ModuleNotFoundError as e: raise ModuleNotFoundError("No module named 'megablocks_ops'.") from e
build.toml-1
torch-ext/megablocks/grouped_gemm/backend.py+2-1
torch-ext/megablocks/layers/arguments.py+2-1
torch-ext/megablocks/layers/common.py+1-1
torch-ext/megablocks/layers/dmlp_registry.py+2-2
torch-ext/megablocks/layers/dmoe.py+8-5
torch-ext/megablocks/layers/glu.py+16-5
torch-ext/megablocks/layers/memory_test.py+2-1
torch-ext/megablocks/layers/mlp.py+9-5
torch-ext/megablocks/layers/moe.py+50-18
torch-ext/megablocks/layers/mpu.py+2-1
torch-ext/megablocks/layers/router.py+4-2
torch-ext/megablocks/layers/sharedexpert_registry.py+4-2
torch-ext/megablocks/ops/__init__.py+14-14
torch-ext/megablocks/ops/all_to_all_benchmark.py+5-2
torch-ext/megablocks/ops/binned_gather.py+1-1
torch-ext/megablocks/ops/binned_scatter.py+1-1
torch-ext/megablocks/ops/cumsum.py+1-1
torch-ext/megablocks/ops/gather.py+1-1
torch-ext/megablocks/ops/histogram.py+1-1
torch-ext/megablocks/ops/histogram_benchmark.py+1-1
torch-ext/megablocks/ops/matmul_benchmark.py+1-1
torch-ext/megablocks/ops/padded_gather.py+1-1
torch-ext/megablocks/ops/padded_scatter.py+1-1
torch-ext/megablocks/ops/padded_scatter_benchmark.py+1-1
torch-ext/megablocks/ops/permute_benchmark.py+1-1
torch-ext/megablocks/ops/replicate.py+1-1
torch-ext/megablocks/ops/scatter.py+1-1
torch-ext/megablocks/ops/sort.py+1-1
torch-ext/megablocks/ops/sort_benchmark.py+1-1
torch-ext/megablocks/ops/topology.py+1-1