Skip to content

np_ml_framework

Framework-agnostic Numpy-like array adapter for ML libraries.

This module allows code to operate on arrays interchangeably from Numpy, PyTorch, JAX, and Tensorflow. Each available framework is registered with check and conversion functions, and resolve() normalizes a set of arguments to the arrays of the highest-priority framework (selected via the NPML_ORDER and NPML_DEFAULT environment variables).

Each registered framework module also gets item() and tolist() helper attributes (e.g. np.tolist(arr), torch.tolist(tensor)) working uniformly across the supported frameworks.

If a framework is not installed, it is simply skipped at import time.

Example
import numpy as np
import torch

mod, args = np_ml_framework.resolve(np.array([1.0]), torch.tensor([2.0]))
# mod is torch (the highest-priority framework) and args are torch tensors.

resolve(*args)

Normalize arguments to the arrays of the highest-priority framework.

Each argument is checked against the registered frameworks. The framework with the highest priority (as per NPML_ORDER) having at least one argument becomes the target, and every argument belonging to a different framework is converted to it using the framework conversion function.

Parameters:

Name Type Description Default
*args

Arrays from any of the supported frameworks (or other objects, which are passed through unchanged).

()

Returns:

Type Description

A tuple with the resolved target module and the tuple of converted

arguments. If no argument matches any framework, the default module

(NPML_DEFAULT) is returned together with the original arguments.

Example
import numpy as np
import torch

mod, (a, b) = np_ml_framework.resolve(np.array([1.0]), torch.tensor([2.0]))
# isinstance(a, torch.Tensor) and b is torch_tensor (highest priority wins).
Source code in py_misc_utils/np_ml_framework.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
def resolve(*args):
  """Normalize arguments to the arrays of the highest-priority framework.

  Each argument is checked against the registered frameworks. The framework
  with the highest priority (as per NPML_ORDER) having at least one argument
  becomes the target, and every argument belonging to a different framework
  is converted to it using the framework conversion function.

  Args:
    *args: Arrays from any of the supported frameworks (or other objects,
      which are passed through unchanged).

  Returns:
    A tuple with the resolved target module and the tuple of converted
    arguments. If no argument matches any framework, the default module
    (NPML_DEFAULT) is returned together with the original arguments.

  Example:
    import numpy as np
    import torch

    mod, (a, b) = np_ml_framework.resolve(np.array([1.0]), torch.tensor([2.0]))
    # isinstance(a, torch.Tensor) and b is torch_tensor (highest priority wins).
  """
  mods = []
  tprio, tmod, tref = -1, None, None
  for i, t in enumerate(args):
    mod = _get_module(t)
    mods.append(mod)
    if mod is not None:
      prio = _MODULES_PRIORITY[mod.__npml_name]
      if prio > tprio:
        tmod = mod
        tprio = prio
        tref = t

  if tmod is None:
    return _MODULES[_DEFAULT_MODULE], args

  rargs = list(args)
  for i, mod in enumerate(mods):
    if tmod is not mod:
      rargs[i] = tmod.__npml_from(mod, args[i], tref)

  return tmod, tuple(rargs)