Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Support lstm and lstmCell operations #227

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
67 changes: 67 additions & 0 deletions src/nn/graph_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {HardSigmoid} from './ops/hard_sigmoid';
import {InstanceNormalization} from './ops/instance_norm';
import {LeakyRelu} from './ops/leaky_relu';
import {Linear} from './ops/linear';
import {Lstm, LstmCell} from './ops/lstm';
import {Pad} from './ops/pad';
import {AveragePool2d, L2Pool2d, MaxPool2d} from './ops/pool2d';
import {PRelu} from './ops/prelu';
Expand Down Expand Up @@ -211,9 +212,43 @@ export interface MLLinearOptions {
beta?: number;
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#enumdef-mllstmweightlayout)
*/
export enum MLLstmWeightLayout {
'iofg' = 'iofg', // input-output-forget-cell gate ordering
'ifgo' = 'ifgo' // input-forget-cell-output gate ordering
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#enumdef-mlpaddingmode)
*/
export interface MLLstmOptions {
bias?: MLOperand;
recurrentBias?: MLOperand;
peepholeWeight?: MLOperand;
initialHiddenState?: MLOperand;
initialCellState?: MLOperand;
returnSequence?: boolean;
direction?: MLRecurrentNetworkDirection;
layout?: MLLstmWeightLayout;
activations?: MLActivation[];
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#dictdef-mllstmcelloptions)
*/
export interface MLLstmCellOptions {
bias?: MLOperand;
recurrentBias?: MLOperand;
peepholeWeight?: MLOperand;
layout?: MLLstmWeightLayout;
activations?: MLActivation[];
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#dictdef-mllstmoptions)
*/
export enum MLPaddingMode {
'constant' = 'constant',
'edge' = 'edge',
Expand Down Expand Up @@ -769,6 +804,38 @@ export class MLGraphBuilder {
}
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-lstm)
*/
lstm(input: MLOperand, weight: MLOperand, recurrentWeight: MLOperand,
steps: number, hiddenSize: number, options: MLLstmOptions = {}):
MLOperand[] {
this.validateOperandBuilder([
input, weight, recurrentWeight, options.bias, options.recurrentBias,
options.peepholeWeight, options.initialHiddenState,
options.initialCellState
]);
return (new Lstm(
input, weight, recurrentWeight, steps, hiddenSize, options))
.outputs;
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#api-mlgraphbuilder-lstmcell)
*/
lstmCell(input: MLOperand, weight: MLOperand, recurrentWeight: MLOperand,
hiddenState: MLOperand, cellState: MLOperand, hiddenSize: number,
options: MLLstmCellOptions = {}): MLOperand[] {
this.validateOperandBuilder([
input, weight, recurrentWeight, hiddenState, cellState, options.bias,
options.recurrentBias, options.peepholeWeight
]);
return (new LstmCell(
input, weight, recurrentWeight, hiddenState, cellState,
hiddenSize, options))
.outputs;
}

/**
* [spec](https://webmachinelearning.github.io/webnn/#dom-mlgraphbuilder-matmul)
*/
Expand Down
Loading