Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Remove dist importing #289

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
44 changes: 44 additions & 0 deletions .tslint/noImportsFromDistRule.js
Original file line number Diff line number Diff line change
@@ -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));
30 changes: 30 additions & 0 deletions .tslint/noImportsFromDistRule.ts
Original file line number Diff line number Diff line change
@@ -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);
}

}
8 changes: 4 additions & 4 deletions src/io/file_system_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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([
Expand All @@ -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();
Expand Down
10 changes: 5 additions & 5 deletions src/nodejs_kernel_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

// 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 {EPSILON_FLOAT32} from '@tensorflow/tfjs-core';
import {Conv2DInfo, Conv3DInfo} from '@tensorflow/tfjs-core';
import {Activation} from '@tensorflow/tfjs-core';
import {Tensor5D} from '@tensorflow/tfjs-core';
import {BackendValues, upcastType} from '@tensorflow/tfjs-core';
import {isNullOrUndefined} from 'util';
import {Int64Scalar} from './int64_tensors';
// tslint:disable-next-line:max-line-length
Expand Down
9 changes: 4 additions & 5 deletions src/nodejs_kernel_backend_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ', () => {
Expand Down
1 change: 1 addition & 0 deletions src/run_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tfjs_binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* =============================================================================
*/

import {BackendValues} from '@tensorflow/tfjs-core/dist/types';
import {BackendValues} from '@tensorflow/tfjs-core';

export declare class TensorMetadata {
id: number;
Expand Down
2 changes: 2 additions & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"rulesDirectory": ".tslint",
"rules": {
"array-type": [true, "array-simple"],
"arrow-return-shorthand": true,
Expand Down Expand Up @@ -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,
Expand Down