-
Notifications
You must be signed in to change notification settings - Fork 120
Open
Labels
cleanupNot broken, but could be more streamlinedNot broken, but could be more streamlined
Description
@pfackeldey - I wonder if we should try to avoid this boilerplate code. Perhaps, something like:
class BackendArraySetter:
def __init__(self, array, backend):
self.array = array
self.backend = backend
self.nplike = backend.nplike
def __setitem__(self, index, value):
if hasattr(self.nplike, "at"): # JAX-like
self.array = self.array.at[index].set(value)
elif hasattr(self.array, "__setitem__"): # NumPy, CuPy, etc.
self.array[index] = value
elif hasattr(self.nplike, "scatter"): # PyTorch-like (custom handling)
# You can define custom scatter logic here if needed
raise NotImplementedError("Add scatter logic for torch here.")
else:
raise TypeError(f"Unsupported nplike type: {type(self.nplike)}")
def result(self):
return self.arrayOriginally posted by @ianna in #3457 (comment)
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
cleanupNot broken, but could be more streamlinedNot broken, but could be more streamlined