nemo_deploy.utils
#
Module Contents#
Functions#
Converts a type dictionary class into a tuple of PyTriton Tensor objects. |
|
Determines the version of a NeMo checkpoint from its file structure. |
|
Converts a list of strings to a numpy array of UTF-8 encoded bytes. |
|
Converts a numpy array of UTF-8 encoded bytes back to a list of strings. |
|
Converts a numpy array of images to a list of PIL Image objects. |
|
Casts input data to a numpy array with the required dtype. |
|
Broadcasts a list of text data to all processes. |
Data#
API#
- nemo_deploy.utils.NEMO2 = 'NEMO 2.0'#
- nemo_deploy.utils.NEMO1 = 'NEMO 1.0'#
- nemo_deploy.utils.typedict2tensor(
- typedict_class,
- overwrite_kwargs: Optional[Dict[str, Any]] = None,
- defaults: Optional[Dict[str, Any]] = None,
Converts a type dictionary class into a tuple of PyTriton Tensor objects.
This function takes a class with type hints and converts each typed field into a PyTriton Tensor specification, handling nested list types and mapping Python types to numpy dtypes.
- Parameters:
typedict_class β A class with type hints that will be converted to Tensor specs
overwrite_kwargs β Optional dictionary of kwargs to override default Tensor parameters
defaults β Optional dictionary of default values (unused)
- Returns:
A tuple of PyTriton Tensor objects, one for each typed field in the input class
- Return type:
tuple
- Raises:
Exception β If an unsupported type is encountered during type mapping
- nemo_deploy.utils.nemo_checkpoint_version(path: str) str #
Determines the version of a NeMo checkpoint from its file structure.
Examines the provided checkpoint path to determine if it follows the NeMo 2.0 or NeMo 1.0 format based on the presence of βcontextβ and βweightsβ directories.
- Parameters:
path (str) β Path to the NeMo checkpoint file or directory
- Returns:
Version string - either NEMO2 or NEMO1 constant indicating the checkpoint version
- Return type:
str
- nemo_deploy.utils.str_list2numpy(str_list: List[str]) numpy.ndarray #
Converts a list of strings to a numpy array of UTF-8 encoded bytes.
Takes a list of strings and converts it to a numpy array with an additional dimension, then encodes the strings as UTF-8 bytes.
- Parameters:
str_list (List[str]) β List of strings to convert
- Returns:
Numpy array of UTF-8 encoded bytes with shape (N, 1) where N is the length of the input list
- Return type:
np.ndarray
- nemo_deploy.utils.str_ndarray2list(str_ndarray: numpy.ndarray) List[str] #
Converts a numpy array of UTF-8 encoded bytes back to a list of strings.
Takes a numpy array of UTF-8 encoded bytes and decodes it back to strings, removing any extra dimensions, and returns the result as a Python list.
- Parameters:
str_ndarray (np.ndarray) β Numpy array of UTF-8 encoded bytes, typically with shape (N, 1) where N is the length of the resulting list
- Returns:
List of decoded strings
- Return type:
List[str]
- nemo_deploy.utils.ndarray2img(
- img_ndarray: numpy.ndarray,
Converts a numpy array of images to a list of PIL Image objects.
Takes a numpy array containing one or more images and converts each image to a PIL Image object using Image.fromarray().
- Parameters:
img_ndarray (np.ndarray) β Numpy array of images, where each image is a 2D or 3D array representing pixel values
- Returns:
List of PIL Image objects created from the input array
- Return type:
List[Image.Image]
- nemo_deploy.utils.cast_output(data, required_dtype)#
Casts input data to a numpy array with the required dtype.
Takes input data that may be a torch.Tensor, numpy array, or other sequence type and converts it to a numpy array with the specified dtype. For string dtypes, the data is encoded as UTF-8 bytes. The output array is ensured to have at least 2 dimensions.
- Parameters:
data β Input data to cast. Can be a torch.Tensor, numpy array, or sequence type that can be converted to a numpy array.
required_dtype β The desired numpy dtype for the output array.
- Returns:
A numpy array containing the input data cast to the required dtype, with at least 2 dimensions.
- Return type:
np.ndarray
- nemo_deploy.utils.broadcast_list(data, src=0, group=None)#
Broadcasts a list of text data to all processes.
- Parameters:
data (list) β List of strings to broadcast.
src (int, optional) β Source rank. Defaults to 0.
group (ProcessGroup, optional) β The process group to work on. If None, the default process group will be used.