Upload standard.py with huggingface_hub
Browse files12345678910111 unmodified line1314151617181920212223242526272829303132333 unmodified lines3738394041425 unmodified lines484950515253541 unmodified line56575859606162636465666768699 unmodified lines798081828384851 unmodified line8788899091929394959697985 unmodified lines10410510610710810912 unmodified lines1221231241251261271281291301314 unmodified lines1361371381391401411421431441 unmodified line1461471481491501518 unmodified lines160161162163164165from typing import Listfrom .card import TaskCardfrom .dataclass import InternalField, OptionalFieldfrom .formats import ICLFormatfrom .instructions import Instructionfrom .operator import SourceSequntialOperator, StreamingOperatorfrom .operators import StreamRefinerfrom .recipe import Recipefrom .renderers import StandardRendererfrom .schema import ToUnitxtGroup1 unmodified linefrom .templates import Templateclass BaseRecipe(Recipe, SourceSequntialOperator):card: TaskCardtemplate: Template = Noneinstruction: Instruction = Noneformat: ICLFormat = ICLFormat()max_train_instances: int = Nonemax_validation_instances: int = Nonemax_test_instances: int = Nonetrain_refiner: StreamRefiner = OptionalField(default_factory=lambda: StreamRefiner(apply_to_streams=["train"]))validation_refiner: StreamRefiner = OptionalField(default_factory=lambda: StreamRefiner(apply_to_streams=["validation"]))test_refiner: StreamRefiner = OptionalField(default_factory=lambda: StreamRefiner(apply_to_streams=["test"]))demos_pool_size: int = Nonenum_demos: int = 03 unmodified linesdemos_field: str = "demos"sampler: Sampler = Nonesteps: List[StreamingOperator] = InternalField(default_factory=list)def verify(self):5 unmodified lines)if self.demos_pool_size < self.num_demos:raise ValueError(f"demos_pool_size must be bigger than num_demos={self.num_demos}, Got demos_pool_size={self.demos_pool_size}")def prepare(self):1 unmodified lineself.card.loader,]if self.card.preprocess_steps is not None:self.steps.extend(self.card.preprocess_steps)self.steps.append(self.card.task)if self.demos_pool_size is not None:self.steps.append(SeparateSplit(from_split=self.demos_taken_from,to_split_names=[self.demos_pool_name, self.demos_taken_from],to_split_sizes=[int(self.demos_pool_size)],9 unmodified linessampler.set_size(self.num_demos)self.steps.append(SpreadSplit(source_stream=self.demos_pool_name,target_field=self.demos_field,sampler=sampler,1 unmodified line)self.train_refiner.max_instances = self.max_train_instancesself.steps.append(self.train_refiner)self.validation_refiner.max_instances = self.max_validation_instancesself.steps.append(self.validation_refiner)self.test_refiner.max_instances = self.max_test_instancesself.steps.append(self.test_refiner)render = StandardRenderer(5 unmodified linesself.steps.append(render)postprocessors = render.get_postprocessors()self.steps.append(12 unmodified linesdef prepare(self):assert (self.template_card_index is None or self.template is None), "Specify either template or template_card_index"if self.template_card_index is not None:self.template = self.card.templates[int(self.template_card_index)]assert (self.instruction_card_index is None or self.instruction is None), "Specify either instruction or instruction_card_index"4 unmodified linesclass StandardRecipe(StandardRecipeWithIndexes):"""This class represents a standard recipe for data processing and preperation.This class can be used to prepare a recipewith all necessary steps, refiners and renderers included. It allows to set variousparameters and steps in a sequential manner for preparing the recipe.1 unmodified linecard (TaskCard): TaskCard object associated with the recipe.template (Template, optional): Template object to be used for the recipe.instruction (Instruction, optional): Instruction object to be used for the recipe.format (ICLFormat, optional): ICLFormat object to be used for the recipe.train_refiner (StreamRefiner, optional): Train refiner to be used in the recipe.max_train_instances (int, optional): Maximum training instances for the refiner.8 unmodified linesdemos_field (str, optional): Field name for demos. Default is "demos".sampler (Sampler, optional): Sampler object to be used in the recipe.steps (List[StreamingOperator], optional): List of StreamingOperator objects to be used in the recipe.instruction_card_index (int, optional): Index of instruction card to be usedfor preparing the recipe.template_card_index (int, optional): Index of template card to be used for1234567891011121 unmodified line1415161718192021222324252627282930313233343536373839404142433 unmodified lines47484950515253545 unmodified lines606162636465666768697071727374757677787980818283848586878889901 unmodified line92939495969798991001011021031041051061071081091101111121131149 unmodified lines1241251261271281291301 unmodified line1321331341351361371381391401411421431441451465 unmodified lines15215315415515615715815916012 unmodified lines1731741751761771781791801811821831841851861871881891901911921934 unmodified lines1981992002012022032042052061 unmodified line2082092102112122132148 unmodified lines223224225226227228229import loggingfrom typing import Listfrom .card import TaskCardfrom .dataclass import InternalField, OptionalFieldfrom .formats import ICLFormatfrom .instructions import Instructionfrom .operator import SourceSequentialOperator, StreamingOperatorfrom .operators import Augmentor, NullAugmentor, StreamRefinerfrom .recipe import Recipefrom .renderers import StandardRendererfrom .schema import ToUnitxtGroup1 unmodified linefrom .templates import Template# Used to give meaningful name to recipe stepsclass CreateDemosPool(SeparateSplit):passclass AddDemosField(SpreadSplit):passclass BaseRecipe(Recipe, SourceSequentialOperator):card: TaskCardtemplate: Template = Noneinstruction: Instruction = Noneformat: ICLFormat = ICLFormat()loader_limit: int = Nonemax_train_instances: int = Nonemax_validation_instances: int = Nonemax_test_instances: int = Nonetrain_refiner: StreamRefiner = OptionalField(default_factory=StreamRefiner)validation_refiner: StreamRefiner = OptionalField(default_factory=StreamRefiner)test_refiner: StreamRefiner = OptionalField(default_factory=StreamRefiner)demos_pool_size: int = Nonenum_demos: int = 03 unmodified linesdemos_field: str = "demos"sampler: Sampler = Noneaugmentor: Augmentor = OptionalField(default_factory=NullAugmentor)steps: List[StreamingOperator] = InternalField(default_factory=list)def verify(self):5 unmodified lines)if self.demos_pool_size < self.num_demos:raise ValueError(f"demos_pool_size must be bigger than num_demos ({self.num_demos}), Got demos_pool_size={self.demos_pool_size}")if self.loader_limit and self.demos_pool_size > self.loader_limit:raise ValueError(f"demos_pool_size must be bigger than loader_limit ({self.loader_limit}), Got demos_pool_size={self.demos_pool_size}")if self.loader_limit:if self.max_test_instances and self.max_test_instances > self.loader_limit:raise ValueError(f"max_test_instances must be bigger than loader_limit ({self.loader_limit}), Got max_test_instances={self.max_test_instances}")if (self.max_validation_instancesand self.max_validation_instances > self.loader_limit):raise ValueError(f"max_validation_instances must be bigger than loader_limit ({self.loader_limit}), Got max_validation_instances={self.max_validation_instances}")if (self.max_train_instancesand self.max_train_instances > self.loader_limit):raise ValueError(f"max_train_instances must be bigger than loader_limit ({self.loader_limit}), Got max_train_instances={self.max_train_instances}")def prepare(self):1 unmodified lineself.card.loader,]if self.loader_limit:self.card.loader.loader_limit = self.loader_limitlogging.info(f"Loader line limit was set to {self.loader_limit}")self.steps.append(StreamRefiner(max_instances=self.loader_limit))if self.card.preprocess_steps is not None:self.steps.extend(self.card.preprocess_steps)self.steps.append(self.card.task)if self.augmentor.augment_task_input:self.augmentor.set_task_input_fields(self.card.task.augmentable_inputs)self.steps.append(self.augmentor)if self.demos_pool_size is not None:self.steps.append(CreateDemosPool(from_split=self.demos_taken_from,to_split_names=[self.demos_pool_name, self.demos_taken_from],to_split_sizes=[int(self.demos_pool_size)],9 unmodified linessampler.set_size(self.num_demos)self.steps.append(AddDemosField(source_stream=self.demos_pool_name,target_field=self.demos_field,sampler=sampler,1 unmodified line)self.train_refiner.max_instances = self.max_train_instancesself.train_refiner.apply_to_streams = ["train"]self.steps.append(self.train_refiner)self.validation_refiner.max_instances = self.max_validation_instancesself.validation_refiner.apply_to_streams = ["validation"]self.steps.append(self.validation_refiner)self.test_refiner.max_instances = self.max_test_instancesself.test_refiner.apply_to_streams = ["test"]self.steps.append(self.test_refiner)render = StandardRenderer(5 unmodified linesself.steps.append(render)if self.augmentor.augment_model_input:self.steps.append(self.augmentor)postprocessors = render.get_postprocessors()self.steps.append(12 unmodified linesdef prepare(self):assert (self.template_card_index is None or self.template is None), f"Specify either template ({self.template}) or template_card_index ({self.template_card_index}) but not both"assert not (self.template_card_index is None and self.template is None), "Specify either template or template_card_index in card"if self.template_card_index is not None:try:self.template = self.card.templates[self.template_card_index]except Exception as e:if isinstance(self.card.templates, dict):options = self.card.templates.keys()else:options = list(range(0, len(self.card.templates)))raise ValueError(f"card_template_index '{self.template_card_index}' is not in card. Available options: {options}") from eassert (self.instruction_card_index is None or self.instruction is None), "Specify either instruction or instruction_card_index"4 unmodified linesclass StandardRecipe(StandardRecipeWithIndexes):"""This class represents a standard recipe for data processing and preperation.This class can be used to prepare a recipe.with all necessary steps, refiners and renderers included. It allows to set variousparameters and steps in a sequential manner for preparing the recipe.1 unmodified linecard (TaskCard): TaskCard object associated with the recipe.template (Template, optional): Template object to be used for the recipe.instruction (Instruction, optional): Instruction object to be used for the recipe.loader_limit (int, optional): Specifies the maximum number of instances per stream to be returned from the loader (used to reduce loading time in large datasets)format (ICLFormat, optional): ICLFormat object to be used for the recipe.train_refiner (StreamRefiner, optional): Train refiner to be used in the recipe.max_train_instances (int, optional): Maximum training instances for the refiner.8 unmodified linesdemos_field (str, optional): Field name for demos. Default is "demos".sampler (Sampler, optional): Sampler object to be used in the recipe.steps (List[StreamingOperator], optional): List of StreamingOperator objects to be used in the recipe.augmentor (Augmentor) : Augmentor to be used to pseudo randomly augment the source textinstruction_card_index (int, optional): Index of instruction card to be usedfor preparing the recipe.template_card_index (int, optional): Index of template card to be used for
standard.py+81-17