-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[TF] Added support for advanced indexing and slicing #23684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
96a894a
0afbbea
5610318
40f6021
e2cdf31
2ae2c2c
d0f43b8
4237986
0f37daa
6d55664
7e6d040
c22a0e3
d739d4e
44d22a8
b6c9156
89f2801
7c57019
216aa79
7626d13
9a3e1a7
548f98c
f745756
e291758
08a2950
f4cbf03
ae2f4a0
72abd58
f26fad8
d42ed49
00af1a4
01b0f2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -135,6 +135,29 @@ TensorTests.testAllBackends("ElementIndexing") { | |
| expectEqual([43], array0D.scalars) | ||
| } | ||
|
|
||
| TensorTests.testAllBackends("NestedElementIndexing") { | ||
| // NOTE: This tests the `subscript(indices:)` method, which is distinct from | ||
| // the `subscript(index:)` method. | ||
| // NOTE: This test could use a clearer name, along with other "indexing" | ||
| // tests. Note to update corresponding test names in other files | ||
| // (shaped_array.test) as well. | ||
| let tensor3D = Tensor<Float>(shape: [3, 4, 5], | ||
| scalars: Array(stride(from: 0.0, to: 60, by: 1))) | ||
| let element1D = tensor3D[1, 3] | ||
| let element0D = tensor3D[2, 0, 3] | ||
|
|
||
| let array1D = element1D.array | ||
| let array0D = element0D.array | ||
|
|
||
| /// Test shapes | ||
| expectEqual([5], array1D.shape) | ||
| expectEqual([], array0D.shape) | ||
|
|
||
| /// Test scalars | ||
| expectEqual(Array(stride(from: 35.0, to: 40, by: 1)), array1D.scalars) | ||
| expectEqual([43], array0D.scalars) | ||
| } | ||
|
|
||
| TensorTests.testAllBackends("SliceIndexing") { | ||
| // XLA compilation error under TPU. | ||
| if _RuntimeConfig.executionMode.isTPU { return } | ||
|
|
@@ -173,6 +196,23 @@ TensorTests.test("WholeTensorSlicing") { | |
| slice2.array) | ||
| } | ||
|
|
||
| TensorTests.testAllBackends("AdvancedIndexing") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find any tests that use strides in subscripts. Could you make sure every feature is reasonably tested for correctness?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have no syntax for strides currently and so I thought of strided slices as a future feature. This PR adds support in the striding mechanism and that's all tested in TensorFlow core (we're calling the TF op directly with these strides without doing any processing on the Swift side). So, I believe these tests would better be added once we add better syntax for strided slices. Does that sound reasonable?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since a lot of logic is being wired up in pure Swift instead of going straight to TensorFlow, I wanted to be more careful landing unused core features without tests. It is not entirely the case that we can't use them in subscripts. You can define static computed properties
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right ok sounds good. I'll add these properties and associated tests later today. |
||
| // NOTE: cannot test multiple `Tensor.shape` or `Tensor.scalars` directly | ||
| // until send and receive are implemented (without writing a bunch of mini | ||
| // tests). Instead, `Tensor.array` is called to make a ShapedArray host copy | ||
| // and the ShapedArray is tested. | ||
| let tensor3D = Tensor<Float>(shape: [3, 4, 5], | ||
| scalars: Array(stride(from: 0.0, to: 60, by: 1))) | ||
| let element2D = tensor3D[1 ..< 3, 0, 3...] | ||
eaplatanios marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let array2D = element2D.array | ||
|
|
||
| // Test shape | ||
| expectEqual([2, 2], array2D.shape) | ||
|
|
||
| // Test scalars | ||
| expectEqual(Array([23.0, 24.0, 43.0, 44.0]), array2D.scalars) | ||
| } | ||
|
|
||
| TensorTests.testAllBackends("Reduction") { | ||
| // TODO(b/111815968): triage and fix this TPU issue | ||
| #if !TPU | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.