-
Notifications
You must be signed in to change notification settings - Fork 222
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge pull request #51 #53
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
b9ffae3
Added interface for auxiliary labels
danpovey a9e1768
Add notes on Python interface
danpovey 9f76458
Fix typos
danpovey 09915bb
Merge master
danpovey 10e6712
Fix conflicts, remove some typedefs
danpovey 735f83d
Fixes from review
danpovey 80339a5
Small fixes in determinization code
danpovey 14b50b1
Merge remote-tracking branch 'upstream/master' into aux_labels
danpovey 3afad81
Progress on determinization code; add new declarations of un-pruned f…
danpovey f78f328
Merge remote-tracking branch 'upstream/master' into aux_labels
danpovey 14a7cca
Fix compile error
danpovey 3167864
Resolve conflicts
danpovey c8378d4
Add LogAdd
danpovey 04fafe3
Fix compile errors in util.h
danpovey 908457b
More progress on determinization code
danpovey 9218fc1
Merge master
danpovey 3ed45fa
More progress on determinizaton draft.
danpovey a79f527
More work on Determinize code.
danpovey 151adbb
Draft of ConstFsa interface and CfsaVec
danpovey 8f729e3
Add interface for ConstFsa; more notes on python interface
danpovey ff625fd
Small fixes to ConstFsa interface
danpovey 283ec84
Changes from review
danpovey d9ebcd4
Merge remote-tracking branch 'upstream/master' into cfsa_etc
danpovey 96a4bd5
Fix style issues
danpovey 7e54a4d
Add itf for DenseFsa (not compiled)
danpovey 4407079
Merge remote-tracking branch 'upstream/master' into cfsa_etc
danpovey fd34f65
Draft of Array2/Array3
danpovey 97d1496
Merge master
danpovey f4c6f9f
Some notes on how this would work in Python
danpovey 1a12dc3
Fix conflict
danpovey 8e8b74d
[src] More drafts in array stuff, RE interface of functions.
danpovey 18823fe
Further changes
danpovey 37aafd8
Merge branch 'master'
qindazhu 4dbfb57
merge Dan's PR about Array2
qindazhu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// k2/csrc/array.h | ||
|
||
// Copyright (c) 2020 Xiaomi Corporation (author: Daniel Povey) | ||
|
||
// See ../../LICENSE for clarification regarding multiple authors | ||
|
||
#ifndef K2_CSRC_ARRAY_H_ | ||
#define K2_CSRC_ARRAY_H_ | ||
|
||
#include <functional> | ||
#include <limits> | ||
#include <memory> | ||
#include <vector> | ||
|
||
namespace k2 { | ||
|
||
/* | ||
We will use e.g. StridedPtr<int32_t, T> when the stride is not 1, and | ||
otherwise just T* (which presumably be faster). | ||
*/ | ||
template <typename I, typename T> | ||
struct StridedPtr { | ||
T *data; | ||
I stride; | ||
T &operator[](I i) { return data[i]; } | ||
StridedPtr(T *data, I stride) : data(data), stride(stride) {} | ||
}; | ||
|
||
/* MIGHT NOT NEED THIS */ | ||
template <typename I, typename Ptr> | ||
struct Array1 { | ||
// Irregular two dimensional array of something, like vector<vector<X> > | ||
// where Ptr is, or behaves like, X*. | ||
using IndexT = I; | ||
using PtrT = Ptr; | ||
|
||
// 'begin' and 'end' are the first and one-past-the-last indexes into `data` | ||
// that we are allowed to use. | ||
IndexT begin; | ||
IndexT end; | ||
|
||
PtrT data; | ||
}; | ||
|
||
/* | ||
This struct stores the size of an Array2 object; it will generally be used as | ||
an output argument by functions that work out this size. | ||
*/ | ||
template <typename I> | ||
struct Array2Size { | ||
using IndexT = I; | ||
// `size1` is the top-level size of the array, equal to the object's .size | ||
// element | ||
I size1; | ||
// `size2` is the nunber of elements in the array, equal to | ||
// o->indexes[o->size] - o->indexes[0] (if the Array2 object o is | ||
// initialized). | ||
I size2; | ||
}; | ||
|
||
template <typename I, typename Ptr> | ||
struct Array2 { | ||
// Irregular two dimensional array of something, like vector<vector<X> > | ||
// where Ptr is, or behaves like, X*. | ||
using IndexT = I; | ||
using PtrT = Ptr; | ||
|
||
IndexT size; | ||
const IndexT *indexes; // indexes[0,1,...size] should be defined; note, this | ||
// means the array must be of at least size+1. We | ||
// require that indexes[i] <= indexes[i+1], but it is | ||
// not required that indexes[0] == 0, it may be | ||
// greater than 0. | ||
|
||
PtrT data; // `data` might be an actual pointer, or might be some object | ||
// supporting operator []. data[indexes[0]] through | ||
// data[indexes[size] - 1] must be accessible through this | ||
// object. | ||
|
||
/* initialized definition: | ||
|
||
An Array2 object is initialized if its `size` member is set and its | ||
`indexes` and `data` pointer allocated, and the values of its `indexes` | ||
array are set for indexes[0] and indexes[size]. | ||
*/ | ||
}; | ||
|
||
template <typename I, typename Ptr> | ||
struct Array3 { | ||
// Irregular three dimensional array of something, like vector<vector<vetor<X> | ||
// > > where Ptr is or behaves like X*. | ||
using IndexT = I; | ||
using PtrT = Ptr; | ||
|
||
IndexT size; | ||
const IndexT *indexes1; // indexes1[0,1,...size] should be defined; note, | ||
// this means the array must be of at least size+1. | ||
// We require that indexes[i] <= indexes[i+1], but it | ||
// is not required that indexes[0] == 0, it may be | ||
// greater than 0. | ||
|
||
const IndexT *indexes2; // indexes2[indexes1[0]] | ||
// .. indexes2[indexes1[size]-1] should be defined. | ||
|
||
Ptr data; // `data` might be an actual pointer, or might be some object | ||
// supporting operator []. data[indexes[0]] through | ||
// data[indexes[size] - 1] must be accessible through this | ||
// object. | ||
|
||
Array2<I, Ptr> operator[](I i) { | ||
// TODO(haowen): fill real data here | ||
Array2<I, Ptr> array; | ||
return array; | ||
} | ||
}; | ||
|
||
// Note: we can create Array4 later if we need it. | ||
|
||
} // namespace k2 | ||
|
||
#endif // K2_CSRC_ARRAY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
|
||
|
||
|
||
|
||
# defining type k2.Array | ||
|
||
|
||
class Array: | ||
|
||
# `indexes` is a Tensor with one | ||
Tensor indexes; | ||
|
||
# `data` is either: | ||
# - of type Tensor (if this corresponds to Array2 == 2-dimensional | ||
# array in C++) | ||
# - of type Array (if this corresponds to Array3 or higher-dimensional | ||
# array in C++) | ||
# The Python code is structured a bit differently from the C++ code, | ||
# due to the differences in the languages. | ||
# When we dispatch things to C++ code there would be some | ||
# big switch statement or if-statement to select the right | ||
# template instantiation. | ||
data; | ||
|
||
def __len__(self): | ||
return indexes.shape[0] - 1 | ||
|
||
@property | ||
def shape(self): | ||
# e.g. if indexes.shape is (15,) and | ||
# data.shape is (150) -> this.shape would be (15,None) | ||
# If data.shape is (150,4), this.shape would be (15,4) | ||
# If data.shape is (150,None) (since data is an Array), this.shape | ||
# would be (150,None,None). | ||
# The Nones are for dimensions where the shape is not known | ||
# because it is variable. | ||
return (indexes.shape[0] - 1, None, *data.shape[1:]) | ||
|
||
|
||
|
||
class Fsa(Array): | ||
|
||
# Think of this as a vector of vector of Arc, or in C++, | ||
# an Array2<Arc>. | ||
# An Arc has 3 int32_t's, so this.data is a Tensor with | ||
# dtype int32 and shape (_, 3). | ||
|
||
|
||
|
||
class FsaVec(Array): | ||
|
||
# Think of this as a vector of vector of vector of Arc, or in C++, | ||
# an Array3<Arc>. | ||
# | ||
# this.data is an Array, and this.data.data is a Tensor with | ||
# dtype int32 and shape (_, 3). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comments are confusing compared to
return (indexes.shape[0] - 1, None, *data.shape[1:])
.Could you please make it clearer? @danpovey
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think indexes.shape should be 16 here. Can you please edit though? Sorry I am a bit busy today. I am hoping @qindazhu can have a look at your PR. Thanks so much!!