File size: 7,655 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/**
 * Unit tests for TensorShapeInspector
 * Validates: Requirements 21.1, 21.2, 21.3, 21.4, 21.5
 */

import { describe, it, expect, beforeEach } from 'vitest';

/**
 * Extract the pure tensor lookup logic for testing (mirrors TensorShapeInspector._buildTensorInfoMap + lookupTensor).
 * @param {Object} parsedModel
 * @returns {{ lookup: (name: string) => Object|null, size: number }}
 */
function buildTensorLookup(parsedModel) {
    const map = new Map();

    // 1. Inputs
    if (Array.isArray(parsedModel.inputs)) {
        for (const inp of parsedModel.inputs) {
            if (inp.name) {
                map.set(inp.name, {
                    name: inp.name,
                    shape: inp.shape || [],
                    dataType: inp.dataType || 'UNKNOWN'
                });
            }
        }
    }

    // 2. Outputs
    if (Array.isArray(parsedModel.outputs)) {
        for (const out of parsedModel.outputs) {
            if (out.name) {
                map.set(out.name, {
                    name: out.name,
                    shape: out.shape || [],
                    dataType: out.dataType || 'UNKNOWN'
                });
            }
        }
    }

    // 3. value_info (intermediate tensors)
    const valueInfo = parsedModel.graph && parsedModel.graph.valueInfo;
    if (valueInfo && typeof valueInfo === 'object') {
        const entries = Object.entries(valueInfo);
        for (const [key, vi] of entries) {
            if (key && !map.has(key)) {
                map.set(key, {
                    name: vi.name || key,
                    shape: vi.shape || [],
                    dataType: vi.dataType || 'UNKNOWN'
                });
            }
        }
    }

    return {
        lookup: (name) => map.get(name) || null,
        size: map.size
    };
}

/**
 * Build tooltip text (mirrors TensorShapeInspector._buildTooltipHTML logic, text-only).
 */
function buildTooltipText(tensorName, tensorInfo) {
    const displayName = tensorName || 'Unnamed tensor';
    if (tensorInfo) {
        const shapeStr = tensorInfo.shape && tensorInfo.shape.length > 0
            ? '[' + tensorInfo.shape.join(', ') + ']'
            : 'unknown';
        return { name: displayName, shape: shapeStr, dataType: tensorInfo.dataType };
    }
    return { name: displayName, shape: 'unknown', dataType: null };
}

describe('TensorShapeInspector - Tensor Lookup', () => {
    let model;

    beforeEach(() => {
        model = {
            inputs: [
                { name: 'input_0', shape: [1, 3, 224, 224], dataType: 'FLOAT' }
            ],
            outputs: [
                { name: 'output_0', shape: [1, 1000], dataType: 'FLOAT' }
            ],
            graph: {
                nodes: [],
                edges: [],
                valueInfo: {
                    'conv1_out': { name: 'conv1_out', shape: [1, 64, 112, 112], dataType: 'FLOAT' },
                    'relu1_out': { name: 'relu1_out', shape: [1, 64, 112, 112], dataType: 'FLOAT' }
                }
            },
            initializers: []
        };
    });

    it('should find input tensors by name', () => {
        const { lookup } = buildTensorLookup(model);
        const info = lookup('input_0');
        expect(info).not.toBeNull();
        expect(info.name).toBe('input_0');
        expect(info.shape).toEqual([1, 3, 224, 224]);
        expect(info.dataType).toBe('FLOAT');
    });

    it('should find output tensors by name', () => {
        const { lookup } = buildTensorLookup(model);
        const info = lookup('output_0');
        expect(info).not.toBeNull();
        expect(info.shape).toEqual([1, 1000]);
        expect(info.dataType).toBe('FLOAT');
    });

    it('should find intermediate tensors from valueInfo', () => {
        const { lookup } = buildTensorLookup(model);
        const info = lookup('conv1_out');
        expect(info).not.toBeNull();
        expect(info.shape).toEqual([1, 64, 112, 112]);
        expect(info.dataType).toBe('FLOAT');
    });

    it('should return null for unknown tensor names', () => {
        const { lookup } = buildTensorLookup(model);
        expect(lookup('nonexistent_tensor')).toBeNull();
    });

    it('should return null for empty/null name', () => {
        const { lookup } = buildTensorLookup(model);
        expect(lookup('')).toBeNull();
        expect(lookup(null)).toBeNull();
        expect(lookup(undefined)).toBeNull();
    });

    it('should count all tensors from all sources', () => {
        const { size } = buildTensorLookup(model);
        // 1 input + 1 output + 2 valueInfo = 4
        expect(size).toBe(4);
    });

    it('should handle model with no valueInfo', () => {
        model.graph.valueInfo = {};
        const { lookup, size } = buildTensorLookup(model);
        expect(size).toBe(2); // only input + output
        expect(lookup('conv1_out')).toBeNull();
    });

    it('should handle model with no inputs or outputs', () => {
        const emptyModel = { inputs: [], outputs: [], graph: { valueInfo: {} }, initializers: [] };
        const { size } = buildTensorLookup(emptyModel);
        expect(size).toBe(0);
    });

    it('should prioritize inputs/outputs over valueInfo for same name', () => {
        model.graph.valueInfo['input_0'] = { name: 'input_0', shape: [999], dataType: 'INT32' };
        const { lookup } = buildTensorLookup(model);
        const info = lookup('input_0');
        // Input should take priority
        expect(info.shape).toEqual([1, 3, 224, 224]);
        expect(info.dataType).toBe('FLOAT');
    });

    it('should handle tensors with missing shape', () => {
        model.graph.valueInfo['no_shape'] = { name: 'no_shape', dataType: 'FLOAT' };
        const { lookup } = buildTensorLookup(model);
        const info = lookup('no_shape');
        expect(info).not.toBeNull();
        expect(info.shape).toEqual([]);
    });

    it('should handle tensors with missing dataType', () => {
        model.graph.valueInfo['no_dtype'] = { name: 'no_dtype', shape: [1, 10] };
        const { lookup } = buildTensorLookup(model);
        const info = lookup('no_dtype');
        expect(info.dataType).toBe('UNKNOWN');
    });
});

describe('TensorShapeInspector - Tooltip Content', () => {
    it('should show shape and type for known tensor', () => {
        const tensorInfo = { name: 'conv1_out', shape: [1, 64, 112, 112], dataType: 'FLOAT' };
        const result = buildTooltipText('conv1_out', tensorInfo);
        expect(result.name).toBe('conv1_out');
        expect(result.shape).toBe('[1, 64, 112, 112]');
        expect(result.dataType).toBe('FLOAT');
    });

    it('should show "Shape: unknown" when tensor info is null', () => {
        const result = buildTooltipText('mystery_tensor', null);
        expect(result.name).toBe('mystery_tensor');
        expect(result.shape).toBe('unknown');
        expect(result.dataType).toBeNull();
    });

    it('should show "Shape: unknown" when tensor has empty shape', () => {
        const tensorInfo = { name: 'empty', shape: [], dataType: 'FLOAT' };
        const result = buildTooltipText('empty', tensorInfo);
        expect(result.shape).toBe('unknown');
    });

    it('should handle dynamic dimensions in shape', () => {
        const tensorInfo = { name: 'dynamic', shape: ['batch', 3, 224, 224], dataType: 'FLOAT' };
        const result = buildTooltipText('dynamic', tensorInfo);
        expect(result.shape).toBe('[batch, 3, 224, 224]');
    });

    it('should show "Unnamed tensor" when name is empty', () => {
        const result = buildTooltipText('', null);
        expect(result.name).toBe('Unnamed tensor');
    });
});