-
Notifications
You must be signed in to change notification settings - Fork 8
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
completed lstm #71
Merged
Merged
completed lstm #71
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
abfceb9
complete lstm_cell
mei1127 9a7e0a4
revised lstm_cell.js and lstm_cell_test.js
mei1127 24d6d03
completed lstm
mei1127 8d6e71e
revised lstm.js lstm_cell.js and validate-input.js
mei1127 95c0ab1
revised validata-input.js to ensure length<max-len
mei1127 6995143
revised some files related to squeeze
mei1127 bda1651
revised reduce.js
mei1127 c451f91
revised arg_max_min.js
mei1127 cf52d51
Merge branch 'main' into add_lstm
mei1127 190fe60
revised reshape.js
mei1127 76746d9
Merge branch 'add_lstm' of https://github.com/mei1127/webnn-baseline …
mei1127 d8ebc93
revised reshape.js
mei1127 5b098bc
revised reshape.js
mei1127 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,107 @@ | ||
'use strict'; | ||
|
||
import {concat} from './concat.js'; | ||
import {lstmCell} from './lstm_cell.js'; | ||
import {reshape} from './reshape.js'; | ||
import {sizeOfShape, Tensor} from './lib/tensor.js'; | ||
import {sigmoid} from './sigmoid.js'; | ||
import {slice} from './slice.js'; | ||
import {squeeze} from './squeeze.js'; | ||
import {tanh} from './tanh.js'; | ||
import {validateLstmParams} from './lib/validate-input.js'; | ||
|
||
/** | ||
*Long Short-Term Memory [LSTM] recurrent network uses an input, output, forget, | ||
*and cell gate to compute the output state that rolls into the output across the | ||
* temporal sequence of the network. | ||
* @param {Tensor} input | ||
* @param {Tensor} weight | ||
* @param {Tensor} recurrentWeight | ||
* @param {Number} steps | ||
* @param {Number} hiddenSize | ||
* @param {MLLstmOptions} options | ||
* @return {Array.<Tensor>} | ||
*/ | ||
export function lstm(input, weight, recurrentWeight, steps, hiddenSize, | ||
{bias, recurrentBias, peepholeWeight, initialHiddenState, | ||
initialCellState, returnSequence = false, direction = 'forward', layout = 'iofg', | ||
activations = [sigmoid, tanh, tanh]} = {}) { | ||
validateLstmParams(...arguments); | ||
const numDirections = (direction == 'both' ? 2 : 1); | ||
const batchSize = input.shape[1]; | ||
const inputSize = input.shape[2]; | ||
|
||
let hiddenState; | ||
let cellState; | ||
if (initialHiddenState) { | ||
hiddenState = initialHiddenState; | ||
} else { | ||
const initialHiddenStateShape = [numDirections, batchSize, hiddenSize]; | ||
hiddenState = new Tensor( | ||
initialHiddenStateShape, new Array(sizeOfShape(initialHiddenStateShape)).fill(0)); | ||
} | ||
if (initialCellState) { | ||
cellState = initialCellState; | ||
} else { | ||
const initialCellState = [numDirections, batchSize, hiddenSize]; | ||
cellState = new Tensor( | ||
initialCellState, new Array(sizeOfShape(initialCellState)).fill(0)); | ||
} | ||
|
||
let sequence; | ||
const currentWeight = []; | ||
const currentRecurrentWeight = []; | ||
const currentBias = []; | ||
const currentRecurrentBias = []; | ||
const currentPeepholeWeight = []; | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
currentWeight.push(squeeze(slice(weight, [dir, 0, 0], [1, 4 * hiddenSize, inputSize]))); | ||
currentRecurrentWeight.push(squeeze(slice(recurrentWeight, | ||
[dir, 0, 0], [1, 4 * hiddenSize, hiddenSize]))); | ||
currentBias.push(bias ? (squeeze(slice(bias, [dir, 0], [1, 4 * hiddenSize]))) : null); | ||
currentRecurrentBias.push(recurrentBias ? | ||
(squeeze(slice(recurrentBias, [dir, 0], [1, 4 * hiddenSize]))) : null); | ||
currentPeepholeWeight.push(peepholeWeight ? | ||
(squeeze(slice(peepholeWeight, [dir, 0], [1, 3 * hiddenSize]))) : null); | ||
} | ||
|
||
for (let step = 0; step < steps; ++step) { | ||
const currentHidden = []; | ||
const currentCell = []; | ||
let nextHidden = null; | ||
let nextCell = null; | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
currentHidden.push(squeeze(slice(hiddenState, [dir, 0, 0], [1, batchSize, hiddenSize]))); | ||
currentCell.push(squeeze(slice(cellState, [dir, 0, 0], [1, batchSize, hiddenSize]))); | ||
} | ||
|
||
for (let dir = 0; dir < numDirections; ++dir) { | ||
const slice0 = (dir == 1 || direction == 'backward' ? steps - step - 1 : step); | ||
const currentInput = squeeze(slice(input, [slice0, 0, 0], [1, batchSize, inputSize])); | ||
|
||
const results = lstmCell( | ||
currentInput, currentWeight[dir], currentRecurrentWeight[dir], | ||
currentHidden[dir], currentCell[dir], hiddenSize, {bias: currentBias[dir], | ||
recurrentBias: currentRecurrentBias[dir], peepholeWeight: currentPeepholeWeight[dir], | ||
layout: layout, activations: activations}); | ||
|
||
const output = reshape(results[0], [1, null, hiddenSize]); | ||
const cell = reshape(results[1], [1, null, hiddenSize]); | ||
|
||
nextHidden = (nextHidden ? concat([nextHidden, output], 0) : output); | ||
nextCell = (nextCell ? concat([nextCell, cell], 0) : cell); | ||
} | ||
|
||
hiddenState = nextHidden; | ||
cellState = nextCell; | ||
|
||
if (returnSequence) { | ||
nextHidden = reshape(nextHidden, [1, numDirections, null, hiddenSize]); | ||
sequence = (sequence ? concat([sequence, nextHidden], 0) : nextHidden); | ||
} | ||
} | ||
|
||
return (sequence ? [hiddenState, cellState, sequence] : [hiddenState, cellState]); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now squeeze op has been removed, would you please help also remove it from this WebNN Baseline.
You can refer to the given squeeze method in Spec, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The definition is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I will revise it next week:)