Skip to content

Commit

Permalink
compiles
Browse files Browse the repository at this point in the history
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@626 ec762483-ff6d-05da-a07a-a48fb63a330f
  • Loading branch information
graehl committed Aug 27, 2010
1 parent 649a4ea commit 04590b8
Show file tree
Hide file tree
Showing 17 changed files with 563 additions and 169 deletions.
342 changes: 266 additions & 76 deletions decoder/apply_fsa_models.cc

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions decoder/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ struct CFG {
void print_nt_name(std::ostream &o,NTHandle n) const {
o << nts[n].from << n;
}
std::string nt_name(NTHandle n) const {
std::ostringstream o;
print_nt_name(o,n);
return o.str();
}
void print_rhs_name(std::ostream &o,WordID w) const {
if (w<=0) print_nt_name(o,-w);
else o<<TD::Convert(w);
}
std::string rhs_name(WordID w) const {
if (w<=0) return nt_name(-w);
else return TD::Convert(w);
}
static void static_print_rhs_name(std::ostream &o,WordID w) {
if (w<=0) o<<'['<<-w<<']';
else o<<TD::Convert(w);
}
static std::string static_rhs_name(WordID w) {
std::ostringstream o;
static_print_rhs_name(o,w);
return o.str();
}

typedef std::pair<WordID,WordID> BinRhs;

Expand Down
3 changes: 2 additions & 1 deletion decoder/ff.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# define DBGINIT(a)
#endif

#include <stdint.h>
#include <vector>
#include <cstring>
#include "fdict.h"
Expand Down Expand Up @@ -243,7 +244,7 @@ void show_all_features(std::vector<FFp> const& models_,DenseWeightVector &weight
return show_features(all_features(models_,weights_,&warn,warn_fid_0),weights_,out,warn,warn_zero_wt);
}

typedef ValueArray<char> FFState; // this is about 10% faster than string.
typedef ValueArray<uint8_t> FFState; // this is about 10% faster than string.
//typedef std::string FFState;

//FIXME: only context.data() is required to be contiguous, and it becomes invalid after next string operation. use ValueArray instead? (higher performance perhaps, save a word due to fixed size)
Expand Down
6 changes: 3 additions & 3 deletions decoder/ff_fsa_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct FsaFeatureFunctionData
return o;
}

FsaFeatureFunctionData(int statesz=0,Sentence const& end_sentence_phrase=Sentence()) : ssz(statesz),start(statesz),h_start(statesz),end_phrase_(end_sentence_phrase) {
FsaFeatureFunctionData(int statesz=0,Sentence const& end_sentence_phrase=Sentence()) : start(statesz),h_start(statesz),end_phrase_(end_sentence_phrase),ssz(statesz) {
debug_=true;
sync_to_=0;
}
Expand Down Expand Up @@ -86,10 +86,10 @@ struct FsaFeatureFunctionData
}

