-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathtypes.py
1078 lines (843 loc) · 38.9 KB
/
types.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2020 Google LLC
#
# 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.
# ==============================================================================
"""Type classes for LIT inputs and outputs.
These are simple dataclasses used in model.input_spec() and model.output_spec()
to describe the semantics of the model outputs, while allowing clients to still
use flexible data structures.
These are used by the LIT framework to configure front-end components and to
enable different generation and visualization modules. For example, the input
spec allows LIT to automatically generate input forms for common types like text
segments or class labels, while the output spec describes how the model output
should be rendered.
"""
import abc
from collections.abc import Callable, Mapping, Sequence
import enum
import inspect
import math
import numbers
import os
from typing import Any, get_args, get_origin, NewType, Optional, TypedDict, Union
import attr
from etils import epath
from lit_nlp.api import dtypes
import numpy as np
JsonDict = Mapping[str, Any]
Input = NewType("Input", JsonDict)
ExampleId = NewType("ExampleId", str)
ScoredTextCandidates = Sequence[tuple[str, Optional[float]]]
TokenTopKPredsList = Sequence[ScoredTextCandidates]
NumericTypes = numbers.Number
class InputMetadata(TypedDict):
added: Optional[bool]
# pylint: disable=invalid-name
parentId: Optional[ExampleId] # Named to match TypeScript data structure
# pylint: enable=invalid-name
source: Optional[str]
class IndexedInput(TypedDict):
data: JsonDict
id: ExampleId
meta: InputMetadata
##
# Base classes, for common functionality and type grouping.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class LitType(metaclass=abc.ABCMeta):
"""Base class for LIT Types."""
required: bool = True # for input fields, mark if required by the model.
annotated: bool = False # If this type is created from an Annotator.
show_in_data_table = True # If true, show this info the data table.
# TODO(lit-dev): Add defaults for all LitTypes
default = None # an optional default value for a given type.
def validate_input(self, value: Any, spec: "Spec", example: Input):
"""Validate a dataset example's value against its spec in an example.
Subtypes should override to validate a provided value and raise a ValueError
if the value is not valid.
Args:
value: The value to validate against the specific LitType.
spec: The spec of the dataset.
example: The entire example of which the value is a part of.
Raises:
ValueError if validation fails.
"""
pass
def validate_output(self, value: Any, output_spec: "Spec",
output_dict: JsonDict, input_spec: "Spec",
dataset_spec: "Spec", input_example: Input):
"""Validate a model output value against its spec and input example.
Subtypes should override to validate a provided value and raise a ValueError
if the value is not valid.
Args:
value: The value to validate against the specific LitType.
output_spec: The output spec of the model.
output_dict: The entire model output for the example.
input_spec: The input spec of the model.
dataset_spec: The dataset spec.
input_example: The example from which the output value is returned.
Raises:
ValueError if validation fails.
"""
del output_spec, output_dict, dataset_spec
# If not overwritten by a LitType, then validate it as an input to re-use
# simple validation code.
self.validate_input(value, input_spec, input_example)
def is_compatible(self, other):
"""Check equality, ignoring some fields."""
# We allow this class to be a subclass of the other.
if not isinstance(self, type(other)):
return False
d1 = attr.asdict(self)
d1.pop("required", None)
d2 = attr.asdict(other)
d2.pop("required", None)
return d1 == d2
def to_json(self) -> JsonDict:
"""Used by serialize.py."""
d = attr.asdict(self)
d["__class__"] = "LitType"
d["__name__"] = self.__class__.__name__
return d
@staticmethod
def from_json(d: JsonDict):
"""Used by serialize.py.
Args:
d: The JSON Object-like dictionary to attempt to parse.
Returns:
An instance of a LitType subclass defined by the contents of `d`.
Raises:
KeyError: If `d` does not have a `__name__` property.
NameError: If `d["__name__"]` is not a `LitType` subclass.
TypeError: If `d["__name__"]` is not a string.
"""
try:
type_name = d["__name__"]
except KeyError as e:
raise KeyError("A __name__ property is required to parse a LitType from "
"JSON.") from e
if not isinstance(type_name, str):
raise TypeError("The value of __name__ must be a string.")
base_cls = globals().get("LitType")
cls = globals().get(type_name) # class by name from this module
if cls is None or not issubclass(cls, base_cls):
raise NameError(f"{type_name} is not a valid LitType.")
return cls(**{k: d[k] for k in d if k != "__name__"})
Spec = dict[str, LitType]
# Attributes that should be treated as a reference to other fields.
FIELD_REF_ATTRIBUTES = frozenset(
{"parent", "align", "align_in", "align_out", "grad_for"})
def _remap_leaf(leaf: LitType, keymap: dict[str, str]) -> LitType:
"""Remap any field references on a LitType."""
d = attr.asdict(leaf) # mutable
d = {
k: (keymap.get(v, v) if k in FIELD_REF_ATTRIBUTES else v)
for k, v in d.items()
}
return leaf.__class__(**d)
def remap_spec(spec: Spec, keymap: dict[str, str]) -> Spec:
"""Rename fields in a spec, with a best-effort to also remap field references."""
ret = {}
for k, v in spec.items():
new_key = keymap.get(k, k)
new_value = _remap_leaf(v, keymap)
ret[new_key] = new_value
return ret
##
# Concrete type clases
# LINT.IfChange
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class StringLitType(LitType):
"""User-editable text input.
All automated edits are disabled for this type.
Mainly used for string inputs that have special formatting, and should only
be edited manually.
"""
default: str = ""
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, str):
raise ValueError(f"{value} is of type {type(value)}, expected str")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TextSegment(StringLitType):
"""Text input (untokenized), a single string."""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageBytes(LitType):
"""An image, an encoded base64 ascii string (starts with 'data:image...')."""
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, str) or not value.startswith("data:image"):
raise ValueError(f"{value} is not an encoded image string.")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class JPEGBytes(ImageBytes):
"""As ImageBytes, but assumed to be in jpeg format."""
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, str) or not value.startswith("data:image/jpg"):
raise ValueError(f"{value} is not an encoded JPEG image string.")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class PNGBytes(ImageBytes):
"""As ImageBytes, but assumed to be in png format."""
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, str) or not value.startswith("data:image/png"):
raise ValueError(f"{value} is not an encoded PNG image string.")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class GeneratedText(TextSegment):
"""Generated (untokenized) text."""
# Name of a TextSegment field to evaluate against
parent: Optional[str] = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, str):
raise ValueError(f"{value} is of type {type(value)}, expected str")
if self.parent and not isinstance(input_spec[self.parent], TextSegment):
raise ValueError(f"parent field {self.parent} is of type "
f"{type(self.parent)}, expected TextSegment")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ListLitType(LitType):
"""List type."""
default: Sequence[Any] = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _StringCandidateList(ListLitType):
"""A list of (text, score) tuples."""
default: ScoredTextCandidates = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, list):
raise ValueError(f"{value} is not a list")
for v in value:
if not (isinstance(v, tuple) and isinstance(v[0], str) and
(v[1] is None or isinstance(v[1], NumericTypes))):
raise ValueError(f"{v} list item is not a (str, float) tuple)")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class GeneratedTextCandidates(_StringCandidateList):
"""Multiple candidates for GeneratedText."""
# Name of a TextSegment field to evaluate against
parent: Optional[str] = None
@staticmethod
def top_text(value: ScoredTextCandidates) -> str:
return value[0][0] if len(value) else ""
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
if self.parent and not isinstance(input_spec[self.parent], TextSegment):
raise ValueError(f"parent field {self.parent} is of type "
f"{type(input_spec[self.parent])}, expected TextSegment")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ReferenceTexts(_StringCandidateList):
"""Multiple candidates for TextSegment."""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TopTokens(_StringCandidateList):
"""Multiple tokens with weight."""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageBytesList(ListLitType):
"""A list of ImageBytes."""
default: Sequence[Any] = []
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class URLLitType(TextSegment):
"""TextSegment that should be interpreted as a URL."""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class GeneratedURL(TextSegment):
"""A URL that was generated as part of a model prediction."""
align: Optional[str] = None # name of a field in the model output
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(value, output_spec, output_dict, input_spec,
dataset_spec, input_example)
if self.align and self.align not in output_spec:
raise ValueError(f"aligned field {self.align} is not in output_spec")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SearchQuery(TextSegment):
"""TextSegment that should be interpreted as a search query."""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _StringList(ListLitType):
"""A list of strings."""
default: Sequence[str] = []
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, list) or not all(
[isinstance(v, str) for v in value]):
raise ValueError(f"{value} is not a list of strings")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Tokens(_StringList):
"""Tokenized text."""
default: Sequence[str] = attr.Factory(list)
# Name of a TextSegment field from the input
# TODO(b/167617375): should we use 'align' here?
parent: Optional[str] = None
mask_token: Optional[str] = None # optional mask token for input
token_prefix: Optional[str] = "##" # optional prefix used in tokens
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenTopKPreds(ListLitType):
"""Predicted tokens, as from a language model.
The inner list should contain (word, probability) in descending order.
"""
default: Sequence[ScoredTextCandidates] = None
align: str = None # name of a Tokens field in the model output
parent: Optional[str] = None
def _validate_scored_candidates(self, scored_candidates):
"""Validates a list of scored candidates."""
prev_val = math.inf
for scored_candidate in scored_candidates:
if not isinstance(scored_candidate, tuple):
raise ValueError(f"{scored_candidate} is not a tuple")
if not isinstance(scored_candidate[0], str):
raise ValueError(f"{scored_candidate} first element is not a str")
if scored_candidate[1] is not None:
if not isinstance(scored_candidate[1], NumericTypes):
raise ValueError(f"{scored_candidate} second element is not a num")
if prev_val < scored_candidate[1]:
raise ValueError(
"TokenTopKPreds candidates are not in descending order")
else:
prev_val = scored_candidate[1]
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, list):
raise ValueError(f"{value} is not a list of scored text candidates")
for scored_candidates in value:
self._validate_scored_candidates(scored_candidates)
if self.align and not isinstance(output_spec[self.align], Tokens):
raise ValueError(
f"aligned field {self.align} is {type(output_spec[self.align])}, "
"expected Tokens")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Scalar(LitType):
"""Scalar value, a single float or int."""
min_val: float = 0
max_val: float = 1
default: float = 0
step: float = .01
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, NumericTypes):
raise ValueError(f"{value} is of type {type(value)}, expected a number")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class RegressionScore(Scalar):
"""Regression score, a single float."""
# name of a Scalar or RegressionScore field in input
parent: Optional[str] = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, NumericTypes):
raise ValueError(f"{value} is of type {type(value)}, expected a number")
if self.parent and not isinstance(dataset_spec[self.parent], Scalar):
raise ValueError(f"parent field {self.parent} is of type "
f"{type(self.parent)}, expected Scalar")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _FloatList(ListLitType):
"""A variable-length list of floats. Not the same as a 1D tensor."""
default: Sequence[float] = []
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, list) or not all(
[isinstance(v, float) for v in value]
):
raise ValueError(f"{value} is not a list of floats")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenScores(_FloatList):
"""Scores, aligned to tokens.
The data should be a list of floats, one for each token.
"""
align: str # name of Tokens field
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ReferenceScores(ListLitType):
"""Score of one or more target sequences."""
default: Sequence[float] = None
# name of a TextSegment or ReferenceTexts field in the input
parent: Optional[str] = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if isinstance(value, list):
if not all([isinstance(v, NumericTypes) for v in value]):
raise ValueError(f"{value} is of type {type(value)}, expected a list "
"of numbers")
elif not isinstance(value, np.ndarray) or not np.issubdtype(
value.dtype, np.number):
raise ValueError(f"{value} is of type {type(value)}, expected a list of "
"numbers")
if self.parent and not isinstance(
input_spec[self.parent], (TextSegment, ReferenceTexts)):
raise ValueError(f"parent field {self.parent} is of type "
f"{type(self.parent)}, expected TextSegment or "
"ReferenceTexts")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class CategoryLabel(StringLitType):
"""Category or class label, a single string."""
# Optional vocabulary to specify allowed values.
# If omitted, any value is accepted.
vocab: Optional[Sequence[str]] = None # label names
def validate_input(self, value, spec: Spec, example: Input):
if not isinstance(value, str):
raise ValueError(f"{value} is of type {type(value)}, expected str")
if self.vocab and value not in list(self.vocab):
raise ValueError(f"{value} is not in provided vocab")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _Tensor(LitType):
"""A tensor type."""
default: Sequence[float] = None
def validate_input(self, value, spec: Spec, example: Input):
if isinstance(value, list):
if not all([isinstance(v, NumericTypes) for v in value]):
raise ValueError(f"{value} is not a list of numbers")
elif isinstance(value, np.ndarray):
if not np.issubdtype(value.dtype, np.number):
raise ValueError(f"{value} is not an array of numbers")
else:
raise ValueError(f"{value} is not a list or ndarray of numbers")
def validate_ndim(self, value, ndim: Union[int, list[int]]):
"""Validate the number of dimensions in a tensor.
Args:
value: The tensor to validate.
ndim: Either a number of dimensions to validate that the value has, or
a list of dimensions any of which are valid for the value to have.
Raises:
ValueError if validation fails.
"""
if isinstance(ndim, int):
ndim = [ndim]
if isinstance(value, np.ndarray):
if value.ndim not in ndim:
raise ValueError(f"{value} ndim is not one of {ndim}")
else:
if 1 not in ndim:
raise ValueError(f"{value} ndim is not 1. "
"Use a numpy array for multidimensional arrays")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MulticlassPreds(_Tensor):
"""Multiclass predicted probabilities, as <float>[num_labels]."""
# Vocabulary is required here for decoding model output.
# Usually this will match the vocabulary in the corresponding label field.
vocab: Sequence[str] # label names
null_idx: Optional[int] = None # vocab index of negative (null) label
parent: Optional[str] = None # CategoryLabel field in input
autosort: Optional[bool] = False # Enable automatic sorting
threshold: Optional[float] = None # binary threshold, used to compute margin
@property
def num_labels(self):
return len(self.vocab)
def validate_input(self, value, spec: Spec, example: Input):
super().validate_input(value, spec, example)
if self.null_idx is not None:
if self.null_idx < 0 or self.null_idx >= self.num_labels:
raise ValueError(f"null_idx {self.null_idx} is not in the vocab range")
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
self.validate_input(value, output_spec, input_example)
if self.parent and not isinstance(
dataset_spec[self.parent], CategoryLabel):
raise ValueError(f"parent field {self.parent} is of type "
f"{type(self.parent)}, expected CategoryLabel")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SequenceTags(_StringList):
"""Sequence tags, aligned to tokens.
The data should be a list of string labels, one for each token.
"""
align: str # name of Tokens field
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SpanLabels(ListLitType):
"""Span labels aligned to tokens.
Span labels can cover more than one token, may not cover all tokens in the
sentence, and may overlap with each other.
"""
default: Sequence[dtypes.SpanLabel] = None
align: str # name of Tokens field
parent: Optional[str] = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, list) or not all(
[isinstance(v, dtypes.SpanLabel) for v in value]):
raise ValueError(f"{value} is not a list of SpanLabels")
if not isinstance(output_spec[self.align], Tokens):
raise ValueError(f"{self.align} is not a Tokens field")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class EdgeLabels(ListLitType):
"""Edge labels between pairs of spans.
This is a general form for structured prediction output; each entry consists
of (span1, span2, label). See
https://arxiv.org/abs/1905.06316 (Tenney et al. 2019) and
https://github.com/nyu-mll/jiant/tree/master/probing#data-format for more
details.
"""
default: Sequence[dtypes.EdgeLabel] = None
align: str # name of Tokens field
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, list) or not all(
[isinstance(v, dtypes.EdgeLabel) for v in value]):
raise ValueError(f"{value} is not a list of EdgeLabel")
if not isinstance(output_spec[self.align], Tokens):
raise ValueError(f"{self.align} is not a Tokens field")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MultiSegmentAnnotations(ListLitType):
"""Very general type for in-line text annotations.
This is a more general version of SpanLabel, EdgeLabel, and other annotation
types, designed to represent annotations that may span multiple segments.
The basic unit is dtypes.AnnotationCluster, which contains a label, optional
score, and one or more SpanLabel annotations, each of which points to a
specific segment from the input.
TODO(lit-dev): by default, spans are treated as bytes in this context.
Make this configurable, if some spans need to refer to tokens instead.
"""
default: Sequence[dtypes.AnnotationCluster] = None
exclusive: bool = False # if true, treat as candidate list
background: bool = False # if true, don't emphasize in visualization
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
if not isinstance(value, list) or not all(
[isinstance(v, dtypes.AnnotationCluster) for v in value]):
raise ValueError(f"{value} is not a list of AnnotationCluster")
##
# Model internals, for interpretation.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Embeddings(_Tensor):
"""Embeddings or model activations, as fixed-length <float>[emb_dim]."""
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(value, output_spec, output_dict, input_spec,
dataset_spec, input_example)
self.validate_ndim(value, 1)
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class _GradientsBase(_Tensor):
"""Shared gradient attributes."""
align: Optional[str] = None # name of a Tokens field
grad_for: Optional[str] = None # name of Embeddings field
# Name of the field in the input that can be used to specify the target class
# for the gradients.
grad_target_field_key: Optional[str] = None
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
if self.align is not None:
align_entry = (output_spec[self.align] if self.align in output_spec
else input_spec[self.align])
if not isinstance(align_entry, (Tokens, ImageBytes)):
raise ValueError(f"{self.align} is not a Tokens or ImageBytes field")
if self.grad_for is not None and not isinstance(
output_spec[self.grad_for], (Embeddings, TokenEmbeddings)):
raise ValueError(f"{self.grad_for} is not a Embeddings field")
if (self.grad_target_field_key is not None and
self.grad_target_field_key not in input_spec):
raise ValueError(f"{self.grad_target_field_key} is not in input_spec")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Gradients(_GradientsBase):
"""1D gradients with respect to embeddings."""
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
self.validate_ndim(value, 1)
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenEmbeddings(_Tensor):
"""Per-token embeddings, as <float>[num_tokens, emb_dim]."""
align: Optional[str] = None # name of a Tokens field
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
self.validate_ndim(value, 2)
if self.align is not None and not isinstance(
output_spec[self.align], Tokens):
raise ValueError(f"{self.align} is not a Tokens field")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenGradients(_GradientsBase):
"""Gradients for per-token inputs, as <float>[num_tokens, emb_dim]."""
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
self.validate_ndim(value, 2)
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageGradients(_GradientsBase):
"""Gradients with respect to per-pixel inputs, as a multidimensional array."""
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
self.validate_ndim(value, [2, 3])
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class AttentionHeads(_Tensor):
"""One or more attention heads, as <float>[num_heads, num_tokens, num_tokens]."""
# input and output Tokens fields; for self-attention these can be the same
align_in: str
align_out: str
def validate_output(self, value, output_spec: Spec, output_dict: JsonDict,
input_spec: Spec, dataset_spec: Spec,
input_example: Input):
super().validate_output(
value, output_spec, output_dict, input_spec, dataset_spec,
input_example)
self.validate_ndim(value, 3)
if self.align_in is None or not isinstance(
output_spec[self.align_in], Tokens):
raise ValueError(f"{self.align_in} is not a Tokens field")
if self.align_out is None or not isinstance(
output_spec[self.align_out], Tokens):
raise ValueError(f"{self.align_out} is not a Tokens field")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SubwordOffsets(ListLitType):
"""Offsets to align input tokens to wordpieces or characters.
offsets[i] should be the index of the first wordpiece for input token i.
"""
default: Sequence[int] = None
align_in: str # name of field in data spec
align_out: str # name of field in model output spec
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SparseMultilabel(_StringList):
"""Sparse multi-label represented as a list of strings."""
vocab: Optional[Sequence[str]] = None # label names
separator: str = "," # Used for display purposes.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SparseMultilabelPreds(_StringCandidateList):
"""Sparse multi-label predictions represented as a list of tuples.
The tuples are of the label and the score.
"""
default: ScoredTextCandidates = None
vocab: Optional[Sequence[str]] = None # label names
parent: Optional[str] = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class FieldMatcher(LitType):
"""For matching spec fields.
The front-end will perform spec matching and fill in the vocab field
accordingly.
"""
spec: str # which spec to check, 'dataset', 'input', or 'output'.
types: Union[str, Sequence[str]] # types of LitType to match in the spec.
vocab: Optional[Sequence[str]] = None # names matched from the spec.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SingleFieldMatcher(FieldMatcher):
"""For matching a single spec field.
UI will materialize this to a dropdown-list.
"""
default: str = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MultiFieldMatcher(FieldMatcher):
"""For matching multiple spec fields.
UI will materialize this to multiple checkboxes. Use this when the user needs
to pick more than one field in UI.
"""
default: Sequence[str] = [] # default names of selected items.
select_all: bool = False # Select all by default (overriddes default).
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Salience(LitType):
"""Metadata about a returned salience map."""
autorun: bool = False # If the saliency technique is automatically run.
signed: bool # If the returned values are signed.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class TokenSalience(Salience):
"""Metadata about a returned token salience map."""
default: dtypes.TokenSalience = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class FeatureSalience(Salience):
"""Metadata about a returned feature salience map."""
default: dtypes.FeatureSalience = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class FrameSalience(Salience):
"""Metadata about a returned frame salience map."""
default: dtypes.FrameSalience = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class ImageSalience(Salience):
"""Metadata about a returned image saliency.
The data is returned as an image in the base64 URL encoded format, e.g.,
data:image/jpg;base64,w4J3k1Bfa...
"""
signed: bool = False # If the returned values are signed.
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SequenceSalience(Salience):
"""Metadata about a returned sequence salience map."""
default: dtypes.SequenceSalienceMap = None
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class BooleanLitType(LitType):
"""Boolean value."""
default: bool = False
def validate_input(self, value, spec, example: Input):
if not isinstance(value, bool):
raise ValueError(f"{value} is not a boolean")
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class CurveDataPoints(LitType):
"""Represents data points of a curve.
A list of tuples where the first and second elements of the tuple are the
x and y coordinates of the corresponding curve point respectively.
"""
pass
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class InfluentialExamples(LitType):
"""Represents influential examples from the training set.
This is as returned by a training-data attribution method like TracIn or
influence functions.
This describes a generator component; values are Sequence[Sequence[JsonDict]].
"""
pass
@enum.unique
class MetricBestValue(dtypes.EnumSerializableAsValues, enum.Enum):
"""The method to use to determine the best value for a Metric."""
HIGHEST = "highest"
LOWEST = "lowest"
NONE = "none"
ZERO = "zero"
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class MetricResult(LitType):
"""Score returned from the computation of a Metric."""
default: float = 0
description: str = ""
best_value: MetricBestValue = MetricBestValue.NONE
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class Integer(Scalar):
step: int = 1
min_val: int = -32768
max_val: int = 32767
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class SalienceTargetInfo(LitType):
"""Target information for salience interpreters, used in config_spec().
Value is a dict with keys 'field' (str) and 'index' (Optional[int]).
"""
default: Optional[Mapping[str, Any]] = None
# LINT.ThenChange(../client/lib/lit_types.ts)
# Type aliases for backend use.
# The following names are existing datatypes in TypeScript, so we add a
# `LitType` suffix to avoid collisions with language features on the front-end.
Boolean = BooleanLitType
String = StringLitType
URL = URLLitType
def get_type_by_name(typename: str) -> type[LitType]:
cls = globals()[typename]
assert issubclass(cls, LitType)
return cls
# A map from Python's native type annotations to their LitType corollary for use
# by infer_spec_for_func().
_INFERENCE_TYPES_TO_LIT_TYPES: dict[type[Any], Callable[..., LitType]] = {
bool: Boolean,
Optional[bool]: Boolean,
float: Scalar,
Optional[float]: Scalar,
Union[float, int]: Scalar,
Optional[Union[float, int]]: Scalar,
int: Integer,
Optional[int]: Integer,
str: String,
Optional[str]: String,
epath.PathLike: String,
Optional[epath.PathLike]: String,
os.PathLike[str]: String,
Optional[os.PathLike[str]]: String,
}
def infer_spec_for_func(func: Callable[..., Any]) -> Spec:
"""Infers a Spec from the arguments of a Callable's signature.
LIT uses
[Specs](https://pair-code.github.io/lit/documentation/api.md#type-system)
as a mechanism to communicate how the web app should construct user interface
elements to enable user input for certain tasks, such as parameterizing an
Interpreter or loading a Model or Dataset at runtime. This includes
information about the type, default value (if any), required status, etc. of
the arguments to enable robust construction of HTML input elements.
As many LIT components are essentially Python functions that can be
parameterized and run via the LIT web app, this function exists to automate
the creation of Specs for some use cases, e.g., the `init_spec()` API of
`lit_nlp.api.dataset.Dataset` and `lit_nlp.api.model.Model` classes. It
attempts to infer a Spec for the Callable passed in as the value of `func` by:
1. Using `inspect.signature()` to retreive the Callable's signature info;
2. Processing `signature.parameters` to transform them into a corollary
`LitType` object that is consumable by the web app, either using
`Parameter.annotation` or by inferring a type from `Parameter.default`;
3. Adding an entry to a `Spec` dictionary where the key is `Paramater.name`
and the value is the `LitType`; and then
4. Returning the `Spec` dictionary after all arguments are processed.