-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathdataset.py
More file actions
528 lines (457 loc) · 17.1 KB
/
dataset.py
File metadata and controls
528 lines (457 loc) · 17.1 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
# pylint: disable=no-name-in-module
from __future__ import annotations
import asyncio
import contextlib
import dataclasses
import tempfile
from collections.abc import AsyncIterator, Iterator
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable, TypeVar, cast
import aiofiles
import httpx
from typing_extensions import Self
from yandex.cloud.ai.dataset.v1.dataset_pb2 import DatasetInfo as ProtoDatasetInfo
from yandex.cloud.ai.dataset.v1.dataset_pb2 import ValidationError as ProtoValidationError
from yandex.cloud.ai.dataset.v1.dataset_service_pb2 import (
DeleteDatasetRequest, DeleteDatasetResponse, FinishMultipartUploadDraftRequest, FinishMultipartUploadDraftResponse,
GetDownloadUrlsRequest, GetDownloadUrlsResponse, GetUploadDraftUrlRequest, GetUploadDraftUrlResponse,
StartMultipartUploadDraftRequest, StartMultipartUploadDraftResponse, UpdateDatasetRequest, UpdateDatasetResponse,
UploadedPartInfo
)
from yandex.cloud.ai.dataset.v1.dataset_service_pb2_grpc import DatasetServiceStub
from yandex_cloud_ml_sdk._logging import get_logger
from yandex_cloud_ml_sdk._types.misc import UNDEFINED, PathLike, UndefinedOr, coerce_path, get_defined_value
from yandex_cloud_ml_sdk._types.resource import BaseDeleteableResource, safe_on_delete
from yandex_cloud_ml_sdk._utils.packages import requires_package
from yandex_cloud_ml_sdk._utils.pyarrow import read_dataset_records
from yandex_cloud_ml_sdk._utils.sync import run_sync, run_sync_generator
from .status import DatasetStatus
if TYPE_CHECKING:
from yandex_cloud_ml_sdk._sdk import BaseSDK
logger = get_logger(__name__)
DEFAULT_CHUNK_SIZE = 100 * 1024 ** 2
@dataclasses.dataclass(frozen=True)
class ValidationErrorInfo:
error: str
description: str
rows: tuple[int, ...]
@classmethod
def _from_proto(cls, proto: ProtoValidationError) -> ValidationErrorInfo:
return cls(
error=proto.error,
description=proto.error_description,
rows=tuple(proto.row_numbers)
)
@dataclasses.dataclass(frozen=True)
class DatasetInfo:
folder_id: str
name: str | None
description: str | None
metadata: str | None
created_by: str
created_at: datetime
updated_at: datetime
labels: dict[str, str] | None
allow_data_logging: bool
status: DatasetStatus
task_type: str
rows: int
size_bytes: int
validation_errors: tuple[ValidationErrorInfo, ...]
@dataclasses.dataclass(frozen=True)
class BaseDataset(DatasetInfo, BaseDeleteableResource):
@classmethod
def _kwargs_from_message(cls, proto: ProtoDatasetInfo, sdk: BaseSDK) -> dict[str, Any]: # type: ignore[override]
kwargs = super()._kwargs_from_message(proto, sdk=sdk)
kwargs['id'] = proto.dataset_id
kwargs['created_by'] = proto.created_by_id
kwargs['status'] = DatasetStatus._from_proto(proto.status)
kwargs['validation_errors'] = tuple(
ValidationErrorInfo._from_proto(p) for p in proto.validation_error
)
kwargs['allow_data_logging'] = proto.allow_data_log
return kwargs
@safe_on_delete
async def _update(
self,
*,
name: UndefinedOr[str] = UNDEFINED,
description: UndefinedOr[str] = UNDEFINED,
labels: UndefinedOr[dict[str, str]] = UNDEFINED,
timeout: float = 60,
) -> Self:
logger.debug("Updating dataset %s", self.id)
request = UpdateDatasetRequest(
dataset_id=self.id,
name=get_defined_value(name, ''),
description=get_defined_value(description, ''),
labels=get_defined_value(labels, {}),
)
self._fill_update_mask(
request.update_mask,
{
'name': name,
'description': description,
'labels': labels,
}
)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
response = await self._client.call_service(
stub.Update,
request,
timeout=timeout,
expected_type=UpdateDatasetResponse,
)
self._update_from_proto(response.dataset)
logger.info("Dataset %s successfully updated", self.id)
return self
@safe_on_delete
async def _delete(
self,
*,
timeout: float = 60,
) -> None:
logger.debug("Deleting dataset %s", self.id)
request = DeleteDatasetRequest(dataset_id=self.id)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
await self._client.call_service(
stub.Delete,
request,
timeout=timeout,
expected_type=DeleteDatasetResponse,
)
object.__setattr__(self, '_deleted', True)
logger.info("Dataset %s successfully deleted", self.id)
@safe_on_delete
async def _download(
self,
*,
download_path: PathLike,
timeout: float = 60,
exist_ok: bool = False,
) -> tuple[Path, ...]:
logger.debug("Downloading dataset %s", self.id)
base_path = coerce_path(download_path)
if not base_path.exists():
raise ValueError(f"{base_path} does not exist")
if not base_path.is_dir():
raise ValueError(f"{base_path} is not a directory")
return await asyncio.wait_for(self.__download_impl(
base_path=base_path,
exist_ok=exist_ok,
timeout=timeout,
), timeout)
async def _read(
self,
*,
timeout: float,
batch_size: UndefinedOr[int],
) -> AsyncIterator[dict[Any, Any]]:
batch_size_ = get_defined_value(batch_size, None)
urls = await self._get_download_urls(timeout=timeout)
async with self._client.httpx() as client:
for _, url in urls:
_, filename = tempfile.mkstemp()
path = Path(filename)
try:
await self.__download_file(
path=path,
url=url,
client=client,
timeout=timeout
)
async for record in read_dataset_records(filename, batch_size=batch_size_):
yield record
finally:
path.unlink(missing_ok=True)
async def __download_impl(
self,
base_path: Path,
exist_ok: bool,
timeout: float,
) -> tuple[Path, ...]:
urls = await self._get_download_urls(timeout=timeout)
async with self._client.httpx() as client:
coroutines = []
for key, url in urls:
file_path = base_path / key
if file_path.exists() and not exist_ok:
raise ValueError(f"{file_path} already exists")
coroutines.append(
self.__download_file(file_path, url, client, timeout=timeout),
)
await asyncio.gather(*coroutines)
return tuple(base_path / key for key, _ in urls)
async def __download_file(
self,
path: Path | str,
url: str,
client: httpx.AsyncClient,
timeout: float,
) -> None:
async with aiofiles.open(path, "wb") as file:
logger.debug(
'Going to download file for dataset %s from url %s to %s',
self.id, url, file.name
)
async for chunk in self.__read_from_url(url, client, timeout=timeout):
await file.write(chunk)
async def __read_from_url(
self,
url: str,
client: httpx.AsyncClient,
timeout: float,
chunk_size: int = 1024 * 1024 * 8, # 8Mb
) -> AsyncIterator[bytes]:
resp = await client.get(url, timeout=timeout)
resp.raise_for_status()
async for chunk in resp.aiter_bytes(chunk_size=chunk_size):
yield chunk
async def _list_upload_formats(
self,
*,
timeout: float = 60,
) -> tuple[str, ...]:
# pylint: disable=protected-access
return await self._sdk.datasets._list_upload_formats(
task_type=self.task_type,
timeout=timeout
)
async def _get_upload_url(
self,
*,
size: int,
timeout: float = 60,
) -> str:
logger.debug("Fetching upload url for dataset %s", self.id)
request = GetUploadDraftUrlRequest(
dataset_id=self.id,
size_bytes=size
)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
result = await self._client.call_service(
stub.GetUploadDraftUrl,
request,
timeout=timeout,
expected_type=GetUploadDraftUrlResponse,
)
logger.info("Dataset %s upload url successfully fetched", self.id)
return result.upload_url
async def _get_download_urls(
self,
*,
timeout: float = 60,
) -> Iterable[tuple[str, str]]:
logger.debug("Fetching download urls for dataset %s", self.id)
request = GetDownloadUrlsRequest(
dataset_id=self.id,
)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
result = await self._client.call_service(
stub.GetDownloadUrls,
request,
timeout=timeout,
expected_type=GetDownloadUrlsResponse,
)
logger.debug("Dataset %s returned next download urls: %r", self.id, result.download_urls)
return [
(r.key, r.url) for r in result.download_urls
]
async def _start_multipart_upload(
self,
*,
size_bytes: int,
parts: int,
timeout: float,
) -> tuple[str, ...]:
logger.debug(
"Starting multipart upload for dataset %s with size of %d bytes and %d parts",
self.id, size_bytes, parts,
)
request = StartMultipartUploadDraftRequest(
dataset_id=self.id,
size_bytes=size_bytes,
parts=parts,
)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
result = await self._client.call_service(
stub.StartMultipartUploadDraft,
request,
timeout=timeout,
expected_type=StartMultipartUploadDraftResponse,
)
logger.info(
"Multipart upload for dataset %s started and returned %d upload urls",
self.id, len(result.multipart_upload_urls)
)
return tuple(result.multipart_upload_urls)
async def _finish_multipart_upload(
self,
parts: Iterable[tuple[int, str]],
timeout: float,
) -> None:
parts_proto = [
UploadedPartInfo(
part_num=part_num,
etag=etag
) for part_num, etag in parts
]
logger.debug("Finishing multipart upload of dataset %s with %d parts", self.id, len(parts_proto))
request = FinishMultipartUploadDraftRequest(
dataset_id=self.id,
uploaded_parts=parts_proto
)
async with self._client.get_service_stub(DatasetServiceStub, timeout=timeout) as stub:
await self._client.call_service(
stub.FinishMultipartUploadDraft,
request,
timeout=timeout,
expected_type=FinishMultipartUploadDraftResponse,
)
logger.debug("Multipart upload of dataset %s with %d parts finished", self.id, len(parts_proto))
class AsyncDataset(BaseDataset):
async def update(
self,
*,
name: UndefinedOr[str] = UNDEFINED,
description: UndefinedOr[str] = UNDEFINED,
labels: UndefinedOr[dict[str, str]] = UNDEFINED,
timeout: float = 60,
) -> Self:
return await self._update(
name=name,
description=description,
labels=labels,
timeout=timeout
)
async def delete(
self,
*,
timeout: float = 60,
) -> None:
await self._delete(timeout=timeout)
async def list_upload_formats(
self,
*,
timeout: float = 60,
) -> tuple[str, ...]:
return await self._list_upload_formats(timeout=timeout)
async def download(
self,
*,
download_path: PathLike,
timeout: float = 60,
exist_ok: bool = False,
) -> tuple[Path, ...]:
return await self._download(
download_path=download_path,
timeout=timeout,
exist_ok=exist_ok,
)
@requires_package('pyarrow', '>=19', 'AsyncDataset.read')
async def read(
self,
*,
timeout: float = 60,
batch_size: UndefinedOr[int] = UNDEFINED,
) -> AsyncIterator[dict[Any, Any]]:
"""Reads the dataset from backend and yields it records one by one.
This method lazily loads records by chunks, minimizing memory usage for large datasets.
The iterator yields dictionaries where keys are field names and values are parsed data.
.. note::
This method creates temporary files in the system's default temporary directory
during operation. To control the location of temporary files, refer to Python's
:func:`tempfile.gettempdir` documentation. Temporary files are automatically
cleaned up after use.
:param timeout: Maximum time in seconds for both gRPC and HTTP operations.
Includes connection establishment, data transfer, and processing time.
Defaults to 60 seconds.
:type timeout: float
:param batch_size: Number of records to load to memory in one chunk.
When UNDEFINED (default), uses backend's optimal chunk size (typically
corresponds to distinct Parquet files storage layout).
:type batch_size: int or Undefined
:yields: Dictionary representing single record with field-value pairs
:rtype: AsyncIterator[dict[Any, Any]]
"""
async for record in self._read(
timeout=timeout,
batch_size=batch_size
):
yield record
class Dataset(BaseDataset):
__update = run_sync(BaseDataset._update)
__delete = run_sync(BaseDataset._delete)
__list_upload_formats = run_sync(BaseDataset._list_upload_formats)
__download = run_sync(BaseDataset._download)
__read = run_sync_generator(BaseDataset._read)
def update(
self,
*,
name: UndefinedOr[str] = UNDEFINED,
description: UndefinedOr[str] = UNDEFINED,
labels: UndefinedOr[dict[str, str]] = UNDEFINED,
timeout: float = 60,
) -> Self:
return self.__update(
name=name,
description=description,
labels=labels,
timeout=timeout
)
def delete(
self,
*,
timeout: float = 60,
) -> None:
self.__delete(timeout=timeout)
def list_upload_formats(
self,
*,
timeout: float = 60,
) -> tuple[str, ...]:
return self.__list_upload_formats(timeout=timeout)
def download(
self,
*,
download_path: PathLike,
timeout: float = 60,
exist_ok: bool = False,
) -> tuple[Path, ...]:
return self.__download(
download_path=download_path,
timeout=timeout,
exist_ok=exist_ok,
)
@requires_package('pyarrow', '>=19', 'Dataset.read')
def read(
self,
*,
timeout: float = 60,
batch_size: UndefinedOr[int] = UNDEFINED,
) -> Iterator[dict[Any, Any]]:
"""Reads the dataset from backend and yields it records one by one.
This method lazily loads records by chunks, minimizing memory usage for large datasets.
The iterator yields dictionaries where keys are field names and values are parsed data.
.. note::
This method creates temporary files in the system's default temporary directory
during operation. To control the location of temporary files, refer to Python's
:func:`tempfile.gettempdir` documentation. Temporary files are automatically
cleaned up after use.
:param timeout: Maximum time in seconds for both gRPC and HTTP operations.
Includes connection establishment, data transfer, and processing time.
Defaults to 60 seconds.
:type timeout: float
:param batch_size: Number of records to load to memory in one chunk.
When UNDEFINED (default), uses backend's optimal chunk size (typically
corresponds to distinct Parquet files storage layout).
:type batch_size: int or Undefined
:yields: Dictionary representing single record with field-value pairs
:rtype Iterator[dict[Any, Any]]
"""
yield from self.__read(
timeout=timeout,
batch_size=batch_size
)
DatasetTypeT = TypeVar('DatasetTypeT', bound=BaseDataset)