Skip to content
This repository has been archived by the owner on Aug 15, 2019. It is now read-only.

Add complex64 support for tf.abs(). #1331

Merged
merged 5 commits into from
Oct 18, 2018
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
16 changes: 12 additions & 4 deletions src/kernels/backend_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,13 +1107,21 @@ export class MathBackendCPU implements KernelBackend {
}

abs<T extends Tensor>(x: T): T {
this.assertNotComplex(x, 'abs');

const resultValues = new Float32Array(x.size);
const values = x.dataSync();
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.abs(values[i]);

if (x.dtype === 'complex64') {
for (let i = 0; i < x.size; ++i) {
const real = values[i * 2];
const imag = values[i * 2 + 1];
resultValues[i] = Math.sqrt(real * real + imag * imag);
}
} else {
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.abs(values[i]);
}
}

return Tensor.make(x.shape, {values: resultValues}) as T;
}

Expand Down
12 changes: 12 additions & 0 deletions src/kernels/backend_webgl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {BinaryOpComplexProgram} from './webgl/binaryop_complex_gpu';
import * as binaryop_gpu from './webgl/binaryop_gpu';
import {BinaryOpProgram} from './webgl/binaryop_gpu';
import {ClipProgram} from './webgl/clip_gpu';
import {ComplexAbsProgram} from './webgl/complex_abs_gpu';
import {ConcatProgram} from './webgl/concat_gpu';
import {Conv2DDerFilterProgram, Conv2DDerInputProgram} from './webgl/conv_backprop_gpu';
import {DepthwiseConv2DDerFilterProgram, DepthwiseConv2DDerInputProgram} from './webgl/conv_backprop_gpu_depthwise';
Expand Down Expand Up @@ -1231,6 +1232,17 @@ export class MathBackendWebGL implements KernelBackend {
}

abs<T extends Tensor>(x: T): T {
if (x.dtype === 'complex64') {
const xData = this.texData.get(x.dataId);

const program = new ComplexAbsProgram(x.shape);
const inputs = [
this.makeComplexComponentTensorHandle(x, xData.complexTensors.real),
this.makeComplexComponentTensorHandle(x, xData.complexTensors.imag),
];

return this.compileAndRun<Tensor>(program, inputs) as T;
}
const program = new UnaryOpProgram(x.shape, unary_op.ABS);
return this.compileAndRun(program, [x]) as T;
}
Expand Down
37 changes: 37 additions & 0 deletions src/kernels/webgl/complex_abs_gpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* @license
* Copyright 2018 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/

import {GPGPUProgram} from './gpgpu_math';

export class ComplexAbsProgram implements GPGPUProgram {
variableNames = ['real', 'imag'];
userCode: string;
outputShape: number[];

constructor(shape: number[]) {
this.outputShape = shape;
this.userCode = `
void main() {
float real = getRealAtOutCoords();
float imag = getImagAtOutCoords();
vec2 v = vec2(real, imag);

setOutput(sqrt(dot(v, v)));
}
`;
}
}
36 changes: 36 additions & 0 deletions src/ops/unary_ops_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ describeWithFlags('abs', ALL_ENVS, () => {
expectArraysClose(result, [1, 2, 5, 3, 1, 4, 7, 8]);
});

it('complex64 rank-1', () => {
const a = tf.complex([-2, -1, 0, 1, 2], [1, 2, 3, 0, -1]);
const result = tf.abs(a);
expectArraysClose(result, [
Math.sqrt(-2 * -2 + 1 * 1), Math.sqrt(-1 * -1 + 2 * 2),
Math.sqrt(0 * 0 + 3 * 3), Math.sqrt(1 * 1 + 0 * 0),
Math.sqrt(2 * 2 + -1 * -1)
]);
expect(result.shape).toEqual([5]);
});

it('complex64 rank-2', () => {
const a = tf.complex([[-3, -2, -1], [0, 1, 2]], [[4, 1, 2], [3, 0, -1]]);
const result = tf.abs(a);
expectArraysClose(result, [
Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1),
Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3),
Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1)
]);
expect(result.shape).toEqual([2, 3]);
});

it('complex64 rank-3', () => {
const a = tf.complex(
[[[-3, -2], [-1, 0]], [[1, 2], [3, 4]]],
[[[4, 1], [2, 3]], [[0, -1], [-3, -4]]]);
const result = tf.abs(a);
expectArraysClose(result, [
Math.sqrt(-3 * -3 + 4 * 4), Math.sqrt(-2 * -2 + 1 * 1),
Math.sqrt(-1 * -1 + 2 * 2), Math.sqrt(0 * 0 + 3 * 3),
Math.sqrt(1 * 1 + 0 * 0), Math.sqrt(2 * 2 + -1 * -1),
Math.sqrt(3 * 3 + -3 * -3), Math.sqrt(4 * 4 + -4 * -4)
]);
expect(result.shape).toEqual([2, 2, 2]);
});

it('propagates NaNs', () => {
const a = tf.tensor1d([1, -2, 0, 3, -0.1, NaN]);
const result = tf.abs(a);
Expand Down