Code Monkey home page Code Monkey logo

swarms-pytorch's Introduction

Multi-Modality

Swarms in Torch

Swarms in Torch exclusivley hosts a vast array of 100% novel swarming models. Our purpose for this repo is to create, optimize, and train novel foundation models that outperform the status quo of model architectures such as the Transformer and SSM model architectures. We provide implementations of various novel models like PSO with transformers as particles, ant colony with transformers as ants, a basic NN with transformers as neurons, Mixture of Mambas and many more. If you would like to help contribute to the future of AI model architecture's please join Agora, the open source lab here. And, if you have any idea's please submit them as issues and notify me.

Installation

pip3 install swarms-torch

Usage

ParticleSwarmOptimization

from swarms_torch import ParticleSwarmOptimization


pso = ParticleSwarmOptimization(goal="Attention is all you need", n_particles=100)

pso.optimize(iterations=1000)

Ant Colony Optimization

from swarms_torch.ant_colony_swarm import AntColonyOptimization

# Usage:
goal_string = "Hello ACO"
aco = AntColonyOptimization(goal_string, num_iterations=1000)
best_solution = aco.optimize()
print("Best Matched String:", best_solution)

Neural Network with Transformers as synapases

import torch
from swarms_torch.nnt import NNTransformer

x = torch.randn(1, 10)

network = NNTransformer(
    neuron_count = 5, 
    num_states = 10,
    input_dim = 10,
    output_dim = 10,
    nhead = 2,
)
output = network(x)
print(output)

CellularSwarm

a Cellular Neural Net with transformers as cells, time simulation, and a local neighboorhood!

from swarms_torch import CellularSwarm 

x = torch.randn(10, 32, 512)  # sequence length of 10, batch size of 32, embedding size of 512
model = CellularSwarm(cell_count=5, input_dim=512, nhead=8)
output = model(x)

Fish School/Sakana

  • An all-new innovative approaches to machine learning that leverage the power of the Transformer model architecture. These systems are designed to mimic the behavior of a school of fish, where each fish represents an individual Transformer model. The goal is to optimize the performance of the entire school by learning from the best-performing fish.
import torch
from swarms_torch.fish_school import Fish, FishSchool

# Create random source and target sequences
src = torch.randn(10, 32, 512)
tgt = torch.randn(10, 32, 512)

# Create random labels
labels = torch.randint(0, 512, (10, 32))

# Create a fish and train it on the random data
fish = Fish(512, 8, 6)
fish.train(src, tgt, labels)
print(fish.food)  # Print the fish's food

# Create a fish school and optimize it on the random data
school = FishSchool(10, 512, 8, 6, 100)
school.forward(src, tgt, labels)
print(school.fish[0].food)  # Print the first fish's food

Swarmalators

from swarms_torch import visualize_swarmalators, simulate_swarmalators

# Init for Swarmalator
# Example usage:
N = 100
J, alpha, beta, gamma, epsilon_a, epsilon_r, R = [0.1] * 7
D = 3  # Ensure D is an integer
xi, sigma_i = simulate_swarmalators(
    N, J, alpha, beta, gamma, epsilon_a, epsilon_r, R, D
)


# Call the visualization function
visualize_swarmalators(xi)

Mixture of Mambas

  • An 100% novel implementation of a swarm of MixtureOfMambas.
  • Various fusion methods through averages, weighted_aggegrate, and more to come like a gating mechanism or other various methods.
  • fusion methods: average, weighted, absmax, weighted_softmax, or your own custom function
import torch
from swarms_torch import MixtureOfMambas

# Create a 3D tensor for text
x = torch.rand(1, 512, 512)

# Create an instance of the MixtureOfMambas model
model = MixtureOfMambas(
    num_mambas=2,            # Number of Mambas in the model
    dim=512,                 # Dimension of the input tensor
    d_state=1024,            # Dimension of the hidden state
    depth=4,                 # Number of layers in the model
    d_conv=1024,             # Dimension of the convolutional layers
    expand=4,                # Expansion factor for the model
    fusion_method="absmax",  # Fusion method for combining Mambas' outputs
    custom_fusion_func=None  # Custom fusion function (if any)
)

# Pass the input tensor through the model and print the output shape
print(model(x).shape)

SwitchMoE

import torch 
from swarms_torch import SwitchMoE

# Example usage:
input_dim = 768  # Dimension of input tokens
hidden_dim = 2048  # Hidden dimension of experts
output_dim = 768  # Output dimension, should match input dimension for residual connection
num_experts = 16  # Number of experts

moe_layer = SwitchMoE(
    dim=input_dim,
    hidden_dim=hidden_dim,
    output_dim=output_dim,
    num_experts=num_experts,
    use_aux_loss=False,
)

# Create a sample input tensor (batch_size, seq_len, input_dim)
x = torch.rand(32, 128, input_dim)

# Forward pass through the MoE layer with auxiliary loss computation
output, auxiliary_loss = moe_layer(x)

# Now, 'output' contains the MoE output, and 'auxiliary_loss' contains the load balancing loss.
# This auxiliary loss should be added to the main loss function during training.

print(output)
print(auxiliary_loss)

SimpleMoE

A very simple Mixture of Experts with softmax as a gating mechanism.

import torch 
from swarms_torch import SimpleMoE

# Example usage:
input_dim = 512  # Dimension of input tokens
hidden_dim = 1024  # Hidden dimension of experts
output_dim = 512  # Output dimension, should match input dimension for residual connection
num_experts = 4  # Number of experts

moe = SimpleMoE(input_dim, hidden_dim, output_dim, num_experts)

# Create a sample input tensor (batch_size, seq_len, input_dim)
x = torch.rand(10, 16, input_dim)

# Forward pass through the MoE layer
output = moe(x)
print(output)

