add ace (MockupSampler)
Browse files
ace.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright 2020 The HuggingFace Datasets Authors.
|
2 |
+
# Copyright 2023 Cyril Zhang.
|
3 |
+
#
|
4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5 |
+
# you may not use this file except in compliance with the License.
|
6 |
+
# You may obtain a copy of the License at
|
7 |
+
#
|
8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9 |
+
#
|
10 |
+
# Unless required by applicable law or agreed to in writing, software
|
11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 |
+
# See the License for the specific language governing permissions and
|
14 |
+
# limitations under the License.
|
15 |
+
|
16 |
+
|
17 |
+
import csv
|
18 |
+
import json
|
19 |
+
import os
|
20 |
+
|
21 |
+
import datasets
|
22 |
+
import numpy as np
|
23 |
+
|
24 |
+
|
25 |
+
_CITATION = """\
|
26 |
+
"""
|
27 |
+
|
28 |
+
_DESCRIPTION = """\
|
29 |
+
Online dataset mockup.
|
30 |
+
"""
|
31 |
+
|
32 |
+
_HOMEPAGE = ""
|
33 |
+
|
34 |
+
_LICENSE = ""
|
35 |
+
|
36 |
+
_URLS = {}
|
37 |
+
|
38 |
+
class MockupDataset(datasets.GeneratorBasedBuilder):
|
39 |
+
"""TODO: Short description of my dataset."""
|
40 |
+
|
41 |
+
VERSION = datasets.Version("0.0.0")
|
42 |
+
BUILDER_CONFIGS = []
|
43 |
+
|
44 |
+
def __init__(self, name=None, data_config={}, **kwargs):
|
45 |
+
super().__init__(**kwargs)
|
46 |
+
|
47 |
+
"""
|
48 |
+
Set default configs
|
49 |
+
"""
|
50 |
+
if name is None:
|
51 |
+
name = 'parity'
|
52 |
+
if 'length' not in data_config:
|
53 |
+
data_config['length'] = 20
|
54 |
+
if 'size' not in data_config:
|
55 |
+
data_config['size'] = 100
|
56 |
+
|
57 |
+
self.data_config = data_config
|
58 |
+
# self.sampler = AutomatonSampler(name, data_config)
|
59 |
+
self.sampler = dataset_map[name](data_config)
|
60 |
+
|
61 |
+
def _info(self):
|
62 |
+
features = datasets.Features(
|
63 |
+
{
|
64 |
+
"x": datasets.Value("null"),
|
65 |
+
"y": datasets.Value("null")
|
66 |
+
}
|
67 |
+
)
|
68 |
+
|
69 |
+
return datasets.DatasetInfo(
|
70 |
+
description=_DESCRIPTION,
|
71 |
+
features=features,
|
72 |
+
homepage=_HOMEPAGE,
|
73 |
+
license=_LICENSE,
|
74 |
+
citation=_CITATION,
|
75 |
+
)
|
76 |
+
|
77 |
+
def _split_generators(self, dl_manager):
|
78 |
+
return [
|
79 |
+
datasets.SplitGenerator(
|
80 |
+
name=datasets.Split.TRAIN,
|
81 |
+
gen_kwargs={
|
82 |
+
"split": "train",
|
83 |
+
},
|
84 |
+
)
|
85 |
+
]
|
86 |
+
|
87 |
+
# method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
88 |
+
def _generate_examples(self, split):
|
89 |
+
for i in range(self.data_config['size']):
|
90 |
+
x, y = self.sampler.sample()
|
91 |
+
yield i, {
|
92 |
+
"x": x,
|
93 |
+
"y": y
|
94 |
+
}
|
95 |
+
|
96 |
+
|
97 |
+
class AutomatonSampler:
|
98 |
+
def __init__(self, data_config):
|
99 |
+
# self.name = name
|
100 |
+
self.data_config = data_config
|
101 |
+
|
102 |
+
if 'seed' in self.data_config:
|
103 |
+
self.np_rng = np.random.default_rng(self.data_config['seed'])
|
104 |
+
else:
|
105 |
+
self.np_rng = np.random.default_rng()
|
106 |
+
|
107 |
+
self.n_states = data_config['n_states']
|
108 |
+
self.T = self.data_config['length']
|
109 |
+
|
110 |
+
def f(self, x):
|
111 |
+
"""
|
112 |
+
Get output sequence given an input seq
|
113 |
+
"""
|
114 |
+
raise NotImplementedError()
|
115 |
+
|
116 |
+
def sample(self):
|
117 |
+
raise NotImplementedError()
|
118 |
+
|
119 |
+
|
120 |
+
class ParitySampler(AutomatonSampler):
|
121 |
+
def __init__(self, data_config):
|
122 |
+
super(ParitySampler, self).__init__(data_config)
|
123 |
+
self.name = 'parity'
|
124 |
+
self.data_config = data_config
|
125 |
+
|
126 |
+
def f(self, x):
|
127 |
+
return np.cumsum(x) % 2
|
128 |
+
|
129 |
+
def sample(self):
|
130 |
+
x = self.np_rng.binomial(1,0.5,size=self.T)
|
131 |
+
return x, self.f(x)
|
132 |
+
|
133 |
+
|
134 |
+
class FlipflopSampler(AutomatonSampler):
|
135 |
+
def __init__(self, data_config):
|
136 |
+
super(FlipflopSampler, self).__init__(data_config)
|
137 |
+
self.name = 'parity'
|
138 |
+
self.data_config = data_config
|
139 |
+
|
140 |
+
self.n_actions = self.n_states + 1
|
141 |
+
self.transition = np.array([list(range(self.n_actions))] + [[i+1]*self.n_actions for i in range(self.n_states)]).T
|
142 |
+
|
143 |
+
def f(self, x):
|
144 |
+
state, states = 0, []
|
145 |
+
for action in x:
|
146 |
+
state = self.transition[state, action]
|
147 |
+
states += state,
|
148 |
+
return np.array(states)
|
149 |
+
|
150 |
+
def sample(self):
|
151 |
+
rand = np.random.uniform(size=self.T)
|
152 |
+
nonzero_pos = (rand < 0.5).astype(np.int64)
|
153 |
+
writes = np.random.choice(range(1, self.n_states+1), size=self.T)
|
154 |
+
x = writes * nonzero_pos
|
155 |
+
return x, self.f(x)
|
156 |
+
|
157 |
+
|
158 |
+
dataset_map = {
|
159 |
+
'parity': ParitySampler,
|
160 |
+
'flipflop': FlipflopSampler,
|
161 |
+
# TODO: more datasets
|
162 |
+
}
|
163 |
+
|
164 |
+
|