Skip to content

Add tolist method to JAX Array and TensorFlow EagerTensor frontends#28917

Merged
Sam-Armstrong merged 5 commits intoivy-llc:mainfrom
kallal79:feature/implement-tolist-frontend-methods
Jul 21, 2025
Merged

Add tolist method to JAX Array and TensorFlow EagerTensor frontends#28917
Sam-Armstrong merged 5 commits intoivy-llc:mainfrom
kallal79:feature/implement-tolist-frontend-methods

Conversation

@kallal79
Copy link
Contributor

@kallal79 kallal79 commented Jul 21, 2025

What does this PR do?

This PR implements the missing tolist() method for JAX Array and TensorFlow EagerTensor frontend classes, resolving issue #19170.

Description

I noticed that while most Ivy frontends (NumPy, PyTorch, Paddle) already have the tolist() method implemented, JAX and TensorFlow frontends were missing this functionality. This creates inconsistency in the API and can cause issues when users expect this method to be available.

Changes Made

JAX Array Frontend (ivy/functional/frontends/jax/array.py)

  • Added tolist() instance method that calls ivy.to_list(self.ivy_array)
  • Added proper docstring following Ivy's documentation standards

TensorFlow EagerTensor Frontend (ivy/functional/frontends/tensorflow/tensor.py)

  • Added tolist() instance method that calls ivy.to_list(self.ivy_array)
  • Added proper docstring following Ivy's documentation standards

Test Coverage

  • Added comprehensive test case for JAX Array tolist() method
  • Added comprehensive test case for TensorFlow EagerTensor tolist() method
  • Both tests use Ivy's standard testing framework with proper decorators

Testing

I've tested the implementation with various array shapes and data types:

# JAX Array usage
jax_array = jax_frontend.Array([[1, 2, 3], [4, 5, 6]])
result = jax_array.tolist()  # Returns [[1, 2, 3], [4, 5, 6]]

# TensorFlow EagerTensor usage
tf_tensor = tf_frontend.EagerTensor([[1, 2, 3], [4, 5, 6]])
result = tf_tensor.tolist()  # Returns [[1, 2, 3], [4, 5, 6]]

Consistency Check

Verified that existing implementations in other frontends work correctly:

  • NumPy frontend: return self._ivy_array.to_list()
  • PyTorch frontend: return self.ivy_array.to_list()
  • Paddle frontend: Function-level implementation available

All implementations now use the same underlying ivy.to_list() function for consistency.

Why This Matters

  1. API Consistency: All major frontends now have complete tolist functionality
  2. User Experience: Users can rely on tolist() being available across all frontends
  3. Framework Compatibility: Provides seamless compatibility with native framework APIs
  4. Issue Resolution: Fully resolves the long-standing issue tolist #19170

Checklist

  • Implementation follows Ivy's coding standards
  • Added proper docstrings and comments
  • Added comprehensive test cases
  • Verified consistency with existing implementations
  • Tested with various data types and shapes
  • No breaking changes to existing functionality

Related Issues

Closes #19170


Note: This implementation maintains full backward compatibility and doesn't modify any existing functionality. It simply adds the missing methods that users would naturally expect to be available.

Looking forward to your feedback! 😊


…r frontends

- Add tolist() instance method to JAX Array frontend class
- Add tolist() instance method to TensorFlow EagerTensor frontend class
- Both methods use ivy.to_list() for consistency with existing implementations
- Add comprehensive test cases for both frontend implementations
- Resolves issue ivy-llc#19170

The tolist method converts arrays/tensors to Python lists, providing
compatibility with native framework APIs. This completes the tolist
functionality across all major Ivy frontends (NumPy, PyTorch, TensorFlow,
JAX, and Paddle).

Co-authored-by: Kallal Mukherjee <ritamukherje62@gmail.com>
- Document that test_torch_avg_pool2d failure is unrelated to tolist implementation
- Clarify that our tolist methods are working correctly and isolated
- Provide analysis of the dtype issue in pooling functions
- Confirm our changes are safe and follow established patterns
@kallal79
Copy link
Contributor Author

kallal79 commented Jul 21, 2025

Test Status Update

I've noticed there's a failing test in the CI, but I want to clarify that this test failure is completely unrelated to our tolist implementation.

Failing Test (Unrelated)

Test: test_torch_avg_pool2d[cpu-numpy-False-False]
Error: AssertionError: returned dtype = float64, ground-truth returned dtype = float32
Location: ivy_tests/test_ivy/test_frontends/test_torch/test_nn/test_functional/test_pooling_functions.py

Analysis

This is a pre-existing issue with dtype handling in the PyTorch frontend's avg_pool2d function. The test expects float32 but gets float64, which is a dtype preservation problem in the pooling implementation.

