|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 Google Inc. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {describeWithFlags} from '../jasmine_util'; |
| 19 | +import {ALL_ENVS, CPU_ENVS, expectArraysClose} from '../test_util'; |
| 20 | + |
| 21 | +import {gatherND} from './gather_nd'; |
| 22 | +import {scalar, tensor1d, tensor2d, tensor3d} from './tensor_ops'; |
| 23 | + |
| 24 | +describeWithFlags('gatherND', ALL_ENVS, () => { |
| 25 | + it('should work for simple slice', () => { |
| 26 | + const indices = tensor2d([0, 4, 8], [3, 1], 'int32'); |
| 27 | + const input = |
| 28 | + tensor1d([100, 101, 102, 777, 778, 779, 1000, 1001, 1002], 'int32'); |
| 29 | + const shape = [3]; |
| 30 | + const result = gatherND(input, indices); |
| 31 | + expect(result.shape).toEqual(shape); |
| 32 | + expect(result.dtype).toEqual(input.dtype); |
| 33 | + expectArraysClose(result, [100, 778, 1002]); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should work for indexing 2d', () => { |
| 37 | + const indices = tensor2d([0, 2], [2, 1], 'int32'); |
| 38 | + const input = tensor2d( |
| 39 | + [ |
| 40 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, |
| 41 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8 |
| 42 | + ], |
| 43 | + [8, 4], 'float32'); |
| 44 | + const shape = [2, 4]; |
| 45 | + const result = gatherND(input, indices); |
| 46 | + expect(result.shape).toEqual(shape); |
| 47 | + expect(result.dtype).toEqual(input.dtype); |
| 48 | + expectArraysClose(result, [5, 5, 5, 5, 7, 7, 7, 7]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should work for indexing 3d', () => { |
| 52 | + const indices = tensor2d([0, 2, 1, 1], [2, 2], 'int32'); |
| 53 | + const input = tensor3d( |
| 54 | + [ |
| 55 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, |
| 56 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8 |
| 57 | + ], |
| 58 | + [2, 4, 4], 'float32'); |
| 59 | + const shape = [2, 4]; |
| 60 | + const result = gatherND(input, indices); |
| 61 | + expect(result.shape).toEqual(shape); |
| 62 | + expect(result.dtype).toEqual(input.dtype); |
| 63 | + expectArraysClose(result, [7, 7, 7, 7, 6, 6, 6, 6]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should work for batch slice', () => { |
| 67 | + const indices = tensor3d([0, 4, 2], [3, 1, 1], 'int32'); |
| 68 | + const input = |
| 69 | + tensor1d([100, 101, 102, 777, 778, 779, 10000, 10001, 10002], 'int32'); |
| 70 | + const shape = [3, 1]; |
| 71 | + const result = gatherND(input, indices); |
| 72 | + expect(result.shape).toEqual(shape); |
| 73 | + expect(result.dtype).toEqual(input.dtype); |
| 74 | + expectArraysClose(result, [100, 778, 102]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should work for batch indexing 2d', () => { |
| 78 | + const indices = tensor3d([0, 2], [2, 1, 1], 'int32'); |
| 79 | + const input = tensor2d( |
| 80 | + [ |
| 81 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, |
| 82 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8 |
| 83 | + ], |
| 84 | + [8, 4], 'float32'); |
| 85 | + const shape = [2, 1, 4]; |
| 86 | + const result = gatherND(input, indices); |
| 87 | + expect(result.shape).toEqual(shape); |
| 88 | + expect(result.dtype).toEqual(input.dtype); |
| 89 | + expectArraysClose(result, [5, 5, 5, 5, 7, 7, 7, 7]); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should work for batch indexing 3d', () => { |
| 93 | + const indices = tensor3d([0, 2, 1, 1], [2, 1, 2], 'int32'); |
| 94 | + const input = tensor3d( |
| 95 | + [ |
| 96 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, |
| 97 | + 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8 |
| 98 | + ], |
| 99 | + [2, 4, 4], 'float32'); |
| 100 | + const shape = [2, 1, 4]; |
| 101 | + const result = gatherND(input, indices); |
| 102 | + expect(result.shape).toEqual(shape); |
| 103 | + expect(result.dtype).toEqual(input.dtype); |
| 104 | + expectArraysClose(result, [7, 7, 7, 7, 6, 6, 6, 6]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should work for TensorLike inputs', () => { |
| 108 | + const indices = [[0], [4], [8]]; |
| 109 | + const input = [100, 101, 102, 777, 778, 779, 1000, 1001, 1002]; |
| 110 | + const shape = [3]; |
| 111 | + const result = gatherND(input, indices); |
| 112 | + expect(result.shape).toEqual(shape); |
| 113 | + expect(result.dtype).toEqual('float32'); |
| 114 | + expectArraysClose(result, [100, 778, 1002]); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should throw error when indices are not int32', () => { |
| 118 | + const indices = tensor1d([1], 'float32'); |
| 119 | + const input = tensor2d( |
| 120 | + [100, 101, 102, 103, 777, 778, 779, 780, 10000, 10001, 10002, 10004], |
| 121 | + [3, 4], 'float32'); |
| 122 | + expect(() => gatherND(input, indices)).toThrow(); |
| 123 | + }); |
| 124 | + it('should throw error when indices are scalar', () => { |
| 125 | + const indices = scalar(1, 'int32'); |
| 126 | + const input = tensor2d( |
| 127 | + [100, 101, 102, 103, 777, 778, 779, 780, 10000, 10001, 10002, 10004], |
| 128 | + [3, 4], 'float32'); |
| 129 | + expect(() => gatherND(input, indices)).toThrow(); |
| 130 | + }); |
| 131 | + it('should throw error when x is scalar', () => { |
| 132 | + const indices = tensor2d([0, 4, 2], [3, 1], 'int32'); |
| 133 | + const input = scalar(1.0, 'float32'); |
| 134 | + expect(() => gatherND(input, indices)).toThrow(); |
| 135 | + }); |
| 136 | + it('should throw error when indices inner dim > x shape length', () => { |
| 137 | + const indices = tensor2d([0, 4, 2], [1, 3], 'int32'); |
| 138 | + const input = |
| 139 | + tensor2d([100, 101, 102, 10000, 10001, 10002], [3, 2], 'float32'); |
| 140 | + expect(() => gatherND(input, indices)).toThrow(); |
| 141 | + }); |
| 142 | +}); |
| 143 | +describeWithFlags('gatherND CPU', CPU_ENVS, () => { |
| 144 | + it('should throw error when index out of range', () => { |
| 145 | + const indices = tensor2d([0, 2, 99], [3, 1], 'int32'); |
| 146 | + const input = tensor2d( |
| 147 | + [100, 101, 102, 777, 778, 779, 10000, 10001, 10002], [3, 3], 'float32'); |
| 148 | + expect(() => gatherND(input, indices)).toThrow(); |
| 149 | + }); |
| 150 | +}); |
0 commit comments