def _run(self): frame = TFFramework() if self.args['list']: print 'Supported Exemplars:' for primop in frame.ExemplarRegistry: print ' ',primop return 0 elif self.args['primop'] is None: self.subparser.error('A primop must be specified unless using --list') primop_t = self.args['primop'] uas = UniformArgSampler() primop_args = uas.sample(primop_t, n=self.args['n'], seed=self.args['seed']) Exemplar = frame.ExemplarRegistry.lookup(primop_t) self.features = Features() for p_args in primop_args: exemplar = Exemplar(p_args) synthmodel = frame.SyntheticModel(exemplar) frame.set_model(synthmodel) profile = frame.get_timing(mode='inference', ops='native') graph = frame.get_graph(mode='inference', scope='dynamic', ops='native') id = graph.get_vertex_id_from_tf_name( exemplar.get_op_name() ) self.features.append( p_args, profile[id][0] ) if self.args['output'] is None: self.args['output'] = str(primop_t)+'.features' self.features.write(self.args['output'])
def test_argument_length_mismatch(self): f = Features() f.append([1,2,3], 0) with self.assertRaises(AssertionError): f.append([1,2,3,4], 0) with self.assertRaises(AssertionError): f.append([1,2], 0) f.append([4,5,6], 0)
class SweepTool(AbstractTool): TOOL_NAME='sweep' TOOL_SUMMARY='Generating timing measurements suitable for training a performance estimator.' CACHE_FORMAT=[] # FIXME: Features are not cached. def add_subparser(self, argparser): super(SweepTool, self).add_subparser(argparser) self.subparser.add_argument('primop', nargs='?', type=str, help='Which primitive operation to sweep.') self.subparser.add_argument('-n', type=int, default=1, help='Number of data points to collect.') self.subparser.add_argument('-o', '--output', type=str, default=None, help='Output file with Estimator training features (default: <primop>.features).') self.subparser.add_argument('--seed', type=int, default=None, help='Set the PRNG seed for this sweep.') self.subparser.add_argument('-l','--list', default=False, action='store_true', help="Don't actually run a sweep, just print which primops are supported,") # FIXME: Add argument to limit scope of sweep return self.subparser def _run(self): frame = TFFramework() if self.args['list']: print 'Supported Exemplars:' for primop in frame.ExemplarRegistry: print ' ',primop return 0 elif self.args['primop'] is None: self.subparser.error('A primop must be specified unless using --list') primop_t = self.args['primop'] uas = UniformArgSampler() primop_args = uas.sample(primop_t, n=self.args['n'], seed=self.args['seed']) Exemplar = frame.ExemplarRegistry.lookup(primop_t) self.features = Features() for p_args in primop_args: exemplar = Exemplar(p_args) synthmodel = frame.SyntheticModel(exemplar) frame.set_model(synthmodel) profile = frame.get_timing(mode='inference', ops='native') graph = frame.get_graph(mode='inference', scope='dynamic', ops='native') id = graph.get_vertex_id_from_tf_name( exemplar.get_op_name() ) self.features.append( p_args, profile[id][0] ) if self.args['output'] is None: self.args['output'] = str(primop_t)+'.features' self.features.write(self.args['output']) # FIXME: Features objects cannot be cached in the normal way. #def _load(self, filename): # feats = Features() # feats.read(filename) # return feats #def _save(self, data, filename): # pylint: disable=W0221 # data.write(filename) def _output(self): print 'Output' for args,time in zip(self.features.op_arguments, self.features.measurements): print args,':',time
def test_accessors(self): f = Features() f.append([1,2,3], 10) f.append([4,5,6], 11) op_args = f.op_arguments meas = f.measurements assert op_args.shape==(2,3) assert meas.shape==(2,) assert meas[0]==10 and meas[1]==11
def test_reproducible_parameters(self): N = 1000 np.random.seed(13) noise = 20 * np.random.randn(N) line_x = np.random.randint(0, 1000, (N, 1)) m_true, b_true = 3, 20 line_y = m_true * line_x[:, 0] + b_true + noise v_true = m_true * 100 + b_true feats = Features().extend(line_x, line_y) est = OLSEstimator() est.fit(feats) v = est.estimate([100]) assert np.abs(v - v_true) < 10., 'Incorrect estimation' params = est.get_params() assert np.abs(m_true - params[0]) < 1., 'Incorrect slope fit' assert np.abs(b_true - params[1]) < 10., 'Incorrect intercept fit' v = est.estimate([100]) assert np.abs(v - v_true) < 10., 'Incorrect estimation' est2 = OLSEstimator() est2.set_params(params) v = est2.estimate([100]) assert np.abs(v - v_true) < 10., 'Incorrect estimation'
def _run(self): if self.args['output'] is None: self.args['output'] = str(self.args['primop']) + '-' + str( self.args['estimator']) + '.est' EstimatorClass = EstimatorRegistry.lookup(self.args['estimator']) est = EstimatorClass() f = Features() for feats_file in self.args['features']: f2 = Features() f2.read(feats_file) f.concatenate(f2) est.fit(f) self.data = est.get_params() self._save(self.data, self.args['output'])
def test_io(self): with in_temporary_directory() as d: filename = os.path.join(os.path.abspath(d), 'zero-linear.est') est = OLSEstimator() feats = Features().append([0, 1, 2, 3], 0) est.fit(feats) est.write(filename) # FIXME: flush? est2 = OLSEstimator() est2.read(filename) assert len(est.get_params()) == len(est2.get_params()) for i, (lhs, rhs) in enumerate(zip(est.get_params(), est2.get_params())): assert lhs == rhs, 'Parameter ' + str( i) + ' does not match: "' + str(lhs) + '" vs. "' + str( rhs) + '"'
def test_blind_predict(self): est = OLSEstimator() feats = Features().append([0], 0) est.fit(feats) v = est.estimate([0]) assert (v + 1), 'Estimate returned non-numeric value: ' + str(v)
def test_instantiate(self): _ = Features()
def test_io(self): f = Features() f.append([1,2,3], 10) f.append([2,3,4], 11) f.append([3,4,5], 12) with in_temporary_directory(): f.write('test_file') f2 = Features() f2.read('test_file') assert np.all(f.array==f2.array)
def test_invalid_shapes(self): f = Features() with self.assertRaises(AssertionError): f.append(1, 0) with self.assertRaises(AssertionError): f.append([1,2,3],[4,5,6])
def test_add(self): f = Features() f.append([1,2,3], 4) f.append(np.array([1,2,3]), np.array(4)) assert len(f.measurements)==2 f2 = Features() f2.extend([[1,2,3],[4,5,6]], [7,8]) assert len(f2.measurements)==2 f2.concatenate(f) assert len(f2.measurements)==4