Our Changes Are Safe

  1. No modifications to existing functionality - We only added new methods
  2. Isolated implementation - Our tolist methods don't interact with pooling functions
  3. Consistent pattern - We use the same ivy.to_list() approach as other frontends
  4. Return Python lists - No array dtype issues since we return native Python lists
  5. Proper test coverage - Added comprehensive tests for both JAX and TensorFlow frontends

Our Implementation Tests

# JAX Array tolist -  Working
jax_array = jax_frontend.Array([[1, 2, 3], [4, 5, 6]])
result = jax_array.tolist()  # Returns [[1, 2, 3], [4, 5, 6]]

# TensorFlow EagerTensor tolist -  Working  
tf_tensor = tf_frontend.EagerTensor([[1, 2, 3], [4, 5, 6]])
result = tf_tensor.tolist()  # Returns [[1, 2, 3], [4, 5, 6]]

Documentation

I've added TEST_STATUS_NOTES.md to document this analysis and confirm that our implementation is working correctly.

Summary: The failing test is a pre-existing pooling function issue unrelated to our tolist implementation. Our changes are safe, well-tested, and ready for review!

Copy link
Contributor

@Sam-Armstrong Sam-Armstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that's fine, don't worry about the other test failure. Could you just remove the TEST_STATUS_NOTES.md file you've added, and address the other couple of comments I've made, then we should be good to merge. Thanks for the contribution!

min_num_dims=0,
max_num_dims=5,
min_dim_size=1,
max_dim_size=10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
max_dim_size=10,
max_dim_size=10,
min_value=-1e05,
max_value=1e05,

We need to add this so the values don't overflow and fail the test when running with a high number of examples (--num-examples 100)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed TEST_STATUS_NOTES.md file - Eliminated unnecessary documentation clutter

min_num_dims=0,
max_num_dims=5,
min_dim_size=1,
max_dim_size=10,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
max_dim_size=10,
max_dim_size=10,
min_value=-1e05,
max_value=1e05,

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed docstrings from frontend methods - Now follows Ivy's convention for clean, minimal frontend implementations

Comment on lines 417 to 423
"""Convert the array to a (possibly nested) list.

Returns
-------
list
A list representation of the array.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Convert the array to a (possibly nested) list.
Returns
-------
list
A list representation of the array.
"""

The convention we've been following is to not include docstrings for frontend functions/methods, so you can remove these.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added value limits to test cases - Both tests now include min_value=-1e05 and max_value=1e05 to prevent overflow with high example counts

Comment on lines 229 to 235
"""Convert the tensor to a (possibly nested) list.

Returns
-------
list
A list representation of the tensor.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Convert the tensor to a (possibly nested) list.
Returns
-------
list
A list representation of the tensor.
"""

kallal79 added 2 commits July 22, 2025 01:11
- Remove TEST_STATUS_NOTES.md file as requested
- Remove docstrings from frontend methods following Ivy convention
- Add min_value and max_value parameters to test cases to prevent overflow
- Ensure tests work correctly with high number of examples (--num-examples 100)

Thanks for the feedback @Sam-Armstrong! Ready for merge.
@kallal79
Copy link
Contributor Author

kallal79 commented Jul 21, 2025

Hi @Sam-Armstrong!

Thanks for the quick review and helpful feedback! I've addressed all your comments:

Changes Made:

  1. Removed TEST_STATUS_NOTES.md file - You're absolutely right, that was unnecessary documentation clutter.

  2. Removed docstrings from frontend methods - Got it! I wasn't aware of this Ivy convention. Both JAX Array and TensorFlow EagerTensor tolist() methods now have clean, minimal implementations without docstrings.

  3. Added value limits to test cases - Added min_value=-1e05 and max_value=1e05 to both test configurations to prevent overflow issues when running with --num-examples 100.

Updated Implementation:

JAX Array:

def tolist(self):
    return ivy.to_list(self.ivy_array)

TensorFlow EagerTensor:

def tolist(self):
    return ivy.to_list(self.ivy_array)

Test configurations now include:

dtype_and_x=helpers.dtype_and_values(
    available_dtypes=helpers.get_dtypes("valid"),
    min_num_dims=0,
    max_num_dims=5,
    min_dim_size=1,
    max_dim_size=10,
    min_value=-1e05,  # ← Added
    max_value=1e05,   # ← Added
),

The implementation is now much cleaner and follows Ivy's conventions properly. Should be ready for merge!

Thanks again for the guidance and for helping me learn the Ivy codebase conventions!

@kallal79 kallal79 requested a review from Sam-Armstrong July 21, 2025 19:51
Copy link
Contributor

@Sam-Armstrong Sam-Armstrong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good :)

@Sam-Armstrong Sam-Armstrong merged commit 573e714 into ivy-llc:main Jul 21, 2025
@kallal79
Copy link
Contributor Author

kallal79 commented Jul 22, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tolist

2 participants