gx
chenyc
2025-02-12 ea42ff3ebee1eeb3fb29423aa848a249441db81c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * @license
 * Copyright 2020 Google Inc. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */
 
import {esmImportProvider} from './esm_module_provider';
 
describe('ESM Module Provider', () => {
  it('importCoreStr forwardModeOnly=true', () => {
    const forwardModeOnly = true;
    const res = esmImportProvider.importCoreStr(forwardModeOnly);
    expect(res).toContain(
        `import {registerKernel} from '@tensorflow/tfjs-core/dist/base'`);
    expect(res).not.toContain(
        `import {registerGradient} from '@tensorflow/tfjs-core/dist/base'`);
    expect(res).toContain(
        `import '@tensorflow/tfjs-core/dist/base_side_effects';`);
    expect(res).toContain(`export * from '@tensorflow/tfjs-core/dist/base';`);
  });
 
  it('importCoreStr forwardModeOnly=false', () => {
    const forwardModeOnly = false;
    const res = esmImportProvider.importCoreStr(forwardModeOnly);
    expect(res).toContain(
        `import {registerKernel} from '@tensorflow/tfjs-core/dist/base'`);
    expect(res).toContain(
        `import {registerGradient} from '@tensorflow/tfjs-core/dist/base'`);
    expect(res).toContain(
        `import '@tensorflow/tfjs-core/dist/base_side_effects';`);
    expect(res).toContain(`export * from '@tensorflow/tfjs-core/dist/base';`);
  });
 
  it('importConverterStr', () => {
    const res = esmImportProvider.importConverterStr();
    expect(res).toBe(`export * from '@tensorflow/tfjs-converter';`);
  });
 
  it('importBackendStr cpu', () => {
    const res = esmImportProvider.importBackendStr('cpu');
    expect(res).toBe(`export * from '@tensorflow/tfjs-backend-cpu/dist/base';`);
  });
 
  it('importBackendStr webgl', () => {
    const res = esmImportProvider.importBackendStr('webgl');
    expect(res).toBe(
        `export * from '@tensorflow/tfjs-backend-webgl/dist/base';`);
  });
 
  it('importKernelStr Max cpu', () => {
    const res = esmImportProvider.importKernelStr('Max', 'cpu');
    expect(res.importStatement).toContain('import {maxConfig as Max_cpu}');
    expect(res.importStatement)
        .toContain(`from '@tensorflow/tfjs-backend-cpu/dist/kernels/Max'`);
 
    expect(res.kernelConfigId).toBe('Max_cpu');
  });
 
  it('importGradientConfigStr Max', () => {
    const res = esmImportProvider.importGradientConfigStr('Max');
    expect(res.importStatement).toContain('import {maxGradConfig}');
    expect(res.importStatement)
        .toContain(`from '@tensorflow/tfjs-core/dist/gradients/Max_grad'`);
 
    expect(res.gradConfigId).toBe('maxGradConfig');
  });
 
  it('importGradientConfigStr Max', () => {
    const res = esmImportProvider.importGradientConfigStr('Max');
    expect(res.importStatement).toContain('import {maxGradConfig}');
    expect(res.importStatement)
        .toContain(`from '@tensorflow/tfjs-core/dist/gradients/Max_grad'`);
 
    expect(res.gradConfigId).toBe('maxGradConfig');
  });
 
  it('importOpForConverterStr add', () => {
    const res = esmImportProvider.importOpForConverterStr('add');
    expect(res).toBe(`export {add} from '@tensorflow/tfjs-core/dist/ops/add';`);
  });
 
  it('importOpForConverterStr gatherND', () => {
    const res = esmImportProvider.importOpForConverterStr('gatherND');
    expect(res).toBe(
        `export {gatherND} from '@tensorflow/tfjs-core/dist/ops/gather_nd';`);
  });
 
  it('importOpForConverterStr batchToSpaceND', () => {
    const res = esmImportProvider.importOpForConverterStr('batchToSpaceND');
    expect(res).toBe(
        // tslint:disable-next-line:max-line-length
        `export {batchToSpaceND} from '@tensorflow/tfjs-core/dist/ops/batch_to_space_nd';`);
  });
 
  it('importOpForConverterStr concat1d', () => {
    const res = esmImportProvider.importOpForConverterStr('concat1d');
    expect(res).toBe(
        `export {concat1d} from '@tensorflow/tfjs-core/dist/ops/concat_1d';`);
  });
 
  it('importOpForConverterStr avgPool3d', () => {
    const res = esmImportProvider.importOpForConverterStr('avgPool3d');
    expect(res).toBe(
        // tslint:disable-next-line:max-line-length
        `export {avgPool3d} from '@tensorflow/tfjs-core/dist/ops/avg_pool_3d';`);
  });
 
  it('importOpForConverterStr stridedSlice', () => {
    const res = esmImportProvider.importOpForConverterStr('stridedSlice');
    expect(res).toBe(
        // tslint:disable-next-line: max-line-length
        `export {stridedSlice} from '@tensorflow/tfjs-core/dist/ops/strided_slice';`);
  });
 
  it('importNamespacedOpsForConverterStr image.resizeBilinear', () => {
    const res = esmImportProvider.importNamespacedOpsForConverterStr(
        'image', ['resizeBilinear']);
    expect(res).toBe(
        // tslint:disable-next-line: max-line-length
        `import {resizeBilinear as resizeBilinear_image} from '@tensorflow/tfjs-core/dist/ops/image/resize_bilinear';
export const image = {
\tresizeBilinear: resizeBilinear_image,
};`);
  });
 
  it('importNamespacedOpsForConverterStr two ops in namespace', () => {
    const res = esmImportProvider.importNamespacedOpsForConverterStr(
        'image', ['resizeBilinear', 'resizeNearestNeighbor']);
    expect(res).toBe(
        // tslint:disable-next-line: max-line-length
        `import {resizeBilinear as resizeBilinear_image} from '@tensorflow/tfjs-core/dist/ops/image/resize_bilinear';
import {resizeNearestNeighbor as resizeNearestNeighbor_image} from '@tensorflow/tfjs-core/dist/ops/image/resize_nearest_neighbor';
export const image = {
\tresizeBilinear: resizeBilinear_image,
\tresizeNearestNeighbor: resizeNearestNeighbor_image,
};`);
  });
});