import pickle from safetensors import saveopen import datasets _DESCRIPTION = """\ Data sampled from an efficient-zero policy in the pong environment. The MCTS hidden state is included in the dataset. """ _HOMEPAGE = "https://github.com/opendilab/DI-engine" _LICENSE = "Apache-2.0" # The HuggingFace Datasets library doesn't host the datasets but only points to the original files. # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method) _BASE_URL = "https://huggingface.co/datasets/OpenDILabCommunity/Pong-v4-expert-MCTS/resolve/main" _URLS = { "Pong-v4-expert-MCTS": f"{_BASE_URL}/pong-v4-expert.safetensors", } class DecisionTransformerGymDataset(datasets.GeneratorBasedBuilder): VERSION = datasets.Version("0.0.1") # This is an example of a dataset with multiple configurations. # If you don't want/need to define several sub-sets in your dataset, # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes. # If you need to make complex sub-parts in the datasets with configurable options # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig # BUILDER_CONFIG_CLASS = MyBuilderConfig # You will be able to load one or the other configurations in the following list with # data = datasets.load_dataset('my_dataset', 'first_domain') # data = datasets.load_dataset('my_dataset', 'second_domain') BUILDER_CONFIGS = [ datasets.BuilderConfig( name="Pong-v4-expert-MCTS", version=VERSION, description="Data sampled from an efficient-zero policy in the pong environment", ) ] def _info(self): features = datasets.Features( { "observation": datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value("uint8")))), "action": datasets.Value("int64"), "hidden_state": datasets.Sequence(datasets.Sequence(datasets.Sequence(datasets.Value("float32")))), # These are the features of your dataset like images, labels ... } ) return datasets.DatasetInfo( # This is the description that will appear on the datasets page. description=_DESCRIPTION, # This defines the different columns of the dataset and their types # Here we define them above because they are different between the two configurations features=features, # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and # specify them. They'll be used if as_supervised=True in builder.as_dataset. # supervised_keys=("sentence", "label"), # Homepage of the dataset for documentation homepage=_HOMEPAGE, # License for the dataset if available license=_LICENSE, ) def _split_generators(self, dl_manager): urls = _URLS[self.config.name] data_dir = dl_manager.download_and_extract(urls) return [ datasets.SplitGenerator( name=datasets.Split.TRAIN, # These kwargs will be passed to _generate_examples gen_kwargs={ "filepath": data_dir, "split": "train", }, ) ] # method parameters are unpacked from `gen_kwargs` as given in `_split_generators` def _generate_examples(self, filepath, split): data = {} with safe_open(filepath, framework="pt", device="cpu") as f: for key in f.keys(): data[key] = f.get_tensor(key) for idx in range(len(data['obs'])): yield idx, { 'observation': data['obs'][idx], 'action': data['actions'][idx], 'hidden_state': data['hidden_state'][idx], }