Add noexcept for Cython 3

This commit is contained in:
Matthew Honnibal 2025-04-04 22:54:44 +02:00
parent 2384184710
commit f4109a1fcf
16 changed files with 134 additions and 134 deletions

View File

@ -35,7 +35,7 @@ cdef class Lexeme:
return self return self
@staticmethod @staticmethod
cdef inline void set_struct_attr(LexemeC* lex, attr_id_t name, attr_t value) nogil: cdef inline void set_struct_attr(LexemeC* lex, attr_id_t name, attr_t value) noexcept nogil:
if name < (sizeof(flags_t) * 8): if name < (sizeof(flags_t) * 8):
Lexeme.c_set_flag(lex, name, value) Lexeme.c_set_flag(lex, name, value)
elif name == ID: elif name == ID:
@ -54,7 +54,7 @@ cdef class Lexeme:
lex.lang = value lex.lang = value
@staticmethod @staticmethod
cdef inline attr_t get_struct_attr(const LexemeC* lex, attr_id_t feat_name) nogil: cdef inline attr_t get_struct_attr(const LexemeC* lex, attr_id_t feat_name) noexcept nogil:
if feat_name < (sizeof(flags_t) * 8): if feat_name < (sizeof(flags_t) * 8):
if Lexeme.c_check_flag(lex, feat_name): if Lexeme.c_check_flag(lex, feat_name):
return 1 return 1
@ -82,7 +82,7 @@ cdef class Lexeme:
return 0 return 0
@staticmethod @staticmethod
cdef inline bint c_check_flag(const LexemeC* lexeme, attr_id_t flag_id) nogil: cdef inline bint c_check_flag(const LexemeC* lexeme, attr_id_t flag_id) noexcept nogil:
cdef flags_t one = 1 cdef flags_t one = 1
if lexeme.flags & (one << flag_id): if lexeme.flags & (one << flag_id):
return True return True
@ -90,7 +90,7 @@ cdef class Lexeme:
return False return False
@staticmethod @staticmethod
cdef inline bint c_set_flag(LexemeC* lex, attr_id_t flag_id, bint value) nogil: cdef inline bint c_set_flag(LexemeC* lex, attr_id_t flag_id, bint value) noexcept nogil:
cdef flags_t one = 1 cdef flags_t one = 1
if value: if value:
lex.flags |= one << flag_id lex.flags |= one << flag_id

View File

@ -1,4 +1,4 @@
# cython: binding=True, infer_types=True # cython: binding=True, infer_types=True, language_level=3
from cpython.object cimport PyObject from cpython.object cimport PyObject
from libc.stdint cimport int64_t from libc.stdint cimport int64_t

View File

