Skip to content
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
479 changes: 3 additions & 476 deletions tfjs-backend-cpu/src/backend_cpu.ts

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions tfjs-backend-cpu/src/kernels/Abs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license
* Copyright 2020 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 {Abs, AbsInputs, KernelConfig, KernelFunc, TypedArray, util} from '@tensorflow/tfjs-core';

import {MathBackendCPU} from '../backend_cpu';

export const absKernelFunc =
(args: {inputs: AbsInputs, backend: MathBackendCPU}) => {
const {x} = args.inputs;
const cpuBackend = args.backend;
const resultValues = new Float32Array(util.sizeFromShape(x.shape));
if (x.dtype !== 'complex64') {
const values = cpuBackend.data.get(x.dataId).values as TypedArray;
for (let i = 0; i < values.length; ++i) {
resultValues[i] = Math.abs(values[i]);
}
} else {
const complexVals = cpuBackend.data.get(x.dataId);
const real = complexVals.complexTensorInfos.real;
const imag = complexVals.complexTensorInfos.imag;
const realVals =
cpuBackend.data.get(real.dataId).values as Float32Array;
const imagVals =
cpuBackend.data.get(imag.dataId).values as Float32Array;
for (let i = 0; i < realVals.length; i++) {
const real = realVals[i];
const imag = imagVals[i];
resultValues[i] = Math.hypot(real, imag);
}
}
return cpuBackend.makeOutput(resultValues, x.shape, 'float32');
};

export const absConfig: KernelConfig = {
kernelName: Abs,
backendName: 'cpu',
kernelFunc: absKernelFunc as {} as KernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Acos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Acos, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const acosKernelFunc = unaryKernelFunc(Acos, (xi) => Math.acos(xi));

export const acosConfig: KernelConfig = {
kernelName: Acos,
backendName: 'cpu',
kernelFunc: acosKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Acosh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Acosh, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const acoshKernelFunc = unaryKernelFunc(Acosh, (xi) => Math.acosh(xi));

export const acoshConfig: KernelConfig = {
kernelName: Acosh,
backendName: 'cpu',
kernelFunc: acoshKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Asin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Asin, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const asinKernelFunc = unaryKernelFunc(Asin, (xi) => Math.asin(xi));

export const asinConfig: KernelConfig = {
kernelName: Asin,
backendName: 'cpu',
kernelFunc: asinKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Asinh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Asinh, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const asinhKernelFunc = unaryKernelFunc(Asinh, (xi) => Math.asinh(xi));

export const asinhConfig: KernelConfig = {
kernelName: Asinh,
backendName: 'cpu',
kernelFunc: asinhKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Atan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Atan, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const atanKernelFunc = unaryKernelFunc(Atan, (xi) => Math.atan(xi));

export const atanConfig: KernelConfig = {
kernelName: Atan,
backendName: 'cpu',
kernelFunc: atanKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Atanh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Atanh, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const atanhKernelFunc = unaryKernelFunc(Atanh, (xi) => Math.atanh(xi));

export const atanhConfig: KernelConfig = {
kernelName: Atanh,
backendName: 'cpu',
kernelFunc: atanhKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Ceil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Ceil, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const ceilKernelFunc = unaryKernelFunc(Ceil, (xi) => Math.ceil(xi));

export const ceilConfig: KernelConfig = {
kernelName: Ceil,
backendName: 'cpu',
kernelFunc: ceilKernelFunc,
};
34 changes: 34 additions & 0 deletions tfjs-backend-cpu/src/kernels/Clip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @license
* Copyright 2020 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 {ClipByValue, ClipByValueAttrs, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const clipKernelFunc = unaryKernelFunc(ClipByValue, (xi, attrs) => {
const clipAttrs = attrs as {} as ClipByValueAttrs;
if (xi > clipAttrs.clipValueMax) {
return clipAttrs.clipValueMax;
}
return xi < clipAttrs.clipValueMin ? clipAttrs.clipValueMin : xi;
});

export const clipConfig: KernelConfig = {
kernelName: ClipByValue,
backendName: 'cpu',
kernelFunc: clipKernelFunc,
};
22 changes: 5 additions & 17 deletions tfjs-backend-cpu/src/kernels/Cos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,14 @@
* =============================================================================
*/

import {Cos, CosInputs, KernelConfig, TypedArray, util} from '@tensorflow/tfjs-core';
import {Cos, KernelConfig} from '@tensorflow/tfjs-core';

import {MathBackendCPU} from '../backend_cpu';
import {assertNotComplex} from '../cpu_util';
import {unaryKernelFunc} from '../utils/unary_utils';

export const cosKernelFunc = unaryKernelFunc(Cos, (xi) => Math.cos(xi));

export const cosConfig: KernelConfig = {
kernelName: Cos,
backendName: 'cpu',
kernelFunc: ({inputs, backend}) => {
const {x} = inputs as CosInputs;
const cpuBackend = backend as MathBackendCPU;
assertNotComplex(x, 'cos');

const values = cpuBackend.data.get(x.dataId).values as TypedArray;
const xSize = util.sizeFromShape(x.shape);
const newValues = new Float32Array(xSize);
for (let i = 0; i < xSize; ++i) {
newValues[i] = Math.cos(values[i]);
}
const dataId = cpuBackend.write(newValues, x.shape, x.dtype);
return {dataId, shape: x.shape, dtype: x.dtype};
}
kernelFunc: cosKernelFunc,
};
28 changes: 28 additions & 0 deletions tfjs-backend-cpu/src/kernels/Cosh.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 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 {Cosh, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const coshKernelFunc = unaryKernelFunc(Cosh, (xi) => Math.cosh(xi));

export const coshConfig: KernelConfig = {
kernelName: Cosh,
backendName: 'cpu',
kernelFunc: coshKernelFunc,
};
29 changes: 29 additions & 0 deletions tfjs-backend-cpu/src/kernels/Elu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright 2020 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 {Elu, KernelConfig} from '@tensorflow/tfjs-core';

import {unaryKernelFunc} from '../utils/unary_utils';

export const eluKernelFunc =
unaryKernelFunc(Elu, (xi) => xi >= 0 ? xi : (Math.exp(xi) - 1));

export const eluConfig: KernelConfig = {
kernelName: Elu,
backendName: 'cpu',
kernelFunc: eluKernelFunc,
};
Loading