Convert Figma logo to code with AI

elixir-nx logonx

Multi-dimensional arrays (tensors) and numerical definitions for Elixir

2,583
189
2,583
21

Top Related Projects

82,049

Tensors and Dynamic neural networks in Python with strong GPU acceleration

185,446

An Open Source Machine Learning Framework for Everyone

29,761

Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

scikit-learn: machine learning in Python

27,505

The fundamental package for scientific computing with Python.

Quick Overview

Nx is a numerical computation library for Elixir, providing a high-performance, GPU-accelerated tensor computation engine. It aims to bring the power of libraries like NumPy and PyTorch to the Elixir ecosystem, enabling efficient data manipulation and machine learning tasks.

Pros

  • High Performance: Nx leverages the CUDA and ROCm backends to provide GPU acceleration, enabling efficient numerical computations.
  • Seamless Integration: Nx is designed to integrate seamlessly with the Elixir ecosystem, allowing developers to leverage Elixir's strengths in concurrent and distributed systems.
  • Extensibility: Nx is built on a modular architecture, allowing developers to extend its functionality with custom operations and data types.
  • Ease of Use: Nx provides a user-friendly API that closely resembles the syntax and functionality of popular libraries like NumPy, making it accessible to developers familiar with those tools.

Cons

  • Elixir Ecosystem Maturity: As a relatively new library, Nx may not have the same level of community support and ecosystem maturity as more established libraries in other languages.
  • GPU Dependency: Leveraging Nx's GPU acceleration requires the presence of a compatible GPU and the necessary driver/runtime installations, which may not be available in all deployment environments.
  • Learning Curve: Developers new to Elixir or numerical computing may face a steeper learning curve when adopting Nx, especially if they are not familiar with the underlying concepts and libraries.
  • Ecosystem Integration: While Nx integrates well with the Elixir ecosystem, it may require additional effort to seamlessly integrate with existing Elixir applications or libraries.

Code Examples

# Creating a tensor
tensor = Nx.tensor([[1, 2], [3, 4]])

# Performing element-wise operations
result = tensor + 2
result = tensor * tensor

# Applying a reduction operation
sum = Nx.sum(tensor)
mean = Nx.mean(tensor)

# Performing a matrix multiplication
matrix1 = Nx.tensor([[1, 2], [3, 4]])
matrix2 = Nx.tensor([[5, 6], [7, 8]])
product = Nx.dot(matrix1, matrix2)

These examples demonstrate the basic usage of Nx for creating tensors, performing element-wise operations, applying reduction operations, and computing matrix multiplications.

Getting Started

To get started with Nx, you can follow these steps:

  1. Add the nx dependency to your Elixir project's mix.exs file:
def deps do
  [
    {:nx, "~> 0.3.0"}
  ]
end
  1. Install the required dependencies by running mix deps.get.

  2. In your Elixir module, import the Nx module:

defmodule MyModule do
  import Nx
end
  1. You can now start using Nx to perform numerical computations and tensor operations. Refer to the Nx documentation for more detailed information and examples.

Competitor Comparisons

82,049

Tensors and Dynamic neural networks in Python with strong GPU acceleration

Pros of PyTorch

  • Mature ecosystem with extensive libraries and community support
  • Widely adopted in industry and academia for deep learning research
  • Dynamic computational graphs for flexible model development

Cons of PyTorch

  • Steeper learning curve for beginners
  • Higher memory usage compared to static graph frameworks
  • Primarily focused on Python, limiting language options

Code Comparison

PyTorch:

import torch

x = torch.tensor([1, 2, 3])
y = torch.tensor([4, 5, 6])
z = x + y
print(z)

Nx:

import Nx.Defn

x = Nx.tensor([1, 2, 3])
y = Nx.tensor([4, 5, 6])
z = Nx.add(x, y)
IO.inspect(z)

Both PyTorch and Nx provide tensor operations and numerical computing capabilities. PyTorch is a well-established framework for deep learning, while Nx is a newer project aimed at bringing numerical computing to Elixir. Nx offers a fresh approach with Elixir's functional programming paradigm, potentially providing better performance in certain scenarios. However, PyTorch's extensive ecosystem and widespread adoption give it an edge in terms of available resources and community support.

185,446

An Open Source Machine Learning Framework for Everyone

Pros of TensorFlow

  • Extensive ecosystem with a wide range of tools and libraries
  • Strong support for production deployment and mobile/edge devices
  • Large community and extensive documentation

Cons of TensorFlow

  • Steeper learning curve, especially for beginners
  • Can be more complex to set up and configure
  • Slower development cycle compared to more dynamic alternatives

Code Comparison

TensorFlow (Python):

import tensorflow as tf

x = tf.constant([[1., 2.], [3., 4.]])
y = tf.constant([[5., 6.], [7., 8.]])
z = tf.matmul(x, y)

Nx (Elixir):

import Nx.Defn

x = Nx.tensor([[1., 2.], [3., 4.]])
y = Nx.tensor([[5., 6.], [7., 8.]])
z = Nx.dot(x, y)

