Skip to content

Commit d274a50

Browse files
[OpenVINO Backend] update __getitem__
1 parent 771b001 commit d274a50

File tree

2 files changed

+108
-19
lines changed

2 files changed

+108
-19
lines changed

keras/src/backend/openvino/core.py

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import builtins
12
import contextlib
23
import warnings
34

@@ -308,30 +309,119 @@ def __ne__(self, other):
308309
return OpenVINOKerasTensor(ov_opset.not_equal(first, other).output(0))
309310

310311
def __getitem__(self, indices):
311-
# now it has limited functionaly
312-
# and supports only a case with one integer index in indices
313-
# other indices must be None
314312
data = self.output
315-
axis = []
316-
gather_index = None
317-
if isinstance(indices, int):
313+
rank = len(data.get_partial_shape())
314+
axes, gather_indices_nodes = [], []
315+
slice_axes, slice_starts, slice_ends, slice_steps = [], [], [], []
316+
unsqueeze_axes = []
317+
318+
if not isinstance(indices, tuple):
318319
indices = (indices,)
319-
assert isinstance(indices, tuple), "only tuple is supported"
320+
321+
if any(i is Ellipsis for i in indices):
322+
ellipsis_pos = indices.index(Ellipsis)
323+
num_specified = sum(
324+
i is not Ellipsis and i is not None for i in indices
325+
)
326+
num_missing = rank - num_specified
327+
indices = (
328+
indices[:ellipsis_pos]
329+
+ (builtins.slice(None),) * num_missing
330+
+ indices[ellipsis_pos + 1 :]
331+
)
332+
333+
def count_unsqueeze_before(dim):
334+
return sum(1 for i in range(dim) if indices[i] is None)
335+
336+
partial_shape = data.get_partial_shape()
337+
320338
for dim, index in enumerate(indices):
321339
if isinstance(index, int):
322-
axis.append(dim)
323-
gather_index = ov_opset.constant(index, Type.i32)
340+
actual_dim = dim - count_unsqueeze_before(dim)
341+
if not (0 <= actual_dim < rank):
342+
raise IndexError(
343+
f"""Index {index} is out of bounds for
344+
axis {dim} with rank {rank}"""
345+
)
346+
length = partial_shape[actual_dim].get_length()
347+
idx_value = index if index >= 0 else length + index
348+
axes.append(dim)
349+
gather_indices_nodes.append(
350+
ov_opset.constant([idx_value], Type.i32).output(0)
351+
)
352+
elif isinstance(index, builtins.slice):
353+
if index == builtins.slice(None):
354+
continue
355+
if index.step is not None and index.step < 0:
356+
raise ValueError("OpenVINO doesn't support negative steps")
357+
slice_axes.append(dim)
358+
slice_starts.append(0 if index.start is None else index.start)
359+
slice_ends.append(
360+
2**31 - 1 if index.stop is None else index.stop
361+
)
362+
slice_steps.append(1 if index.step is None else index.step)
363+
elif index is None:
364+
unsqueeze_axes.append(dim)
365+
elif isinstance(index, OpenVINOKerasTensor):
366+
axes.append(dim)
367+
gather_indices_nodes.append(get_ov_output(index))
324368
else:
325-
assert (
326-
index.start is None
327-
and index.stop is None
328-
and index.step is None
369+
raise ValueError(
370+
f"""Unsupported index type {type(index)}
371+
in OpenVINOKerasTensor.__getitem__"""
329372
)
330-
assert len(axis) == 1, "axis must contain one element"
331-
axis = ov_opset.constant(axis, Type.i32)
332-
return OpenVINOKerasTensor(
333-
ov_opset.gather(data, gather_index, axis).output(0)
334-
)
373+
374+
if slice_axes:
375+
step = ov_opset.constant(slice_steps, Type.i32).output(0)
376+
start = ov_opset.constant(slice_starts, Type.i32).output(0)
377+
stop = ov_opset.constant(slice_ends, Type.i32).output(0)
378+
adjusted_slice_axes = [
379+
ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax)
380+
for ax in slice_axes
381+
]
382+
axes_const = ov_opset.constant(
383+
adjusted_slice_axes, Type.i32
384+
).output(0)
385+
data = ov_opset.slice(data, start, stop, step, axes_const).output(0)
386+
387+
if axes:
388+
gather_indices_const = (
389+
gather_indices_nodes[0]
390+
if len(gather_indices_nodes) == 1
391+
else ov_opset.concat(gather_indices_nodes, axis=0).output(0)
392+
)
393+
adjusted_axes = [
394+
ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax)
395+
for ax in axes
396+
]
397+
if len(axes) == 1:
398+
data = ov_opset.gather(
399+
data, gather_indices_const, adjusted_axes[0]
400+
).output(0)
401+
data = ov_opset.squeeze(data, adjusted_axes[0]).output(0)
402+
else:
403+
rank = len(data.get_partial_shape())
404+
remaining_axes = [
405+
i for i in range(rank) if i not in adjusted_axes
406+
]
407+
perm = ov_opset.constant(
408+
adjusted_axes + remaining_axes, Type.i32
409+
)
410+
data = ov_opset.transpose(data, perm).output(0)
411+
data = ov_opset.gather_nd(data, gather_indices_const).output(0)
412+
413+
if unsqueeze_axes:
414+
adjusted_unsqueeze = []
415+
for ax in unsqueeze_axes:
416+
ax -= sum(1 for s in axes if s < ax)
417+
ax -= sum(1 for s in slice_axes if s < ax)
418+
adjusted_unsqueeze.append(ax)
419+
unsqueeze_const = ov_opset.constant(
420+
adjusted_unsqueeze, Type.i32
421+
).output(0)
422+
data = ov_opset.unsqueeze(data, unsqueeze_const).output(0)
423+
424+
return OpenVINOKerasTensor(data)
335425

336426
def __len__(self):
337427
ov_output = self.output

keras/src/backend/openvino/excluded_tests.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
keras/src/activations
22
keras/src/backend/common/dtypes_test.py
3-
keras/src/backend/common/variables_test.py
43
keras/src/callbacks/early_stopping_test.py
54
keras/src/dtype_policies/dtype_policy_map_test.py
65
keras/src/layers/attention

0 commit comments

Comments
 (0)