Skip to content

Create a new tf.env() getter that returns the global ENV #2034

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

Merged
merged 11 commits into from
Oct 1, 2019
2 changes: 1 addition & 1 deletion tfjs-core/benchmarks/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ <h2>TensorFlow.js Model Benchmark</h2>

async function showEnvironment() {
await tf.time(() => tf.add(tf.tensor1d([1]), tf.tensor1d([1])).data());
envDiv.innerHTML += `<br/>${JSON.stringify(tf.ENV.features, null, 2)
envDiv.innerHTML += `<br/>${JSON.stringify(tf.env().features, null, 2)
} `;
}

Expand Down
19 changes: 11 additions & 8 deletions tfjs-core/src/backends/cpu/backend_cpu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import * as seedrandom from 'seedrandom';

import {ENGINE} from '../../engine';
import {ENV} from '../../environment';
import {env} from '../../environment';

import {warn} from '../../log';
import * as array_ops_util from '../../ops/array_ops_util';
import * as axis_util from '../../ops/axis_util';
Expand Down Expand Up @@ -92,7 +93,7 @@ export class MathBackendCPU implements KernelBackend {
private firstUse = true;

constructor() {
if (ENV.get('IS_BROWSER')) {
if (env().get('IS_BROWSER')) {
const canvas = createCanvas();
if (canvas !== null) {
this.fromPixels2DContext =
Expand All @@ -105,7 +106,7 @@ export class MathBackendCPU implements KernelBackend {
register(dataId: DataId, shape: number[], dtype: DataType): void {
if (this.firstUse) {
this.firstUse = false;
if (ENV.get('IS_NODE')) {
if (env().get('IS_NODE')) {
warn(
'\n============================\n' +
'Hi there 👋. Looks like you are running TensorFlow.js in ' +
Expand Down Expand Up @@ -154,7 +155,7 @@ export class MathBackendCPU implements KernelBackend {
[pixels.width, pixels.height];
let vals: Uint8ClampedArray|Uint8Array;
// tslint:disable-next-line:no-any
if (ENV.get('IS_NODE') && (pixels as any).getContext == null) {
if (env().get('IS_NODE') && (pixels as any).getContext == null) {
throw new Error(
'When running in node, pixels must be an HTMLCanvasElement ' +
'like the one returned by the `canvas` npm package');
Expand Down Expand Up @@ -828,7 +829,8 @@ export class MathBackendCPU implements KernelBackend {
const newValues = this.readSync(result.dataId) as TypedArray;
let index = 0;
const offset = condition.rank === 0 || condition.rank > 1 || a.rank === 1 ?
1 : util.sizeFromShape(a.shape.slice(1));
1 :
util.sizeFromShape(a.shape.slice(1));

for (let i = 0; i < values.length; i++) {
for (let j = 0; j < offset; j++) {
Expand Down Expand Up @@ -1522,9 +1524,10 @@ export class MathBackendCPU implements KernelBackend {
const sign = Math.sign(values[i]);
const v = Math.abs(values[i]);
const t = 1.0 / (1.0 + p * v);
resultValues[i] = sign * (1.0 -
(((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
Math.exp(-v * v));
resultValues[i] = sign *
(1.0 -
(((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
Math.exp(-v * v));
}
return Tensor.make(x.shape, {values: resultValues});
}
Expand Down
Loading