@ -625,7 +625,7 @@ cdef action_t get_action(
const TokenC * token, const TokenC * token,
const attr_t * extra_attrs, const attr_t * extra_attrs,
const int8_t * predicate_matches const int8_t * predicate_matches
) nogil: ) noexcept nogil:
"""We need to consider: """We need to consider:
a) Does the token match the specification? [Yes, No] a) Does the token match the specification? [Yes, No]
b) What's the quantifier? [1, 0+, ?] b) What's the quantifier? [1, 0+, ?]
@ -740,7 +740,7 @@ cdef int8_t get_is_match(
const TokenC* token, const TokenC* token,
const attr_t* extra_attrs, const attr_t* extra_attrs,
const int8_t* predicate_matches const int8_t* predicate_matches
) nogil: ) noexcept nogil:
for i in range(state.pattern.nr_py): for i in range(state.pattern.nr_py):
if predicate_matches[state.pattern.py_predicates[i]] == -1: if predicate_matches[state.pattern.py_predicates[i]] == -1:
return 0 return 0
@ -755,14 +755,14 @@ cdef int8_t get_is_match(
return True return True
cdef inline int8_t get_is_final(PatternStateC state) nogil: cdef inline int8_t get_is_final(PatternStateC state) noexcept nogil:
if state.pattern[1].quantifier == FINAL_ID: if state.pattern[1].quantifier == FINAL_ID:
return 1 return 1
else: else:
return 0 return 0
cdef inline int8_t get_quantifier(PatternStateC state) nogil: cdef inline int8_t get_quantifier(PatternStateC state) noexcept nogil:
return state.pattern.quantifier return state.pattern.quantifier
@ -805,7 +805,7 @@ cdef TokenPatternC* init_pattern(Pool mem, attr_t entity_id, object token_specs)
return pattern return pattern
cdef attr_t get_ent_id(const TokenPatternC* pattern) nogil: cdef attr_t get_ent_id(const TokenPatternC* pattern) noexcept nogil:
while pattern.quantifier != FINAL_ID: while pattern.quantifier != FINAL_ID:
pattern += 1 pattern += 1
id_attr = pattern[0].attrs[0] id_attr = pattern[0].attrs[0]

View File

@ -52,14 +52,14 @@ cdef SizesC get_c_sizes(model, int batch_size) except *:
return output return output
cdef ActivationsC alloc_activations(SizesC n) nogil: cdef ActivationsC alloc_activations(SizesC n) noexcept nogil:
cdef ActivationsC A cdef ActivationsC A
memset(&A, 0, sizeof(A)) memset(&A, 0, sizeof(A))
resize_activations(&A, n) resize_activations(&A, n)
return A return A
cdef void free_activations(const ActivationsC* A) nogil: cdef void free_activations(const ActivationsC* A) noexcept nogil:
free(A.token_ids) free(A.token_ids)
free(A.scores) free(A.scores)
free(A.unmaxed) free(A.unmaxed)
@ -67,7 +67,7 @@ cdef void free_activations(const ActivationsC* A) nogil:
free(A.is_valid) free(A.is_valid)
cdef void resize_activations(ActivationsC* A, SizesC n) nogil: cdef void resize_activations(ActivationsC* A, SizesC n) noexcept nogil:
if n.states <= A._max_size: if n.states <= A._max_size:
A._curr_size = n.states A._curr_size = n.states
return return
@ -100,7 +100,7 @@ cdef void resize_activations(ActivationsC* A, SizesC n) nogil:
cdef void predict_states( cdef void predict_states(
CBlas cblas, ActivationsC* A, StateC** states, const WeightsC* W, SizesC n CBlas cblas, ActivationsC* A, StateC** states, const WeightsC* W, SizesC n
) nogil: ) noexcept nogil:
resize_activations(A, n) resize_activations(A, n)
for i in range(n.states): for i in range(n.states):
states[i].set_context_tokens(&A.token_ids[i*n.feats], n.feats) states[i].set_context_tokens(&A.token_ids[i*n.feats], n.feats)
@ -159,7 +159,7 @@ cdef void sum_state_features(
int B, int B,
int F, int F,
int O int O
) nogil: ) noexcept nogil:
cdef int idx, b, f cdef int idx, b, f
cdef const float* feature cdef const float* feature
padding = cached padding = cached
@ -183,7 +183,7 @@ cdef void cpu_log_loss(
const int* is_valid, const int* is_valid,
const float* scores, const float* scores,
int O int O
) nogil: ) noexcept nogil:
"""Do multi-label log loss""" """Do multi-label log loss"""
cdef double max_, gmax, Z, gZ cdef double max_, gmax, Z, gZ
best = arg_max_if_gold(scores, costs, is_valid, O) best = arg_max_if_gold(scores, costs, is_valid, O)
@ -209,7 +209,7 @@ cdef void cpu_log_loss(
cdef int arg_max_if_gold( cdef int arg_max_if_gold(
const weight_t* scores, const weight_t* costs, const int* is_valid, int n const weight_t* scores, const weight_t* costs, const int* is_valid, int n
) nogil: ) noexcept nogil:
# Find minimum cost # Find minimum cost
cdef float cost = 1 cdef float cost = 1
for i in range(n): for i in range(n):
@ -224,7 +224,7 @@ cdef int arg_max_if_gold(
return best return best
cdef int arg_max_if_valid(const weight_t* scores, const int* is_valid, int n) nogil: cdef int arg_max_if_valid(const weight_t* scores, const int* is_valid, int n) noexcept nogil:
cdef int best = -1 cdef int best = -1
for i in range(n): for i in range(n):
if is_valid[i] >= 1: if is_valid[i] >= 1:

View File

@ -17,7 +17,7 @@ from ...typedefs cimport attr_t
from ...vocab cimport EMPTY_LEXEME from ...vocab cimport EMPTY_LEXEME
cdef inline bint is_space_token(const TokenC* token) nogil: cdef inline bint is_space_token(const TokenC* token) noexcept nogil:
return Lexeme.c_check_flag(token.lex, IS_SPACE) return Lexeme.c_check_flag(token.lex, IS_SPACE)
cdef struct ArcC: cdef struct ArcC:
@ -41,7 +41,7 @@ cdef cppclass StateC:
int offset int offset
int _b_i int _b_i
inline __init__(const TokenC* sent, int length) nogil: inline __init__(const TokenC* sent, int length) noexcept nogil:
this._sent = sent this._sent = sent
this._heads = <int*>calloc(length, sizeof(int)) this._heads = <int*>calloc(length, sizeof(int))
if not (this._sent and this._heads): if not (this._sent and this._heads):
@ -60,7 +60,7 @@ cdef cppclass StateC:
inline __dealloc__(): inline __dealloc__():
free(this._heads) free(this._heads)
inline void set_context_tokens(int* ids, int n) nogil: inline void set_context_tokens(int* ids, int n) noexcept nogil:
cdef int i, j cdef int i, j
if n == 1: if n == 1:
if this.B(0) >= 0: if this.B(0) >= 0:
@ -131,14 +131,14 @@ cdef cppclass StateC:
else: else:
ids[i] = -1 ids[i] = -1
inline int S(int i) nogil const: inline int S(int i) noexcept nogil const:
if i >= this._stack.size(): if i >= this._stack.size():
return -1 return -1
elif i < 0: elif i < 0:
return -1 return -1
return this._stack.at(this._stack.size() - (i+1)) return this._stack.at(this._stack.size() - (i+1))
inline int B(int i) nogil const: inline int B(int i) noexcept nogil const:
if i < 0: if i < 0:
return -1 return -1
elif i < this._rebuffer.size(): elif i < this._rebuffer.size():
@ -150,19 +150,19 @@ cdef cppclass StateC:
else: else:
return b_i return b_i
inline const TokenC* B_(int i) nogil const: inline const TokenC* B_(int i) noexcept nogil const:
return this.safe_get(this.B(i)) return this.safe_get(this.B(i))
inline const TokenC* E_(int i) nogil const: inline const TokenC* E_(int i) noexcept nogil const:
return this.safe_get(this.E(i)) return this.safe_get(this.E(i))
inline const TokenC* safe_get(int i) nogil const: inline const TokenC* safe_get(int i) noexcept nogil const:
if i < 0 or i >= this.length: if i < 0 or i >= this.length:
return &this._empty_token return &this._empty_token
else: else:
return &this._sent[i] return &this._sent[i]
inline void map_get_arcs(const unordered_map[int, vector[ArcC]] &heads_arcs, vector[ArcC]* out) nogil const: inline void map_get_arcs(const unordered_map[int, vector[ArcC]] &heads_arcs, vector[ArcC]* out) noexcept nogil const:
cdef const vector[ArcC]* arcs cdef const vector[ArcC]* arcs
head_arcs_it = heads_arcs.const_begin() head_arcs_it = heads_arcs.const_begin()
while head_arcs_it != heads_arcs.const_end(): while head_arcs_it != heads_arcs.const_end():
@ -175,23 +175,23 @@ cdef cppclass StateC:
incr(arcs_it) incr(arcs_it)
incr(head_arcs_it) incr(head_arcs_it)
inline void get_arcs(vector[ArcC]* out) nogil const: inline void get_arcs(vector[ArcC]* out) noexcept nogil const:
this.map_get_arcs(this._left_arcs, out) this.map_get_arcs(this._left_arcs, out)
this.map_get_arcs(this._right_arcs, out) this.map_get_arcs(this._right_arcs, out)
inline int H(int child) nogil const: inline int H(int child) noexcept nogil const:
if child >= this.length or child < 0: if child >= this.length or child < 0:
return -1 return -1
else: else:
return this._heads[child] return this._heads[child]
inline int E(int i) nogil const: inline int E(int i) noexcept nogil const:
if this._ents.size() == 0: if this._ents.size() == 0:
return -1 return -1
else: else:
return this._ents.back().start return this._ents.back().start
inline int nth_child(const unordered_map[int, vector[ArcC]]& heads_arcs, int head, int idx) nogil const: inline int nth_child(const unordered_map[int, vector[ArcC]]& heads_arcs, int head, int idx) noexcept nogil const:
if idx < 1: if idx < 1:
return -1 return -1
@ -215,22 +215,22 @@ cdef cppclass StateC:
return -1 return -1
inline int L(int head, int idx) nogil const: inline int L(int head, int idx) noexcept nogil const:
return this.nth_child(this._left_arcs, head, idx) return this.nth_child(this._left_arcs, head, idx)
inline int R(int head, int idx) nogil const: inline int R(int head, int idx) noexcept nogil const:
return this.nth_child(this._right_arcs, head, idx) return this.nth_child(this._right_arcs, head, idx)
inline bint empty() nogil const: inline bint empty() noexcept nogil const:
return this._stack.size() == 0 return this._stack.size() == 0
inline bint eol() nogil const: inline bint eol() noexcept nogil const:
return this.buffer_length() == 0 return this.buffer_length() == 0
inline bint is_final() nogil const: inline bint is_final() noexcept nogil const:
return this.stack_depth() <= 0 and this.eol() return this.stack_depth() <= 0 and this.eol()
inline int cannot_sent_start(int word) nogil const: inline int cannot_sent_start(int word) noexcept nogil const:
if word < 0 or word >= this.length: if word < 0 or word >= this.length:
return 0 return 0
elif this._sent[word].sent_start == -1: elif this._sent[word].sent_start == -1:
@ -238,7 +238,7 @@ cdef cppclass StateC:
else: else:
return 0 return 0
inline int is_sent_start(int word) nogil const: inline int is_sent_start(int word) noexcept nogil const:
if word < 0 or word >= this.length: if word < 0 or word >= this.length:
return 0 return 0
elif this._sent[word].sent_start == 1: elif this._sent[word].sent_start == 1:
@ -248,20 +248,20 @@ cdef cppclass StateC:
else: else:
return 0 return 0
inline void set_sent_start(int word, int value) nogil: inline void set_sent_start(int word, int value) noexcept nogil:
if value >= 1: if value >= 1:
this._sent_starts.insert(word) this._sent_starts.insert(word)
inline bint has_head(int child) nogil const: inline bint has_head(int child) noexcept nogil const:
return this._heads[child] >= 0 return this._heads[child] >= 0
inline int l_edge(int word) nogil const: inline int l_edge(int word) noexcept nogil const:
return word return word
inline int r_edge(int word) nogil const: inline int r_edge(int word) noexcept nogil const:
return word return word
inline int n_arcs(const unordered_map[int, vector[ArcC]] &heads_arcs, int head) nogil const: inline int n_arcs(const unordered_map[int, vector[ArcC]] &heads_arcs, int head) noexcept nogil const:
cdef int n = 0 cdef int n = 0
head_arcs_it = heads_arcs.const_find(head) head_arcs_it = heads_arcs.const_find(head)
if head_arcs_it == heads_arcs.const_end(): if head_arcs_it == heads_arcs.const_end():
@ -277,28 +277,28 @@ cdef cppclass StateC:
return n return n
inline int n_L(int head) nogil const: inline int n_L(int head) noexcept nogil const:
return n_arcs(this._left_arcs, head) return n_arcs(this._left_arcs, head)
inline int n_R(int head) nogil const: inline int n_R(int head) noexcept nogil const:
return n_arcs(this._right_arcs, head) return n_arcs(this._right_arcs, head)
inline bint stack_is_connected() nogil const: inline bint stack_is_connected() noexcept nogil const:
return False return False
inline bint entity_is_open() nogil const: inline bint entity_is_open() noexcept nogil const:
if this._ents.size() == 0: if this._ents.size() == 0:
return False return False
else: else:
return this._ents.back().end == -1 return this._ents.back().end == -1
inline int stack_depth() nogil const: inline int stack_depth() noexcept nogil const:
return this._stack.size() return this._stack.size()
inline int buffer_length() nogil const: inline int buffer_length() noexcept nogil const:
return (this.length - this._b_i) + this._rebuffer.size() return (this.length - this._b_i) + this._rebuffer.size()
inline void push() nogil: inline void push() noexcept nogil:
b0 = this.B(0) b0 = this.B(0)
if this._rebuffer.size(): if this._rebuffer.size():
b0 = this._rebuffer.back() b0 = this._rebuffer.back()
@ -308,32 +308,32 @@ cdef cppclass StateC:
this._b_i += 1 this._b_i += 1
this._stack.push_back(b0) this._stack.push_back(b0)
inline void pop() nogil: inline void pop() noexcept nogil:
this._stack.pop_back() this._stack.pop_back()
inline void force_final() nogil: inline void force_final() noexcept nogil:
# This should only be used in desperate situations, as it may leave # This should only be used in desperate situations, as it may leave
# the analysis in an unexpected state. # the analysis in an unexpected state.
this._stack.clear() this._stack.clear()
this._b_i = this.length this._b_i = this.length
inline void unshift() nogil: inline void unshift() noexcept nogil:
s0 = this._stack.back() s0 = this._stack.back()
this._unshiftable[s0] = 1 this._unshiftable[s0] = 1
this._rebuffer.push_back(s0) this._rebuffer.push_back(s0)
this._stack.pop_back() this._stack.pop_back()
inline int is_unshiftable(int item) nogil const: inline int is_unshiftable(int item) noexcept nogil const:
if item >= this._unshiftable.size(): if item >= this._unshiftable.size():
return 0 return 0
else: else:
return this._unshiftable.at(item) return this._unshiftable.at(item)
inline void set_reshiftable(int item) nogil: inline void set_reshiftable(int item) noexcept nogil:
if item < this._unshiftable.size(): if item < this._unshiftable.size():
this._unshiftable[item] = 0 this._unshiftable[item] = 0
inline void add_arc(int head, int child, attr_t label) nogil: inline void add_arc(int head, int child, attr_t label) noexcept nogil:
if this.has_head(child): if this.has_head(child):
this.del_arc(this.H(child), child) this.del_arc(this.H(child), child)
cdef ArcC arc cdef ArcC arc
@ -346,7 +346,7 @@ cdef cppclass StateC:
this._right_arcs[arc.head].push_back(arc) this._right_arcs[arc.head].push_back(arc)
this._heads[child] = head this._heads[child] = head
inline void map_del_arc(unordered_map[int, vector[ArcC]]* heads_arcs, int h_i, int c_i) nogil: inline void map_del_arc(unordered_map[int, vector[ArcC]]* heads_arcs, int h_i, int c_i) noexcept nogil:
arcs_it = heads_arcs.find(h_i) arcs_it = heads_arcs.find(h_i)
if arcs_it == heads_arcs.end(): if arcs_it == heads_arcs.end():
return return
@ -367,13 +367,13 @@ cdef cppclass StateC:
arc.label = 0 arc.label = 0
break break
inline void del_arc(int h_i, int c_i) nogil: inline void del_arc(int h_i, int c_i) noexcept nogil:
if h_i > c_i: if h_i > c_i:
this.map_del_arc(&this._left_arcs, h_i, c_i) this.map_del_arc(&this._left_arcs, h_i, c_i)
else: else:
this.map_del_arc(&this._right_arcs, h_i, c_i) this.map_del_arc(&this._right_arcs, h_i, c_i)
inline SpanC get_ent() nogil const: inline SpanC get_ent() noexcept nogil const:
cdef SpanC ent cdef SpanC ent
if this._ents.size() == 0: if this._ents.size() == 0:
ent.start = 0 ent.start = 0
@ -383,17 +383,17 @@ cdef cppclass StateC:
else: else:
return this._ents.back() return this._ents.back()
inline void open_ent(attr_t label) nogil: inline void open_ent(attr_t label) noexcept nogil:
cdef SpanC ent cdef SpanC ent
ent.start = this.B(0) ent.start = this.B(0)
ent.label = label ent.label = label
ent.end = -1 ent.end = -1
this._ents.push_back(ent) this._ents.push_back(ent)
inline void close_ent() nogil: inline void close_ent() noexcept nogil:
this._ents.back().end = this.B(0)+1 this._ents.back().end = this.B(0)+1
inline void clone(const StateC* src) nogil: inline void clone(const StateC* src) noexcept nogil:
this.length = src.length this.length = src.length
this._sent = src._sent this._sent = src._sent
this._stack = src._stack this._stack = src._stack

View File

@ -155,7 +155,7 @@ cdef GoldParseStateC create_gold_state(
return gs return gs
cdef void update_gold_state(GoldParseStateC* gs, const StateC* s) nogil: cdef void update_gold_state(GoldParseStateC* gs, const StateC* s) noexcept nogil:
for i in range(gs.length): for i in range(gs.length):
gs.state_bits[i] = set_state_flag( gs.state_bits[i] = set_state_flag(
gs.state_bits[i], gs.state_bits[i],
@ -239,12 +239,12 @@ def _get_aligned_sent_starts(example):
return [None] * len(example.x) return [None] * len(example.x)
cdef int check_state_gold(char state_bits, char flag) nogil: cdef int check_state_gold(char state_bits, char flag) noexcept nogil:
cdef char one = 1 cdef char one = 1
return 1 if (state_bits & (one << flag)) else 0 return 1 if (state_bits & (one << flag)) else 0
cdef int set_state_flag(char state_bits, char flag, int value) nogil: cdef int set_state_flag(char state_bits, char flag, int value) noexcept nogil:
cdef char one = 1 cdef char one = 1
if value: if value:
return state_bits | (one << flag) return state_bits | (one << flag)
@ -252,27 +252,27 @@ cdef int set_state_flag(char state_bits, char flag, int value) nogil:
return state_bits & ~(one << flag) return state_bits & ~(one << flag)
cdef int is_head_in_stack(const GoldParseStateC* gold, int i) nogil: cdef int is_head_in_stack(const GoldParseStateC* gold, int i) noexcept nogil:
return check_state_gold(gold.state_bits[i], HEAD_IN_STACK) return check_state_gold(gold.state_bits[i], HEAD_IN_STACK)
cdef int is_head_in_buffer(const GoldParseStateC* gold, int i) nogil: cdef int is_head_in_buffer(const GoldParseStateC* gold, int i) noexcept nogil:
return check_state_gold(gold.state_bits[i], HEAD_IN_BUFFER) return check_state_gold(gold.state_bits[i], HEAD_IN_BUFFER)
cdef int is_head_unknown(const GoldParseStateC* gold, int i) nogil: cdef int is_head_unknown(const GoldParseStateC* gold, int i) noexcept nogil:
return check_state_gold(gold.state_bits[i], HEAD_UNKNOWN) return check_state_gold(gold.state_bits[i], HEAD_UNKNOWN)
cdef int is_sent_start(const GoldParseStateC* gold, int i) nogil: cdef int is_sent_start(const GoldParseStateC* gold, int i) noexcept nogil:
return check_state_gold(gold.state_bits[i], IS_SENT_START) return check_state_gold(gold.state_bits[i], IS_SENT_START)
cdef int is_sent_start_unknown(const GoldParseStateC* gold, int i) nogil: cdef int is_sent_start_unknown(const GoldParseStateC* gold, int i) noexcept nogil:
return check_state_gold(gold.state_bits[i], SENT_START_UNKNOWN) return check_state_gold(gold.state_bits[i], SENT_START_UNKNOWN)
# Helper functions for the arc-eager oracle # Helper functions for the arc-eager oracle
cdef weight_t push_cost(const StateC* state, const GoldParseStateC* gold) nogil: cdef weight_t push_cost(const StateC* state, const GoldParseStateC* gold) noexcept nogil:
cdef weight_t cost = 0 cdef weight_t cost = 0
b0 = state.B(0) b0 = state.B(0)
if b0 < 0: if b0 < 0:
@ -285,7 +285,7 @@ cdef weight_t push_cost(const StateC* state, const GoldParseStateC* gold) nogil:
return cost return cost
cdef weight_t pop_cost(const StateC* state, const GoldParseStateC* gold) nogil: cdef weight_t pop_cost(const StateC* state, const GoldParseStateC* gold) noexcept nogil:
cdef weight_t cost = 0 cdef weight_t cost = 0
s0 = state.S(0) s0 = state.S(0)
if s0 < 0: if s0 < 0:
@ -296,7 +296,7 @@ cdef weight_t pop_cost(const StateC* state, const GoldParseStateC* gold) nogil:
return cost return cost
cdef bint arc_is_gold(const GoldParseStateC* gold, int head, int child) nogil: cdef bint arc_is_gold(const GoldParseStateC* gold, int head, int child) noexcept nogil:
if is_head_unknown(gold, child): if is_head_unknown(gold, child):
return True return True
elif gold.heads[child] == head: elif gold.heads[child] == head:
@ -305,7 +305,7 @@ cdef bint arc_is_gold(const GoldParseStateC* gold, int head, int child) nogil:
return False return False
cdef bint label_is_gold(const GoldParseStateC* gold, int child, attr_t label) nogil: cdef bint label_is_gold(const GoldParseStateC* gold, int child, attr_t label) noexcept nogil:
if is_head_unknown(gold, child): if is_head_unknown(gold, child):
return True return True
elif label == 0: elif label == 0:
@ -316,7 +316,7 @@ cdef bint label_is_gold(const GoldParseStateC* gold, int child, attr_t label) no
return False return False
cdef bint _is_gold_root(const GoldParseStateC* gold, int word) nogil: cdef bint _is_gold_root(const GoldParseStateC* gold, int word) noexcept nogil:
return gold.heads[word] == word or is_head_unknown(gold, word) return gold.heads[word] == word or is_head_unknown(gold, word)
@ -336,7 +336,7 @@ cdef class Shift:
* Advance buffer * Advance buffer
""" """
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if st.stack_depth() == 0: if st.stack_depth() == 0:
return 1 return 1
elif st.buffer_length() < 2: elif st.buffer_length() < 2:
@ -349,11 +349,11 @@ cdef class Shift:
return 1 return 1
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.push() st.push()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) noexcept nogil:
gold = <const GoldParseStateC*>_gold gold = <const GoldParseStateC*>_gold
return gold.push_cost return gold.push_cost
@ -375,7 +375,7 @@ cdef class Reduce:
cost by those arcs. cost by those arcs.
""" """
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if st.stack_depth() == 0: if st.stack_depth() == 0:
return False return False
elif st.buffer_length() == 0: elif st.buffer_length() == 0:
@ -386,14 +386,14 @@ cdef class Reduce:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
if st.has_head(st.S(0)) or st.stack_depth() == 1: if st.has_head(st.S(0)) or st.stack_depth() == 1:
st.pop() st.pop()
else: else:
st.unshift() st.unshift()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) noexcept nogil:
gold = <const GoldParseStateC*>_gold gold = <const GoldParseStateC*>_gold
if state.is_sent_start(state.B(0)): if state.is_sent_start(state.B(0)):
return 0 return 0
@ -421,7 +421,7 @@ cdef class LeftArc:
pop_cost - Arc(B[0], S[0], label) + (Arc(S[1], S[0]) if H(S[0]) else Arcs(S, S[0])) pop_cost - Arc(B[0], S[0], label) + (Arc(S[1], S[0]) if H(S[0]) else Arcs(S, S[0]))
""" """
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if st.stack_depth() == 0: if st.stack_depth() == 0:
return 0 return 0
elif st.buffer_length() == 0: elif st.buffer_length() == 0:
@ -434,7 +434,7 @@ cdef class LeftArc:
return 1 return 1
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.add_arc(st.B(0), st.S(0), label) st.add_arc(st.B(0), st.S(0), label)
# If we change the stack, it's okay to remove the shifted mark, as # If we change the stack, it's okay to remove the shifted mark, as
# we can't get in an infinite loop this way. # we can't get in an infinite loop this way.
@ -442,7 +442,7 @@ cdef class LeftArc:
st.pop() st.pop()
@staticmethod @staticmethod
cdef inline weight_t cost(const StateC* state, const void* _gold, attr_t label) nogil: cdef inline weight_t cost(const StateC* state, const void* _gold, attr_t label) noexcept nogil:
gold = <const GoldParseStateC*>_gold gold = <const GoldParseStateC*>_gold
cdef weight_t cost = gold.pop_cost cdef weight_t cost = gold.pop_cost
s0 = state.S(0) s0 = state.S(0)
@ -474,7 +474,7 @@ cdef class RightArc:
push_cost + (not shifted[b0] and Arc(B[1:], B[0])) - Arc(S[0], B[0], label) push_cost + (not shifted[b0] and Arc(B[1:], B[0])) - Arc(S[0], B[0], label)
""" """
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if st.stack_depth() == 0: if st.stack_depth() == 0:
return 0 return 0
elif st.buffer_length() == 0: elif st.buffer_length() == 0:
@ -488,12 +488,12 @@ cdef class RightArc:
return 1 return 1
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.add_arc(st.S(0), st.B(0), label) st.add_arc(st.S(0), st.B(0), label)
st.push() st.push()
@staticmethod @staticmethod
cdef inline weight_t cost(const StateC* state, const void* _gold, attr_t label) nogil: cdef inline weight_t cost(const StateC* state, const void* _gold, attr_t label) noexcept nogil:
gold = <const GoldParseStateC*>_gold gold = <const GoldParseStateC*>_gold
cost = gold.push_cost cost = gold.push_cost
s0 = state.S(0) s0 = state.S(0)
@ -525,7 +525,7 @@ cdef class Break:
* Arcs between S and B[1] * Arcs between S and B[1]
""" """
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if st.buffer_length() < 2: if st.buffer_length() < 2:
return False return False
elif st.B(1) != st.B(0) + 1: elif st.B(1) != st.B(0) + 1:
@ -538,11 +538,11 @@ cdef class Break:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.set_sent_start(st.B(1), 1) st.set_sent_start(st.B(1), 1)
@staticmethod @staticmethod
cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* state, const void* _gold, attr_t label) noexcept nogil:
gold = <const GoldParseStateC*>_gold gold = <const GoldParseStateC*>_gold
cdef int b0 = state.B(0) cdef int b0 = state.B(0)
cdef int cost = 0 cdef int cost = 0
@ -785,7 +785,7 @@ cdef class ArcEager(TransitionSystem):
else: else:
return False return False
cdef int set_valid(self, int* output, const StateC* st) nogil: cdef int set_valid(self, int* output, const StateC* st) noexcept nogil:
cdef int[N_MOVES] is_valid cdef int[N_MOVES] is_valid
is_valid[SHIFT] = Shift.is_valid(st, 0) is_valid[SHIFT] = Shift.is_valid(st, 0)
is_valid[REDUCE] = Reduce.is_valid(st, 0) is_valid[REDUCE] = Reduce.is_valid(st, 0)

View File

@ -110,7 +110,7 @@ cdef void update_gold_state(GoldNERStateC* gs, const StateC* state) except *:
cdef do_func_t[N_MOVES] do_funcs cdef do_func_t[N_MOVES] do_funcs
cdef bint _entity_is_sunk(const StateC* state, Transition* golds) nogil: cdef bint _entity_is_sunk(const StateC* state, Transition* golds) noexcept nogil:
if not state.entity_is_open(): if not state.entity_is_open():
return False return False
@ -347,21 +347,21 @@ cdef class BiluoPushDown(TransitionSystem):
cdef class Missing: cdef class Missing:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
return False return False
@staticmethod @staticmethod
cdef int transition(StateC* s, attr_t label) nogil: cdef int transition(StateC* s, attr_t label) noexcept nogil:
pass pass
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
return 9000 return 9000
cdef class Begin: cdef class Begin:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
cdef int preset_ent_iob = st.B_(0).ent_iob cdef int preset_ent_iob = st.B_(0).ent_iob
cdef attr_t preset_ent_label = st.B_(0).ent_type cdef attr_t preset_ent_label = st.B_(0).ent_type
if st.entity_is_open(): if st.entity_is_open():
@ -400,13 +400,13 @@ cdef class Begin:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.open_ent(label) st.open_ent(label)
st.push() st.push()
st.pop() st.pop()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
gold = <GoldNERStateC*>_gold gold = <GoldNERStateC*>_gold
b0 = s.B(0) b0 = s.B(0)
cdef int cost = 0 cdef int cost = 0
@ -439,7 +439,7 @@ cdef class Begin:
cdef class In: cdef class In:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
if not st.entity_is_open(): if not st.entity_is_open():
return False return False
if st.buffer_length() < 2: if st.buffer_length() < 2:
@ -475,12 +475,12 @@ cdef class In:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.push() st.push()
st.pop() st.pop()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
gold = <GoldNERStateC*>_gold gold = <GoldNERStateC*>_gold
cdef int next_act = gold.ner[s.B(1)].move if s.B(1) >= 0 else OUT cdef int next_act = gold.ner[s.B(1)].move if s.B(1) >= 0 else OUT
cdef int g_act = gold.ner[s.B(0)].move cdef int g_act = gold.ner[s.B(0)].move
@ -510,7 +510,7 @@ cdef class In:
cdef class Last: cdef class Last:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
cdef int preset_ent_iob = st.B_(0).ent_iob cdef int preset_ent_iob = st.B_(0).ent_iob
cdef attr_t preset_ent_label = st.B_(0).ent_type cdef attr_t preset_ent_label = st.B_(0).ent_type
if label == 0: if label == 0:
@ -535,13 +535,13 @@ cdef class Last:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.close_ent() st.close_ent()
st.push() st.push()
st.pop() st.pop()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
gold = <GoldNERStateC*>_gold gold = <GoldNERStateC*>_gold
b0 = s.B(0) b0 = s.B(0)
ent_start = s.E(0) ent_start = s.E(0)
@ -581,7 +581,7 @@ cdef class Last:
cdef class Unit: cdef class Unit:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
cdef int preset_ent_iob = st.B_(0).ent_iob cdef int preset_ent_iob = st.B_(0).ent_iob
cdef attr_t preset_ent_label = st.B_(0).ent_type cdef attr_t preset_ent_label = st.B_(0).ent_type
if label == 0: if label == 0:
@ -609,14 +609,14 @@ cdef class Unit:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.open_ent(label) st.open_ent(label)
st.close_ent() st.close_ent()
st.push() st.push()
st.pop() st.pop()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
gold = <GoldNERStateC*>_gold gold = <GoldNERStateC*>_gold
cdef int g_act = gold.ner[s.B(0)].move cdef int g_act = gold.ner[s.B(0)].move
cdef attr_t g_tag = gold.ner[s.B(0)].label cdef attr_t g_tag = gold.ner[s.B(0)].label
@ -646,7 +646,7 @@ cdef class Unit:
cdef class Out: cdef class Out:
@staticmethod @staticmethod
cdef bint is_valid(const StateC* st, attr_t label) nogil: cdef bint is_valid(const StateC* st, attr_t label) noexcept nogil:
cdef int preset_ent_iob = st.B_(0).ent_iob cdef int preset_ent_iob = st.B_(0).ent_iob
if st.entity_is_open(): if st.entity_is_open():
return False return False
@ -658,12 +658,12 @@ cdef class Out:
return True return True
@staticmethod @staticmethod
cdef int transition(StateC* st, attr_t label) nogil: cdef int transition(StateC* st, attr_t label) noexcept nogil:
st.push() st.push()
st.pop() st.pop()
@staticmethod @staticmethod
cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) nogil: cdef weight_t cost(const StateC* s, const void* _gold, attr_t label) noexcept nogil:
gold = <GoldNERStateC*>_gold gold = <GoldNERStateC*>_gold
cdef int g_act = gold.ner[s.B(0)].move cdef int g_act = gold.ner[s.B(0)].move
cdef weight_t cost = 0 cdef weight_t cost = 0

View File

@ -94,7 +94,7 @@ cdef bool _has_head_as_ancestor(int tokenid, int head, const vector[int]& heads)
return False return False
cdef string heads_to_string(const vector[int]& heads) nogil: cdef string heads_to_string(const vector[int]& heads) noexcept nogil:
cdef vector[int].const_iterator citer cdef vector[int].const_iterator citer
cdef string cycle_str cdef string cycle_str

View File

@ -15,22 +15,22 @@ cdef struct Transition:
weight_t score weight_t score
bint (*is_valid)(const StateC* state, attr_t label) nogil bint (*is_valid)(const StateC* state, attr_t label) noexcept nogil
weight_t (*get_cost)(const StateC* state, const void* gold, attr_t label) nogil weight_t (*get_cost)(const StateC* state, const void* gold, attr_t label) noexcept nogil
int (*do)(StateC* state, attr_t label) nogil int (*do)(StateC* state, attr_t label) noexcept nogil
ctypedef weight_t (*get_cost_func_t)( ctypedef weight_t (*get_cost_func_t)(
const StateC* state, const void* gold, attr_tlabel const StateC* state, const void* gold, attr_tlabel
) nogil ) noexcept nogil
ctypedef weight_t (*move_cost_func_t)( ctypedef weight_t (*move_cost_func_t)(
const StateC* state, const void* gold const StateC* state, const void* gold
) nogil ) noexcept nogil
ctypedef weight_t (*label_cost_func_t)( ctypedef weight_t (*label_cost_func_t)(
const StateC* state, const void* gold, attr_t label const StateC* state, const void* gold, attr_t label
) nogil ) noexcept nogil
ctypedef int (*do_func_t)(StateC* state, attr_t label) nogil ctypedef int (*do_func_t)(StateC* state, attr_t label) noexcept nogil
ctypedef void* (*init_state_t)(Pool mem, int length, void* tokens) except NULL ctypedef void* (*init_state_t)(Pool mem, int length, void* tokens) except NULL
@ -53,7 +53,7 @@ cdef class TransitionSystem:
cdef Transition init_transition(self, int clas, int move, attr_t label) except * cdef Transition init_transition(self, int clas, int move, attr_t label) except *
cdef int set_valid(self, int* output, const StateC* st) nogil cdef int set_valid(self, int* output, const StateC* st) noexcept nogil
cdef int set_costs(self, int* is_valid, weight_t* costs, cdef int set_costs(self, int* is_valid, weight_t* costs,
const StateC* state, gold) except -1 const StateC* state, gold) except -1

View File

@ -149,7 +149,7 @@ cdef class TransitionSystem:
action = self.lookup_transition(move_name) action = self.lookup_transition(move_name)
return action.is_valid(stcls.c, action.label) return action.is_valid(stcls.c, action.label)
cdef int set_valid(self, int* is_valid, const StateC* st) nogil: cdef int set_valid(self, int* is_valid, const StateC* st) noexcept nogil:
cdef int i cdef int i
for i in range(self.n_moves): for i in range(self.n_moves):
is_valid[i] = self.c[i].is_valid(st, self.c[i].label) is_valid[i] = self.c[i].is_valid(st, self.c[i].label)

View File

@ -19,7 +19,7 @@ cdef class Parser(TrainablePipe):
StateC** states, StateC** states,
WeightsC weights, WeightsC weights,
SizesC sizes SizesC sizes
) nogil ) noexcept nogil
cdef void c_transition_batch( cdef void c_transition_batch(
self, self,
@ -27,4 +27,4 @@ cdef class Parser(TrainablePipe):
const float* scores, const float* scores,
int nr_class, int nr_class,
int batch_size int batch_size
) nogil ) noexcept nogil

