Convert Figma logo to code with AI

elixir-nx logonx

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

2,674
199
2,674
20

Top Related Projects

85,015

Tensors and Dynamic neural networks in Python with strong GPU acceleration

186,879

An Open Source Machine Learning Framework for Everyone

30,218

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,792

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

85,015

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.

186,879

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.

30,218

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

Pros of JAX

  • More mature and widely adopted in the machine learning community
  • Supports automatic differentiation and GPU/TPU acceleration
  • Extensive ecosystem of libraries and tools built on top of JAX

Cons of JAX

  • Steeper learning curve, especially for those new to functional programming
  • Limited support for dynamic shapes and control flow compared to some other frameworks

Code Comparison

JAX:

import jax.numpy as jnp

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

grad_f = jax.grad(f)

Nx:

defn f(x) do
  Nx.sum(Nx.power(Nx.sin(x), 2))
end

grad_f = grad(f)

Key Differences

  • JAX is Python-based, while Nx is built for Elixir
  • Nx aims to provide a more ergonomic API for numerical computing in Elixir
  • JAX has a larger community and more resources available
  • Nx integrates well with the Elixir ecosystem and leverages its concurrency model

Both projects aim to provide efficient numerical computing capabilities, but they cater to different programming languages and ecosystems. JAX is more established in the machine learning community, while Nx is gaining traction among Elixir developers looking for numerical computing solutions.

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,792

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.