ml-explore / ml-explore/mlx

[Feature]: Supporting normal python datatypes in the grad method obtained from mlx.core.value_and_grad

Open
#3,774 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement low priority
Dominant language
C++
Stars
28.5k
Forks
2.3k
Avg merge
3d 8h
Merged PRs (30d)
62

Description

Title

value_and_grad fails when function arguments contain native Python numbers

Description

Problem

In MLX, mlx.core.value_and_grad expects all inputs (and nested structures) to be mlx.core.array . Passing native Python
numeric types ( float , int ) raises ValueError .

Reproduction Code
import mlx.core as mx

# Simple function
def f(x):
    return x * 2

# Wrap with value_and_grad
fn = mx.value_and_grad(f)

# Pass native Python float
fn(3.0)
Traceback
ValueError: [tree_flatten] The argument should contain only arrays
Comparison with JAX

In JAX, jax.value_and_grad supports native Python numeric types via implicit coercion/promotion:

import jax

fn = jax.value_and_grad(lambda x: x * 2)
print(fn(3.0))
# Output: (Array(6., dtype=float32, weak_type=True), Array(2., dtype=float32, weak_type=True))				
Proposed Solution

Modify mlx.core.value_and_grad (and other transform API entry points) to automatically convert native Python numbers to
mlx.core.array before performing tree_flatten .

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at mlx.core.value_and_grad and the tree_flatten path; reproduce the supplied fn(3.0) case and compare it with an array input. Trace the transform API entry points mentioned in the issue, then verify that native numeric arguments and nested structures are accepted without breaking existing array inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend-api-design, machine-learning
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.