View File

@ -316,7 +316,7 @@ cdef class Parser(TrainablePipe):
cdef void _parseC( cdef void _parseC(
self, CBlas cblas, StateC** states, WeightsC weights, SizesC sizes self, CBlas cblas, StateC** states, WeightsC weights, SizesC sizes
) nogil: ) noexcept nogil:
cdef int i cdef int i
cdef vector[StateC*] unfinished cdef vector[StateC*] unfinished
cdef ActivationsC activations = alloc_activations(sizes) cdef ActivationsC activations = alloc_activations(sizes)
@ -359,7 +359,7 @@ cdef class Parser(TrainablePipe):
const float* scores, const float* scores,
int nr_class, int nr_class,
int batch_size int batch_size
) nogil: ) noexcept nogil:
# n_moves should not be zero at this point, but make sure to avoid zero-length mem alloc # n_moves should not be zero at this point, but make sure to avoid zero-length mem alloc
with gil: with gil:
assert self.moves.n_moves > 0, Errors.E924.format(name=self.name) assert self.moves.n_moves > 0, Errors.E924.format(name=self.name)

View File

@ -867,11 +867,11 @@ cdef extern from "<algorithm>" namespace "std" nogil:
bint (*)(SpanC, SpanC)) bint (*)(SpanC, SpanC))
cdef bint len_start_cmp(SpanC a, SpanC b) nogil: cdef bint len_start_cmp(SpanC a, SpanC b) noexcept nogil:
if a.end - a.start == b.end - b.start: if a.end - a.start == b.end - b.start:
return b.start < a.start return b.start < a.start
return a.end - a.start < b.end - b.start return a.end - a.start < b.end - b.start
cdef bint start_cmp(SpanC a, SpanC b) nogil: cdef bint start_cmp(SpanC a, SpanC b) noexcept nogil:
return a.start < b.start return a.start < b.start

