Skip to content

Commit 19a51a6

Browse files
author
Damien Sileo
committed
fix smoketest
1 parent 817bbde commit 19a51a6

2 files changed

Lines changed: 45 additions & 26 deletions

File tree

reasoning_core/tasks/coreference.py

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,17 @@ def _surface_salience_name_pred(q, mentions, pronoun_window=3):
418418

419419

420420
def _query_candidates(mentions, pool, target_hops=2, cfg=None, challenge=None):
421+
pronoun_window = cfg.pronoun_window if cfg else 3
422+
require_same_gender_distractor = cfg.require_same_gender_distractor if cfg else False
423+
require_opaque_link = cfg.require_opaque_link_for_multihop if cfg else False
424+
use_global = bool(cfg and (
425+
cfg.n_ambiguous_mentions
426+
or cfg.n_constraints
427+
or cfg.n_rules
428+
or cfg.n_identity_links
429+
or cfg.n_state_changes
430+
))
431+
421432
counts = {}
422433
for m in mentions:
423434
counts[m['entity']] = counts.get(m['entity'], 0) + 1
@@ -430,20 +441,15 @@ def _query_candidates(mentions, pool, target_hops=2, cfg=None, challenge=None):
430441
for m in mentions:
431442
if m['mode'] not in (PRONOUN, DESC, DESC_EVENT):
432443
continue
433-
use_global = cfg and any([
434-
cfg.n_ambiguous_mentions, cfg.n_constraints, cfg.n_rules,
435-
cfg.n_identity_links, cfg.n_state_changes,
436-
])
437444
if use_global and not m.get('ambiguous'):
438445
continue
439-
if not use_global and not surface_identifiable(m, mentions,
440-
cfg.pronoun_window if cfg else 3):
446+
if not use_global and not surface_identifiable(m, mentions, pronoun_window):
441447
continue
442448
if use_global and not globally_pins_query(
443449
m, mentions, pool,
444450
(challenge or {}).get('constraints'),
445451
(challenge or {}).get('rules'),
446-
cfg.pronoun_window):
452+
pronoun_window):
447453
continue
448454
direct_constraint = use_global and _constraint_lookup_pred(m, mentions, challenge or {}) == m['entity']
449455
hops = _get_hops(m, mentions)
@@ -458,12 +464,12 @@ def _query_candidates(mentions, pool, target_hops=2, cfg=None, challenge=None):
458464
elif m['mode'] == DESC_EVENT:
459465
if len(event_matches_for_mention(m, mentions)) != 1:
460466
continue
461-
elif cfg and cfg.require_same_gender_distractor:
467+
elif require_same_gender_distractor:
462468
if not _has_same_gender_distractor(m['entity'], mentions, m['sent'],
463-
cfg.pronoun_window):
469+
pronoun_window):
464470
continue
465471
ant = mentions[m['antecedent']]
466-
if not (1 < m['sent'] - ant['sent'] <= cfg.pronoun_window):
472+
if not (1 < m['sent'] - ant['sent'] <= pronoun_window):
467473
continue
468474

469475
if target_hops >= 2:
@@ -473,10 +479,9 @@ def _query_candidates(mentions, pool, target_hops=2, cfg=None, challenge=None):
473479
path = antecedent_path(m, mentions)
474480
if len(path) <= 2:
475481
continue
476-
if _surface_salience_name_pred(m, mentions,
477-
cfg.pronoun_window if cfg else 3) == m['entity']:
482+
if _surface_salience_name_pred(m, mentions, pronoun_window) == m['entity']:
478483
continue
479-
if (cfg and cfg.require_opaque_link_for_multihop and
484+
if (require_opaque_link and
480485
(not any(p['mode'] in (DESC, DESC_EVENT) for p in path[:-1]) or
481486
not any(p['mode'] == PRONOUN for p in path[:-1]))):
482487
continue
@@ -892,7 +897,7 @@ class CoreferenceConfig(Config):
892897
single_gender_pool: bool = True
893898
balanced_generation: bool = True
894899
oversample: int = 4
895-
balance_batch_size: int = 256
900+
balance_batch_size: int = 64
896901
shortcut_eps: float = 0.08
897902
shortcut_min_n: int = 20
898903
pronoun_window: int = 8
@@ -921,21 +926,33 @@ def __init__(self, config=CoreferenceConfig()):
921926

922927
def generate_raw_candidate(self):
923928
cfg = self.config
924-
if cfg.n_entities < 2:
929+
n_entities = cfg.n_entities
930+
if n_entities < 2:
925931
raise ValueError("Coreference requires at least 2 entities")
932+
chain_len = cfg.chain_len
933+
single_gender_pool = cfg.single_gender_pool
934+
n_distractors = cfg.n_distractors
935+
p_pronoun = cfg.p_pronoun
936+
p_desc = cfg.p_desc
937+
p_desc_event = cfg.p_desc_event
938+
p_shortcut = cfg.p_shortcut
939+
pronoun_window = cfg.pronoun_window
940+
n_ambiguous_mentions = cfg.n_ambiguous_mentions
941+
target_hops = cfg.target_hops
942+
p_compositional_query = cfg.p_compositional_query
926943
for _ in range(1000):
927-
clen = cfg.chain_len
928-
if clen > 2 and random.random() < cfg.p_shortcut:
944+
clen = chain_len
945+
if clen > 2 and random.random() < p_shortcut:
929946
clen = random.randint(2, clen - 1)
930-
pool = _pool(cfg.n_entities, cfg.single_gender_pool)
931-
plan = _plan(pool, clen, cfg.n_distractors)
932-
lines, mentions = _emit(plan, pool, cfg.p_pronoun, cfg.p_desc,
933-
cfg.p_desc_event,
934-
cfg.pronoun_window,
935-
cfg.n_ambiguous_mentions)
947+
pool = _pool(n_entities, single_gender_pool)
948+
plan = _plan(pool, clen, n_distractors)
949+
lines, mentions = _emit(plan, pool, p_pronoun, p_desc,
950+
p_desc_event,
951+
pronoun_window,
952+
n_ambiguous_mentions)
936953
challenge = _add_global_challenge(lines, mentions, pool, cfg)
937954

938-
cands = _query_candidates(mentions, pool, target_hops=cfg.target_hops,
955+
cands = _query_candidates(mentions, pool, target_hops=target_hops,
939956
cfg=cfg, challenge=challenge)
940957
if not cands:
941958
continue
@@ -957,7 +974,7 @@ def generate_raw_candidate(self):
957974
'challenge': challenge,
958975
'hops': hops,
959976
'mode': q['mode'],
960-
'target_hops': cfg.target_hops,
977+
'target_hops': target_hops,
961978
})
962979
raise RuntimeError("Could not generate a valid coreference problem")
963980

tests/smoke_test.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ def test_all_tasks_validate():
1313
task = get_task(t)
1414
task.validate(n_samples=5)
1515
print(f"{time.time() - t0:.5f}")
16-
except Exception as e:
16+
except KeyboardInterrupt:
17+
raise
18+
except BaseException as e:
1719
print(f"EXCEPTION: {e}")
1820
failed.append(t)
1921

0 commit comments

Comments
 (0)