|
| 1 | +import builtins |
1 | 2 | import contextlib
|
2 | 3 | import warnings
|
3 | 4 |
|
@@ -308,30 +309,121 @@ 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 | + 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): |
318 | 319 | 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 = len( |
| 324 | + [i for i in indices if i is not Ellipsis and i is not None] |
| 325 | + ) |
| 326 | + rank = len(data.get_partial_shape()) |
| 327 | + num_missing = rank - num_specified |
| 328 | + indices = ( |
| 329 | + indices[:ellipsis_pos] |
| 330 | + + (builtins.slice(None),) * num_missing |
| 331 | + + indices[ellipsis_pos + 1 :] |
| 332 | + ) |
| 333 | + |
| 334 | + def count_unsqueeze_before(dim): |
| 335 | + return sum(1 for i in range(dim) if indices[i] is None) |
| 336 | + |
320 | 337 | for dim, index in enumerate(indices):
|
321 | 338 | if isinstance(index, int):
|
322 |
| - axis.append(dim) |
323 |
| - gather_index = ov_opset.constant(index, Type.i32) |
324 |
| - else: |
325 |
| - assert ( |
| 339 | + num_unsqueeze_before = count_unsqueeze_before(dim) |
| 340 | + actual_dim = dim - num_unsqueeze_before |
| 341 | + |
| 342 | + if actual_dim < 0 or actual_dim >= rank: |
| 343 | + raise IndexError( |
| 344 | + f"""Index {index} is out of bounds |
| 345 | + for axis {dim} with rank {rank}""" |
| 346 | + ) |
| 347 | + length = data.get_partial_shape()[actual_dim].get_length() |
| 348 | + idx_value = index if index >= 0 else length + index |
| 349 | + axes.append(dim) |
| 350 | + index_node = ov_opset.constant([idx_value], Type.i32).output(0) |
| 351 | + gather_indices_nodes.append(index_node) |
| 352 | + elif isinstance(index, builtins.slice): |
| 353 | + if ( |
326 | 354 | index.start is None
|
327 | 355 | and index.stop is None
|
328 | 356 | and index.step is None
|
| 357 | + ): |
| 358 | + continue |
| 359 | + slice_axes.append(dim) |
| 360 | + slice_starts.append(0 if index.start is None else index.start) |
| 361 | + slice_ends.append( |
| 362 | + 2**31 - 1 if index.stop is None else index.stop |
329 | 363 | )
|
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 |
| - ) |
| 364 | + slice_steps.append(1 if index.step is None else index.step) |
| 365 | + elif index is None: |
| 366 | + unsqueeze_axes.append(dim) |
| 367 | + elif isinstance(index, OpenVINOKerasTensor): |
| 368 | + axes.append(dim) |
| 369 | + gather_indices_nodes.append(get_ov_output(index)) |
| 370 | + else: |
| 371 | + raise ValueError( |
| 372 | + f"""Unsupported index type {type(index)} |
| 373 | + in OpenVINOKerasTensor.__getitem__""" |
| 374 | + ) |
| 375 | + |
| 376 | + if slice_axes: |
| 377 | + step = ov_opset.constant(slice_steps, Type.i32).output(0) |
| 378 | + start = ov_opset.constant(slice_starts, Type.i32).output(0) |
| 379 | + stop = ov_opset.constant(slice_ends, Type.i32).output(0) |
| 380 | + adjusted_slice_axes = [ |
| 381 | + ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax) |
| 382 | + for ax in slice_axes |
| 383 | + ] |
| 384 | + axes_const = ov_opset.constant( |
| 385 | + adjusted_slice_axes, Type.i32 |
| 386 | + ).output(0) |
| 387 | + data = ov_opset.slice(data, start, stop, step, axes_const).output(0) |
| 388 | + |
| 389 | + if axes: |
| 390 | + gather_indices_const = ( |
| 391 | + gather_indices_nodes[0] |
| 392 | + if len(gather_indices_nodes) == 1 |
| 393 | + else ov_opset.concat(gather_indices_nodes, axis=0).output(0) |
| 394 | + ) |
| 395 | + adjusted_axes = [ |
| 396 | + ax - sum(1 for unsq in unsqueeze_axes if unsq <= ax) |
| 397 | + for ax in axes |
| 398 | + ] |
| 399 | + if len(axes) == 1: |
| 400 | + data = ov_opset.gather( |
| 401 | + data, gather_indices_const, adjusted_axes[0] |
| 402 | + ).output(0) |
| 403 | + data = ov_opset.squeeze(data, adjusted_axes[0]).output(0) |
| 404 | + else: |
| 405 | + rank = len(data.get_partial_shape()) |
| 406 | + remaining_axes = [ |
| 407 | + i for i in range(rank) if i not in adjusted_axes |
| 408 | + ] |
| 409 | + perm = ov_opset.constant( |
| 410 | + adjusted_axes + remaining_axes, Type.i32 |
| 411 | + ) |
| 412 | + data = ov_opset.transpose(data, perm).output(0) |
| 413 | + data = ov_opset.gather_nd(data, gather_indices_const).output(0) |
| 414 | + |
| 415 | + if unsqueeze_axes: |
| 416 | + adjusted_unsqueeze = [] |
| 417 | + for ax in unsqueeze_axes: |
| 418 | + ax -= sum(1 for s in axes if s < ax) |
| 419 | + ax -= sum(1 for s in slice_axes if s < ax) |
| 420 | + adjusted_unsqueeze.append(ax) |
| 421 | + unsqueeze_const = ov_opset.constant( |
| 422 | + adjusted_unsqueeze, Type.i32 |
| 423 | + ).output(0) |
| 424 | + data = ov_opset.unsqueeze(data, unsqueeze_const).output(0) |
| 425 | + |
| 426 | + return OpenVINOKerasTensor(data) |
335 | 427 |
|
336 | 428 | def __len__(self):
|
337 | 429 | ov_output = self.output
|
|
0 commit comments