View File

@ -7,8 +7,8 @@ from ..typedefs cimport attr_t
from ..vocab cimport Vocab from ..vocab cimport Vocab
cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) nogil cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) noexcept nogil
cdef attr_t get_token_attr_for_matcher(const TokenC* token, attr_id_t feat_name) nogil cdef attr_t get_token_attr_for_matcher(const TokenC* token, attr_id_t feat_name) noexcept nogil
ctypedef const LexemeC* const_Lexeme_ptr ctypedef const LexemeC* const_Lexeme_ptr

View File

@ -71,7 +71,7 @@ cdef int bounds_check(int i, int length, int padding) except -1:
raise IndexError(Errors.E026.format(i=i, length=length)) raise IndexError(Errors.E026.format(i=i, length=length))
cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) nogil: cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) noexcept nogil:
if feat_name == LEMMA: if feat_name == LEMMA:
return token.lemma return token.lemma
elif feat_name == NORM: elif feat_name == NORM:
@ -106,7 +106,7 @@ cdef attr_t get_token_attr(const TokenC* token, attr_id_t feat_name) nogil:
return Lexeme.get_struct_attr(token.lex, feat_name) return Lexeme.get_struct_attr(token.lex, feat_name)
cdef attr_t get_token_attr_for_matcher(const TokenC* token, attr_id_t feat_name) nogil: cdef attr_t get_token_attr_for_matcher(const TokenC* token, attr_id_t feat_name) noexcept nogil:
if feat_name == SENT_START: if feat_name == SENT_START:
if token.sent_start == 1: if token.sent_start == 1:
return True return True

