2using System.Collections.Generic;
16using System.Security.Cryptography;
18using System.IO.Compression;
35 Random m_random =
new Random();
40 Stopwatch m_swUpdateTimer =
new Stopwatch();
41 double m_dfLastProgress = 0;
42 AutoResetEvent m_evtDownloadDone =
new AutoResetEvent(
false);
47 public enum VOCABULARY
85 m_evtCancel = evtCancel;
89 m_blobTriangle.
Name =
"triangle";
114 col.
Add(m_blobTriangle);
143 download_vocab_data();
160 private void download_vocab_data()
165 string strProgData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
169 if (
string.IsNullOrEmpty(strDataFile))
170 m_log.
FAIL(
"You must specify a 'vocab_data_dst_file' when using 'vocab_data_url'.");
172 string strPath = Path.GetDirectoryName(strDataFile);
173 if (!Directory.Exists(strPath))
174 Directory.CreateDirectory(strPath);
176 string strFile1 = Path.GetFileName(strDataFile);
178 if (!File.Exists(strFile))
181 if (!File.Exists(strVocabFile))
182 ZipFile.ExtractToDirectory(strFile, strPath);
185 private string downloadData(
string strUrl,
string strPath,
string strFileName)
187 if (!Directory.Exists(strPath))
188 Directory.CreateDirectory(strPath);
190 string strDataFile = strPath +
"\\" + strFileName;
191 if (!File.Exists(strDataFile))
193 using (WebClient webClient =
new WebClient())
195 string strFile1 = strFileName;
196 string strFile = strPath +
"\\" + strFile1;
198 m_swUpdateTimer.Start();
199 m_dfLastProgress = 0;
201 webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
202 webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
203 webClient.DownloadFileAsync(
new Uri(strUrl), strFile, strFile1);
205 m_evtDownloadDone.WaitOne();
212 private void WebClient_DownloadFileCompleted(
object sender,
System.
ComponentModel.AsyncCompletedEventArgs e)
215 m_evtDownloadDone.Set();
218 private void WebClient_DownloadProgressChanged(
object sender, DownloadProgressChangedEventArgs e)
220 if (m_swUpdateTimer.Elapsed.TotalMilliseconds >= 1000)
222 if (m_dfLastProgress != e.ProgressPercentage)
224 m_dfLastProgress = e.ProgressPercentage;
225 string strFile = e.UserState.ToString();
227 double dfPct = e.ProgressPercentage / 100.0;
228 m_log.
WriteLine(
"Downloading '" + strFile +
"' at " + dfPct.ToString(
"P") +
"...",
true);
231 m_swUpdateTimer.Restart();
244 int nTokenSize = (int)m_encoderData.
TokenSize;
255 Blob<T> blobDecOut = colTop[2];
256 Blob<T> blobEncMask = colTop[3];
257 Blob<T> blobDecMask = colTop[4];
262 int[] rgShape =
new int[nCount];
267 rgShape[0] = nBatchSize;
268 rgShape[1] = nBlockSize;
269 if (rgShape.Length > 2)
270 rgShape[2] = nTokenSize;
275 blobEncMask.
Reshape(nBatchSize, nBlockSize, 1, 1);
276 blobDecMask.
Reshape(nBatchSize, nBlockSize, nBlockSize, 1);
282 T[] rgMask =
new T[m_blobTriangle.
count()];
283 for (
int n = 0; n < m_blobTriangle.
num; n++)
285 for (
int c = 0; c < m_blobTriangle.
channels; c++)
287 for (
int h = 0; h < m_blobTriangle.
height; h++)
289 int nIdx = n * nBlockSize * nBlockSize + c * nBlockSize + h;
359 colTop[0].CopyFromAndPad(colBottom[0]);
360 colTop[1].CopyFromAndPad(colBottom[1]);
369 colTop[0].mutable_cpu_data =
convert(encData.Item1);
370 colTop[1].mutable_cpu_data =
convert(decData.Item1);
371 colTop[2].mutable_cpu_data =
convert(decData.Item2);
375 m_cuda.sign(colTop[0].count(), colTop[0].gpu_data, colTop[3].mutable_gpu_data);
377 m_cuda.channel_duplicate(colTop[4].count(), colTop[1].num, colTop[1].channels, colTop[4].count(2), colTop[1].gpu_data, colTop[4].mutable_gpu_data);
378 m_cuda.sign(colTop[4].count(), colTop[4].gpu_data, colTop[4].mutable_gpu_data);
380 m_cuda.mul(colTop[4].count(), colTop[4].gpu_data, m_blobTriangle.
gpu_data, colTop[4].mutable_gpu_data);
410 public List<int>
Tokenize(
string str, VOCABULARY vocab)
412 if (vocab == VOCABULARY.ENCODER)
413 return m_encoderData.
Tokenize(str,
false,
false);
415 return m_decoderData.
Tokenize(str,
false,
false);
426 public string Detokenize(
float[] rg,
int nStartIdx,
int nCount, VOCABULARY vocab)
428 InputData inputData = (vocab == VOCABULARY.ENCODER) ? m_encoderData : m_decoderData;
429 return inputData.
Detokenize(rg, nStartIdx, nCount,
false,
false);
439 InputData input = (src == VOCABULARY.ENCODER) ? m_encoderData : m_decoderData;
454 if (m_blobEncIn ==
null)
457 if (m_blobDecIn ==
null)
460 string strInput = customInput.
GetProperty(
"InputData");
461 if (
string.IsNullOrEmpty(strInput))
462 throw new Exception(
"Could not find 'InputData' property!");
464 List<int> rgTokens = m_encoderData.
Tokenize(strInput,
true,
true);
465 float[] rgInput =
new float[rgTokens.Count];
467 for (
int i = 0; i < rgTokens.Count; i++)
469 rgInput[i] = rgTokens[i];
472 int[] rgShape =
new int[2];
474 rgShape[1] = rgInput.Length;
499 Blob<T> blobBtm = (colBottom.Count > 1) ? colBottom[1] : colBottom[0];
503 rgTok.Add(nTokIdx.Value);
507 List<int> rgShape =
Utility.Clone<
int>(blobBtm.
shape());
508 rgShape[1] = rgTok.
Count;
531 int nVocabCount = blobLogits.
count(nAxis);
532 float[] rgLogits =
new float[nVocabCount];
533 Dictionary<int, float> rgTopK =
new Dictionary<int, float>();
544 if (m_softmax ==
null)
549 m_softmax.
Setup(colBottom, colTop);
555 if (m_argmax ==
null)
562 softmax.
Reshape(colBottom, colTop);
564 colBottom[0] = m_blobX;
565 m_argmax.
Setup(colBottom, colTop);
568 colBottom[0] = blobLogits;
569 softmax.
Forward(colBottom, colTop);
571 colBottom[0] = m_blobX;
572 m_argmax.
Forward(colBottom, colTop);
574 float[] rgArgMax =
convertF(colTop[0].mutable_cpu_data);
575 int nCharIdx = (int)rgArgMax[nCurIdx];
577 string str = m_decoderData.
Detokenize(nCharIdx,
true,
true);
580 return new List<Tuple<string, int, double>>() {
new Tuple<string, int, double>(str, nCharIdx, 0) };
593 foreach (
float fTok
in rgData)
598 strOut += m_decoderData.
Detokenize((
int)fTok,
true,
true);
610 List<string> m_rgstrData =
new List<string>();
611 List<Tuple<int[], int[]>> m_rgnData =
new List<Tuple<int[], int[]>>();
613 float[] m_rgData =
null;
614 float[] m_rgTgt =
null;
648 Stopwatch sw =
new Stopwatch();
657 string strProgData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
660 string[] rgstr = File.ReadAllLines(strSrcFile);
664 for (
int i = 0; i < rgstr.Length; i++)
666 m_rgstrData.Add(rgstr[i]);
667 m_vocab.Add(rgstr[i]);
669 if (sw.Elapsed.TotalMilliseconds > 1000)
672 double dfPct = (double)i/(
double)rgstr.Length;
674 m_log.
WriteLine(
"Loading vocabulary " + i.ToString(
"N0") +
" of " + rgstr.Length.ToString(
"N0") +
" (" + dfPct.ToString(
"P") +
") ...",
true);
680 for (
int i = 0; i < m_rgstrData.Count; i++)
682 string str = m_rgstrData[i];
683 int[] rgnSrc = m_vocab.Tokenize(str, bIncludeTarget, !bIncludeTarget);
687 rgnTrg = m_vocab.CreateTarget(rgnSrc);
689 m_rgnData.Add(
new Tuple<
int[],
int[]>(rgnSrc, rgnTrg));
691 if (sw.Elapsed.TotalMilliseconds > 1000)
694 double dfPct = (double)i / (
double)m_rgstrData.Count;
696 m_log.
WriteLine(
"Tokenizing data " + i.ToString(
"N0") +
" of " + m_rgstrData.Count.ToString(
"N0") +
" (" + dfPct.ToString(
"P") +
") ...",
true);
700 m_log.
WriteLine(
"'" + strSrcFile +
"' vocabulary size = " + m_vocab.Count.ToString(
"N0"));
708 get {
return m_rgstrData; }
724 get {
return (uint)m_vocab.Count; }
736 if (bIncludeSrc && m_rgnData[nIdx].Item1.Length == 0)
739 if (bIncludeTrg && m_rgnData[nIdx].Item2.Length == 0)
754 public override Tuple<float[], float[]>
GetData(
int nBatchSize,
int nBlockSize,
InputData trgData, out
int[] rgnIdx)
756 int nSize = nBatchSize * nBlockSize;
758 if (m_rgData ==
null || m_rgData.Length != nSize)
759 m_rgData =
new float[nSize];
761 Array.Clear(m_rgData, 0, m_rgData.Length);
763 if (m_rgTgt ==
null || m_rgTgt.Length != nSize)
764 m_rgTgt =
new float[nSize];
766 Array.Clear(m_rgTgt, 0, m_rgTgt.Length);
768 rgnIdx =
new int[nBatchSize];
770 for (
int i = 0; i < nBatchSize; i++)
772 int nDataIdx =
m_random.Next(m_rgnData.Count);
773 int[] rgSrc = m_rgnData[nDataIdx].Item1;
778 nDataIdx =
m_random.Next(m_rgnData.Count);
779 rgSrc = m_rgnData[nDataIdx].Item1;
783 throw new Exception(
"Could not find a non-empty source data item!");
786 int[] rgTrg = m_rgnData[nDataIdx].Item2;
787 int nDstIdx = i * nBlockSize;
789 rgnIdx[i] = nDataIdx;
792 for (
int j = 0; j < nBlockSize; j++)
794 if (j < rgSrc.Length)
795 m_rgData[nDstIdx + j] = rgSrc[j];
797 if (rgTrg !=
null && j < rgTrg.Length)
798 m_rgTgt[nDstIdx + j] = rgTrg[j];
802 rgTrg[rgTrg.Length - 1] ==
EOS &&
803 m_rgTgt[nDstIdx + nBlockSize - 1] != 0 &&
804 m_rgTgt[nDstIdx + nBlockSize - 1] !=
EOS)
805 m_rgTgt[nDstIdx + nBlockSize - 1] =
EOS;
807 if (rgSrc[rgSrc.Length - 1] ==
EOS &&
808 m_rgData[nDstIdx + nBlockSize - 1] != 0 &&
809 m_rgData[nDstIdx + nBlockSize - 1] !=
EOS)
810 m_rgData[nDstIdx + nBlockSize - 1] =
EOS;
813 return new Tuple<float[], float[]>(m_rgData, m_rgTgt);
823 public override Tuple<float[], float[]>
GetDataAt(
int nBatchSize,
int nBlockSize,
int[] rgnIdx)
825 int nSize = nBatchSize * nBlockSize;
827 if (m_rgData ==
null || m_rgData.Length != nSize)
828 m_rgData =
new float[nSize];
830 Array.Clear(m_rgData, 0, m_rgData.Length);
832 if (m_rgTgt ==
null || m_rgTgt.Length != nSize)
833 m_rgTgt =
new float[nSize];
835 Array.Clear(m_rgTgt, 0, m_rgTgt.Length);
837 for (
int i = 0; i < rgnIdx.Length; i++)
839 int nDataIdx = rgnIdx[i];
840 int nDstIdx = i * nBlockSize;
842 int[] rgSrc = m_rgnData[nDataIdx].Item1;
843 int[] rgTrg = m_rgnData[nDataIdx].Item2;
845 for (
int j = 0; j < nBlockSize; j++)
847 if (j < rgSrc.Length)
848 m_rgData[nDstIdx + j] = rgSrc[j];
850 if (j < rgTrg.Length && rgTrg !=
null)
851 m_rgTgt[nDstIdx + j] = rgTrg[j];
855 rgTrg[rgTrg.Length - 1] ==
EOS &&
856 m_rgTgt[nDstIdx + nBlockSize - 1] != 0 &&
857 m_rgTgt[nDstIdx + nBlockSize - 1] !=
EOS)
858 m_rgTgt[nDstIdx + nBlockSize - 1] =
EOS;
860 if (rgSrc[rgSrc.Length - 1] ==
EOS &&
861 m_rgData[nDstIdx + nBlockSize - 1] != 0 &&
862 m_rgData[nDstIdx + nBlockSize - 1] !=
EOS)
863 m_rgData[nDstIdx + nBlockSize - 1] =
EOS;
866 return new Tuple<float[], float[]>(m_rgData, m_rgTgt);
876 public override List<int>
Tokenize(
string str,
bool bAddBos,
bool bAddEos)
878 return m_vocab.Tokenize(str, bAddBos, bAddEos).ToList();
890 public override string Detokenize(
float[] rgfTokIdx,
int nStartIdx,
int nCount,
bool bIgnoreBos,
bool bIgnoreEos)
893 for (
int i=nStartIdx; i<nStartIdx + nCount; i++)
895 string strItem = m_vocab.Detokenize((
int)rgfTokIdx[i], bIgnoreBos, bIgnoreEos);
896 if (
string.IsNullOrEmpty(strItem))
899 str += strItem +
" ";
902 return str.TrimEnd(
' ');
912 public override string Detokenize(
int nTokIdx,
bool bIgnoreBos,
bool bIgnoreEos)
914 return m_vocab.Detokenize(nTokIdx, bIgnoreBos, bIgnoreEos);
922 get {
return m_vocab.BOS; }
930 get {
return m_vocab.EOS; }
939 List<Tuple<DateTime, int[], int[]>> m_rgnData =
new List<Tuple<DateTime, int[], int[]>>();
940 List<int> m_rgVocabulary =
new List<int>();
942 string m_strVocabInfo;
943 float[] m_rgData =
null;
944 float[] m_rgTgt =
null;
947 int m_nVocabularySize;
967 m_strVocabInfo = strVocabInfo;
969 if (phase !=
Phase.RUN)
971 string strProgData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
974 m_iTokenInput = loadCustomInput(strCustomDllFile);
976 m_rgnData = (strVocabInfo ==
"ENC") ? m_iTokenInput.
LoadAllEncoderTokens(evtCancel, m_log, phase, out m_nVocabularySize) : m_iTokenInput.
LoadAllDecoderTokens(evtCancel, m_log, phase, out m_nVocabularySize);
977 if (m_rgnData.Count < nBlockSizeSrc + nBlockSizeSrc)
978 throw new Exception(
"Insufficient number of tokens, must have at least " + (nBlockSizeSrc + nBlockSizeSrc).ToString() +
" tokens.");
980 log.
WriteLine(strVocabInfo +
" vocabulary size = " + m_nVocabularySize.ToString());
988 Assembly a = Assembly.LoadFile(strCustomDllFile);
989 AssemblyName aName = a.GetName();
991 foreach (Type t
in a.GetTypes())
995 Type iface = t.GetInterface(
"ICustomTokenInput");
999 object obj = Activator.CreateInstance(t);
1007 catch (Exception excpt)
1018 get {
throw new NotImplementedException(
"Raw data not supported by Custom Input"); }
1034 get {
return (uint)m_nVocabularySize; }
1046 if (bIncludeSrc && m_rgnData[nIdx].Item2.Length == 0)
1049 if (bIncludeTrg && m_rgnData[nIdx].Item3.Length == 0)
1064 public override Tuple<float[], float[]>
GetData(
int nBatchSize,
int nBlockSize,
InputData trgData, out
int[] rgnIdx)
1066 int nSize = nBatchSize * nBlockSize;
1068 if (m_rgData ==
null || m_rgData.Length != nSize)
1069 m_rgData =
new float[nSize];
1071 Array.Clear(m_rgData, 0, m_rgData.Length);
1073 if (m_rgTgt ==
null || m_rgTgt.Length != nSize)
1074 m_rgTgt =
new float[nSize];
1076 Array.Clear(m_rgTgt, 0, m_rgTgt.Length);
1078 rgnIdx =
new int[nBatchSize];
1080 for (
int i = 0; i < nBatchSize; i++)
1082 int nDataIdx =
m_random.Next(m_rgnData.Count);
1083 int[] rgSrc = m_rgnData[nDataIdx].Item2;
1084 int nRetryCount = 0;
1088 nDataIdx =
m_random.Next(m_rgnData.Count);
1089 rgSrc = m_rgnData[nDataIdx].Item2;
1092 if (nRetryCount > 20 && (rgSrc.Length == 0 || !trgData.
GetDataAvailabilityAt(nDataIdx,
true,
true)))
1093 throw new Exception(
"Could not find a non-empty source data item!");
1096 int[] rgTrg = m_rgnData[nDataIdx].Item3;
1097 int nDstIdx = i * nBlockSize;
1099 rgnIdx[i] = nDataIdx;
1102 for (
int j = 0; j < nBlockSize; j++)
1104 if (j < rgSrc.Length)
1105 m_rgData[nDstIdx + j] = rgSrc[j];
1107 if (rgTrg !=
null && j < rgTrg.Length)
1108 m_rgTgt[nDstIdx + j] = rgTrg[j];
1111 if (rgTrg !=
null &&
1112 rgTrg[rgTrg.Length - 1] ==
EOS &&
1113 m_rgTgt[nDstIdx + nBlockSize - 1] != 0 &&
1114 m_rgTgt[nDstIdx + nBlockSize - 1] !=
EOS)
1115 m_rgTgt[nDstIdx + nBlockSize - 1] =
EOS;
1117 if (rgSrc[rgSrc.Length - 1] ==
EOS &&
1118 m_rgData[nDstIdx + nBlockSize - 1] != 0 &&
1119 m_rgData[nDstIdx + nBlockSize - 1] !=
EOS)
1120 m_rgData[nDstIdx + nBlockSize - 1] =
EOS;
1123 return new Tuple<float[], float[]>(m_rgData, m_rgTgt);
1133 public override Tuple<float[], float[]>
GetDataAt(
int nBatchSize,
int nBlockSize,
int[] rgnIdx)
1135 int nSize = nBatchSize * nBlockSize;
1137 if (m_rgData ==
null || m_rgData.Length != nSize)
1138 m_rgData =
new float[nSize];
1140 Array.Clear(m_rgData, 0, m_rgData.Length);
1142 if (m_rgTgt ==
null || m_rgTgt.Length != nSize)
1143 m_rgTgt =
new float[nSize];
1145 Array.Clear(m_rgTgt, 0, m_rgTgt.Length);
1147 for (
int i = 0; i < rgnIdx.Length; i++)
1149 int nDataIdx = rgnIdx[i];
1150 int nDstIdx = i * nBlockSize;
1152 int[] rgSrc = m_rgnData[nDataIdx].Item2;
1153 int[] rgTrg = m_rgnData[nDataIdx].Item3;
1155 for (
int j = 0; j < nBlockSize; j++)
1157 if (j < rgSrc.Length)
1158 m_rgData[nDstIdx + j] = rgSrc[j];
1160 if (j < rgTrg.Length && rgTrg !=
null)
1161 m_rgTgt[nDstIdx + j] = rgTrg[j];
1164 if (rgTrg !=
null &&
1165 rgTrg[rgTrg.Length - 1] ==
EOS &&
1166 m_rgTgt[nDstIdx + nBlockSize - 1] != 0 &&
1167 m_rgTgt[nDstIdx + nBlockSize - 1] !=
EOS)
1168 m_rgTgt[nDstIdx + nBlockSize - 1] =
EOS;
1170 if (rgSrc[rgSrc.Length - 1] ==
EOS &&
1171 m_rgData[nDstIdx + nBlockSize - 1] != 0 &&
1172 m_rgData[nDstIdx + nBlockSize - 1] !=
EOS)
1173 m_rgData[nDstIdx + nBlockSize - 1] =
EOS;
1176 return new Tuple<float[], float[]>(m_rgData, m_rgTgt);
1186 public override List<int>
Tokenize(
string str,
bool bAddBos,
bool bAddEos)
1188 throw new NotImplementedException(
"Tokenize not supported by Custom Input.");
1200 public override string Detokenize(
float[] rgfTokIdx,
int nStartIdx,
int nCount,
bool bIgnoreBos,
bool bIgnoreEos)
1202 throw new NotImplementedException(
"Detokenize not supported by Custom Input.");
1212 public override string Detokenize(
int nTokIdx,
bool bIgnoreBos,
bool bIgnoreEos)
1214 throw new NotImplementedException(
"Detokenize not supported by Custom Input.");
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
double Progress
Get/set the progress associated with the Log.
void CHECK_EQ(double df1, double df2, string str)
Test whether one number is equal to another.
Specifies a key-value pair of properties.
string GetProperty(string strName, bool bThrowExceptions=true)
Returns a property as a string value.
The Utility class provides general utility funtions.
static string ReplaceMacro(string strRaw, string strMacroName, string strReplacement)
The ConvertMacro method is used to replace a set of macros in a given string.
static int Count(List< int > rgShape, int nStartIdx=0, int nEndIdx=-1)
Return the count of items given the shape.
The BlobCollection contains a list of Blobs.
void Add(Blob< T > b)
Add a new Blob to the collection.
int Count
Returns the number of items in the collection.
The Blob is the main holder of data that moves through the Layers of the Net.
int channels
DEPRECIATED; legacy shape accessor channels: use shape(1) instead.
void SetData(T[] rgData, int nCount=-1, bool bSetCount=true)
Sets a number of items within the Blob's data.
int height
DEPRECIATED; legacy shape accessor height: use shape(2) instead.
T[] mutable_cpu_data
Get data from the GPU and bring it over to the host, or Set data from the Host and send it over to th...
void Reshape(int nNum, int nChannels, int nHeight, int nWidth, bool? bUseHalfSize=null)
DEPRECIATED; use
bool CompareShape(List< int > rgShape, bool bCompareCpuDataLen=false)
Compares the shape of this blob to another shape.
void CopyFrom(Blob< T > src, int nSrcOffset, int nDstOffset, int nCount, bool bCopyData, bool bCopyDiff)
Copy from a source Blob.
void SetParameter(string strName, double dfVal)
Set a blob parameter.
List< int > shape()
Returns an array where each element contains the shape of an axis of the Blob.
int count()
Returns the total number of items in the Blob.
void ReshapeLike(Blob< T > b, bool? bUseHalfSize=null)
Reshape this Blob to have the same shape as another Blob.
string Name
Get/set the name of the Blob.
int num
DEPRECIATED; legacy shape accessor num: use shape(0) instead.
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
An interface for the units of computation which can be composed into a Net.
Log m_log
Specifies the Log for output.
LayerParameter m_param
Specifies the LayerParameter describing the Layer.
void convert(BlobCollection< T > col)
Convert a collection of blobs from / to half size.
T m_tZero
Specifies a generic type equal to 0.0.
T m_tOne
Specifies a generic type equal to 1.0.
double Forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Given the bottom (input) Blobs, this function computes the top (output) Blobs and the loss.
float convertF(T df)
Converts a generic to a float value.
abstract void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Adjust the shapes of top blobs and internal buffers to accomodate the shapes of the bottom blobs.
Phase m_phase
Specifies the Phase under which the Layer is run.
CudaDnn< T > m_cuda
Specifies the CudaDnn connection to Cuda.
void Setup(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Implements common Layer setup functionality.
static Layer< T > Create(CudaDnn< T > cuda, Log log, LayerParameter p, CancelEvent evtCancel, IXDatabaseBase db=null, TransferInput trxinput=null)
Create a new Layer based on the LayerParameter.
LayerParameter.LayerType m_type
Specifies the Layer type.
The CustomData supports external data input via an external Assembly DLL that supports the ICustomTok...
override bool GetDataAvailabilityAt(int nIdx, bool bIncludeSrc, bool bIncludeTrg)
Returns true if data is available at the given index.
override List< string > RawData
Returns the raw data.
CustomListData(CancelEvent evtCancel, Log log, string strCustomDllFile, string strVocabInfo, int nBlockSizeSrc, int? nRandomSeed=null, Phase phase=Phase.NONE)
The constructor.
override string Detokenize(int nTokIdx, bool bIgnoreBos, bool bIgnoreEos)
Detokenize a single token.
override uint TokenSize
Returns the token size.
override Tuple< float[], float[]> GetDataAt(int nBatchSize, int nBlockSize, int[] rgnIdx)
Fill a batch of data from a specified array of indexes.
override List< int > Tokenize(string str, bool bAddBos, bool bAddEos)
Tokenize an input string using the internal vocabulary.
override Tuple< float[], float[]> GetData(int nBatchSize, int nBlockSize, InputData trgData, out int[] rgnIdx)
Retrieve random blocks from the source data where the data and target are the same but offset by one ...
override char EOS
Return the special end of sequence character.
override char BOS
Return the special begin of sequence character.
override uint VocabularySize
Returns the vocabulary size.
override string Detokenize(float[] rgfTokIdx, int nStartIdx, int nCount, bool bIgnoreBos, bool bIgnoreEos)
Detokenize an array into a string.
The TextListData manages parallel lists of data where the first list contains the encoder input data ...
override string Detokenize(float[] rgfTokIdx, int nStartIdx, int nCount, bool bIgnoreBos, bool bIgnoreEos)
Detokenize an array into a string.
override uint VocabularySize
Returns the number of unique characters in the data.
override uint TokenSize
The text data token size is a single character.
override List< string > RawData
Return the raw data.
override string Detokenize(int nTokIdx, bool bIgnoreBos, bool bIgnoreEos)
Detokenize a single token.
override Tuple< float[], float[]> GetData(int nBatchSize, int nBlockSize, InputData trgData, out int[] rgnIdx)
Retrieve random blocks from the source data where the data and target are the same but offset by one ...
override char EOS
Return the special end of sequence character.
override List< int > Tokenize(string str, bool bAddBos, bool bAddEos)
Tokenize an input string using the internal vocabulary.
VOCABUARY_TYPE
Defines the vocabulary time to use.
override char BOS
Return the special begin of sequence character.
override Tuple< float[], float[]> GetDataAt(int nBatchSize, int nBlockSize, int[] rgnIdx)
Fill a batch of data from a specified array of indexes.
override bool GetDataAvailabilityAt(int nIdx, bool bIncludeSrc, bool bIncludeTrg)
Returns true if data is available at the given index.
TextListData(Log log, string strSrcFile, string strVocabFile, bool bIncludeTarget, TokenizedDataParameter.VOCABULARY_TYPE vocabType, int? nRandomSeed=null, Phase phase=Phase.NONE)
The constructor.
The TokenizedDataPairsLayer loads and tokenizes data for a transformer model where data is loaded in ...
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation, which fills the data into the top (output) Blobs.
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the top based on the parameter batch and block size.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - data Layers do not perform backward..
override string PostProcessFullOutput(Blob< T > blobSoftmax)
The PostProcessFullOutput allows derivative data layers to post-process the results,...
override void setup_internal_blobs(BlobCollection< T > col)
Add all internal blobs.
override int ExactNumTopBlobs
Returns the minimum number of required top (output) Blobs: enc_in, dec_in, dec_out,...
override bool SupportsPostProcessingLogits
Specifies that this layer supports post processing the logits.
override BlobCollection< T > PreProcessInput(PropertySet customInput, out int nSeqLen, BlobCollection< T > colBottom=null)
Preproces the input and return as a set of bottom blobs.
override bool SupportsPreProcessing
Specifies that this layer supports preprocessing.
override void LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override List< Tuple< string, int, double > > PostProcessLogitsOutput(int nCurIdx, Blob< T > blobLogits, Layer< T > softmax, int nAxis, int nK=1)
Allows post processing the logits output data by converting the logits to and selecting from the prob...
override void dispose()
Release all internal blobs.
override bool PreProcessInput(string str, int? nTokIdx, BlobCollection< T > colBottom=null)
Preproces the input and return as a set of bottom blobs.
List< int > Tokenize(string str, VOCABULARY vocab)
Tokenize an input string using the internal vocabulary.
TokenizedDataPairsLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
The TokenizedDataPairsLayer constructor.
string Detokenize(float[] rg, int nStartIdx, int nCount, VOCABULARY vocab)
Detokenize a set of tokens from the data specified.
override int? ExactNumBottomBlobs
Specifies the exact number of bottom blobs (TRAIN|TEST: 0, RUN:2 encin, decin)
uint GetVocabuarySize(VOCABULARY src)
Get the vocabulary size for the specified vocabulary source.
The VocabularyCharacters class manages the data vocabulary of characters.
The VocabularySentencePieces class manages the data vocabulary of words.
The VocabularyWords class manages the data vocabulary of words.
bool enable_cuda_impl
Specifies to use the low-level full cuda implementation of LayerNorm (default = false).
bool out_max_val
If true produce pairs (argmax, maxval)
int? axis
The axis along which to maximize – may be negative to index from the end (e.g., -1 for the last axis)...
Specifies the base parameter for all layers.
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
ArgMaxParameter argmax_param
Returns the parameter set when initialized with LayerType.ARGMAX
TokenizedDataPairsParameter tokenized_data_pairs_param
Returns the parameter set when initialized with LayerType.TOKENIZED_DATA_PAIRS
Phase phase
Specifies the Phase for which this LayerParameter is run.
LayerType
Specifies the layer type.
int axis
The axis along which to perform the softmax – may be negative to index from the end (e....
string vocab_data_url
Specifies the URL to the vocabulary data file used with the SENTENCEPIECE vocabulary type....
string vocab_data_dst_file
Specifies the destination file where the vocabulary data file data is downloaded. This pre-created vo...
string target_vocab_file
Specifies the target vocabulary file used with the SENTENCEPIECE vocabulary type. The vocabulary file...
string source_vocab_file
Specifies the source vocabulary file used with the SENTENCEPIECE vocabulary type. The vocabulary file...
string target
Specifies the data source based on the INPUT_TYPE used. Each dataset has both a training and testing ...
Specifies the parameters for the TokenizedDataLayer.
INPUT_TYPE input_type
Specifies data source input type.
string source
Specifies the data source based on the INPUT_TYPE used. Each dataset has both a training and testing ...
uint block_size
Specifies size of the block.
uint batch_size
The number of heads used.
VOCABULARY_TYPE vocabulary_type
Specifies the vocabulary type to use.
int? seed
Specifies the seed used to initialize the random number generator (normally only for testing).
INPUT_TYPE
Defines the input type used.
VOCABULARY_TYPE
Defines the vocabulary type to use.
The IXDatabaseBase interface defines the general interface to the in-memory database.
The IVocabulary interface specifies the interface that all Vocabularies implement.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Phase
Defines the Phase under which to run a Net.
SPECIAL_TOKENS
Specifies the special tokens.
The MyCaffe.common namespace contains common MyCaffe classes.
The MyCaffe.db.image namespace contains all image database related classes.
The MyCaffe.fillers namespace contains all fillers including the Filler class.
The MyCaffe.layers.gpt namespace contains all GPT related layers.
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...