Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Add LogSoftmax for CPU #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion docs/operators.md
Original file line number Diff line number Diff line change
@@ -75,7 +75,7 @@ _This file is automatically generated from the def files via [this script](/tool
| [Less](https://github.com/onnx/onnx/blob/master/docs/Operators.md#Less) | | | [7-8](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Less-7), [9+](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Less-9) |
| [LessOrEqual](https://github.com/onnx/onnx/blob/master/docs/Operators.md#LessOrEqual) | | | |
| [Log](https://github.com/onnx/onnx/blob/master/docs/Operators.md#Log) | [6+](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Log-6) | | [6+](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#Log-6) |
| [LogSoftmax](https://github.com/onnx/onnx/blob/master/docs/Operators.md#LogSoftmax) | | | |
| [LogSoftmax](https://github.com/onnx/onnx/blob/master/docs/Operators.md#LogSoftmax) | [1-10](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#LogSoftmax-1), [11+](https://github.com/onnx/onnx/blob/master/docs/Changelog.md#LogSoftmax-11) | | |
| [Loop](https://github.com/onnx/onnx/blob/master/docs/Operators.md#Loop) | | | |
| [LpNormalization](https://github.com/onnx/onnx/blob/master/docs/Operators.md#LpNormalization) | | | |
| [LpPool](https://github.com/onnx/onnx/blob/master/docs/Operators.md#LpPool) | | | |
2 changes: 2 additions & 0 deletions lib/backends/cpu/op-resolve-rules.ts
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import {CpuGather} from './ops/gather';
import {CpuGemm} from './ops/gemm';
import {CpuImageScaler} from './ops/image-scaler';
import {CpuInstanceNormalization} from './ops/instance-normalization';
import {CpuLogSoftmax} from './ops/log-softmax';
import {CpuLrn} from './ops/lrn';
import {CpuMatMul} from './ops/matmul';
import {CpuPad} from './ops/pad';
@@ -72,6 +73,7 @@ export const CPU_OP_RESOLVE_RULES: ReadonlyArray<OpSet.ResolveRule> = [
['IsNaN', '', '9+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.isNan, undefined, 'bool')],
['LeakyRelu', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.leakyRelu, unaryOps.leakyReluInitializer)],
['Log', '', '6+', () => new CpuUnaryOp(FLOAT_TYPES, unaryOps.log)],
['LogSoftmax', '', '1+', () => new CpuLogSoftmax()],
['LRN', '', '1+', () => new CpuLrn()],
['MatMul', '', '1+', () => new CpuMatMul()],
['MaxPool', '', '1-9', () => new CpuMaxPool()], // TODO: support new attributes for MaxPool-8 and MaxPool-10
28 changes: 28 additions & 0 deletions lib/backends/cpu/ops/log-softmax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import {LogSoftmax} from '../../../ops/log-softmax';
import {Tensor} from '../../../tensor';
import {CpuInferenceHandler} from '../inference-handler';
import {softmax} from './softmax';

export class CpuLogSoftmax extends LogSoftmax {
run(inferenceHandler: CpuInferenceHandler, inputs: Tensor[]): Tensor[] {
const output = logSoftmax(inputs[0], this.axis);
return [output];
}
}

export function logSoftmax(x: Tensor, axis: number): Tensor {
const y = softmax(x, axis);
const yData = y.numberData;

const output = new Tensor(x.dims, x.type);
const data = output.numberData;

for (let i = 0; i < yData.length; ++i) {
data[i] = Math.log(yData[i]);
}

return output;
}
33 changes: 33 additions & 0 deletions lib/ops/log-softmax.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

import {Attribute} from '../attribute';
import {InferenceHandler} from '../backend';
import {Operator} from '../operators';
import {Tensor} from '../tensor';

export abstract class LogSoftmax implements Operator {
abstract run(inferenceHandler: InferenceHandler, inputs: Tensor[]): Tensor[]|Promise<Tensor[]>;

initialize(attributes: Attribute): void {
this.axis = attributes.getInt('axis', 1);
}

checkInputs(inputs: Tensor[]): boolean {
if (!inputs || inputs.length !== 1) {
return false;
}

return this.checkInputTypes(inputs);
}

protected checkInputTypes(inputs: Tensor[]): boolean {
if (inputs[0].type !== 'float32' && inputs[0].type !== 'float64') {
return false;
}

return true;
}

protected axis: number;
}
8 changes: 8 additions & 0 deletions test/test-suite-whitelist.jsonc
Original file line number Diff line number Diff line change
@@ -101,6 +101,13 @@
"test_leakyrelu",
// "test_lrn_default", <-- failing due to low precison. If absolute CPU error threshold is increased from 1e-4 to 1e-2 (100x increase), it passes the test.
// "test_lrn", <-- failing due to low precison. If absolute CPU error threshold is increased from 1e-4 to 1e-3 (10x increase), it passes the test.
"test_logsoftmax_example_1",
"test_logsoftmax_large_number",
"test_logsoftmax_axis_0",
"test_logsoftmax_axis_1",
"test_logsoftmax_axis_2",
"test_logsoftmax_negative_axis",
"test_logsoftmax_default_axis",
"test_matmul_2d",
"test_matmul_3d",
"test_matmul_4d",
@@ -245,6 +252,7 @@
"image-scaler.jsonc",
//"less.jsonc",
"log.jsonc",
// "log-softmax.jsonc",
"matmul.jsonc",
"mul.jsonc",
"neg.jsonc",