View File

@ -33,7 +33,7 @@ cdef class Token:
cpdef bint check_flag(self, attr_id_t flag_id) except -1 cpdef bint check_flag(self, attr_id_t flag_id) except -1
@staticmethod @staticmethod
cdef inline attr_t get_struct_attr(const TokenC* token, attr_id_t feat_name) nogil: cdef inline attr_t get_struct_attr(const TokenC* token, attr_id_t feat_name) noexcept nogil:
if feat_name < (sizeof(flags_t) * 8): if feat_name < (sizeof(flags_t) * 8):
return Lexeme.c_check_flag(token.lex, feat_name) return Lexeme.c_check_flag(token.lex, feat_name)
elif feat_name == LEMMA: elif feat_name == LEMMA:
@ -70,7 +70,7 @@ cdef class Token:
@staticmethod @staticmethod
cdef inline attr_t set_struct_attr(TokenC* token, attr_id_t feat_name, cdef inline attr_t set_struct_attr(TokenC* token, attr_id_t feat_name,
attr_t value) nogil: attr_t value) noexcept nogil:
if feat_name == LEMMA: if feat_name == LEMMA:
token.lemma = value token.lemma = value
elif feat_name == NORM: elif feat_name == NORM:
@ -99,9 +99,9 @@ cdef class Token:
token.sent_start = value token.sent_start = value
@staticmethod @staticmethod
cdef inline int missing_dep(const TokenC* token) nogil: cdef inline int missing_dep(const TokenC* token) noexcept nogil:
return token.dep == MISSING_DEP return token.dep == MISSING_DEP
@staticmethod @staticmethod
cdef inline int missing_head(const TokenC* token) nogil: cdef inline int missing_head(const TokenC* token) noexcept nogil:
return Token.missing_dep(token) return Token.missing_dep(token)