Skip to content

Commit 0dbd779

Browse files
author
Nikhil Thorat
authored
Add tf.deprecationWarn() and tf.disableDeprecationWarnings() (tensorflow#1539)
This adds an environment flag for whether deprecation warnings are enabled, and two methods, `deprecationWarn` which will warn the user about deprecated methods (turned off by the global environment flag) and `disableDeprecationWarnings` which will completely turn off all deprecation warnings. FEATURE
1 parent 20f753b commit 0dbd779

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

src/environment.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import * as device_util from './device_util';
1919
import {Engine, MemoryInfo, ProfileInfo, ScopeFn, TimingInfo} from './engine';
2020
import {Features, getFeaturesFromURL, getMaxTexturesInShader, getNumMBBeforePaging, getWebGLDisjointQueryTimerVersion, getWebGLMaxTextureSize, isChrome, isDownloadFloatTextureEnabled, isRenderToFloatTextureEnabled, isWebGLFenceEnabled, isWebGLVersionEnabled} from './environment_util';
2121
import {KernelBackend} from './kernels/backend';
22-
import {DataId, setTensorTracker, Tensor} from './tensor';
22+
import {DataId, setDeprecationWarningFn, setTensorTracker, Tensor} from './tensor';
2323
import {TensorContainer} from './tensor_types';
2424
import {getTensorsInContainer} from './tensor_util';
2525

@@ -378,6 +378,8 @@ export class Environment {
378378
return false;
379379
} else if (feature === 'TENSORLIKE_CHECK_SHAPE_CONSISTENCY') {
380380
return !this.get('PROD');
381+
} else if (feature === 'DEPRECATION_WARNINGS_ENABLED') {
382+
return true;
381383
}
382384
throw new Error(`Unknown feature ${feature}.`);
383385
}
@@ -498,4 +500,20 @@ export function enableProdMode(): void {
498500
ENV.set('PROD', true);
499501
}
500502

503+
/** Globally disables deprecation warnings */
504+
export function disableDeprecationWarnings(): void {
505+
ENV.set('DEPRECATION_WARNINGS_ENABLED', false);
506+
console.warn(`TensorFlow.js deprecation warnings have been disabled.`);
507+
}
508+
509+
/** Warn users about deprecated functionality. */
510+
export function deprecationWarn(msg: string) {
511+
if (ENV.get('DEPRECATION_WARNINGS_ENABLED')) {
512+
console.warn(
513+
msg + ' You can disable deprecation warnings with ' +
514+
'tf.disableDeprecationWarnings().');
515+
}
516+
}
517+
setDeprecationWarningFn(deprecationWarn);
518+
501519
export let ENV = getOrMakeEnvironment();

src/environment_test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,35 @@ describeWithFlags('WEBGL_SIZE_UPLOAD_UNIFORM', WEBGL_ENVS, () => {
218218
expect(env.get('WEBGL_SIZE_UPLOAD_UNIFORM')).toBeGreaterThan(0);
219219
});
220220
});
221+
222+
describe('deprecation warnings', () => {
223+
let oldWarn: (msg: string) => void;
224+
beforeEach(() => {
225+
oldWarn = console.warn;
226+
spyOn(console, 'warn').and.callFake((msg: string): void => null);
227+
});
228+
afterEach(() => {
229+
console.warn = oldWarn;
230+
});
231+
232+
it('deprecationWarn warns', () => {
233+
tf.deprecationWarn('xyz is deprecated.');
234+
expect(console.warn).toHaveBeenCalledTimes(1);
235+
expect(console.warn)
236+
.toHaveBeenCalledWith(
237+
'xyz is deprecated. You can disable deprecation warnings with ' +
238+
'tf.disableDeprecationWarnings().');
239+
});
240+
241+
it('disableDeprecationWarnings called, deprecationWarn doesnt warn', () => {
242+
tf.disableDeprecationWarnings();
243+
expect(console.warn).toHaveBeenCalledTimes(1);
244+
expect(console.warn)
245+
.toHaveBeenCalledWith(
246+
'TensorFlow.js deprecation warnings have been disabled.');
247+
248+
// deprecationWarn no longer warns.
249+
tf.deprecationWarn('xyz is deprecated.');
250+
expect(console.warn).toHaveBeenCalledTimes(1);
251+
});
252+
});

src/environment_util.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ export interface Features {
9292
// Whether to do sanity checks when inferring a shape from user-provided
9393
// values, used when creating a new tensor.
9494
'TENSORLIKE_CHECK_SHAPE_CONSISTENCY'?: boolean;
95+
// Whether deprecation warnings are enabled.
96+
'DEPRECATION_WARNINGS_ENABLED'?: boolean;
9597
}
9698

9799
export enum Type {
@@ -126,6 +128,7 @@ export const URL_PROPERTIES: URLProperty[] = [
126128
{name: 'EPSILON', type: Type.NUMBER},
127129
{name: 'PROD', type: Type.BOOLEAN},
128130
{name: 'TENSORLIKE_CHECK_SHAPE_CONSISTENCY', type: Type.BOOLEAN},
131+
{name: 'DEPRECATION_WARNINGS_ENABLED', type: Type.BOOLEAN},
129132
];
130133

131134
export interface URLProperty {

src/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import './kernels/backend_cpu';
2323

2424
import {nextFrame} from './browser_util';
2525
import * as environment from './environment';
26-
import {enableProdMode, Environment} from './environment';
26+
import {deprecationWarn, disableDeprecationWarnings, enableProdMode, Environment} from './environment';
2727
// Serialization.
2828
import * as io from './io/io';
2929
import * as math from './math';
@@ -65,19 +65,11 @@ export const disposeVariables = Environment.disposeVariables;
6565
export const memory = Environment.memory;
6666
export {version as version_core};
6767

68-
export {nextFrame};
68+
// Top-level method exports.
69+
export {nextFrame, enableProdMode, disableDeprecationWarnings, deprecationWarn};
6970

7071
// Second level exports.
71-
export {
72-
environment,
73-
io,
74-
math,
75-
serialization,
76-
test_util,
77-
util,
78-
webgl,
79-
enableProdMode
80-
};
72+
export {environment, io, math, serialization, test_util, util, webgl};
8173

8274
// Backend specific.
8375
export {KernelBackend, BackendTimingInfo, DataMover, DataStorage} from './kernels/backend';

src/tensor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ export interface OpHandler {
360360
let trackerFn: () => TensorTracker = null;
361361
// Used by chaining methods to call into ops.
362362
let opHandler: OpHandler = null;
363+
// Used to warn about deprecated methods.
364+
let deprecationWarningFn: (msg: string) => void = null;
365+
// This here so that we can use this method on dev branches and keep the
366+
// functionality at master.
367+
// tslint:disable-next-line:no-unused-expression
368+
[deprecationWarningFn];
363369

364370
/**
365371
* An external consumer can register itself as the tensor tracker. This way
@@ -378,6 +384,14 @@ export function setOpHandler(handler: OpHandler) {
378384
opHandler = handler;
379385
}
380386

387+
/**
388+
* Sets the deprecation warning function to be used by this file. This way the
389+
* Tensor class can be a leaf but still use the environment.
390+
*/
391+
export function setDeprecationWarningFn(fn: (msg: string) => void) {
392+
deprecationWarningFn = fn;
393+
}
394+
381395
/**
382396
* We wrap data id since we use weak map to avoid memory leaks.
383397
* Since we have our own memory management, we have a reference counter

0 commit comments

Comments
 (0)