[cad161]: / edsnlp / core / stream.py

Download this file

1079 lines (943 with data), 39.6 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
from __future__ import annotations
import abc
import random
import sys
import textwrap
import warnings
from collections import namedtuple
from copy import copy
from functools import wraps
from inspect import isgeneratorfunction, signature
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Iterable,
List,
Optional,
TypeVar,
Union,
)
from confit import VisibleDeprecationWarning
from typing_extensions import Literal
import edsnlp.data
from edsnlp.utils.batching import BatchBy, BatchFn, BatchSizeArg, batchify, batchify_fns
from edsnlp.utils.collections import flatten, flatten_once, shuffle
from edsnlp.utils.stream_sentinels import StreamSentinel
if TYPE_CHECKING:
import torch
from edsnlp import Pipeline
from edsnlp.core.torch_component import TorchComponent
from edsnlp.data.base import BaseReader, BaseWriter, BatchWriter
def deep_isgeneratorfunction(x):
if hasattr(x, "__call__"):
return isgeneratorfunction(x) or isgeneratorfunction(x.__call__)
elif hasattr(x, "batch_process"):
return isgeneratorfunction(x.batch_process) or isgeneratorfunction(
x.batch_process.__call__
)
raise ValueError(f"{x} does not have a __call__ or batch_process method.")
CONTEXT = [{}]
T = TypeVar("T")
def with_non_default_args(fn: T) -> T:
@wraps(fn)
def wrapper(self, **kwargs):
return fn(self, **kwargs, _non_default_args=kwargs.keys())
return wrapper
Batchable = namedtuple("Batchable", ["batch_process"])
def make_kwargs_str(kwargs, first=True):
pre_sep, join_sep = ("", ", ") if first else (", ", "")
return join_sep.join(pre_sep + f"{k}={v!r}" for k, v in kwargs.items())
class Op(abc.ABC):
elementwise: bool
def __call__(self, items):
raise NotImplementedError()
class FlattenOp(Op):
elementwise = False
def __call__(self, items):
return flatten(items)
def __repr__(self):
return "flatten()"
class UnbatchifyOp(Op):
elementwise = True
def __call__(self, items):
return flatten_once(items)
def __repr__(self):
return "unbatchify()"
class BatchifyOp(Op):
elementwise = True
def __init__(
self,
size,
batch_fn: BatchFn,
sentinel_mode: Optional[Literal["drop", "split", "auto"]] = None,
):
if batch_fn is None:
if size is None:
size = None
batch_fn = None
else:
batch_fn = batchify_fns["docs"]
self.size = size
self.batch_fn = batch_fn
self.sentinel_mode = sentinel_mode
def __call__(self, items):
assert self.sentinel_mode != "auto"
return self.batch_fn(
items,
self.size,
**{"sentinel_mode": self.sentinel_mode}
if self.sentinel_mode is not None
else {},
)
def __repr__(self):
return (
"batchify("
f"size={self.size}, "
f"fn={self.batch_fn}, "
f"sentinel_mode={self.sentinel_mode})"
)
class MapOp(Op):
def __init__(self, pipe, kwargs, context=None):
self.pipe = pipe
self.kwargs = kwargs
self.is_generator = deep_isgeneratorfunction(pipe)
self.elementwise = not self.is_generator
self.context = context or {}
def __call__(self, items):
for item in items:
if isinstance(item, StreamSentinel):
yield item
continue
CONTEXT[0], old = self.context, CONTEXT[0]
res = self.pipe(item, **self.kwargs)
CONTEXT[0] = old
if self.is_generator:
yield from res
else:
yield res
def __repr__(self):
if hasattr(self.pipe, "__self__"):
op_str = f"{self.pipe.__name__}[{object.__repr__(self.pipe.__self__)}]"
else:
op_str = object.__repr__(self.pipe)
return "map({}{})".format(op_str, make_kwargs_str(self.kwargs, False))
class MapBatchesOp(Op):
def __init__(self, pipe, kwargs, context=None, elementwise=False):
self.pipe = pipe
self.kwargs = kwargs
self.is_generator = deep_isgeneratorfunction(pipe)
if elementwise and self.is_generator:
raise ValueError("Cannot use elementwise=True with a generator function")
self.elementwise = elementwise
self.context = context or {}
def __call__(self, batches):
if hasattr(self.pipe, "batch_process"):
for batch in batches:
if isinstance(batch, StreamSentinel):
yield batch
continue
CONTEXT[0], old = self.context, CONTEXT[0]
res = self.pipe.batch_process(batch, **self.kwargs)
CONTEXT[0] = old
res = list(res) if self.is_generator else (res,)
yield from res
else:
for batch in batches:
if isinstance(batch, StreamSentinel):
yield batch
continue
results = []
for item in batch:
CONTEXT[0], old = self.context, CONTEXT[0]
res = (
item
if isinstance(item, StreamSentinel)
else self.pipe(item, **self.kwargs)
)
CONTEXT[0] = old
res = list(res) if self.is_generator else (res,)
results.extend(res)
yield results
def __repr__(self):
pipe = (
self.pipe.batch_process if isinstance(self.pipe, Batchable) else self.pipe
)
if hasattr(pipe, "__self__"):
op_str = f"{pipe.__name__}[{object.__repr__(pipe.__self__)}]"
else:
op_str = object.__repr__(pipe)
return f"map_batches_op({op_str}{make_kwargs_str(self.kwargs, False)})"
class QuickTorchPipe:
def __init__(self, prepare_batch, forward, postprocess, elementwise=False):
self.prepare_batch = prepare_batch
self.forward = forward
self.postprocess = postprocess
self.elementwise = elementwise
def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
def batch_process(self, batch):
res = self.forward(self.prepare_batch(batch, None))
return self.postprocess(batch, res) if self.postprocess is not None else res
def enable_cache(self, cache_id=None):
pass
def disable_cache(self, cache_id=None):
pass
class Stage:
def __init__(self, cpu_ops: List[Op], gpu_op: Optional[TorchComponent]):
self.cpu_ops = cpu_ops
self.gpu_op = gpu_op
def __repr__(self):
args_str = ",\n".join(textwrap.indent(repr(op), " ") for op in self.cpu_ops)
return (
f"Stage(\n"
f" cpu_ops=[\n{args_str}\n ],\n"
f" gpu_op={object.__repr__(self.gpu_op) if self.gpu_op else None})"
)
class MetaStream(type):
def __getattr__(self, item):
if item in edsnlp.data.__all__:
fn = getattr(edsnlp.data, item)
setattr(self, item, fn)
return fn
raise AttributeError(item)
def __dir__(self): # pragma: no cover
return (*super().__dir__(), *edsnlp.data.__all__)
class Stream(metaclass=MetaStream):
def __init__(
self,
reader: Optional[BaseReader] = None,
writer: Optional[Union[BaseWriter, BatchWriter]] = None,
ops: List[Any] = [],
config: Optional[Dict] = None,
):
self.reader = reader
self.writer = writer
self.ops: List[Op] = ops
self.config = config or {}
@classmethod
def validate_batching(cls, batch_size, batch_by):
if isinstance(batch_size, str):
if batch_by is not None:
raise ValueError(
"Cannot use both a batch_size expression and a batch_by function"
)
batch_size, batch_by = BatchSizeArg.validate(batch_size)
if batch_size is not None and not isinstance(batch_size, int):
raise ValueError(
f"Invalid batch_size (must be an integer or None): {batch_size}"
)
if (
batch_by is not None
and batch_by not in batchify_fns
and not callable(batch_by)
):
raise ValueError(f"Invalid batch_by function: {batch_by}")
return batch_size, batch_by
@property
def batch_size(self):
return self.config.get("batch_size", None)
@property
def batch_by(self):
return self.config.get("batch_by", None)
@property
def disable_implicit_parallelism(self):
return self.config.get("disable_implicit_parallelism", True)
@property
def num_cpu_workers(self):
return self.config.get("num_cpu_workers")
@property
def num_gpu_workers(self):
return self.config.get("num_gpu_workers")
@property
def gpu_worker_devices(self):
return self.config.get("gpu_worker_devices")
@property
def cpu_worker_devices(self):
return self.config.get("cpu_worker_devices")
@property
def autocast(self):
return self.config.get("autocast", True)
@property
def backend(self):
backend = self.config.get("backend")
return {"mp": "multiprocessing"}.get(backend, backend)
@property
def show_progress(self):
return self.config.get("show_progress", False)
@property
def process_start_method(self):
return self.config.get("process_start_method")
@property
def deterministic(self):
return self.config.get("deterministic", True)
# noinspection PyIncorrectDocstring
@with_non_default_args
def set_processing(
self,
batch_size: Optional[Union[int, str]] = None,
batch_by: BatchBy = None,
split_into_batches_after: str = None,
num_cpu_workers: Optional[int] = None,
num_gpu_workers: Optional[int] = None,
disable_implicit_parallelism: bool = True,
backend: Optional[Literal["simple", "multiprocessing", "mp", "spark"]] = None,
autocast: Union[bool, Any] = None,
show_progress: bool = False,
gpu_pipe_names: Optional[List[str]] = None,
process_start_method: Optional[Literal["fork", "spawn"]] = None,
gpu_worker_devices: Optional[List[str]] = None,
cpu_worker_devices: Optional[List[str]] = None,
deterministic: bool = True,
chunk_size: int = None,
sort_chunks: bool = False,
_non_default_args: Iterable[str] = (),
) -> "Stream":
"""
Parameters
----------
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
num_cpu_workers: int
Number of CPU workers. A CPU worker handles the non deep-learning components
and the preprocessing, collating and postprocessing of deep-learning
components. If no GPU workers are used, the CPU workers also handle the
forward call of the deep-learning components.
num_gpu_workers: Optional[int]
Number of GPU workers. A GPU worker handles the forward call of the
deep-learning components. Only used with "multiprocessing" backend.
disable_implicit_parallelism: bool
Whether to disable OpenMP and Huggingface tokenizers implicit parallelism in
multiprocessing mode. Defaults to True.
backend: Optional[Literal["simple", "multiprocessing", "spark"]]
The backend to use for parallel processing. If not set, the backend is
automatically selected based on the input data and the number of workers.
- "simple" is the default backend and is used when `num_cpu_workers` is 1
and `num_gpu_workers` is 0.
- "multiprocessing" is used when `num_cpu_workers` is greater than 1 or
`num_gpu_workers` is greater than 0.
- "spark" is used when the input data is a Spark dataframe and the output
writer is a Spark writer.
autocast: Union[bool, Any]
Whether to use
[automatic mixed precision (AMP)](https://pytorch.org/docs/stable/amp.html)
for the forward pass of the deep-learning components. If True (by default),
AMP will be used with the default settings. If False, AMP will not be used.
If a dtype is provided, it will be passed to the `torch.autocast` context
manager.
show_progress: Optional[bool]
Whether to show progress bars (only applicable with "simple" and
"multiprocessing" backends).
gpu_pipe_names: Optional[List[str]]
List of pipe names to accelerate on a GPUWorker, defaults to all pipes
that inherit from TorchComponent. Only used with "multiprocessing" backend.
Inferred from the pipeline if not set.
process_start_method: Optional[Literal["fork", "spawn"]]
Whether to use "fork" or "spawn" as the start method for the multiprocessing
backend. The default is "fork" on Unix systems and "spawn" on Windows.
- "fork" is the default start method on Unix systems and is the fastest
start method, but it is not available on Windows, can cause issues
with CUDA and is not safe when using multiple threads.
- "spawn" is the default start method on Windows and is the safest start
method, but it is not available on Unix systems and is slower than
"fork".
gpu_worker_devices: Optional[List[str]]
List of GPU devices to use for the GPU workers. Defaults to all available
devices, one worker per device. Only used with "multiprocessing" backend.
cpu_worker_devices: Optional[List[str]]
List of GPU devices to use for the CPU workers. Used for debugging purposes.
deterministic: bool
Whether to try and preserve the order of the documents in "multiprocessing"
mode. If set to `False`, workers will process documents whenever they are
available in a dynamic fashion, which may result in out-of-order but usually
faster processing. If set to true, tasks will be distributed in a
static, round-robin fashion to workers. Defaults to `True`.
Returns
-------
Stream
"""
kwargs = {k: v for k, v in locals().items() if k in _non_default_args}
if (
kwargs.pop("chunk_size", None) is not None
or kwargs.pop("sort_chunks", None) is not None
):
warnings.warn(
"chunk_size and sort_chunks are deprecated, use "
"map_batched(sort_fn, batch_size=chunk_size) instead.",
VisibleDeprecationWarning,
)
if kwargs.pop("split_into_batches_after", None) is not None:
warnings.warn(
"split_into_batches_after is deprecated.", VisibleDeprecationWarning
)
return Stream(
reader=self.reader,
writer=self.writer,
ops=self.ops,
config={
**self.config,
**{k: v for k, v in kwargs.items() if v is not None},
},
)
@classmethod
def ensure_stream(cls, data):
from edsnlp.data.base import IterableReader
if isinstance(data, cls):
return data
return cls(reader=IterableReader(data))
# For backwards compatibility
ensure_lazy = ensure_stream
def map(self, pipe, name: Optional[str] = None, kwargs={}) -> "Stream":
"""
Maps a callable to the documents. It takes a callable as input and an optional
dictionary of keyword arguments. The function will be applied to each element
of the collection. If the callable is a generator function, each element will
be yielded to the stream as is.
Parameters
----------
pipe: Any
The callable to map to the documents.
kwargs: Dict
The keyword arguments to pass to the callable.
Returns
-------
Stream
"""
return Stream(
reader=self.reader,
writer=self.writer,
ops=[*self.ops, MapOp(pipe, kwargs)],
config=self.config,
)
def flatten(self) -> "Stream":
"""
Flattens the stream.
Returns
-------
Stream
"""
return Stream(
reader=self.reader,
writer=self.writer,
ops=[*self.ops, FlattenOp()],
config=self.config,
)
def map_batches(
self,
pipe,
name: Optional[str] = None,
kwargs={},
batch_size: Optional[Union[int, str]] = None,
batch_by: BatchBy = None,
) -> "Stream":
"""
Maps a callable to a batch of documents. The callable should take a list of
inputs. The output of the callable will be flattened if it is a list or
a generator, or yielded to the stream as is if it is a single output (tuple
or any other type).
Parameters
----------
pipe: Any
The callable to map to the documents.
kwargs: Dict
The keyword arguments to pass to the callable.
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
Returns
-------
Stream
"""
batch_size, batch_by = self.validate_batching(batch_size, batch_by)
batch_fn = batchify_fns.get(batch_by, batch_by)
infer_batch = batch_size is None and batch_by is None
ops = list(self.ops)
if infer_batch and len(ops) and isinstance(ops[-1], UnbatchifyOp):
ops.pop()
else:
ops.append(BatchifyOp(batch_size, batch_fn))
ops.append(MapBatchesOp(Batchable(pipe), kwargs))
ops.append(UnbatchifyOp())
stream = Stream(
reader=self.reader,
writer=self.writer,
ops=ops,
config=self.config,
)
stream.validate_ops(ops=stream.ops, update=False)
return stream
def batchify(
self,
batch_size: Optional[Union[int, str]] = None,
batch_by: BatchBy = None,
) -> "Stream":
"""
Accumulates the documents into batches and yield each batch to the stream.
Parameters
----------
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
Returns
-------
Stream
"""
batch_size, batch_by = self.validate_batching(batch_size, batch_by)
batch_fn = batchify_fns.get(batch_by, batch_by)
ops = list(self.ops)
ops.append(BatchifyOp(batch_size, batch_fn))
stream = Stream(
reader=self.reader,
writer=self.writer,
ops=ops,
config=self.config,
)
stream.validate_ops(ops=stream.ops, update=False)
return stream
def map_gpu(
self,
prepare_batch: Callable[[List, Union[str, torch.device]], Any],
forward: Callable[[Any], Any],
postprocess: Optional[Callable[[List, Any], Any]] = None,
name: Optional[str] = None,
batch_size: Optional[Union[int, str]] = None,
batch_by: BatchBy = None,
) -> "Stream":
"""
Maps a deep learning operation to a batch of documents, on a GPU worker.
Parameters
----------
prepare_batch: Callable[[List, Union[str, torch.device]], Any]
A callable that takes a list of documents and a device and returns a batch
of tensors (or anything that can be passed to the `forward` callable). This
will be called on a CPU-bound worker, and may be parallelized.
forward: Callable[[Any], Any]
A callable that takes the output of `prepare_batch` and returns the output
of the deep learning operation. This will be called on a GPU-bound worker.
postprocess: Optional[Callable[[List, Any], Any]]
An optional callable that takes the list of documents and the output of the
deep learning operation, and returns the final output. This will be called
on the same CPU-bound worker that called the `prepare_batch` function.
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
Returns
-------
Stream
"""
batch_size, batch_by = self.validate_batching(batch_size, batch_by)
batch_fn = batchify_fns.get(batch_by, batch_by)
infer_batch = batch_size is None and batch_by is None
ops = list(self.ops)
if infer_batch and len(ops) and isinstance(ops[-1], UnbatchifyOp):
ops.pop()
else:
ops.append(BatchifyOp(batch_size, batch_fn))
pipe = QuickTorchPipe(prepare_batch, forward, postprocess)
ops.append(MapBatchesOp(pipe, {}, elementwise=True))
ops.append(UnbatchifyOp())
stream = Stream(
reader=self.reader,
writer=self.writer,
ops=ops,
config=self.config,
)
stream.validate_ops(ops=stream.ops, update=False)
return stream
def map_pipeline(
self,
model: Pipeline,
batch_size: Optional[Union[int, str]] = None,
batch_by: BatchBy = None,
) -> "Stream":
"""
Maps a pipeline to the documents, i.e. adds each component of the pipeline to
the stream operations. This function is called under the hood by `nlp.pipe()`
Parameters
----------
model: Pipeline
The pipeline to map to the documents.
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
Returns
-------
Stream
"""
new_ops = []
tokenizer = model.tokenizer
for op in self.ops:
# check if the pipe has a "tokenizer" kwarg and update the kwargs if needed
op = copy(op)
if (
(
isinstance(op, MapOp)
and "tokenizer" in signature(op.pipe).parameters
and "tokenizer" not in op.kwargs
)
or (
isinstance(op, MapBatchesOp)
and hasattr(op.pipe, "batch_process")
and "tokenizer" in signature(op.pipe.batch_process).parameters
and "tokenizer" not in op.kwargs
)
or (
isinstance(op, MapBatchesOp)
and callable(op.pipe)
and "tokenizer" in signature(op.pipe).parameters
and "tokenizer" not in op.kwargs
)
):
op.kwargs["tokenizer"] = tokenizer
if isinstance(op, (MapOp, MapBatchesOp)):
op.context["tokenizer"] = tokenizer
new_ops.append(op)
new_ops.append(MapOp(model._ensure_doc, {}))
batch_size, batch_by = self.validate_batching(batch_size, batch_by)
batch_by = batchify_fns.get(batch_by, batch_by)
new_ops.append(BatchifyOp(batch_size, batch_by))
for name, pipe in model.pipeline:
if name not in model._disabled:
op = MapBatchesOp(
pipe, {}, elementwise=not deep_isgeneratorfunction(pipe)
)
new_ops.append(op)
new_ops.append(UnbatchifyOp())
config = (
{**self.config, "batch_size": model.batch_size}
if self.batch_size is None
else self.config
)
stream = Stream(
reader=self.reader,
writer=self.writer,
ops=new_ops,
config=config,
)
stream.validate_ops(ops=stream.ops, update=False)
return stream
def shuffle(
self,
batch_size: Optional[Union[str, int]] = None,
batch_by: Optional[str, BatchFn] = None,
seed: Optional[int] = None,
shuffle_reader: Optional[Union[bool, str]] = None,
) -> "Stream":
"""
Shuffles the stream by accumulating the documents into batches and shuffling
the batches. We try to optimize and avoid the accumulation by shuffling items
directly in the reader, but if some upstream operations are not elementwise
or if the reader is not compatible with the batching mode, we have to accumulate
the documents into batches and shuffle the batches.
For instance, imagine a reading from list of 2 very large documents and applying
an operation to split the documents into sentences. Shuffling only in the
reader, then applying the split operation would not shuffle the sentences across
documents and may lead to a lack of randomness when training a model. Think of
this as having lumps after mixing your data. In our case, we detect that the
split op is not elementwise and trigger the accumulation of sentences into
batches after their generation before shuffling the batches.
Parameters
----------
batch_size: Optional[Union[int, str]]
The batch size. Can also be a batching expression like
"32 docs", "1024 words", "dataset", "fragment", etc.
batch_by: BatchBy
Function to compute the batches. If set, it should take an iterable of
documents and return an iterable of batches. You can also set it to
"docs", "words" or "padded_words" to use predefined batching functions.
Defaults to "docs".
seed: Optional[int]
The seed to use for shuffling.
shuffle_reader: Optional[bool]
Whether to shuffle the reader. Defaults to True if the reader is compatible
with the batch_by mode, False otherwise.
Returns
-------
Stream
"""
batch_size, batch_by = self.validate_batching(batch_size, batch_by)
if batch_by is None and batch_size is None:
batch_by = "dataset"
if shuffle_reader is None or shuffle_reader is True:
possible_shuffle_reader = (
batch_by
if batch_by in self.reader.emitted_sentinels and not self.reader.shuffle
else False
)
if not possible_shuffle_reader and shuffle_reader:
# Maybe should we be more explicit about why we cannot shuffle ?
raise ValueError(
"You cannot shuffle the reader given the current stream and the "
f"batching mode {batch_by!r}."
)
shuffle_reader = possible_shuffle_reader
stream = self
if shuffle_reader:
if shuffle_reader not in self.reader.emitted_sentinels:
raise ValueError(f"Cannot shuffle by {shuffle_reader}")
stream = Stream(
reader=copy(stream.reader),
writer=stream.writer,
ops=stream.ops,
config=stream.config,
)
stream.reader.shuffle = shuffle_reader
# Ensure that we have a "deterministic" random seed, meaning
# if the user sets a global seed before in the program and execute the
# same program twice, the shuffling should be the same in both cases.
# This is not garanteed by just creating random.Random() which does not
# account for the global seed.
if seed is not None:
stream.reader.rng = random.Random(seed)
# Else, if seed is None, then the reader rng stays the same
if any(not op.elementwise for op in self.ops) or shuffle_reader != batch_by:
stream = stream.map_batches(
pipe=shuffle,
batch_size=batch_size,
batch_by=batch_by,
kwargs={"rng": random.Random(seed)},
)
stream.validate_ops(ops=stream.ops, update=False)
return stream
def loop(self) -> "Stream":
"""
Loops over the stream indefinitely.
Note that we cycle over items produced by the reader, not the items produced by
the stream operations. This means that the stream operations will be applied to
the same items multiple times, and may produce different results if they are
non-deterministic. This also mean that calling this function will have the same
effect regardless of the operations applied to the stream before calling it, ie:
```
stream.loop().map(...)
# is equivalent to
stream.map(...).loop()
```
Returns
-------
Stream
"""
stream = Stream(
reader=copy(self.reader),
writer=self.writer,
ops=self.ops,
config=self.config,
)
stream.reader.loop = True
return stream
def write(self, writer: BaseWriter, execute: bool = True) -> Any:
if self.writer is not None:
raise ValueError("A writer is already set.")
stream = Stream(
reader=self.reader,
writer=writer,
ops=self.ops,
config=self.config,
)
return stream.execute() if execute else stream
def execute(self):
import edsnlp.processing
backend = self.backend
if backend is None:
try:
SparkReader = sys.modules.get("edsnlp.data.spark").SparkReader
SparkWriter = sys.modules.get("edsnlp.data.spark").SparkWriter
except (KeyError, AttributeError): # pragma: no cover
SparkReader = SparkWriter = None
if (
SparkReader
and isinstance(self.reader, SparkReader)
and SparkWriter
and (self.writer is None or isinstance(self.writer, SparkWriter))
):
backend = "spark"
elif (
self.num_cpu_workers is not None or self.num_gpu_workers is not None
) and (
self.num_cpu_workers is not None
and self.num_cpu_workers > 0
or self.num_gpu_workers is not None
and self.num_gpu_workers > 0
):
backend = "multiprocessing"
else:
backend = "simple"
execute = getattr(edsnlp.processing, f"execute_{backend}_backend")
return execute(self)
def __iter__(self):
return iter(self.execute())
def torch_components(self) -> Iterable["TorchComponent"]:
"""
Yields components that are PyTorch modules.
Returns
-------
Iterable['edsnlp.core.torch_component.TorchComponent']
"""
for op in self.ops:
if hasattr(op, "pipe") and hasattr(op.pipe, "forward"):
yield op.pipe
def train(self, mode=True):
"""
Enables training mode on pytorch modules
Parameters
----------
mode: bool
Whether to enable training or not
"""
class context:
def __enter__(self):
pass
def __exit__(ctx_self, type, value, traceback):
for proc in procs:
proc.train(was_training[proc])
procs = [x for x in self.torch_components() if hasattr(x, "train")]
was_training = {proc: proc.training for proc in procs}
for proc in procs:
proc.train(mode)
return context()
def eval(self):
"""
Enables evaluation mode on pytorch modules
"""
return self.train(False)
def worker_copy(self):
return Stream(
reader=self.reader.worker_copy(),
writer=self.writer,
ops=self.ops,
config=self.config,
)
def __dir__(self): # pragma: no cover
return (*super().__dir__(), *edsnlp.data.__all__)
def __getattr__(self, item):
return getattr(Stream, item).__get__(self)
def _make_stages(self, split_torch_pipes: bool) -> List[Stage]:
current_ops = []
stages = []
ops = [copy(op) for op in self.ops]
for op in ops:
if (
isinstance(op, MapBatchesOp)
and hasattr(op.pipe, "forward")
and split_torch_pipes
):
stages.append(Stage(current_ops, op.pipe))
current_ops = []
else:
current_ops.append(op)
if len(current_ops) or len(stages) == 0:
stages.append(Stage(current_ops, None))
self.validate_ops(ops=ops, update=True)
return stages
def validate_ops(self, ops, update: bool = False):
# Check batchify requirements
requires_sentinels = set()
self_batch_size, self_batch_by = self.validate_batching(
self.batch_size, self.batch_by
)
if self_batch_by is None:
self_batch_by = "docs"
if self_batch_size is None:
self_batch_size = 1
self_batch_fn = batchify_fns.get(self_batch_by, self_batch_by)
if hasattr(self.writer, "batch_fn") and hasattr(
self.writer.batch_fn, "requires_sentinel"
):
requires_sentinels.add(self.writer.batch_fn.requires_sentinel)
for op in reversed(ops):
if isinstance(op, BatchifyOp):
if op.batch_fn is None and op.size is None:
batch_size = self_batch_size
batch_fn = self_batch_fn
elif op.batch_fn is None:
batch_size = op.size
batch_fn = batchify
else:
batch_size = op.size
batch_fn = op.batch_fn
sentinel_mode = op.sentinel_mode or (
"auto"
if "sentinel_mode" in signature(batch_fn).parameters
else None
)
if sentinel_mode == "auto":
sentinel_mode = "split" if requires_sentinels else "drop"
if requires_sentinels and sentinel_mode == "drop":
raise ValueError(
f"Operation {op} drops the stream sentinel values "
f"(markers for the end of a dataset or a dataset "
f"fragment), but some downstream operation(s) require "
f"the following sentinel values: {requires_sentinels}. "
f"Ensure that you do not set `sentinel_mode='drop'` on "
f"any upstream batching operation."
)
if update:
op.size = batch_size
op.batch_fn = batch_fn
op.sentinel_mode = sentinel_mode
if hasattr(op.batch_fn, "requires_sentinel"):
requires_sentinels.add(op.batch_fn.requires_sentinel)
sentinel_str = ", ".join(requires_sentinels)
if requires_sentinels and self.backend == "spark":
raise ValueError(
f"Some operations require sentinel values ({sentinel_str}), "
f"but the Spark backend does not support sentinel values."
)
if requires_sentinels and not self.deterministic:
raise ValueError(
f"Some operations require sentinel values ({sentinel_str}), "
f"but these are not supported in when `deterministic=False`."
)
if not (requires_sentinels <= self.reader.emitted_sentinels):
raise ValueError(
f"Some operations require sentinel values ({sentinel_str}), "
f"but the reader does not emit these values "
f"({', '.join(self.reader.emitted_sentinels)})."
)
def __repr__(self):
ops_str = ",\n".join(textwrap.indent(repr(op), " ") for op in self.ops)
if ops_str:
ops_str = "\n" + ops_str + "\n "
return (
f"Stream(\n"
f" reader={self.reader},\n"
f" ops=[{ops_str}],\n"
f" writer={self.writer})\n"
)
if TYPE_CHECKING:
from edsnlp.data import to_iterable as to_iterable # noqa: F401
from edsnlp.data import to_pandas as to_pandas # noqa: F401
from edsnlp.data import to_polars as to_polars # noqa: F401
from edsnlp.data import to_spark as to_spark # noqa: F401
from edsnlp.data import write_brat as write_brat # noqa: F401
from edsnlp.data import write_json as write_json # noqa: F401
from edsnlp.data import write_parquet as write_parquet # noqa: F401
from edsnlp.data import write_standoff as write_standoff # noqa: F401
# For backwards compatibility
LazyCollection = Stream