diff --git a/.tslint/noImportsFromDistRule.js b/.tslint/noImportsFromDistRule.js new file mode 100644 index 00000000..11c53758 --- /dev/null +++ b/.tslint/noImportsFromDistRule.js @@ -0,0 +1,44 @@ +"use strict"; +var __extends = (this && this.__extends) || (function () { + var extendStatics = function (d, b) { + extendStatics = Object.setPrototypeOf || + ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || + function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; + return extendStatics(d, b); + }; + return function (d, b) { + extendStatics(d, b); + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; +})(); +exports.__esModule = true; +var Lint = require("tslint"); +var Rule = /** @class */ (function (_super) { + __extends(Rule, _super); + function Rule() { + return _super !== null && _super.apply(this, arguments) || this; + } + Rule.prototype.apply = function (sourceFile) { + return this.applyWithWalker(new NoImportsFromDistWalker(sourceFile, this.getOptions())); + }; + Rule.FAILURE_STRING = "importing from dist/ is prohibited. Please use public API"; + return Rule; +}(Lint.Rules.AbstractRule)); +exports.Rule = Rule; +var NoImportsFromDistWalker = /** @class */ (function (_super) { + __extends(NoImportsFromDistWalker, _super); + function NoImportsFromDistWalker() { + return _super !== null && _super.apply(this, arguments) || this; + } + NoImportsFromDistWalker.prototype.visitImportDeclaration = function (node) { + var importFrom = node.moduleSpecifier.getText(); + var reg = /@tensorflow\/tfjs[-a-z]*\/dist/; + if (importFrom.match(reg)) { + var fix = new Lint.Replacement(node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(), importFrom.replace(/\/dist[\/]*/, '')); + this.addFailure(this.createFailure(node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(), Rule.FAILURE_STRING, fix)); + } + _super.prototype.visitImportDeclaration.call(this, node); + }; + return NoImportsFromDistWalker; +}(Lint.RuleWalker)); diff --git a/.tslint/noImportsFromDistRule.ts b/.tslint/noImportsFromDistRule.ts new file mode 100644 index 00000000..583ad119 --- /dev/null +++ b/.tslint/noImportsFromDistRule.ts @@ -0,0 +1,30 @@ +import * as Lint from "tslint"; +import * as ts from "typescript"; + +export class Rule extends Lint.Rules.AbstractRule { + public static FAILURE_STRING = "importing from dist/ is prohibited. Please use public API"; + + public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { + return this.applyWithWalker( + new NoImportsFromDistWalker(sourceFile, this.getOptions())); + } +} + +class NoImportsFromDistWalker extends Lint.RuleWalker { + public visitImportDeclaration(node: ts.ImportDeclaration) { + const importFrom = node.moduleSpecifier.getText(); + const reg = /@tensorflow\/tfjs[-a-z]*\/dist/; + if (importFrom.match(reg)) { + const fix = new Lint.Replacement(node.moduleSpecifier.getStart(), + node.moduleSpecifier.getWidth(), + importFrom.replace(/\/dist[\/]*/, '')); + + this.addFailure(this.createFailure(node.moduleSpecifier.getStart(), + node.moduleSpecifier.getWidth(), + Rule.FAILURE_STRING, fix)); + } + + super.visitImportDeclaration(node); + } + +} diff --git a/src/io/file_system_test.ts b/src/io/file_system_test.ts index 67fda2d7..1fd0332e 100644 --- a/src/io/file_system_test.ts +++ b/src/io/file_system_test.ts @@ -16,7 +16,7 @@ */ import * as tfc from '@tensorflow/tfjs-core'; -import {expectArraysClose} from '@tensorflow/tfjs-core/dist/test_util'; +import {test_util} from '@tensorflow/tfjs-core/'; import * as fs from 'fs'; import * as path from 'path'; import * as rimraf from 'rimraf'; @@ -223,7 +223,7 @@ describe('File system IOHandler', () => { dtype: 'float32', } ]); - expectArraysClose( + test_util.expectArraysClose( new Float32Array(modelArtifacts.weightData), new Float32Array([-1.1, -3.3, -3.3, -7.7])); done(); @@ -346,7 +346,7 @@ describe('File system IOHandler', () => { new NodeFileSystem([`${modelPath}`, `${modelManifestJSONPath}`]); handler.load() .then(modelArtifacts => { - expectArraysClose( + test_util.expectArraysClose( new Uint8Array(modelArtifacts.modelTopology as ArrayBuffer), new Uint8Array(modelData)); expect(modelArtifacts.weightSpecs).toEqual([ @@ -361,7 +361,7 @@ describe('File system IOHandler', () => { dtype: 'float32', } ]); - expectArraysClose( + test_util.expectArraysClose( new Float32Array(modelArtifacts.weightData), new Float32Array([-1.1, -3.3, -3.3, -7.7])); done(); diff --git a/src/nodejs_kernel_backend.ts b/src/nodejs_kernel_backend.ts index ae861f0a..628e9fb6 100644 --- a/src/nodejs_kernel_backend.ts +++ b/src/nodejs_kernel_backend.ts @@ -17,17 +17,17 @@ // tslint:disable-next-line:max-line-length import {BackendTimingInfo, DataMover, DataType, fill, KernelBackend, ones, Rank, rsqrt, Scalar, scalar, ShapeMap, Tensor, Tensor1D, tensor1d, Tensor2D, tensor2d, Tensor3D, tensor3d, Tensor4D, tidy, util} from '@tensorflow/tfjs-core'; -import {EPSILON_FLOAT32} from '@tensorflow/tfjs-core/dist/backends/backend'; -import {Conv2DInfo, Conv3DInfo} from '@tensorflow/tfjs-core/dist/ops/conv_util'; -import {Activation} from '@tensorflow/tfjs-core/dist/ops/fused_util'; -import {Tensor5D} from '@tensorflow/tfjs-core/dist/tensor'; -import {BackendValues, upcastType} from '@tensorflow/tfjs-core/dist/types'; +import {backend_util} from '@tensorflow/tfjs-core'; +import {Tensor5D} from '@tensorflow/tfjs-core'; import {isNullOrUndefined} from 'util'; import {Int64Scalar} from './int64_tensors'; // tslint:disable-next-line:max-line-length import {createTensorsTypeOpAttr, createTypeOpAttr, getTFDType} from './ops/op_utils'; import {TensorMetadata, TFEOpAttr, TFJSBinding} from './tfjs_binding'; +type Conv2DInfo = backend_util.Conv2DInfo; +type BackendValues = backend_util.BackendValues; + type TensorInfo = { shape: number[], dtype: number, @@ -37,6 +37,9 @@ type TensorInfo = { interface DataId {} +const {upcastType} = backend_util; +const EPSILON_FLOAT32 = 1e-7; + export class NodeJSKernelBackend extends KernelBackend { binding: TFJSBinding; isGPUPackage: boolean; @@ -370,7 +373,7 @@ export class NodeJSKernelBackend extends KernelBackend { fusedBatchMatMul( a: Tensor3D, b: Tensor3D, transposeA: boolean, transposeB: boolean, - bias?: Tensor, activation?: Activation, + bias?: Tensor, activation?: backend_util.Activation, preluActivationWeights?: Tensor): Tensor3D { // Core TensorFlow does not have a fused BatchMatMul op. Combine calls to // achieve the same results: diff --git a/src/nodejs_kernel_backend_test.ts b/src/nodejs_kernel_backend_test.ts index aff316b3..357af140 100644 --- a/src/nodejs_kernel_backend_test.ts +++ b/src/nodejs_kernel_backend_test.ts @@ -16,18 +16,17 @@ */ import * as tf from '@tensorflow/tfjs-core'; -import {Tensor5D} from '@tensorflow/tfjs-core/dist/tensor'; -// tslint:disable-next-line:max-line-length -import {expectArraysClose} from '@tensorflow/tfjs-core/dist/test_util'; +import {Tensor5D} from '@tensorflow/tfjs-core'; +import {test_util} from '@tensorflow/tfjs-core'; import {NodeJSKernelBackend} from './nodejs_kernel_backend'; describe('delayed upload', () => { it('should handle data before op execution', async () => { const t = tf.tensor1d([1, 2, 3]); - expectArraysClose(await t.data(), [1, 2, 3]); + test_util.expectArraysClose(await t.data(), [1, 2, 3]); const r = t.add(tf.tensor1d([4, 5, 6])); - expectArraysClose(await r.data(), [5, 7, 9]); + test_util.expectArraysClose(await r.data(), [5, 7, 9]); }); it('Should not cache tensors in the tensor map for device support. ', () => { diff --git a/src/run_tests.ts b/src/run_tests.ts index a8f4b28d..c0e2ca36 100644 --- a/src/run_tests.ts +++ b/src/run_tests.ts @@ -17,6 +17,7 @@ // We import index.ts so that the Node backend gets registered. import './index'; +// tslint:disable-next-line:no-imports-from-dist import * as jasmine_util from '@tensorflow/tfjs-core/dist/jasmine_util'; Error.stackTraceLimit = Infinity; diff --git a/src/tfjs_binding.ts b/src/tfjs_binding.ts index ddcede2a..67b0994e 100644 --- a/src/tfjs_binding.ts +++ b/src/tfjs_binding.ts @@ -15,7 +15,8 @@ * ============================================================================= */ -import {BackendValues} from '@tensorflow/tfjs-core/dist/types'; +import {backend_util} from '@tensorflow/tfjs-core'; +type BackendValues = backend_util.BackendValues; export declare class TensorMetadata { id: number; diff --git a/tslint.json b/tslint.json index 0f38101d..fce97dd6 100644 --- a/tslint.json +++ b/tslint.json @@ -1,4 +1,5 @@ { + "rulesDirectory": ".tslint", "rules": { "array-type": [true, "array-simple"], "arrow-return-shorthand": true, @@ -36,6 +37,7 @@ "no-reference": true, "no-require-imports": true, "no-string-throw": true, + "no-imports-from-dist": true, "no-unused-expression": true, "no-unused-variable": true, "no-var-keyword": true,