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

Fix output sequence issue of lstm with backward direction and both direction #123

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
31 changes: 27 additions & 4 deletions src/lstm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import {concat} from './concat.js';
import {lstmCell} from './lstm_cell.js';
import {reshape, squeeze} from './reshape.js';
import {reverse} from './reverse.js';
import {sizeOfShape, Tensor} from './lib/tensor.js';
import {sigmoid} from './sigmoid.js';
import {split} from './split.js';
import {slice} from './slice.js';
import {tanh} from './tanh.js';
import {validateLstmParams} from './lib/validate-input.js';
Expand Down Expand Up @@ -85,10 +87,11 @@ export function lstm(input, weight, recurrentWeight, steps, hiddenSize,
currentHidden[dir], currentCell[dir], hiddenSize, {bias: currentBias[dir],
recurrentBias: currentRecurrentBias[dir], peepholeWeight: currentPeepholeWeight[dir],
layout: layout, activations: activations});
// Expand [batchSize, hiddenSize] to [numDirections, batchSize, hiddenSize]
const output = reshape(results[0], [1, batchSize, hiddenSize]);
const cell = reshape(results[1], [1, batchSize, hiddenSize]);

const output = reshape(results[0], [1, null, hiddenSize]);
const cell = reshape(results[1], [1, null, hiddenSize]);

// Concat along axis 0 (numDirections dimension)
nextHidden = (nextHidden ? concat([nextHidden, output], 0) : output);
nextCell = (nextCell ? concat([nextCell, cell], 0) : cell);
}
Expand All @@ -97,10 +100,30 @@ export function lstm(input, weight, recurrentWeight, steps, hiddenSize,
cellState = nextCell;

if (returnSequence) {
nextHidden = reshape(nextHidden, [1, numDirections, null, hiddenSize]);
// Expand [numDirections, batchSize, hiddenSize] to
// [steps, numDirections, batchSize, hiddenSize]
nextHidden = reshape(nextHidden, [1, numDirections, batchSize, hiddenSize]);
// Concat output sequence along axis 0 (steps dimension)
sequence = (sequence ? concat([sequence, nextHidden], 0) : nextHidden);
}
}

if (direction === 'backward') {
// Refer to https://www.w3.org/TR/webnn/#api-mlgraphbuilder-lstm, Spec says the
// sequence should contain every output from each time step in the temporal sequence, while
// the loop for steps concatenates sequence in a reversed order when direction is backward,
// so here need reverse output sequence along axis 0 (steps dimension).
sequence = reverse(sequence, {axes: [0]});
} else if (direction === 'both') {
// Split output sequence into forward-sequence and backward-sequence two sequences along axis 1
// (numDirections dimension)
const [sequenceForward, sequenceBackward] = split(sequence, 2, {axis: 1});
// Reverse backward-sequence along axis 0 (steps dimension)
const reversedSequenceBackward = reverse(sequenceBackward, {axes: [0]});
sequence = concat([sequenceForward, reversedSequenceBackward], 1);
} else {
// No need update sequence for 'forward' direction
}

return (sequence ? [hiddenState, cellState, sequence] : [hiddenState, cellState]);
}
74 changes: 67 additions & 7 deletions test/lstm_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ describe('test lstm', function() {
input, weight, recurrentWeight, steps, hiddenSize,
{bias, recurrentBias, peepholeWeight, initialHiddenState,
initialCellState, returnSequence, activations});
console.log('outputs: ', outputs);
utils.checkShape(outputs[0], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[1], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[2], [steps, numDirections, batchSize, hiddenSize]);
Expand All @@ -65,7 +64,7 @@ describe('test lstm', function() {
}
});

it('lstm steps=2 direction="backward" returnSequence=true' +
it('lstm steps=2 direction="backward" returnSequence=true ' +
'activations=[relu, relu, relu]', function() {
const steps = 2;
const numDirections = 1;
Expand Down Expand Up @@ -106,22 +105,83 @@ describe('test lstm', function() {
input, weight, recurrentWeight, steps, hiddenSize,
{bias, recurrentBias, peepholeWeight, initialHiddenState,
initialCellState, direction, returnSequence, activations});
console.log('outputs: ', outputs);
utils.checkShape(outputs[0], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[1], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[2], [steps, numDirections, batchSize, hiddenSize]);
const expected = [
[10.469, 58.02899999999999, 74.529, 518.9490000000001],
[5.51, 20.009999999999998, 19.11, 75.21000000000001],
[
1,
8,
1,
8,
10.469,
58.02899999999999,
74.529,
518.9490000000001,
1,
8,
1,
8,
],
];
for (let i = 0; i < expected.length; ++i) {
utils.checkValue(outputs[i], expected[i]);
}
});

it('lstm steps=2 direction="both" returnSequence=true', function() {
const steps = 2;
const numDirections = 2;
const batchSize = 2;
const inputSize = 2;
const hiddenSize = 2;
const input = new Tensor([steps, batchSize, inputSize],
new Float32Array([1, 2, 2, 1, 3, 4, 1, 2]));
const weight = new Tensor([numDirections, 4 * hiddenSize, inputSize],
new Float32Array([
1, -1, 2, -2, 1, -1, 2, -2,
1, -1, 2, -2, 1, -1, 2, -2,
1, -1, 2, -2, 1, -1, 2, -2,
1, -1, 2, -2, 1, -1, 2, -2,
]));
const recurrentWeight = new Tensor([numDirections, 4 * hiddenSize, hiddenSize],
new Array(2 * 4 * hiddenSize * hiddenSize).fill(0.1));
const bias = new Tensor([numDirections, 4 * hiddenSize],
new Float32Array([
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
]));
const recurrentBias = new Tensor([numDirections, 4 * hiddenSize],
new Float32Array([
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2,
]));
const returnSequence = true;
const direction = 'both';
const outputs = lstm(
input, weight, recurrentWeight, steps, hiddenSize,
{bias, recurrentBias, direction, returnSequence});
utils.checkShape(outputs[0], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[1], [numDirections, batchSize, hiddenSize]);
utils.checkShape(outputs[2], [steps, numDirections, batchSize, hiddenSize]);
const expected = [
[
0.5764073262004139, 0.8236227651782412,
0.6612355785279247, 0.8442635760318142,
0.5764073262004139, 0.8236227651782412,
0.8635294727880538, 0.9491350760903781,
],
[
1.0171455721466105, 1.6205496282195793,
1.338846378789257, 1.7642604746965693,
1.0171455721466105, 1.6205496282195793,
1.485626937219704, 1.8449554199024933,
],
[
0.36960635293570576, 0.6082834181835157,
0.7037753329989016, 0.7586680430344475,
0.5764073262004139, 0.8236227651782412,
0.8635294727880538, 0.9491350760903781,
0.5764073262004139, 0.8236227651782412,
0.6612355785279247, 0.8442635760318142,
0.36960635293570576, 0.6082834181835157,
0.36960635293570576, 0.6082834181835157,
],
];
for (let i = 0; i < expected.length; ++i) {
Expand Down
Loading