Documentation

Examples

  • There are various scripts in the playground folder with various examples for each swarm, like ant colony and fish school and spiral optimization.

๐Ÿซถ Contributions:

The easiest way to contribute is to pick any issue with the good first issue tag ๐Ÿ’ช. Read the Contributing guidelines here. Bug Report? File here | Feature Request? File here

Swarms is an open-source project, and contributions are VERY welcome. If you want to contribute, you can create new features, fix bugs, or improve the infrastructure. Please refer to the CONTRIBUTING.md and our contributing board to participate in Roadmap discussions!


Community

Join our growing community around the world, for real-time support, ideas, and discussions on Swarms ๐Ÿ˜Š


Discovery Call

Book a discovery call to learn how Swarms can lower your operating costs by 40% with swarms of autonomous agents in lightspeed. Click here to book a time that works for you!

Accelerate Backlog

Help us accelerate our backlog by supporting us financially! Note, we're an open source corporation and so all the revenue we generate is through donations at the moment ;)

License

MIT

swarms-pytorch's People

Contributors

dependabot[bot] avatar kyegomez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

swarms-pytorch's Issues

[BUG] build issue in container - requires GPU

Dockerfile:

FROM python:3.9-slim
RUN apt-get update && apt-get -y install libgl1-mesa-dev libglib2.0-0 build-essential; apt-get clean
RUN pip install opencv-contrib-python-headless

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

WORKDIR /usr/src/swarm_cloud

COPY requirements.txt .
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Install the 'swarms' package, assuming it's available on PyPI
RUN pip install swarms

# Copy the rest of the application
COPY . .

The error is related to mamba-ssm:

RUN pip install --no-cache-dir -r requirements.txt:                    
0.763 Collecting torch (from -r requirements.txt (line 1))                      
0.847   Downloading torch-2.1.2-cp39-cp39-manylinux1_x86_64.whl.metadata (25 kB)
0.877 Collecting einops (from -r requirements.txt (line 2))                     
0.884   Downloading einops-0.7.0-py3-none-any.whl.metadata (13 kB)              
1.117 Collecting pandas (from -r requirements.txt (line 3))
1.126   Downloading pandas-2.1.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (18 kB)
1.199 Collecting zetascale (from -r requirements.txt (line 4))
1.276   Downloading zetascale-1.2.1-py3-none-any.whl.metadata (15 kB)
1.297 Collecting mamba-ssm (from -r requirements.txt (line 5))
1.304   Downloading mamba_ssm-1.1.0.tar.gz (34 kB)
1.318   Preparing metadata (setup.py): started
1.618   Preparing metadata (setup.py): finished with status 'error'
1.634   error: subprocess-exited-with-error
1.634   
1.634   ร— python setup.py egg_info did not run successfully.
1.634   โ”‚ exit code: 1
1.634   โ•ฐโ”€> [6 lines of output]
1.634       Traceback (most recent call last):
1.634         File "<string>", line 2, in <module>
1.634         File "<pip-setuptools-caller>", line 34, in <module>
1.634         File "/tmp/pip-install-2fselg6h/mamba-ssm_3266d6b1d5bd4ecfb3810be1763d407b/setup.py", line 8, in <module>
1.634           from packaging.version import parse, Version
1.634       ModuleNotFoundError: No module named 'packaging'
1.634       [end of output]
1.634   
1.634   note: This error originates from a subprocess, and is likely not a problem with pip.
1.639 error: metadata-generation-failed
1.639 
1.639 ร— Encountered error while generating package metadata.
1.639 โ•ฐโ”€> See above for output.
1.639 
1.639 note: This is an issue with the package mentioned above, not pip.
1.639 hint: See above for details.
------
Dockerfile:20
--------------------
  18 |     COPY requirements.txt .
  19 |     RUN pip install --upgrade pip
  20 | >>> RUN pip install --no-cache-dir -r requirements.txt
  21 |     
  22 |     # Install the 'swarms' package, assuming it's available on PyPI
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install --no-cache-dir -r requirements.txt" did not complete successfully: exit code: 1

I believe this is related to this mamba-ssm issue:
state-spaces/mamba#55
which requires nvcc to compile, so can't be compiled on a CPU.

T

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Confirm you do not plan to stop spamming people.

Hi @kyegomez, you closed kyegomez/swarms#62 about you spamming people as "not planned". Can you confirm that you do not intent to take any action?

Please take a look at:

It would be great if you stopped.

spammer

Please stop spamming me, and others. Also see kyegomez/swarms#40

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

[BUGF] missing import numexpr

Shows up in a failing test:

_______________ ERROR collecting tests/test_mixture_of_mamba.py ________________
ImportError while importing test module '/usr/src/swarm_cloud/tests/test_mixture_of_mamba.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_mixture_of_mamba.py:3: in <module>
    from swarms_torch.mixture_of_mamba import MixtureOfMambas
/usr/local/lib/python3.9/site-packages/swarms_torch/__init__.py:4: in <module>
    from swarms_torch.hivemind_swarm_transformer import HivemindSwarm
/usr/local/lib/python3.9/site-packages/swarms_torch/hivemind_swarm_transformer.py:3: in <module>
    from zeta.structs.transformer import (
/usr/local/lib/python3.9/site-packages/zeta/__init__.py:1: in <module>
    from zeta.utils.disable_logging import disable_warnings_and_logs
/usr/local/lib/python3.9/site-packages/zeta/utils/__init__.py:10: in <module>
    from zeta.utils.disable_logging import disable_warnings_and_logs
/usr/local/lib/python3.9/site-packages/zeta/utils/disable_logging.py:5: in <module>
    import numexpr as ne
E   ModuleNotFoundError: No module named 'numexpr'

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.