🔍 Code Extractor

function is_cupy_array

Maturity: 20

Checks whether the provided data object is a CuPy ndarray by conditionally importing CuPy and performing an isinstance check.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1506 - 1510
Complexity:
simple

Purpose

This utility function safely determines if a given data object is a CuPy array without causing import errors when CuPy is not installed. It first checks if CuPy is already loaded in sys.modules before attempting to import it, making it safe to use in environments where CuPy may or may not be available. This is commonly used for type checking and dispatching logic in libraries that support multiple array backends (NumPy, CuPy, etc.).

Source Code

def is_cupy_array(data):
    if 'cupy' in sys.modules:
        import cupy
        return isinstance(data, cupy.ndarray)
    return False

Parameters

Name Type Default Kind
data - - positional_or_keyword

Parameter Details

data: Any Python object to be checked. Typically expected to be an array-like object, but can be any type. The function will return True only if it is specifically a cupy.ndarray instance.

Return Value

Returns a boolean value: True if the data parameter is an instance of cupy.ndarray and CuPy is available in sys.modules, False otherwise (including when CuPy is not installed or not yet imported).

Dependencies

  • sys
  • cupy

Required Imports

import sys

Conditional/Optional Imports

These imports are only needed under specific conditions:

import cupy

Condition: only imported if 'cupy' is already present in sys.modules

Optional

Usage Example

import sys
import numpy as np

def is_cupy_array(data):
    if 'cupy' in sys.modules:
        import cupy
        return isinstance(data, cupy.ndarray)
    return False

# Example 1: With NumPy array
np_array = np.array([1, 2, 3])
print(is_cupy_array(np_array))  # False

# Example 2: With CuPy array (if CuPy is installed)
try:
    import cupy as cp
    cp_array = cp.array([1, 2, 3])
    print(is_cupy_array(cp_array))  # True
except ImportError:
    print("CuPy not available")

# Example 3: With other types
print(is_cupy_array([1, 2, 3]))  # False
print(is_cupy_array("string"))  # False

Best Practices

  • This function uses lazy importing to avoid ImportError when CuPy is not installed, making it safe for optional GPU support
  • The function checks sys.modules first before importing, which is more efficient than using try-except blocks
  • Use this function for type dispatching in libraries that support multiple array backends
  • This pattern is preferable to direct isinstance checks when dealing with optional dependencies
  • The function returns False for all non-CuPy arrays, including NumPy arrays, lists, and other array-like objects

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_dask_array 74.9% similar

    Checks whether the provided data object is a Dask array by conditionally importing dask.array and performing an isinstance check.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_dataframe 62.2% similar

    Checks whether the supplied data object is a pandas DataFrame or a Dask DataFrame, with support for lazy imports of both libraries.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_series 57.1% similar

    Checks whether the supplied data object is a pandas Series or dask Series type, with lazy loading support for dask.

    From: /tf/active/vicechatdev/patches/util.py
  • function isdatetime 55.4% similar

    Determines whether a given value (array or scalar) is a recognized datetime type, checking both NumPy datetime64 arrays and Python datetime objects.

    From: /tf/active/vicechatdev/patches/util.py
  • function asarray 53.9% similar

    Converts array-like objects (lists, pandas Series, objects with __array__ method) to NumPy ndarray format with optional strict validation.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse