forked from Ascend/TransferQueue
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterface.py
More file actions
1150 lines (948 loc) · 46.9 KB
/
interface.py
File metadata and controls
1150 lines (948 loc) · 46.9 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
# Copyright 2025 Huawei Technologies Co., Ltd. All Rights Reserved.
# Copyright 2025 The TransferQueue Team
#
# 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 math
import os
import subprocess
import time
from importlib import resources
from typing import Any, Callable, Optional
from urllib.parse import urlparse
import ray
import torch
from omegaconf import DictConfig, OmegaConf
from tensordict import TensorDict
from tensordict.tensorclass import NonTensorStack
from transfer_queue.client import TransferQueueClient
from transfer_queue.controller import TransferQueueController
from transfer_queue.metadata import KVBatchMeta
from transfer_queue.sampler import * # noqa: F401
from transfer_queue.sampler import BaseSampler
from transfer_queue.storage.simple_backend import SimpleStorageUnit
from transfer_queue.utils.common import get_placement_group
from transfer_queue.utils.logging_utils import get_logger
from transfer_queue.utils.yuanrong_utils import (
cleanup_yuanrong_resources,
initialize_yuanrong_backend,
)
from transfer_queue.utils.zmq_utils import process_zmq_server_info
logger = get_logger(__name__)
_TRANSFER_QUEUE_CLIENT: Any = None
_TRANSFER_QUEUE_STORAGE: Any = None
_TRANSFER_QUEUE_CONTROLLER: Any = None
def _maybe_create_transferqueue_client(
conf: Optional[DictConfig] = None,
) -> TransferQueueClient:
global _TRANSFER_QUEUE_CLIENT
if _TRANSFER_QUEUE_CLIENT is None:
if conf is None:
_init_from_existing()
assert _TRANSFER_QUEUE_CLIENT is not None, (
"TransferQueueController has not been initialized yet. Please call init() first."
)
return _TRANSFER_QUEUE_CLIENT
pid = os.getpid()
_TRANSFER_QUEUE_CLIENT = TransferQueueClient(
client_id=f"TransferQueueClient_{pid}", controller_info=conf.controller.zmq_info
)
backend_name = conf.backend.storage_backend
_TRANSFER_QUEUE_CLIENT.initialize_storage_manager(manager_type=backend_name, config=conf.backend[backend_name])
return _TRANSFER_QUEUE_CLIENT
def _maybe_create_transferqueue_storage(conf: DictConfig) -> DictConfig:
global _TRANSFER_QUEUE_STORAGE
if _TRANSFER_QUEUE_STORAGE is None:
_TRANSFER_QUEUE_STORAGE = {}
if conf.backend.storage_backend == "SimpleStorage":
# initialize SimpleStorageUnit
simple_storage_handles = {}
num_data_storage_units = conf.backend.SimpleStorage.num_data_storage_units
total_storage_size = conf.backend.SimpleStorage.total_storage_size
storage_placement_group = get_placement_group(num_data_storage_units, num_cpus_per_actor=1)
for storage_unit_rank in range(num_data_storage_units):
storage_node = SimpleStorageUnit.options( # type: ignore[attr-defined]
placement_group=storage_placement_group,
placement_group_bundle_index=storage_unit_rank,
name=f"TransferQueueStorageUnit#{storage_unit_rank}",
).remote(
storage_unit_size=math.ceil(total_storage_size / num_data_storage_units),
)
simple_storage_handles[f"TransferQueueStorageUnit#{storage_unit_rank}"] = storage_node
logger.info(f"TransferQueueStorageUnit#{storage_unit_rank} has been created.")
storage_zmq_info = process_zmq_server_info(simple_storage_handles)
backend_name = conf.backend.storage_backend
conf.backend[backend_name].zmq_info = storage_zmq_info
_TRANSFER_QUEUE_STORAGE["SimpleStorage"] = simple_storage_handles
if conf.backend.storage_backend == "MooncakeStore":
if conf.backend.MooncakeStore.auto_init:
# Try to kill existing mooncake_master processes before starting a new one to avoid potential conflicts
check = subprocess.run(["pgrep", "-f", "mooncake_master"], stdout=subprocess.PIPE, text=True)
if check.returncode == 0:
pids = check.stdout.strip().replace("\n", ", ")
logger.info(f"Find existing mooncake_master (PID: {pids}), try to kill first...")
result = os.system('pkill -f "[m]ooncake_master"')
if result == 0:
logger.info("Successfully killed existing mooncake_master processes.")
else:
raise RuntimeError(f"Failed to kill existing mooncake_master processes (exit code: {result}).")
# process metadata_server
metadata_server_raw_address = conf.backend.MooncakeStore.metadata_server
if "://" not in metadata_server_raw_address:
metadata_server_raw_address = "//" + metadata_server_raw_address
metadata_server_parsed = urlparse(metadata_server_raw_address)
if not metadata_server_parsed.hostname or metadata_server_parsed.port is None:
raise ValueError(
f"Invalid metadata_server '{conf.backend.MooncakeStore.metadata_server}'. "
f"Host and port are required (e.g., host:port)."
)
metadata_server_host = metadata_server_parsed.hostname
metadata_server_port = str(metadata_server_parsed.port)
# process master_server
master_server_raw_address = conf.backend.MooncakeStore.master_server_address
if "://" not in master_server_raw_address:
master_server_raw_address = "//" + master_server_raw_address
master_server_parsed = urlparse(master_server_raw_address)
if not master_server_parsed.hostname or master_server_parsed.port is None:
raise ValueError(
f"Invalid master_server_address '{conf.backend.MooncakeStore.master_server_address}'. "
f"Host and port are required (e.g., host:port)."
)
master_server_port = str(master_server_parsed.port)
cmd = [
"mooncake_master",
"-default_kv_lease_ttl=999999",
"-default_kv_soft_pin_ttl=999999",
"--eviction_high_watermark_ratio=1.0",
"--eviction_ratio=0.0",
"--enable_http_metadata_server=true",
"--allow_evict_soft_pinned_objects=false",
f"--http_metadata_server_host={metadata_server_host}",
f"--http_metadata_server_port={metadata_server_port}",
f"--rpc_port={master_server_port}",
]
log_file_path = "/tmp/mooncake_master.log"
with open(log_file_path, "w") as log_file:
process = subprocess.Popen(
cmd,
stdout=log_file,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True,
start_new_session=True,
)
time.sleep(3)
if process.poll() is None:
logger.info(
f"mooncake_master started, PID: {process.pid}. Logs are at: {os.path.abspath(log_file_path)}"
)
else:
error_msg = ""
try:
with open(log_file_path) as f:
error_msg = f.read()
except Exception as e:
error_msg = f"Failed to read log file: {e}"
raise RuntimeError(
f"mooncake_master exited with error. Check {log_file_path} for detailed logs. "
f"Output:\n{error_msg}"
)
_TRANSFER_QUEUE_STORAGE["MooncakeStore"] = process
if conf.backend.storage_backend == "Yuanrong" and conf.backend.Yuanrong.auto_init:
_TRANSFER_QUEUE_STORAGE["Yuanrong"] = initialize_yuanrong_backend(conf)
return conf
def _init_from_existing() -> bool:
"""Initialize the TransferQueueClient from existing controller.
Returns:
True if successfully initialized from existing controller, False otherwise.
"""
global _TRANSFER_QUEUE_CONTROLLER
try:
if _TRANSFER_QUEUE_CONTROLLER is None:
_TRANSFER_QUEUE_CONTROLLER = ray.get_actor("TransferQueueController")
except ValueError:
logger.info("Called _init_from_existing() but TransferQueueController has not been initialized yet.")
return False
logger.info("Found existing TransferQueueController instance. Connecting...")
conf = None
while conf is None:
conf = ray.get(_TRANSFER_QUEUE_CONTROLLER.get_config.remote())
if conf is not None:
_maybe_create_transferqueue_client(conf)
logger.info("TransferQueueClient initialized.")
return True
logger.debug("Waiting for controller to initialize... Retrying in 1s")
time.sleep(1)
return False
# ==================== Initialization API ====================
def init(conf: Optional[DictConfig] = None) -> Optional[DictConfig]:
"""Initialize the TransferQueue system.
This function sets up the TransferQueue controller, distributed storage, and client.
It should be called once at the beginning of the program before any data operations.
If a controller already exists (e.g., initialized by another process), this function
will retrieve the config from existing controller and initialize the TransferQueueClient.
In this case, the `conf` parameter will be ignored.
Args:
conf: Optional configuration dictionary. If provided, it will be merged with
the default config from 'config.yaml'. This is only used for first-time
initializing. When connecting to an existing controller, this parameter
is ignored.
Returns:
The merged configuration dictionary.
Raises:
ValueError: If config is not valid or required configuration keys are missing.
Example:
>>> # In process 0, node A
>>> import transfer_queue as tq
>>> tq.init() # Initialize the TransferQueue
>>> tq.put(...) # then you can use tq for data operations
>>>
>>> # In process 1, node B (with Ray connected to node A)
>>> import transfer_queue as tq
>>> tq.init() # This will only initialize a TransferQueueClient and link with existing TQ
>>> metadata = tq.get_meta(...)
>>> data = tq.get_data(metadata)
"""
if _init_from_existing():
return conf
# First-time initialize TransferQueue
logger.info("No TransferQueueController found. Starting first-time initialization...")
# create config
final_conf = OmegaConf.create({}, flags={"allow_objects": True})
default_conf = OmegaConf.load(resources.files("transfer_queue") / "config.yaml")
final_conf = OmegaConf.merge(final_conf, default_conf)
if conf:
final_conf = OmegaConf.merge(final_conf, conf)
# create controller
try:
sampler = final_conf.controller.sampler
if isinstance(sampler, BaseSampler):
# user pass a pre-initialized sampler instance
sampler = sampler
elif isinstance(sampler, type) and issubclass(sampler, BaseSampler):
# user pass a sampler class
sampler = sampler()
elif isinstance(sampler, str):
# user pass a sampler name str
# try to convert as sampler class
sampler = globals()[final_conf.controller.sampler]
except KeyError:
raise ValueError(f"Could not find sampler {final_conf.controller.sampler}") from None
try:
# Ray will make sure actor with same name can only be created once
global _TRANSFER_QUEUE_CONTROLLER
_TRANSFER_QUEUE_CONTROLLER = TransferQueueController.options(name="TransferQueueController").remote( # type: ignore[attr-defined]
sampler=sampler, polling_mode=final_conf.controller.polling_mode
)
logger.info("TransferQueueController has been created.")
except ValueError:
logger.info("Some other rank has initialized TransferQueueController. Try to connect to existing controller.")
_init_from_existing()
return final_conf
controller_zmq_info = process_zmq_server_info(_TRANSFER_QUEUE_CONTROLLER)
final_conf.controller.zmq_info = controller_zmq_info
# create distributed storage backends
final_conf = _maybe_create_transferqueue_storage(final_conf)
# store the config into controller
ray.get(_TRANSFER_QUEUE_CONTROLLER.store_config.remote(final_conf))
logger.info(f"TransferQueue config: {final_conf}")
# create client
_maybe_create_transferqueue_client(final_conf)
return final_conf
def close():
"""Close the TransferQueue system.
This function cleans up the TransferQueue system, including:
- Closing the client and its associated resources
- Cleaning up distributed storage (only for the process that initialized it)
- Killing the controller actor
Note:
This function should be called when the TransferQueue system is no longer needed.
"""
global _TRANSFER_QUEUE_CLIENT
global _TRANSFER_QUEUE_STORAGE
global _TRANSFER_QUEUE_CONTROLLER
try:
if _TRANSFER_QUEUE_STORAGE:
for key, value in _TRANSFER_QUEUE_STORAGE.items():
if key == "SimpleStorage":
# only the process that do first-time init can clean the distributed storage
for storage in value.values():
ray.kill(storage)
elif key == "MooncakeStore":
check = subprocess.run(["pgrep", "-f", "mooncake_master"], stdout=subprocess.PIPE, text=True)
if check.returncode == 0:
pids = check.stdout.strip().replace("\n", ", ")
logger.warning(
f"TransferQueue will not stop mooncake_master process with PID: {pids}. "
f"Consider manually killing the mooncake_master."
)
if _TRANSFER_QUEUE_CLIENT:
try:
ret = _TRANSFER_QUEUE_CLIENT.storage_manager.storage_client._store.remove_all()
if ret < 0:
logger.error("Failed to remove existing keys in mooncake_master.")
else:
logger.info("Successfully removed all existing keys in mooncake_master.")
except Exception:
pass
elif key == "Yuanrong":
cleanup_yuanrong_resources(value)
else:
logger.warning(f"close for _TRANSFER_QUEUE_STORAGE with key {key} is not supported for now.")
_TRANSFER_QUEUE_STORAGE = None
except Exception:
pass
if _TRANSFER_QUEUE_CLIENT:
_TRANSFER_QUEUE_CLIENT.close()
_TRANSFER_QUEUE_CLIENT = None
if _TRANSFER_QUEUE_CONTROLLER:
try:
ray.kill(_TRANSFER_QUEUE_CONTROLLER)
except Exception:
pass
_TRANSFER_QUEUE_CONTROLLER = None
try:
controller = ray.get_actor("TransferQueueController")
ray.kill(controller)
except Exception:
pass
# ==================== High-Level KV Interface API ====================
def kv_put(
key: str,
partition_id: str,
fields: Optional[TensorDict | dict[str, Any]] = None,
tag: Optional[dict[str, Any]] = None,
data_parser: Optional[Callable[[Any], Any]] = None,
) -> KVBatchMeta:
"""Put a single key-value pair to TransferQueue.
This is a convenience method for putting data using a user-specified key
instead of BatchMeta. Internally, the key is translated to a BatchMeta
and the data is stored using the regular put mechanism.
Args:
key: User-specified key for the data sample (in row)
partition_id: Logical partition to store the data in
fields: Data fields to store. Can be a TensorDict or a dict of tensors.
Each key in `fields` will be treated as a column for the data sample.
If dict is provided, tensors will be unsqueezed to add batch dimension.
If not provided, will only update the newly given tag to the key.
tag: Optional metadata tag to associate with the key
data_parser: Optional callable to parse reference data (e.g., URLs) into real
content. The input is a slice of the `fields` parameter passed to
kv_put / kv_batch_put, in plain dict format (not TensorDict),
mapping field_name -> batched values. For a regular tensor column
the value is a batched tensor; for nested tensors (jagged or
strided) and NonTensorStack columns the values are extracted into
a list. It must modify values in-place based on the original keys;
do not add or remove keys. The number of elements per column must
also remain unchanged. Do not change the inner order of values
within each column. Only supported by SimpleStorage.
Returns:
KVBatchMeta: Metadata containing the key, tags, partition_id, and fields.
The `fields` attribute includes all fields stored for this sample,
including any new fields written by this put operation.
Raises:
ValueError: If neither fields nor tag is provided
ValueError: If nested tensors are provided (use kv_batch_put instead)
RuntimeError: If retrieved BatchMeta size doesn't match length of `keys`
Example:
>>> import transfer_queue as tq
>>> import torch
>>> tq.init()
>>> # Put with both fields and tag
>>> meta = tq.kv_put(
... key="sample_1",
... partition_id="train",
... fields={"input_ids": torch.tensor([1, 2, 3])},
... tag={"score": 0.95}
... )
>>> print(meta.fields) # ['input_ids']
"""
if fields is None and tag is None:
raise ValueError("Please provide at least one parameter of `fields` or `tag`.")
tq_client = _maybe_create_transferqueue_client()
# 1. translate user-specified key to BatchMeta
batch_meta = tq_client.kv_retrieve_meta(keys=[key], partition_id=partition_id, create=True)
if batch_meta.size != 1:
raise RuntimeError(f"Retrieved BatchMeta size {batch_meta.size} does not match with input `key` size of 1!")
# 2. register the user-specified tag to BatchMeta
if tag is not None:
batch_meta.update_custom_meta([tag])
# 3. put data
if fields is not None:
if isinstance(fields, dict):
# TODO: consider whether to support this...
batch = {}
for field_name, value in fields.items():
if isinstance(value, torch.Tensor):
if value.is_nested:
raise ValueError("Please use (async)kv_batch_put for batch operation")
batch[field_name] = value.unsqueeze(0)
else:
batch[field_name] = NonTensorStack(value)
fields = TensorDict(batch, batch_size=[1])
elif not isinstance(fields, TensorDict):
raise ValueError("`fields` can only be dict or TensorDict")
# After put, batch_meta.field_names will include the new fields written by user
batch_meta = tq_client.put(fields, batch_meta, data_parser=data_parser)
else:
# Directly update custom_meta (tag) to controller
tq_client.set_custom_meta(batch_meta)
fields_to_return = batch_meta.field_names
return KVBatchMeta(
keys=[key],
tags=batch_meta.custom_meta,
partition_id=partition_id,
fields=fields_to_return,
extra_info=batch_meta.extra_info,
)
def kv_batch_put(
keys: list[str],
partition_id: str,
fields: Optional[TensorDict] = None,
tags: Optional[list[dict[str, Any]]] = None,
data_parser: Optional[Callable[[Any], Any]] = None,
) -> KVBatchMeta:
"""Put multiple key-value pairs to TransferQueue in batch.
This method stores multiple key-value pairs in a single operation, which is more
efficient than calling kv_put multiple times.
Args:
keys: List of user-specified keys for the data
partition_id: Logical partition to store the data in
fields: TensorDict containing data for all keys. Must have batch_size == len(keys).
If not provided, will only update the newly given tags to the keys.
tags: List of metadata tags, one for each key
data_parser: Optional callable to parse reference data (e.g., URLs) into real
content. The input is a slice of the `fields` parameter passed to
kv_put / kv_batch_put, in plain dict format (not TensorDict),
mapping field_name -> batched values. For a regular tensor column
the value is a batched tensor; for nested tensors (jagged or
strided) and NonTensorStack columns the values are extracted into
a list. It must modify values in-place based on the original keys;
do not add or remove keys. The number of elements per column must
also remain unchanged. Do not change the inner order of values
within each column. Only supported by SimpleStorage.
Returns:
KVBatchMeta: Metadata containing the keys, tags, partition_id, and fields.
The `fields` attribute includes all fields stored for these samples,
including any new fields written by this put operation.
Raises:
ValueError: If neither `fields` nor `tags` is provided
ValueError: If length of `keys` doesn't match length of `tags` or the batch_size of `fields` TensorDict
RuntimeError: If retrieved BatchMeta size doesn't match length of `keys`
Example:
>>> import transfer_queue as tq
>>> from tensordict import TensorDict
>>> tq.init()
>>> keys = ["sample_1", "sample_2", "sample_3"]
>>> fields = TensorDict({
... "input_ids": torch.randn(3, 10),
... "attention_mask": torch.ones(3, 10),
... }, batch_size=3)
>>> tags = [{"score": 0.9}, {"score": 0.85}, {"score": 0.95}]
>>> meta = tq.kv_batch_put(keys=keys, partition_id="train", fields=fields, tags=tags)
>>> print(meta.fields) # ['input_ids', 'attention_mask']
"""
if fields is None and tags is None:
raise ValueError("Please provide at least one parameter of fields or tag.")
if fields is not None and fields.batch_size[0] != len(keys):
raise ValueError(
f"`keys` with length {len(keys)} does not match the `fields` TensorDict with "
f"batch_size {fields.batch_size[0]}"
)
tq_client = _maybe_create_transferqueue_client()
# 1. translate user-specified key to BatchMeta
batch_meta = tq_client.kv_retrieve_meta(keys=keys, partition_id=partition_id, create=True)
if batch_meta.size != len(keys):
raise RuntimeError(
f"Retrieved BatchMeta size {batch_meta.size} does not match with input `keys` size {len(keys)}!"
)
# 2. register the user-specified tags to BatchMeta
if tags is not None:
if len(tags) != len(keys):
raise ValueError(f"keys with length {len(keys)} does not match length of tags {len(tags)}")
batch_meta.update_custom_meta(tags)
# 3. put data
if fields is not None:
# After put, batch_meta.field_names will include the new fields written by user
batch_meta = tq_client.put(fields, batch_meta, data_parser=data_parser)
else:
# Directly update custom_meta (tags) to controller
tq_client.set_custom_meta(batch_meta)
fields_to_return = batch_meta.field_names
return KVBatchMeta(
keys=keys,
tags=batch_meta.custom_meta,
partition_id=partition_id,
fields=fields_to_return,
extra_info=batch_meta.extra_info,
)
def kv_batch_get_by_meta(meta: KVBatchMeta, select_fields: Optional[list[str] | str] = None) -> TensorDict:
"""Get data from TransferQueue using KVBatchMeta.
This is a convenience method for retrieving data using KVBatchMeta returned
from a previous put operation. It extracts the keys and partition_id from
the metadata to fetch the corresponding data.
Args:
meta: KVBatchMeta object returned from a previous put operation (e.g., kv_put,
kv_batch_put). It contains keys, partition_id, and fields information.
select_fields: Optional field(s) to retrieve, which overrides the fields
recorded in the given KVBatchMeta. If None, uses all fields
from meta.fields. Can be a single field name (str) or a list
of field names.
Returns:
TensorDict with the requested data
Raises:
ValueError: If keys or partition are not found
ValueError: If empty fields exist in any key (sample)
ValueError: If any field in select_fields doesn't exist in KVBatchMeta.fields
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> # First put some data
>>> keys = ["sample_1", "sample_2", "sample_3"]
>>> fields = TensorDict({
... "input_ids": torch.randn(3, 10),
... "attention_mask": torch.ones(3, 10),
... }, batch_size=3)
>>> meta = tq.kv_batch_put(keys=keys, partition_id="train", fields=fields)
>>> # Then retrieve it using the returned metadata
>>> data = tq.kv_batch_get_by_meta(meta)
"""
if meta.partition_id is None:
raise ValueError("Must provide partition_id in the input KVBatchMeta.")
if select_fields is not None:
if isinstance(select_fields, str):
fields_to_fetch: Optional[list[str]] = [select_fields]
else:
fields_to_fetch = select_fields
assert fields_to_fetch is not None
if meta.fields is None or any(f not in meta.fields for f in fields_to_fetch):
raise ValueError(
f"Some fields assigned in select_fields not found in the metadata. "
f"Assigned: {fields_to_fetch}; Fields in KVBatchMeta: {meta.fields}."
)
else:
fields_to_fetch = meta.fields
return kv_batch_get(keys=meta.keys, partition_id=meta.partition_id, select_fields=fields_to_fetch)
def kv_batch_get(
keys: list[str] | str, partition_id: str, select_fields: Optional[list[str] | str] = None
) -> TensorDict:
"""Get data from TransferQueue using user-specified keys.
This is a convenience method for retrieving data using keys instead of indexes.
Args:
keys: Single key or list of keys to retrieve
partition_id: Partition containing the keys
select_fields: Optional field(s) to retrieve. If None, retrieves all fields
Returns:
TensorDict with the requested data
Raises:
ValueError: If keys or partition are not found
ValueError: If empty fields exist in any key (sample)
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> # Get single key with all fields
>>> data = tq.kv_batch_get(keys="sample_1", partition_id="train")
>>> # Get multiple keys with specific fields
>>> data = tq.kv_batch_get(
... keys=["sample_1", "sample_2"],
... partition_id="train",
... select_fields="input_ids"
... )
"""
tq_client = _maybe_create_transferqueue_client()
batch_meta = tq_client.kv_retrieve_meta(keys=keys, partition_id=partition_id, create=False)
if batch_meta.size == 0:
raise ValueError("keys or partition were not found!")
fields_to_fetch: list[str] | None
if select_fields is not None:
if isinstance(select_fields, str):
fields_to_fetch = [select_fields]
else:
fields_to_fetch = select_fields
batch_meta = batch_meta.select_fields(fields_to_fetch)
if not batch_meta.is_ready:
raise ValueError("Some fields are not ready in all the requested keys!")
data = tq_client.get_data(batch_meta)
return data
def kv_list(partition_id: Optional[str] = None) -> dict[str, dict[str, Any]]:
"""List all keys and their metadata in one or all partitions.
Args:
partition_id: The specific partition_id to query.
If None (default), returns keys from all partitions.
Returns:
A nested dictionary mapping partition IDs to their keys and metadata.
Structure:
{
"partition_id": {
"key_name": {
"tag1": <value>,
... (other metadata)
},
...,
},
...
}
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> # Case 1: Retrieve a specific partition
>>> partitions = tq.kv_list(partition_id="train")
>>> print(f"Keys: {list(partitions['train'].keys())}")
>>> print(f"Tags: {list(partitions['train'].values())}")
>>> # Case 2: Retrieve all partitions
>>> all_partitions = tq.kv_list()
>>> for pid, keys in all_partitions.items():
>>> print(f"Partition: {pid}, Key count: {len(keys)}")
"""
tq_client = _maybe_create_transferqueue_client()
partition_info = tq_client.kv_list(partition_id)
return partition_info
def kv_clear(keys: list[str] | str, partition_id: str) -> None:
"""Clear key-value pairs from TransferQueue.
This removes the specified keys and their associated data from both
the controller and storage units.
Args:
keys: Single key or list of keys to clear
partition_id: Partition containing the keys
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> # Clear single key
>>> tq.kv_clear(keys="sample_1", partition_id="train")
>>> # Clear multiple keys
>>> tq.kv_clear(keys=["sample_1", "sample_2"], partition_id="train")
"""
if isinstance(keys, str):
keys = [keys]
tq_client = _maybe_create_transferqueue_client()
batch_meta = tq_client.kv_retrieve_meta(keys=keys, partition_id=partition_id, create=False)
if batch_meta.size > 0:
tq_client.clear_samples(batch_meta)
# ==================== KV Interface API ====================
async def async_kv_put(
key: str,
partition_id: str,
fields: Optional[TensorDict | dict[str, Any]] = None,
tag: Optional[dict[str, Any]] = None,
data_parser: Optional[Callable[[Any], Any]] = None,
) -> KVBatchMeta:
"""Asynchronously put a single key-value pair to TransferQueue.
This is a convenience method for putting data using a user-specified key
instead of BatchMeta. Internally, the key is translated to a BatchMeta
and the data is stored using the regular put mechanism.
Args:
key: User-specified key for the data sample (in row)
partition_id: Logical partition to store the data in
fields: Data fields to store. Can be a TensorDict or a dict of tensors.
Each key in `fields` will be treated as a column for the data sample.
If dict is provided, tensors will be unsqueezed to add batch dimension.
If not provided, will only update the newly given tag to the key.
tag: Optional metadata tag to associate with the key
data_parser: Optional callable to parse reference data (e.g., URLs) into real
content. The input is a slice of the `fields` parameter passed to
kv_put / kv_batch_put, in plain dict format (not TensorDict),
mapping field_name -> batched values. For a regular tensor column
the value is a batched tensor; for nested tensors (jagged or
strided) and NonTensorStack columns the values are extracted into
a list. It must modify values in-place based on the original keys;
do not add or remove keys. The number of elements per column must
also remain unchanged. Do not change the inner order of values
within each column. Only supported by SimpleStorage.
Returns:
KVBatchMeta: Metadata containing the key, tags, partition_id, and fields.
The `fields` attribute includes all fields stored for this sample,
including any new fields written by this put operation.
Raises:
ValueError: If neither fields nor tag is provided
ValueError: If nested tensors are provided (use kv_batch_put instead)
RuntimeError: If retrieved BatchMeta size doesn't match length of `keys`
Example:
>>> import transfer_queue as tq
>>> import torch
>>> tq.init()
>>> # Put with both fields and tag
>>> meta = await tq.async_kv_put(
... key="sample_1",
... partition_id="train",
... fields={"input_ids": torch.tensor([1, 2, 3])},
... tag={"score": 0.95}
... )
>>> print(meta.fields) # ['input_ids']
"""
if fields is None and tag is None:
raise ValueError("Please provide at least one parameter of fields or tag.")
tq_client = _maybe_create_transferqueue_client()
# 1. translate user-specified key to BatchMeta
batch_meta = await tq_client.async_kv_retrieve_meta(keys=[key], partition_id=partition_id, create=True)
if batch_meta.size != 1:
raise RuntimeError(f"Retrieved BatchMeta size {batch_meta.size} does not match with input `key` size of 1!")
# 2. register the user-specified tag to BatchMeta
if tag is not None:
batch_meta.update_custom_meta([tag])
# 3. put data
if fields is not None:
if isinstance(fields, dict):
# TODO: consider whether to support this...
batch = {}
for field_name, value in fields.items():
if isinstance(value, torch.Tensor):
if value.is_nested:
raise ValueError("Please use (async)kv_batch_put for batch operation")
batch[field_name] = value.unsqueeze(0)
else:
batch[field_name] = NonTensorStack(value)
fields = TensorDict(batch, batch_size=[1])
elif not isinstance(fields, TensorDict):
raise ValueError("`fields` can only be dict or TensorDict")
# After put, batch_meta.field_names will include the new fields written by user
batch_meta = await tq_client.async_put(fields, batch_meta, data_parser=data_parser)
else:
# Directly update custom_meta (tag) to controller
await tq_client.async_set_custom_meta(batch_meta)
fields_to_return = batch_meta.field_names
return KVBatchMeta(
keys=[key],
tags=batch_meta.custom_meta,
partition_id=partition_id,
fields=fields_to_return,
extra_info=batch_meta.extra_info,
)
async def async_kv_batch_put(
keys: list[str],
partition_id: str,
fields: Optional[TensorDict] = None,
tags: Optional[list[dict[str, Any]]] = None,
data_parser: Optional[Callable[[Any], Any]] = None,
) -> KVBatchMeta:
"""Asynchronously put multiple key-value pairs to TransferQueue in batch.
This method stores multiple key-value pairs in a single operation, which is more
efficient than calling kv_put multiple times.
Args:
keys: List of user-specified keys for the data
partition_id: Logical partition to store the data in
fields: TensorDict containing data for all keys. Must have batch_size == len(keys).
If not provided, will only update the newly given tags to the keys.
tags: List of metadata tags, one for each key
data_parser: Optional callable to parse reference data (e.g., URLs) into real
content. The input is a slice of the `fields` parameter passed to
kv_put / kv_batch_put, in plain dict format (not TensorDict),
mapping field_name -> batched values. For a regular tensor column
the value is a batched tensor; for nested tensors (jagged or
strided) and NonTensorStack columns the values are extracted into
a list. It must modify values in-place based on the original keys;
do not add or remove keys. The number of elements per column must
also remain unchanged. Do not change the inner order of values
within each column. Only supported by SimpleStorage.
Returns:
KVBatchMeta: Metadata containing the keys, tags, partition_id, and fields.
The `fields` attribute includes all fields stored for these samples,
including any new fields written by this put operation.
Raises:
ValueError: If neither `fields` nor `tags` is provided
ValueError: If length of `keys` doesn't match length of `tags` or the batch_size of `fields` TensorDict
RuntimeError: If retrieved BatchMeta size doesn't match length of `keys`
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> keys = ["sample_1", "sample_2", "sample_3"]
>>> fields = TensorDict({
... "input_ids": torch.randn(3, 10),
... "attention_mask": torch.ones(3, 10),
... }, batch_size=3)
>>> tags = [{"score": 0.9}, {"score": 0.85}, {"score": 0.95}]
>>> meta = await tq.async_kv_batch_put(keys=keys, partition_id="train", fields=fields, tags=tags)
>>> print(meta.fields) # ['input_ids', 'attention_mask']
"""
if fields is None and tags is None:
raise ValueError("Please provide at least one parameter of `fields` or `tags`.")
if fields is not None and fields.batch_size[0] != len(keys):
raise ValueError(
f"`keys` with length {len(keys)} does not match the `fields` TensorDict with "
f"batch_size {fields.batch_size[0]}"
)
tq_client = _maybe_create_transferqueue_client()
# 1. translate user-specified key to BatchMeta
batch_meta = await tq_client.async_kv_retrieve_meta(keys=keys, partition_id=partition_id, create=True)
if batch_meta.size != len(keys):
raise RuntimeError(
f"Retrieved BatchMeta size {batch_meta.size} does not match with input `keys` size {len(keys)}!"
)
# 2. register the user-specified tags to BatchMeta
if tags is not None:
if len(tags) != len(keys):
raise ValueError(f"keys with length {len(keys)} does not match length of tags {len(tags)}")
batch_meta.update_custom_meta(tags)
# 3. put data
if fields is not None:
# After put, batch_meta.field_names will include the new fields written by user
batch_meta = await tq_client.async_put(fields, batch_meta, data_parser=data_parser)
else:
# Directly update custom_meta (tags) to controller
await tq_client.async_set_custom_meta(batch_meta)
fields_to_return = batch_meta.field_names
return KVBatchMeta(
keys=keys,
tags=batch_meta.custom_meta,
partition_id=partition_id,
fields=fields_to_return,
extra_info=batch_meta.extra_info,
)
async def async_kv_batch_get_by_meta(meta: KVBatchMeta, select_fields: Optional[list[str] | str] = None) -> TensorDict:
"""Asynchronously get data from TransferQueue using KVBatchMeta.
This is a convenience method for retrieving data using KVBatchMeta returned
from a previous put operation. It extracts the keys and partition_id from
the metadata to fetch the corresponding data.
Args:
meta: KVBatchMeta object returned from a previous put operation (e.g., async_kv_put,
async_kv_batch_put). It contains keys, partition_id, and fields information.
select_fields: Optional field(s) to retrieve, which overrides the fields
recorded in the given KVBatchMeta. If None, uses all fields
from meta.fields. Can be a single field name (str) or a list
of field names.
Returns:
TensorDict with the requested data
Raises:
ValueError: If keys or partition are not found
ValueError: If empty fields exist in any key (sample)
ValueError: If any field in select_fields doesn't exist in KVBatchMeta.fields
Example:
>>> import transfer_queue as tq
>>> tq.init()
>>> # First put some data
>>> keys = ["sample_1", "sample_2", "sample_3"]
>>> fields = TensorDict({
... "input_ids": torch.randn(3, 10),
... "attention_mask": torch.ones(3, 10),
... }, batch_size=3)
>>> meta = await tq.async_kv_batch_put(keys=keys, partition_id="train", fields=fields)
>>> # Then retrieve it using the returned metadata
>>> data = await tq.async_kv_batch_get_by_meta(meta)
"""
if meta.partition_id is None: