|
| 1 | +import builtins |
1 | 2 | import contextlib
|
2 | 3 | import warnings
|
3 | 4 |
|
@@ -308,30 +309,74 @@ def __ne__(self, other):
|
308 | 309 | return OpenVINOKerasTensor(ov_opset.not_equal(first, other).output(0))
|
309 | 310 |
|
310 | 311 | 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 |
314 | 312 | data = self.output
|
315 |
| - axis = [] |
316 |
| - gather_index = None |
317 |
| - if isinstance(indices, int): |
| 313 | + axes, gather_indices = [], [] |
| 314 | + slice_axes, slice_starts, slice_ends, slice_steps = [], [], [], [] |
| 315 | + unsqueeze_axes, ellipsis_index = [], None |
| 316 | + |
| 317 | + if not isinstance(indices, tuple): |
318 | 318 | indices = (indices,)
|
319 |
| - assert isinstance(indices, tuple), "only tuple is supported" |
| 319 | + |
320 | 320 | for dim, index in enumerate(indices):
|
321 | 321 | if isinstance(index, int):
|
322 |
| - axis.append(dim) |
323 |
| - gather_index = ov_opset.constant(index, Type.i32) |
| 322 | + axes.append(dim) |
| 323 | + gather_indices.append(index) |
| 324 | + elif isinstance(index, builtins.slice): |
| 325 | + slice_axes.append(dim) |
| 326 | + slice_starts.append(0 if index.start is None else index.start) |
| 327 | + slice_ends.append( |
| 328 | + 2**31 - 1 if index.stop is None else index.stop |
| 329 | + ) |
| 330 | + slice_steps.append(1 if index.step is None else index.step) |
| 331 | + elif index is Ellipsis: |
| 332 | + ellipsis_index = dim |
| 333 | + elif index is None: |
| 334 | + unsqueeze_axes.append(dim) |
| 335 | + elif isinstance(index, OpenVINOKerasTensor): |
| 336 | + axes.append(dim) |
| 337 | + gather_indices.append(convert_to_numpy(index)) |
324 | 338 | else:
|
325 | 339 | assert (
|
326 | 340 | index.start is None
|
327 | 341 | and index.stop is None
|
328 | 342 | and index.step is None
|
329 | 343 | )
|
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 |
| - ) |
| 344 | + |
| 345 | + if slice_axes: |
| 346 | + step = ov_opset.constant(slice_steps, Type.i32).output(0) |
| 347 | + start = ov_opset.constant(slice_starts, Type.i32).output(0) |
| 348 | + stop = ov_opset.constant(slice_ends, Type.i32).output(0) |
| 349 | + axes_const = ov_opset.constant(slice_axes, Type.i32).output(0) |
| 350 | + data = ov_opset.slice(data, start, stop, step, axes_const).output(0) |
| 351 | + |
| 352 | + if axes: |
| 353 | + adjusted_axes = [ |
| 354 | + ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax) |
| 355 | + for ax in axes |
| 356 | + ] |
| 357 | + rank = len(data.get_partial_shape()) |
| 358 | + remaining_axes = [i for i in range(rank) if i not in adjusted_axes] |
| 359 | + perm = ov_opset.constant(adjusted_axes + remaining_axes, Type.i32) |
| 360 | + gather_indices_const = ov_opset.constant(gather_indices, Type.i32) |
| 361 | + data = ov_opset.transpose(data, perm).output(0) |
| 362 | + data = ov_opset.gather_nd(data, gather_indices_const).output(0) |
| 363 | + |
| 364 | + if unsqueeze_axes: |
| 365 | + expanded_rank = data.get_partial_shape().rank.get_length() + len( |
| 366 | + unsqueeze_axes |
| 367 | + ) |
| 368 | + adjusted_unsqueeze = [] |
| 369 | + for ax in unsqueeze_axes: |
| 370 | + ax -= sum(1 for s in axes if s < ax) |
| 371 | + if ellipsis_index is not None and ax > ellipsis_index: |
| 372 | + ax += expanded_rank - len(indices) |
| 373 | + adjusted_unsqueeze.append(ax) |
| 374 | + unsqueeze_const = ov_opset.constant( |
| 375 | + adjusted_unsqueeze, Type.i32 |
| 376 | + ).output(0) |
| 377 | + data = ov_opset.unsqueeze(data, unsqueeze_const).output(0) |
| 378 | + |
| 379 | + return OpenVINOKerasTensor(data) |
335 | 380 |
|
336 | 381 | def __len__(self):
|
337 | 382 | ov_output = self.output
|
|
0 commit comments