-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added support for sparseReshape op for converter (#4963)
FEATURE * added support for sparseReshape op for converter * fix lint * update kernel2op
- Loading branch information
Showing
8 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
[ | ||
{ | ||
"tfOpName": "SparseReshape", | ||
"category": "sparse", | ||
"inputs": [ | ||
{ | ||
"start": 0, | ||
"name": "inputIndices", | ||
"type": "tensor" | ||
}, | ||
{ | ||
"start": 1, | ||
"name": "inputShape", | ||
"type": "tensor" | ||
}, | ||
{ | ||
"start": 2, | ||
"name": "newShape", | ||
"type": "tensor" | ||
} | ||
], | ||
"attrs": [ | ||
{ | ||
"tfName": "T", | ||
"name": "dtype", | ||
"type": "dtype", | ||
"notSupported": true | ||
} | ||
] | ||
} | ||
] |
45 changes: 45 additions & 0 deletions
45
tfjs-converter/src/operations/executors/sparse_executor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {Tensor, Tensor1D, Tensor2D} from '@tensorflow/tfjs-core'; | ||
// tslint:disable-next-line: no-imports-from-dist | ||
import * as tfOps from '@tensorflow/tfjs-core/dist/ops/ops_for_converter'; | ||
|
||
import {NamedTensorsMap} from '../../data/types'; | ||
import {ExecutionContext} from '../../executor/execution_context'; | ||
import {InternalOpExecutor, Node} from '../types'; | ||
|
||
import {getParamValue} from './utils'; | ||
|
||
export const executeOp: InternalOpExecutor = | ||
(node: Node, tensorMap: NamedTensorsMap, | ||
context: ExecutionContext): Tensor[] => { | ||
switch (node.op) { | ||
case 'SparseReshape': { | ||
const {outputIndices, outputShape} = tfOps.sparse.sparseReshape( | ||
getParamValue('inputIndices', node, tensorMap, context) as | ||
Tensor2D, | ||
getParamValue('inputShape', node, tensorMap, context) as Tensor1D, | ||
getParamValue('newShape', node, tensorMap, context) as Tensor1D); | ||
return [outputIndices, outputShape]; | ||
} | ||
default: | ||
throw TypeError(`Node type ${node.op} is not implemented`); | ||
} | ||
}; | ||
|
||
export const CATEGORY = 'convolution'; |
76 changes: 76 additions & 0 deletions
76
tfjs-converter/src/operations/executors/sparse_executor_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
import {Tensor, test_util} from '@tensorflow/tfjs-core'; | ||
// tslint:disable-next-line: no-imports-from-dist | ||
import * as tfOps from '@tensorflow/tfjs-core/dist/ops/ops_for_converter'; | ||
|
||
import {ExecutionContext} from '../../executor/execution_context'; | ||
import * as sparse from '../op_list/sparse'; | ||
import {Node} from '../types'; | ||
|
||
import {executeOp} from './sparse_executor'; | ||
import {createTensorAttr, validateParam} from './test_helper'; | ||
|
||
describe('sparse', () => { | ||
let node: Node; | ||
const inputIndices = [tfOps.tensor2d( | ||
[0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 2, 3], [5, 3], 'int32')]; | ||
const inputShape = [tfOps.tensor1d([2, 3, 6], 'int32')]; | ||
const newShape = [tfOps.tensor1d([9, -1], 'int32')]; | ||
const context = new ExecutionContext({}, {}, {}); | ||
|
||
beforeEach(() => { | ||
node = { | ||
name: 'test', | ||
op: '', | ||
category: 'sparse', | ||
inputNames: ['inputIndices', 'inputShape', 'newShape'], | ||
inputs: [], | ||
inputParams: { | ||
inputIndices: createTensorAttr(0), | ||
inputShape: createTensorAttr(1), | ||
newShape: createTensorAttr(2) | ||
}, | ||
attrParams: {}, | ||
children: [] | ||
}; | ||
}); | ||
|
||
describe('executeOp', () => { | ||
describe('SparseReshape', () => { | ||
it('should call tfOps.sparse.sparseReshape', async () => { | ||
spyOn(tfOps.sparse, 'sparseReshape').and.callThrough(); | ||
node.op = 'SparseReshape'; | ||
const result = | ||
executeOp(node, {inputIndices, inputShape, newShape}, context) as | ||
Tensor[]; | ||
|
||
expect(tfOps.sparse.sparseReshape) | ||
.toHaveBeenCalledWith(inputIndices[0], inputShape[0], newShape[0]); | ||
test_util.expectArraysClose( | ||
await result[0].data(), [0, 0, 0, 1, 1, 2, 4, 2, 8, 1]); | ||
test_util.expectArraysClose(await result[1].data(), [9, 4]); | ||
}); | ||
|
||
it('should match json def', () => { | ||
node.op = 'SparseReshape'; | ||
|
||
expect(validateParam(node, sparse.json)).toBeTruthy(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {OpMapper} from '../types'; | ||
|
||
export const json: OpMapper[] = [{ | ||
'tfOpName': 'SparseReshape', | ||
'category': 'sparse', | ||
'inputs': [ | ||
{'start': 0, 'name': 'inputIndices', 'type': 'tensor'}, | ||
{'start': 1, 'name': 'inputShape', 'type': 'tensor'}, | ||
{'start': 2, 'name': 'newShape', 'type': 'tensor'}, | ||
], | ||
'attrs': | ||
[{'tfName': 'T', 'name': 'dtype', 'type': 'dtype', 'notSupported': true}] | ||
}]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters