Skip to content

Commit 7e50785

Browse files
authored
fix import path in kernel_names (#3653)
BUG Also adds a custom lint rule to disable such imports. Fixes #3617
1 parent fb2677e commit 7e50785

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

Diff for: .tslint/noImportsWithRegexRule.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"use strict";
2+
var __extends = (this && this.__extends) || (function () {
3+
var extendStatics = function (d, b) {
4+
extendStatics = Object.setPrototypeOf ||
5+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6+
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7+
return extendStatics(d, b);
8+
};
9+
return function (d, b) {
10+
extendStatics(d, b);
11+
function __() { this.constructor = d; }
12+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13+
};
14+
})();
15+
exports.__esModule = true;
16+
var Lint = require("tslint");
17+
var Rule = /** @class */ (function (_super) {
18+
__extends(Rule, _super);
19+
function Rule() {
20+
return _super !== null && _super.apply(this, arguments) || this;
21+
}
22+
Rule.prototype.apply = function (sourceFile) {
23+
return this.applyWithWalker(new NoImportsWithRegexWalker(sourceFile, this.getOptions()));
24+
};
25+
return Rule;
26+
}(Lint.Rules.AbstractRule));
27+
exports.Rule = Rule;
28+
var NoImportsWithRegexWalker = /** @class */ (function (_super) {
29+
__extends(NoImportsWithRegexWalker, _super);
30+
function NoImportsWithRegexWalker() {
31+
return _super !== null && _super.apply(this, arguments) || this;
32+
}
33+
NoImportsWithRegexWalker.prototype.visitImportDeclaration = function (node) {
34+
var options = this.getOptions();
35+
for (var _i = 0, options_1 = options; _i < options_1.length; _i++) {
36+
var regStr = options_1[_i];
37+
var importFrom = node.moduleSpecifier.getText();
38+
var reg = new RegExp(regStr);
39+
if (importFrom.match(reg)) {
40+
this.addFailure(this.createFailure(node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(), "importing from " + regStr + " is prohibited."));
41+
}
42+
}
43+
_super.prototype.visitImportDeclaration.call(this, node);
44+
};
45+
return NoImportsWithRegexWalker;
46+
}(Lint.RuleWalker));

Diff for: .tslint/noImportsWithRegexRule.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as Lint from 'tslint';
2+
import * as ts from 'typescript';
3+
4+
export class Rule extends Lint.Rules.AbstractRule {
5+
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
6+
return this.applyWithWalker(
7+
new NoImportsWithRegexWalker(sourceFile, this.getOptions()));
8+
}
9+
}
10+
11+
class NoImportsWithRegexWalker extends Lint.RuleWalker {
12+
public visitImportDeclaration(node: ts.ImportDeclaration) {
13+
const options = this.getOptions();
14+
for (const regStr of options) {
15+
const importFrom = node.moduleSpecifier.getText();
16+
const reg = new RegExp(regStr);
17+
if (importFrom.match(reg)) {
18+
this.addFailure(this.createFailure(
19+
node.moduleSpecifier.getStart(), node.moduleSpecifier.getWidth(),
20+
`importing from ${regStr} is prohibited.`));
21+
}
22+
}
23+
24+
super.visitImportDeclaration(node);
25+
}
26+
}

Diff for: tfjs-backend-webgpu/src/kernels/reduce_webgpu.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717

1818
import {backend_util, util} from '@tensorflow/tfjs-core';
19-
// TODO : use backend_util.reduce_util with the next release of tfjs-core.
20-
import {ReduceInfo} from '@tensorflow/tfjs-core/src/ops/reduce_util';
2119
import {getCoordsDataType} from '../shader_preprocessor';
2220
import {computeDispatch} from '../webgpu_util';
2321
import {WebGPUProgram} from './webgpu_program';
@@ -32,7 +30,8 @@ export class ReduceProgram implements WebGPUProgram {
3230
variableNames = ['x'];
3331
needsShapesUniforms = true;
3432

35-
constructor(reduceInfo: ReduceInfo, reduceType: 'max'|'min'|'sum') {
33+
constructor(
34+
reduceInfo: backend_util.ReduceInfo, reduceType: 'max'|'min'|'sum') {
3635
const inputShape = [reduceInfo.batchSize, reduceInfo.inSize];
3736
const [outputShape, reduceShape] =
3837
backend_util.computeOutAndReduceShapes(inputShape, [1]);

Diff for: tfjs-core/src/kernel_names.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
// tslint:disable: variable-name
1919
// Unfortunately just enabling PascalCase per file (tslint:enable:
2020
// allow-pascal-case) doesn't work.
21-
import {ExplicitPadding} from '../src/ops/conv_util';
22-
2321
import {NamedTensorInfoMap, TensorInfo} from './kernel_registry';
22+
import {ExplicitPadding} from './ops/conv_util';
2423
import {Activation} from './ops/fused_types';
2524
import {DataType, PixelData} from './types';
2625

Diff for: tslint.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"no-debugger": true,
3939
"no-default-export": true,
4040
"no-imports-from-dist": true,
41+
"no-imports-with-regex": [true, "src"],
4142
"no-inferrable-types": true,
4243
"no-namespace": [true, "allow-declarations"],
4344
"no-reference": true,

0 commit comments

Comments
 (0)