def distribute(node): """ Wrap a copy of the given element around the contents of each of its children, removing the node in the process. """ children = list(c for c in node.childNodes if is_element(c)) unwrap(node) tag_name = node.tagName for c in children: wrap_inner(c, tag_name)
def remove_nesting(dom, tag_name): """ Unwrap items in the node list that have ancestors with the same tag. """ for node in dom.getElementsByTagName(tag_name): for ancestor in ancestors(node): if ancestor is node: continue if ancestor is dom.documentElement: break if ancestor.tagName == tag_name: unwrap(node) break
def read(self, read_size, decode_hdlc=False): buf = b'' buf = self.port.read(read_size) buf = bytes(buf) if decode_hdlc: buf = util.unwrap(write_buf) return buf
def dubins_trajectory(q0, qf, radius, velocity_real, step_size, contact_angle=0): velocity = velocity_real * 10 #1. compute 2d trajectory configurations, N_star = compute_dubins_base(q0, qf, radius, velocity_real, step_size, contact_angle=contact_angle) #2. convert trajectory back to original coordinate system (account for different object sides) configurations_transformed = [] for counter, configuration in enumerate(configurations): configurations_transformed.append( rotate_2d_pose_origin(configuration, contact_angle, q0[0:2])) #3. reformat object trajectory t_star = np.array(N_star) / velocity x_star = np.array(configurations_transformed) if len(configurations) > 0: x_star[:, 2] = util.unwrap(x_star[:, 2]) return configurations_transformed, N_star, x_star, t_star
def read(self, read_size, decode_hdlc = False): buf = b'' try: buf = self.r_handle.read(read_size) buf = bytes(buf) except usb.core.USBError: return b'' if decode_hdlc: buf = util.unwrap(write_buf) return buf
def read(self, read_size, decode_hdlc=False): buf = b'' try: buf = self.f.read(read_size) buf = bytes(buf) except: return b'' if decode_hdlc: buf = util.unwrap(write_buf) return buf
def lazy_multiprocessing_imap(worker: Callable[[T], O], in_data : Iterable[T], num_threads : Optional[int]=os.cpu_count(), chunk_size : Optional[int]=None) -> Iterable[O]: if chunk_size == None: if num_threads: chunk_size = num_threads * 10 else: chunk_size = 100 with multiprocessing.Pool(num_threads) as pool: for chunk in chunks(in_data, unwrap(chunk_size)): yield from list(pool.imap(worker, chunk))
def fix_lists(dom): # <ins> and <del> tags are not allowed within <ul> or <ol> tags. # Move them to the nearest li, so that the numbering isn't interrupted. # Find all del > li and ins > li sets. del_tags = set() ins_tags = set() for node in list(dom.getElementsByTagName('li')): parent = node.parentNode if parent.tagName == 'del': del_tags.add(parent) elif parent.tagName == 'ins': ins_tags.add(parent) # Change ins > li into li > ins. for ins_tag in ins_tags: distribute(ins_tag) # Change del > li into li.del-li > del. for del_tag in del_tags: children = list(del_tag.childNodes) unwrap(del_tag) for c in children: if c.nodeName == 'li': c.setAttribute('class', 'del-li') wrap_inner(c, 'del')
def parse_diag(self, pkt, hdlc_encoded=True, check_crc=True, radio_id=0): # Should contain DIAG command and CRC16 # pkt should not contain trailing 0x7E, and either HDLC encoded or not # When the pkt is not HDLC encoded, hdlc_encoded should be set to True # radio_id = 0 for default, larger than 1 for SIM 1 and such if len(pkt) < 3: return if hdlc_encoded: pkt = util.unwrap(pkt) # Check and strip CRC if existing if check_crc: crc = util.dm_crc16(pkt[:-2]) crc_pkt = (pkt[-1] << 8) | pkt[-2] if crc != crc_pkt: self.logger.log( logging.WARNING, "CRC mismatch: expected 0x{:04x}, got 0x{:04x}".format( crc, crc_pkt)) self.logger.log(logging.DEBUG, util.xxd(pkt)) pkt = pkt[:-2] if pkt[0] == diagcmd.DIAG_LOG_F: self.parse_diag_log(pkt, radio_id) elif pkt[0] == diagcmd.DIAG_EVENT_REPORT_F and self.parse_events: self.parse_diag_event(pkt, radio_id) elif pkt[0] == diagcmd.DIAG_EXT_MSG_F and self.parse_msgs: self.parse_diag_ext_msg(pkt, radio_id) elif pkt[0] == diagcmd.DIAG_QSR_EXT_MSG_TERSE_F and self.parse_msgs: #self.parse_diag_qsr_ext_msg(pkt, radio_id) pass elif pkt[0] == diagcmd.DIAG_QSR4_EXT_MSG_TERSE_F and self.parse_msgs: #self.parse_diag_qsr4_ext_msg(pkt, radio_id) pass elif pkt[0] == diagcmd.DIAG_MULTI_RADIO_CMD_F: self.parse_diag_multisim(pkt) else: #print("Not parsing non-Log packet %02x" % pkt[0]) #util.xxd(pkt) return
def action(self) -> str: return unwrap(self.transition).action
def reinforce_lemma_multithreaded( args: argparse.Namespace, coq: serapi_instance.SerapiInstance, lock: Lock, namespace: multiprocessing.managers.Namespace, worker_idx: int, samples: Queue[LabeledTransition], lemma_statement: str, _module_prefix: str, demonstration: Optional[Demonstration]) -> Tuple[str, ReinforceGraph]: lemma_name = serapi_instance.lemma_name_from_statement(lemma_statement) graph = ReinforceGraph(lemma_name) lemma_memory = [] for i in trange(args.num_episodes, disable=(not args.progress), leave=False, position=worker_idx + 1): cur_node = graph.start_node proof_contexts_seen = [unwrap(coq.proof_context)] episode_memory: List[LabeledTransition] = [] reached_qed = False for t in range(args.episode_length): context_before = coq.tactic_context(coq.local_lemmas[:-1]) proof_context_before = unwrap(coq.proof_context) context_trunced = truncate_tactic_context( context_before, args.max_term_length) if (demonstration and t < len(demonstration) - ((i//args.demonstration_steps)+1)): eprint("Getting demonstration", guard=args.verbose >= 2) ordered_actions = [(demonstration[t], certainty_of(namespace.predictor, args.num_predictions * 2, context_trunced, demonstration[t]))] else: with print_time("Getting predictions", guard=args.verbose >= 2): with lock: eprint(f"Locked in thread {worker_idx}", guard=args.verbose >= 2) predictor = namespace.predictor estimator = namespace.estimator with print_time("Making predictions", guard=args.verbose >= 3): predictions = predictor.predictKTactics( context_trunced, args.num_predictions) if random.random() < args.exploration_factor: eprint("Picking random action", guard=args.verbose >= 2) ordered_actions = order_by_score(predictions) else: with print_time("Picking actions with q_estimator", guard=args.verbose >= 2): q_choices = zip(estimator( [(context_trunced, p.prediction, p.certainty) for p in predictions]), predictions) ordered_actions = [p[1] for p in sorted(q_choices, key=lambda q: q[0], reverse=True)] eprint(f"Unlocked in thread {worker_idx}", guard=args.verbose >= 2) with print_time("Running actions", guard=args.verbose >= 2): action = None original_certainty = None for try_action, try_original_certainty in ordered_actions: try: coq.run_stmt(try_action) proof_context_after = unwrap(coq.proof_context) if any([serapi_instance.contextSurjective( proof_context_after, path_context) for path_context in proof_contexts_seen]): coq.cancel_last() if args.ghosts: transition = assign_failed_reward( context_trunced.relevant_lemmas, context_trunced.prev_tactics, proof_context_before, proof_context_after, try_action, try_original_certainty, 0) ghost_node = graph.addGhostTransition( cur_node, transition) transition.graph_node = ghost_node continue action = try_action original_certainty = try_original_certainty break except (serapi_instance.ParseError, serapi_instance.CoqExn, serapi_instance.TimeoutError): if args.ghosts: transition = assign_failed_reward( context_trunced.relevant_lemmas, context_trunced.prev_tactics, proof_context_before, proof_context_before, try_action, try_original_certainty, 0) ghost_node = graph.addGhostTransition(cur_node, transition) transition.graph_node = ghost_node if action is None: # We'll hit this case of we tried all of the # predictions, and none worked graph.setNodeColor(cur_node, "red") transition = assign_failed_reward( context_trunced.relevant_lemmas, context_trunced.prev_tactics, proof_context_before, proof_context_before, "Abort.", try_original_certainty, -25) samples.put(transition) episode_memory.append(transition) break # Break from episode transition = assign_reward(args, context_trunced.relevant_lemmas, context_trunced.prev_tactics, proof_context_before, proof_context_after, action, unwrap(original_certainty)) cur_node = graph.addTransition(cur_node, transition) transition.graph_node = cur_node assert transition.reward < 2000 samples.put(transition) episode_memory.append(transition) proof_contexts_seen.append(proof_context_after) lemma_memory += episode_memory if coq.goals == "": eprint("QED!", guard=args.verbose >= 2) graph.mkQED(cur_node) for sample in (episode_memory * (args.success_repetitions - 1)): samples.put(sample) reached_qed = True break if not reached_qed: # We'll hit this case of we tried all of the # predictions, and none worked graph.setNodeColor(cur_node, "red") transition = assign_failed_reward( context_trunced.relevant_lemmas, context_trunced.prev_tactics, proof_context_before, proof_context_before, "Abort.", try_original_certainty, -25) samples.put(transition) episode_memory.append(transition) # Clean up episode if lemma_name: coq.run_stmt("Admitted.") coq.run_stmt(f"Reset {lemma_name}.") else: coq.cancel_last() while coq.goals: coq.cancel_last() coq.run_stmt(lemma_statement) # Write out lemma memory to progress file for resuming for sample in lemma_memory: with args.out_weights.with_suffix('.tmp').open('a') as f: f.write(json.dumps(sample.to_dict()) + "\n") graphpath = (args.graphs_dir / lemma_name).with_suffix(".png") return str(graphpath), graph
def _strip_changes_old(node): for ins_node in node.getElementsByTagName('ins'): remove_node(ins_node) for del_node in node.getElementsByTagName('del'): unwrap(del_node)
def dataloader_args(self) -> DataloaderArgs: return extract_dataloader_args(unwrap(self.training_args))
def hyp_encoder(self) -> HypArgEncoder: return unwrap(self._model).hyp_model.arg_encoder
def entire_goal_encoder(self) -> EncoderRNN: return unwrap(self._model).goal_encoder
def goal_token_encoder(self) -> GoalTokenEncoderModel: return unwrap(self._model).goal_args_model.encoder_model
def reward(self) -> float: return unwrap(self.transition).reward
def reinforce_lemma(args: argparse.Namespace, predictor: tactic_predictor.TacticPredictor, estimator: q_estimator.QEstimator, coq: serapi_instance.SerapiInstance, lemma_statement: str, epsilon: float, gamma: float, memory: List[LabeledTransition]) -> None: lemma_name = coq.cur_lemma_name graph = ReinforceGraph(lemma_name) for episode in trange(args.num_episodes, disable=(not args.progress), leave=False): cur_node = graph.start_node proof_contexts_seen = [unwrap(coq.proof_context)] episode_memory = [] for t in range(args.episode_length): with print_time("Getting predictions", guard=args.verbose): context_before = coq.tactic_context(coq.local_lemmas[:-1]) proof_context_before = unwrap(coq.proof_context) predictions = predictor.predictKTactics( context_before, args.num_predictions) if random.random() < epsilon: ordered_actions = [p.prediction for p in random.sample(predictions, len(predictions))] else: with print_time("Picking actions using q_estimator", guard=args.verbose): q_choices = zip(estimator( [(context_before, prediction.prediction) for prediction in predictions]), [p.prediction for p in predictions]) ordered_actions = [p[1] for p in sorted(q_choices, key=lambda q: q[0], reverse=True)] with print_time("Running actions", guard=args.verbose): action = None for try_action in ordered_actions: try: coq.run_stmt(try_action) proof_context_after = unwrap(coq.proof_context) if any([serapi_instance.contextSurjective( proof_context_after, path_context) for path_context in proof_contexts_seen]): coq.cancel_last() transition = assign_failed_reward( context_before.relevant_lemmas, context_before.prev_tactics, proof_context_before, proof_context_after, try_action, -50) assert transition.reward < 2000 memory.append(transition) if args.ghosts: ghost_node = graph.addGhostTransition( cur_node, try_action) transition.graph_node = ghost_node continue action = try_action break except (serapi_instance.ParseError, serapi_instance.CoqExn, serapi_instance.TimeoutError): transition = assign_failed_reward( context_before.relevant_lemmas, context_before.prev_tactics, proof_context_before, proof_context_before, try_action, -500) assert transition.reward < 2000 memory.append(transition) if args.ghosts: ghost_node = graph.addGhostTransition(cur_node, try_action) transition.graph_node = ghost_node pass if action is None: # We'll hit this case of we tried all of the # predictions, and none worked graph.setNodeColor(cur_node, "red") break # Break from episode transition = assign_reward(context_before.relevant_lemmas, context_before.prev_tactics, proof_context_before, proof_context_after, action) cur_node = graph.addTransition(cur_node, action, transition.reward) transition.graph_node = cur_node assert transition.reward < 2000 episode_memory.append(transition) memory.append(transition) proof_contexts_seen.append(proof_context_after) if coq.goals == "": graph.mkQED(cur_node) memory += (episode_memory * (args.success_repetitions - 1)) break with print_time("Assigning scores", guard=args.verbose): transition_samples = sample_batch(memory, args.batch_size) training_samples = assign_scores(transition_samples, estimator, predictor, args.num_predictions, gamma, # Passing this graph # in so we can # maintain a record # of the most recent # q score estimates # in the graph graph) with print_time("Training", guard=args.verbose): estimator.train(training_samples) # Clean up episode coq.run_stmt("Admitted.") coq.run_stmt(f"Reset {lemma_name}.") coq.run_stmt(lemma_statement) graphpath = (args.graphs_dir / lemma_name).with_suffix(".png") graph.draw(str(graphpath)) pass