Both examples demonstrate matrix multiplication, showcasing the syntax differences between TensorFlow and Nx. TensorFlow uses a more verbose approach with explicit tensor creation and operations, while Nx offers a more concise syntax with implicit tensor creation and simpler function calls.

29,761

Composable transformations of Python+NumPy programs: differentiate, vectorize, JIT to GPU/TPU, and more

Pros of JAX

  • Mature ecosystem with extensive documentation and community support
  • Highly optimized for performance, especially on GPU and TPU hardware
  • Seamless integration with NumPy and other popular Python scientific libraries

Cons of JAX

  • Steeper learning curve, especially for those new to functional programming concepts
  • Limited support for dynamic shapes and control flow compared to some other frameworks
  • Primarily focused on Python, which may not be ideal for all use cases

Code Comparison

JAX example:

import jax.numpy as jnp
from jax import grad, jit

def f(x):
    return jnp.sum(jnp.sin(x))

grad_f = jit(grad(f))

Nx example:

import Nx, only: [sum: 1, sin: 1]

defn f(x) do
  x |> sin() |> sum()
end

grad_f = grad(f)

Key Differences

  • JAX is Python-based, while Nx is built for Elixir
  • JAX offers more advanced features and optimizations for high-performance computing
  • Nx provides a more approachable syntax for functional programming enthusiasts
  • JAX has a larger ecosystem and community, while Nx is newer and growing

Both libraries aim to provide efficient numerical computing capabilities, but they cater to different language ecosystems and user preferences.

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator

Pros of ONNX Runtime

  • Broader ecosystem support and compatibility with various ML frameworks
  • Optimized for performance across multiple hardware platforms
  • Extensive language bindings (C++, Python, C#, Java, etc.)

Cons of ONNX Runtime

  • Steeper learning curve for non-ML specialists
  • Less integrated with Elixir ecosystem and functional programming paradigms

Code Comparison

ONNX Runtime (Python):

import onnxruntime as ort
import numpy as np

session = ort.InferenceSession("model.onnx")
input_name = session.get_inputs()[0].name
output = session.run(None, {input_name: np.random.randn(1, 3, 224, 224).astype(np.float32)})

Nx (Elixir):

alias Nx.Tensor

{:ok, model} = Nx.Serving.load_model("model.onnx")
input = Nx.random_normal({1, 3, 224, 224})
output = Nx.Serving.run(model, input)

Summary

ONNX Runtime offers broader compatibility and optimized performance across platforms, while Nx provides a more Elixir-native approach to numerical computing and machine learning. ONNX Runtime has a steeper learning curve but offers more extensive language support, whereas Nx integrates seamlessly with Elixir's ecosystem and functional programming paradigms.

scikit-learn: machine learning in Python

Pros of scikit-learn

  • Mature and widely adopted machine learning library with extensive documentation
  • Comprehensive set of algorithms for classification, regression, clustering, and more
  • Large community support and extensive ecosystem of extensions

Cons of scikit-learn

  • Limited support for GPU acceleration and distributed computing
  • Primarily designed for batch processing, not optimized for online learning
  • Python-specific, which may limit integration with certain systems

Code Comparison

scikit-learn:

from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)

nx:

model = Nx.Linear.logistic_regression(features: 4)
{_params, _state} = Axon.Loop.run(model, X_train, y_train, epochs: 10)
predictions = Nx.Defn.jit(&Axon.predict(model, &1)).(X_test)

Key Differences

  • scikit-learn is Python-based, while nx is built for Elixir
  • nx focuses on numerical computing and deep learning, while scikit-learn covers a broader range of machine learning algorithms
  • nx leverages Elixir's concurrency model and aims for better performance in distributed systems
  • scikit-learn has a larger ecosystem and more extensive documentation due to its maturity
27,505

The fundamental package for scientific computing with Python.

Pros of NumPy

  • Mature and widely adopted in the scientific computing community
  • Extensive documentation and large ecosystem of compatible libraries
  • Highly optimized C implementations for numerical operations

Cons of NumPy

  • Python's Global Interpreter Lock (GIL) can limit parallelism
  • Less seamless integration with functional programming paradigms
  • Steeper learning curve for beginners due to its extensive feature set

Code Comparison

NumPy:

import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.dot(a, b)

Nx:

import Nx.Defn

a = Nx.tensor([1, 2, 3])
b = Nx.tensor([4, 5, 6])
c = Nx.dot(a, b)

Both libraries provide similar functionality for numerical computing, but Nx is designed specifically for Elixir and leverages its functional programming features. NumPy has a more extensive set of functions and is deeply integrated with the Python scientific computing ecosystem, while Nx is newer and growing within the Elixir community. Nx also offers better support for GPU acceleration out of the box, whereas NumPy typically requires additional libraries for GPU computations.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

This repository currently holds the following projects:

  • Nx - Multi-dimensional arrays (tensors) and numerical definitions for Elixir

  • EXLA - Google's XLA (Accelerated Linear Algebra) compiler/backend for Nx

  • Torchx - LibTorch backend for Nx

Each has their own README, which you can access above to learn more. They will be extracted to their own repository in the future. Examples and benchmarks are available in the EXLA project.

Check our organization page for a general introduction to Machine Learning in Elixir.