Skip to content

Commit

Permalink
style changes
Browse files Browse the repository at this point in the history
  • Loading branch information
maxymnaumchyk committed Oct 15, 2024
1 parent 4189a02 commit 27f9e44
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/awkward/operations/ak_to_raggedtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def _impl(array):

# keep the same device
ak_device = ak.backend(array)
if ak_device not in ['cuda', 'cpu']:
if ak_device not in ["cuda", "cpu"]:
raise ValueError("""Only 'cpu' and 'cuda' backend conversions are allowed""")

if ak_device == 'cpu':
device = 'CPU:0'
if ak_device == "cpu":
device = "CPU:0"
else:
_, depth = array.minmax_depth
id = array[depth-1].content.data.device.id
device = 'GPU:' + str(id)
id = array[depth - 1].content.data.device.id
device = "GPU:" + str(id)

with tf.device(device):
if isinstance(array, ak.contents.numpyarray.NumpyArray):
Expand All @@ -71,7 +71,10 @@ def _impl(array):

else:
flat_values, nested_row_splits = _recursive_call(array, ())
return tf.RaggedTensor.from_nested_row_splits(flat_values, nested_row_splits)
return tf.RaggedTensor.from_nested_row_splits(
flat_values, nested_row_splits
)


def _convert_to_tensor_if_cupy(array):
if isinstance(array, np.ndarray):
Expand All @@ -80,8 +83,10 @@ def _convert_to_tensor_if_cupy(array):
# converts cupy directly to tensor,
# since `tf.RaggedTensor.from_nested_row_splits` can not work with Cupy arrays
import tensorflow as tf

return tf.experimental.dlpack.from_dlpack(array.toDlpack())


def _recursive_call(layout, offsets_arr):
try:
# change all the possible layout types to ListOffsetArray
Expand Down Expand Up @@ -112,4 +117,4 @@ def _recursive_call(layout, offsets_arr):
data = layout.data
data = _convert_to_tensor_if_cupy(data)
return data, offsets_arr
return _recursive_call(layout.content, offsets_arr)
return _recursive_call(layout.content, offsets_arr)

0 comments on commit 27f9e44

Please sign in to comment.