Skip to content
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

Add TensorListLength op #6282

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tfjs-converter/docs/supported_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
|TensorListFromTensor|TensorListFromTensor|
|TensorListGather|TensorListGather|
|TensorListGetItem|TensorListGetItem|
|TensorListLength|TensorListLength|
|TensorListPopBack|TensorListPopBack|
|TensorListPushBack|TensorListPushBack|
|TensorListReserve|TensorListReserve|
Expand Down
13 changes: 12 additions & 1 deletion tfjs-converter/python/tensorflowjs/op_list/control.json
Original file line number Diff line number Diff line change
Expand Up @@ -810,5 +810,16 @@
"type": "dtype"
}
]
},
{
"tfOpName": "TensorListLength",
"category": "control",
"inputs": [
{
"start": 0,
"name": "tensorListId",
"type": "tensor"
}
]
}
]
]
1 change: 1 addition & 0 deletions tfjs-converter/src/metadata_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('kernel2op metadata file', () => {
'TensorListFromTensor',
'TensorListGather',
'TensorListGetItem',
'TensorListLength',
'TensorListPopBack',
'TensorListPushBack',
'TensorListReserve',
Expand Down
6 changes: 6 additions & 0 deletions tfjs-converter/src/operations/executors/control_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ export const executeOp: InternalOpAsyncExecutor = async(
context.addTensorList(tensorList);
return [tensorList.idTensor];
}
case 'TensorListLength': {
const idTensor =
getParamValue('tensorListId', node, tensorMap, context) as Tensor;
const tensorList = context.getTensorList(idTensor.id);
return [scalar(tensorList.size(), 'int32')];
}
default:
throw TypeError(`Node type ${node.op} is not implemented`);
}
Expand Down
24 changes: 24 additions & 0 deletions tfjs-converter/src/operations/executors/control_executor_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -993,5 +993,29 @@ describe('control', () => {
expect(validateParam(node, control.json)).toBeTruthy();
});
});

describe('TensorListLength', () => {
it('should get the size of tensorList', async () => {
const input4 = tensor1d([0, 0, 0], 'int32');
const input5 = tensor1d([1, 1, 1], 'int32');
const tensorList = new TensorList([input4, input5], [3], 'int32', 5);
context.addTensorList(tensorList);
node.op = 'TensorListLength';
node.inputParams['tensorListId'] = createTensorAttr(0);
node.inputNames = ['input2'];
const input2 = [tensorList.idTensor];
const size = await executeOp(node, {input2}, context);
expect(size.length).toEqual(1);
expect(size[0].shape).toEqual([]);
test_util.expectArraysClose(size[0].dataSync(), [2]);
});

it('should match json def', () => {
node.op = 'TensorListLength';
node.inputParams['tensorListId'] = createTensorAttr(0);

expect(validateParam(node, control.json)).toBeTruthy();
});
});
});
});