Skip to content

Fix: Deprecation error from NumPy 1.x#554

Open
AI-Ahmed wants to merge 1 commit intoyandexdataschool:masterfrom
AI-Ahmed:patch-13
Open

Fix: Deprecation error from NumPy 1.x#554
AI-Ahmed wants to merge 1 commit intoyandexdataschool:masterfrom
AI-Ahmed:patch-13

Conversation

@AI-Ahmed
Copy link
Copy Markdown
Contributor

What’s going on?

  • Problem:

The replay buffer uses np.array(..., copy=False) inside _encode_sample when building batches of actions (and possibly other fields). With NumPy 2.0, np.array(obj, copy=False) raises a ValueError whenever a copy is required, which happens when obj is a Python scalar (as is typical for env.action_space.sample()). This causes ReplayBuffer.sample(...) to crash under NumPy 2.0.

  • Fix:

Replace np.array(x, copy=False) with a call that allows copying when needed, while still avoiding unnecessary copies for existing arrays. For example:

# Before (NumPy 2.0-incompatible) 

actions.append(np.array(action, copy=False))

# After (NumPy 2.0-compatible) 
actions.append(np.asarray(action))

or simply:

actions.append(np.array(action))  # omit copy=False entirely

Apply the same pattern anywhere else in the replay buffer that uses np.array(..., copy=False) with values that may be Python scalars or non-ndarray objects.

  • Rationale (one sentence): This change restores compatibility with NumPy 2.0 by allowing a copy when necessary, while preserving the previous runtime behavior for NumPy 1.x (where a copy was already being made in these cases).

### What’s going on?

- **Problem**:  
  The replay buffer uses `np.array(..., copy=False)` inside `_encode_sample` when building batches of actions (and possibly other fields). With NumPy 2.0, `np.array(obj, copy=False)` raises a `ValueError` whenever a copy is required, which happens when `obj` is a Python scalar (as is typical for `env.action_space.sample()`). This causes `ReplayBuffer.sample(...)` to crash under NumPy 2.0.

- **Fix**:  
  Replace `np.array(x, copy=False)` with a call that **allows copying when needed**, while still avoiding unnecessary copies for existing arrays. For example:

  ```python
  # Before (NumPy 2.0-incompatible)
  actions.append(np.array(action, copy=False))

  # After (NumPy 2.0-compatible)
  actions.append(np.asarray(action))
  ```

  or simply:

  ```python
  actions.append(np.array(action))  # omit copy=False entirely
  ```

  Apply the same pattern anywhere else in the replay buffer that uses `np.array(..., copy=False)` with values that may be Python scalars or non-ndarray objects.

- **Rationale (one sentence)**:  
  This change restores compatibility with NumPy 2.0 by allowing a copy when necessary, while preserving the previous runtime behavior for NumPy 1.x (where a copy was already being made in these cases).
@dniku
Copy link
Copy Markdown
Collaborator

dniku commented Mar 22, 2026

Thanks for your PR!

Looking into this, it seems .asarray() is preferred to .array(), e.g. here. Could you replace .array() with .asarray()?

Thanks!

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.

2 participants