Features features_;
protected:
int ssz; // don't forget to set this. default 0 (it may depend on params of course)
Bytes start,h_start; // start state and estimated-features (heuristic) start state. set these. default empty.
Sentence end_phrase_; // words appended for final traversal (final state cost is assessed using Scan) e.g. "</s>" for lm.
protected:
int ssz; // don't forget to set this. default 0 (it may depend on params of course)
// this can be called instead or after constructor (also set bytes and end_phrase_)
void set_state_bytes(int sb=0) {
if (start.size()!=sb) start.resize(sb);
Expand Down
86 changes: 52 additions & 34 deletions utils/agenda.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef AGENDA_H
#define AGENDA_H

#define DBG_AGENDA(x) x
/*
a priority queue where you expect to queue the same item at different
priorities several times before finally popping it. higher priority = better.
Expand Down Expand Up @@ -48,54 +49,71 @@ template <class P>
struct priority_traits {
typedef typename P::priority_type priority_type;
};
// P p has priority_traits<P>::type &p->agenda_priority() and unsigned &p->agenda_location(), and bool & p->agenda_done()
// this is really 4 functors in 1 (or lvalue property maps); you can supply your own as the Prio type in Agenda<...> below ; state is allowed.
template <class P>
struct AdjustablePriority {
typedef AdjustablePriority<P> Self;
typedef typename priority_traits<P>::priority_type Priority;
Priority & priority(P const &x) {
return x->agenda_priority();
}
unsigned & location(P const &x) { // this gets updated by push, pop, and adjust
return x->agenda_location();
}
void is_done(P const& x) const {
return x->agenda_done();
}
void set_done(P const& x) const {
x->agenda_done()=true;
}
};
*/

typedef best_t agenda_best_t;
typedef unsigned agenda_location_t;

PMAP_MEMBER_INDIRECT(LocationMap,unsigned,location)
PMAP_MEMBER_INDIRECT(PriorityMap,best_t,priority)
PMAP_MEMBER_INDIRECT(LocationMap,agenda_location_t,location)
PMAP_MEMBER_INDIRECT(PriorityMap,agenda_best_t,priority)

struct Less {
typedef bool result_type;
template <class A,class B>
bool operator()(A const& a,B const& b) const { return a<b; }
};

// LocMap and PrioMap are boost property maps put(locmap,key,size_t), Better(get(priomap,k1),get(priomap,k2)) means k1 should be above k2 (be popped first). Locmap and PrioMap may have state; the rest are assumed stateless functors
template <class Item,class Better=std::less<Item>, /* intern_pool args */ class KeyF=get_key<Item>,class HashKey=boost::hash<typename KeyF::return_type>,class EqKey=std::equal_to<typename KeyF::return_type>, class Pool=boost::object_pool<Item> >
struct Agenda : intern_pool<Item,KeyF,EqKey,Pool> {
template <class Item,class Better=Less, /* intern_pool args */ class KeyF=get_key<Item>,class HashKey=boost::hash<typename KeyF::result_type>,class EqKey=std::equal_to<typename KeyF::result_type>, class Pool=boost::object_pool<Item> >
struct Agenda : intern_pool<Item,KeyF,HashKey,EqKey,Pool> {
typedef intern_pool<Item,KeyF,HashKey,EqKey,Pool> Intern; // inherited because I want to use construct()
/* this is less generic than it could be, because I want to use a single hash mapping to intern to canonical mutable object pointers, where the property maps are just lvalue accessors */
typedef intern_pool<Item,KeyF,EqKey,Pool> Intern; // inherited because I want to use construct()
typedef typename KeyF::result_type Key;
typedef Item * Handle;
typedef LocationMap<Handle> LocMap;
typedef PriorityMap<Handle> PrioMap;
LocMap locmap;
PrioMap priomap;
//NOT NEEDED: initialize function object state (there is none)
PrioMap priomap; // note: priomap[item] is set by caller before giving us the item; then tracks best (for canonicalized item) thereafter

Better better;
//NOT NEEDED: initialize function object state (there is none)

/*
typedef Item *CanonItem;
typedef Item *ItemC; //canonicalized pointer
typedef Item *ItemP;
static const std::size_t heap_arity=4; // might be fastest possible (depends on key size probably - cache locality is bad w/ arity=2)
typedef std::vector<CanonItem> HeapStorage;
typedef boost::detail::d_ary_heap_indirect<Key,heap_arity,LocMap,PrioMap,Better,HeapStorage> Heap;
HASH_SET<Key,Hash,Equal> queued;
Agenda(LocMap const& lm=LocMap(),PrioMap const& pm=PrioMap()) : locmap(lm), priomap(pm) { }
*/
typedef std::vector<ItemC> HeapStorage;
typedef d_ary_heap_indirect<Handle,heap_arity,LocMap,PrioMap,Better,HeapStorage,agenda_location_t> Heap;
Heap q;

// please don't call q.push etc. directly.
void add(ItemP i) {
bool fresh=interneq(i);
DBG_AGENDA(assert(fresh && !q.contains(i)));
q.push(i);
}
bool improve(ItemP i) {
ItemP c=i;
bool fresh=interneq(c);
if (fresh)
return add(c);
DBG_AGENDA(assert(q.contains(c)));
return q.maybe_improve(priomap[i]);
}
inline bool empty() {
return q.empty();
}
// no need to destroy the canon. item because we want to remember the best cost and reject more expensive ways of using it).
ItemC pop() {
DBG_AGENDA(assert(!empty()));
ItemC r=q.top();
q.pop();
return r;
}
agenda_best_t best() const {
return priomap[q.top()]; //TODO: cache/track the global best?
}

Agenda(unsigned reserve=1000000,LocMap const& lm=LocMap(),PrioMap const& pm=PrioMap(),EqKey const& eq=EqKey(),Better const& better=Better()) : locmap(lm), priomap(pm), better(better), q(priomap,locmap,better,reserve) { }
};

#endif
15 changes: 15 additions & 0 deletions utils/best.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@ typedef MaxPlus<double> best_t;
inline bool operator <(best_t const& a,best_t const& b) {
return a.v_>b.v_; // intentionally reversed, so default min-heap, sort, etc. put best first.
}
struct BetterP {
inline bool operator ()(best_t const& a,best_t const& b) const {
return a.v_>b.v_; // intentionally reversed, so default min-heap, sort, etc. put best first.
}
};

inline void maybe_improve(best_t &a,best_t const& b) {
if (a.v_>b.v_)
a.v_=b.v_;
}

template <class O>
inline void maybe_improve(best_t &a,O const& b) {
if (a.v_>b.v_)
a.v_=b.v_;
}

#endif
Loading

0 comments on commit 04590b8

Please sign in to comment.