Skip to main content

tensor library with opencl and metal support

Project description

froog unit test badge num downloads badge

froog the frog
froog: fast real-time optimization of gradients
a beautifully compact tensor library
homepage | documentation | pip

froog is an easy-to-read tensor library (25k pip installs!) with OpenCL support for GPU acceleration. Inspired by pytorch, tinygrad, and micrograd.

Installation

pip install froog

Features

Example

Here's how you set up a simple multilayer perceptron for classification on MNIST. Looks pretty similar to pytorch, right?

from froog.tensor import Tensor
from froog.nn import Linear
import froog.optim as optim

class mnistMLP:
  def __init__(self):
    self.l1 = Tensor(Linear(784, 128)) # layer 1
    self.l2 = Tensor(Linear(128, 10))  # layer 2

  def forward(self, x):
    # forward pass through both layers and softmax for output probabilities
    return x.dot(self.l1).relu().dot(self.l2).logsoftmax() 

model = mnistMLP() # create model
optim = optim.SGD([model.l1, model.l2], lr=0.001) # stochastic gradient descent optimizer

GPU Support

Device management is handled transparently and will automatically select one of [METAL, OPENCL, CPU]. To use the GPU:

from froog.tensor import Tensor
from froog import get_device
# Check if GPU is available
has_gpu = get_device() is not None and get_device().name != "CPU"
# Create a tensor
x = Tensor([1, 2, 3])
# Push to GPU if available
if has_gpu: x = x.to_gpu()
# Operations run on GPU automatically
y = x + x
z = y * y
# Bring back to CPU when needed
result = z.to_cpu()
print(result.data)

You can also check what devices are available:

from froog import get_available_devices
available_devices = get_available_devices()
print(f"Available devices: {available_devices}")

Or set a specific device:

from froog import set_device
set_device("METAL")  # or "OPENCL"

EfficientNet in froog!

pug

We have an implementation of EfficientNet v2 built entirely in froog using the official PyTorch weights! Run inference with:

python3 models/efficientnet.py <https://optional_image_url>

***********output*************
inference 4.34 s

imagenet class: 254
prediction    : pug, pug-dog
probability   : 0.8902361
******************************

I would recommend checking out the code, it's highly documented and pretty cool.

Contributing

Pull requests will be merged if they:

  • increase simplicity
  • increase functionality
  • increase efficiency

More info on contributing. Make sure to run python -m pytest before creating a PR.

API

Basic Math Operations

  • .add(y) - Addition with y
  • .sub(y) - Subtraction with y
  • .mul(y) - Multiplication with y
  • .div(y) - Division by y
  • .pow(y) - Power function (raise to power y)
  • .sum() - Sum all elements
  • .mean() - Mean of all elements
  • .sqrt() - Square root

Linear Algebra Operations

  • .dot(y) - Matrix multiplication with y
  • .matmul(y) - Alias for dot

Neural Network Operations

  • .relu() - Rectified Linear Unit activation
  • .sigmoid() - Sigmoid activation
  • .dropout(p=0.5, training=True) - Dropout regularization
  • .logsoftmax() - Log softmax function
  • .swish() - Swish activation function (x * sigmoid(x))
  • .conv2d(w, stride=1, groups=1) - 2D convolution
  • .im2col2dconv(w) - Image to column for convolution

Pooling Operations

  • .max_pool2d(kernel_size=(2,2)) - 2D max pooling
  • .avg_pool2d(kernel_size=(2,2)) - 2D average pooling

Tensor Manipulation

  • .reshape(*shape) - Change tensor shape
  • .view(*shape) - Alternative to reshape
  • .pad2d(padding=None) - Pad 2D tensors
  • .flatten() - Returns a flattened 1D copy of the tensor
  • .unsqueeze(dim) - Add dimension of size 1 at specified position
  • .squeeze(dim=None) - Remove dimensions of size 1
  • .detach() - Returns a tensor detached from computation graph
  • .assign(x) - Assign values from tensor x to this tensor

Tensor Properties

  • .shape - The shape of the tensor as a tuple
  • .size - Total number of elements in the tensor
  • .ndim - Number of dimensions (rank) of the tensor
  • .transpose - Transpose of the tensor
  • .dtype - Data type of the tensor
  • .is_gpu - Whether tensor is on GPU
  • .grad - Gradient of tensor with respect to some scalar value
  • .data - Underlying NumPy array (or GPU buffer)

Device Management

  • .to_cpu() - Moves tensor to CPU
  • .to_gpu() - Moves tensor to GPU
  • .gpu_() - In-place GPU conversion (modifies tensor)

Data Type Conversion

  • .to_float() - Converts tensor to float32 data type
  • .to_int() - Converts tensor to int32 data type
  • .to_bool() - Converts tensor to boolean data type

Autograd Operations

  • .backward(allow_fill=True) - Performs backpropagation

Tensor Creation Methods

  • Tensor.zeros(*shape) - Create tensor of zeros
  • Tensor.ones(*shape) - Create tensor of ones
  • Tensor.randn(*shape) - Create tensor with random normal values
  • Tensor.eye(dim) - Create identity matrix
  • Tensor.arange(start, stop=None, step=1) - Create tensor with evenly spaced values

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

froog-0.5.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

froog-0.5.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file froog-0.5.0.tar.gz.

File metadata

  • Download URL: froog-0.5.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for froog-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e1873743750907784e1d4dca83ddf696d1f3d556b154f9dea68c71038afdd6ac
MD5 2bf6a6d1b58be6f9fb364d9d41edfcbf
BLAKE2b-256 df01cdf40bf46602336b82de082eb22b3d9fb64c16331341d6923f528641b4fe

See more details on using hashes here.

File details

Details for the file froog-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: froog-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for froog-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ffd5aa0f98c3de116c5bac542d3f9b249772e0ec191f71175cacb54da44b270d
MD5 a289209371da842704029fd551494a16
BLAKE2b-256 87c178cb4c922e6dcfb11ed3d7f74ec36d5e11538de45c5730b84870da2651a9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page