Skip to content
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
6 changes: 4 additions & 2 deletions tfjs-converter/src/executor/tensor_array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ export class TensorArray {
throw new Error(`TensorArray ${this.name} has already been closed.`);
}

if (index < 0 || index >= this.tensors.length) {
if (index < 0 || index >= this.size()) {
throw new Error(`Tried to read from index ${index}, but array size is: ${
this.tensors.length}`);
this.size()}`);
}

const tensorWithState = this.tensors[index];
Expand Down Expand Up @@ -182,6 +182,8 @@ export class TensorArray {
for (let i = 0; i < this.size(); i++) {
indices.push(i);
}
} else {
indices = indices.slice(0, this.size());
}

if (indices.length === 0) {
Expand Down
5 changes: 5 additions & 0 deletions tfjs-converter/src/executor/tensor_array_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ describe('TensorArray', () => {
expect(gathered.shape).toEqual([2, 1, 1]);
test_util.expectArraysClose(await gathered.data(), [2, 1]);
});
it('should return when indices longer than available tensors', async () => {
const gathered = tensorArray.gather([1, 0, 2, 3]);
expect(gathered.shape).toEqual([2, 1, 1]);
test_util.expectArraysClose(await gathered.data(), [2, 1]);
});
it('should fail if dtype is not matched', () => {
expect(() => tensorArray.gather([0, 1], 'float32')).toThrow();
});
Expand Down