Skip to content

unitorch.cli.datasets.hf¤

ASTDatasets¤

Tip

core/dataset/ast is the section for configuration of ASTDatasets.

Get the dataset for the specified split.

Parameters:

Name Type Description Default
split str

The split to get the dataset for.

required

Returns:

Name Type Description
dataset

The dataset for the specified split.

Source code in src/unitorch/cli/datasets/hf.py
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
def __getdataset__(self, split):
    """
    Get the dataset for the specified split.

    Args:
        split (str): The split to get the dataset for.

    Returns:
        dataset: The dataset for the specified split.
    """
    config = self.config

    registered_process_mapping = {
        k.replace("/", "_"): k for k, v in registered_process.items()
    }

    config.set_default_section(f"core/dataset/ast")
    _iterable = config.getoption("iterable", False)
    _template = config.getoption("template", "csv")
    _data_name = config.getoption("data_name", None)
    _config_name = config.getoption("config_name", None)
    _data_dir = config.getoption("data_dir", None)
    _data_files = config.getoption("data_files", None)
    _names = config.getoption("names", None)
    _features = config.getoption("features", None)
    _sep = config.getoption("sep", "\t")
    _quoting = config.getoption("quoting", 3)
    _escapechar = config.getoption("escapechar", None)
    _field = config.getoption("field", None)
    _process_functions = config.getoption("preprocess_functions", None)
    _enable_ddp_partition = config.getoption("enable_ddp_partition", True)

    _HFDatasets = HFIterableDatasets if _iterable else HFDatasets
    _ASTDatasets = ASTHFIterableDatasets if _iterable else ASTHFDatasets

    config.set_default_section(f"core/dataset/ast/{split}")

    template = config.getoption("template", _template)
    if config.getoption("data_name", _data_name) is not None:
        template = "hub"

    assert template in self.templates

    new_split = "validation" if split == "dev" else split
    new_split = config.getoption("split", new_split)

    # get dataset
    dataset = None
    if template == "csv":
        data_dir = config.getoption("data_dir", _data_dir)
        data_files = config.getoption("data_files", _data_files)
        names = config.getoption("names", _names)
        sep = config.getoption("sep", _sep)
        quoting = config.getoption("quoting", _quoting)
        escapechar = config.getoption("escapechar", _escapechar)
        dataset = _HFDatasets.from_csv(
            data_dir=data_dir,
            data_files=data_files,
            names=names,
            sep=sep,
            quoting=quoting,
            escapechar=escapechar,
            split=new_split,
        )

    if template == "json":
        data_dir = config.getoption("data_dir", _data_dir)
        data_files = config.getoption("data_files", _data_files)
        field = config.getoption("field", _field)

        dataset = _HFDatasets.from_json(
            data_dir=data_dir,
            data_files=data_files,
            field=field,
            split=new_split,
        )

    if template == "parquet":
        data_dir = config.getoption("data_dir", _data_dir)
        data_files = config.getoption("data_files", _data_files)
        features = config.getoption("features", _features)
        if isinstance(features, str):
            features = eval(features)
        dataset = _HFDatasets.from_parquet(
            data_dir=data_dir,
            data_files=data_files,
            split=new_split,
            features=features,
        )

    if template == "hub":
        data_name = config.getoption("data_name", _data_name)
        config_name = config.getoption("config_name", _config_name)
        data_dir = config.getoption("data_dir", _data_dir)
        data_files = config.getoption("data_files", _data_files)
        data_name = (
            cached_path(data_name) if data_name.endswith(".py") else data_name
        )
        dataset = _HFDatasets.from_hub(
            data_name=data_name,
            config_name=config_name,
            data_dir=data_dir,
            data_files=data_files,
            split=new_split,
        )

    assert dataset is not None

    # get process functions
    process_functions = config.getoption("preprocess_functions", _process_functions)
    if process_functions is None:
        process_functions = []
    else:
        process_functions = [ASTFunction(func) for func in process_functions]

    for pfunc in process_functions:
        for name in pfunc.__ast_process__:
            globals()[name] = init_registered_process(
                registered_process_mapping[name],
                config,
            )

    enable_ddp_partition = config.getoption(
        "enable_ddp_partition", _enable_ddp_partition
    )

    if isinstance(_ASTDatasets, HFIterableDatasets):
        self.__ASTDatasets__[split] = _ASTDatasets(
            dataset=dataset.dataset,
            process_functions=process_functions,
            enable_ddp_partition=enable_ddp_partition,
        )
    else:
        self.__ASTDatasets__[split] = _ASTDatasets(
            dataset=dataset.dataset,
            process_functions=process_functions,
        )

    return self.__ASTDatasets__.get(split)

Tip

core/dataset/ast/train is the section for configuration of ASTDatasets Training Data.

Tip

core/dataset/ast/dev is the section for configuration of ASTDatasets Validation Data.

Tip

core/dataset/ast/test is the section for configuration of ASTDatasets Test Data.