Skip to content

Commit fe4627f

Browse files
author
Nikhil Thorat
authored
Create a new tf.env() getter that returns the global ENV (#2034)
This is for the no exported objects rule which is against ES2015 module semantics. This will be a replacement for anyone using tf.ENV as a global getter (we will not remove tf.ENV until the next major version bump). We won't use exports = {} because it breaks the OSS build. FEATURE
1 parent ee0ef7d commit fe4627f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+514
-466
lines changed

tfjs-core/benchmarks/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ <h2>TensorFlow.js Model Benchmark</h2>
128128

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

tfjs-core/src/backends/cpu/backend_cpu.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import * as seedrandom from 'seedrandom';
1919

2020
import {ENGINE} from '../../engine';
21-
import {ENV} from '../../environment';
21+
import {env} from '../../environment';
22+
2223
import {warn} from '../../log';
2324
import * as array_ops_util from '../../ops/array_ops_util';
2425
import * as axis_util from '../../ops/axis_util';
@@ -92,7 +93,7 @@ export class MathBackendCPU implements KernelBackend {
9293
private firstUse = true;
9394

9495
constructor() {
95-
if (ENV.get('IS_BROWSER')) {
96+
if (env().get('IS_BROWSER')) {
9697
const canvas = createCanvas();
9798
if (canvas !== null) {
9899
this.fromPixels2DContext =
@@ -105,7 +106,7 @@ export class MathBackendCPU implements KernelBackend {
105106
register(dataId: DataId, shape: number[], dtype: DataType): void {
106107
if (this.firstUse) {
107108
this.firstUse = false;
108-
if (ENV.get('IS_NODE')) {
109+
if (env().get('IS_NODE')) {
109110
warn(
110111
'\n============================\n' +
111112
'Hi there 👋. Looks like you are running TensorFlow.js in ' +
@@ -154,7 +155,7 @@ export class MathBackendCPU implements KernelBackend {
154155
[pixels.width, pixels.height];
155156
let vals: Uint8ClampedArray|Uint8Array;
156157
// tslint:disable-next-line:no-any
157-
if (ENV.get('IS_NODE') && (pixels as any).getContext == null) {
158+
if (env().get('IS_NODE') && (pixels as any).getContext == null) {
158159
throw new Error(
159160
'When running in node, pixels must be an HTMLCanvasElement ' +
160161
'like the one returned by the `canvas` npm package');
@@ -828,7 +829,8 @@ export class MathBackendCPU implements KernelBackend {
828829
const newValues = this.readSync(result.dataId) as TypedArray;
829830
let index = 0;
830831
const offset = condition.rank === 0 || condition.rank > 1 || a.rank === 1 ?
831-
1 : util.sizeFromShape(a.shape.slice(1));
832+
1 :
833+
util.sizeFromShape(a.shape.slice(1));
832834

833835
for (let i = 0; i < values.length; i++) {
834836
for (let j = 0; j < offset; j++) {
@@ -1522,9 +1524,10 @@ export class MathBackendCPU implements KernelBackend {
15221524
const sign = Math.sign(values[i]);
15231525
const v = Math.abs(values[i]);
15241526
const t = 1.0 / (1.0 + p * v);
1525-
resultValues[i] = sign * (1.0 -
1526-
(((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
1527-
Math.exp(-v * v));
1527+
resultValues[i] = sign *
1528+
(1.0 -
1529+
(((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t *
1530+
Math.exp(-v * v));
15281531
}
15291532
return Tensor.make(x.shape, {values: resultValues});
15301533
}

0 commit comments

Comments
 (0)