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
21 changes: 15 additions & 6 deletions tfjs-converter/src/operations/executors/slice_join_executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import {InternalOpExecutor, Node} from '../types';
import {getParamValue} from './utils';

export const executeOp: InternalOpExecutor = (node: Node,
tensorMap: NamedTensorsMap,
context: ExecutionContext):
tfc.Tensor[] => {
tensorMap: NamedTensorsMap,
context: ExecutionContext):
tfc.Tensor[] => {
switch (node.op) {
case 'ConcatV2':
case 'Concat': {
Expand Down Expand Up @@ -126,9 +126,18 @@ export const executeOp: InternalOpExecutor = (node: Node,
const numOrSizeSplits =
getParamValue('numOrSizeSplits', node, tensorMap, context) as number |
number[];
return tfc.split(
getParamValue('x', node, tensorMap, context) as tfc.Tensor,
numOrSizeSplits, axis);
const tensor = getParamValue('x', node, tensorMap, context) as tfc.Tensor;

// Allow the last number of split array to be -1, which indicates the rest
// of dimension is allocated to the last split.
if (Array.isArray(numOrSizeSplits)) {
const negIndex = numOrSizeSplits.indexOf(-1);
if (negIndex !== -1) {
const total = numOrSizeSplits.reduce((a, b) => b > 0 ? a + b : a);
numOrSizeSplits[negIndex] = tensor.shape[axis] - total;
}
}
return tfc.split(tensor, numOrSizeSplits, axis);
}
case 'ScatterNd': {
const indices =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import * as slice_join from '../op_list/slice_join';
import {Node} from '../types';

import {executeOp} from './slice_join_executor';
import {createNumberAttr, createNumberAttrFromIndex, createNumericArrayAttrFromIndex, createTensorAttr, createTensorsAttr, validateParam} from './test_helper';
import {createNumberAttr, createNumberAttrFromIndex, createNumericArrayAttr, createNumericArrayAttrFromIndex, createTensorAttr, createTensorsAttr, validateParam} from './test_helper';

describe('slice join', () => {
let node: Node;
Expand Down Expand Up @@ -337,6 +337,19 @@ describe('slice join', () => {

expect(tfc.split).toHaveBeenCalledWith(input2[0], 2, 1);
});
it('should support -1 split', () => {
spyOn(tfc, 'split');
node.op = 'Split';
node.inputParams.axis = createNumberAttrFromIndex(0);
node.inputParams.x = createTensorAttr(1);
node.attrParams.numOrSizeSplits = createNumericArrayAttr([1, 1, -1]);
node.inputNames = ['input4', 'input3'];
const input3 = [tfc.tensor1d([1, 2, 3, 4, 5])];
const input4 = [tfc.scalar(0)];
executeOp(node, {input3, input4}, context);

expect(tfc.split).toHaveBeenCalledWith(input3[0], [1, 1, 3], 0);
});
it('should match json def for split', () => {
node.op = 'Split';
node.inputParams.axis = createNumberAttrFromIndex(0);
Expand Down