2using System.Collections.Generic;
26 Random m_random =
new Random();
28 bool m_bEnableEos =
false;
29 bool m_bEnableBos =
false;
55 m_evtCancel = evtCancel;
119 List<int> rgShape =
Utility.Clone<
int>(colTop[1].shape());
120 rgShape[1] = nBlockSize;
124 float[] rgPos =
new float[nBlockSize];
125 for (
int i = 0; i < nBlockSize; i++)
142 int nBatchSize = colBottom[0].num;
150 if (colTop.
Count > 2)
151 blobTarget = colTop[2];
156 int[] rgShape =
new int[nCount];
160 int nC = colBottom[0].channels;
162 throw new Exception(
"The bottom input channel count cannot exceed the block_size=" + nBlockSize.ToString());
165 rgShape[0] = nBatchSize;
167 if (rgShape.Length > 2)
168 rgShape[2] = nTokenSize;
171 if (blobTarget !=
null)
174 if (blobPos.
count() < nBlockSize)
177 if (rgShape.Length > 2)
183 rgShape[0] = nBatchSize;
187 m_blobX.
Reshape(1, nBlockSize, 1, 1);
200 Blob<T> blobTarget = colTop[2];
205 int[] rgShape =
new int[nCount];
209 rgShape[0] = nBatchSize;
210 rgShape[1] = nBlockSize;
211 if (rgShape.Length > 2)
212 rgShape[2] = nTokenSize;
215 blobTarget.Reshape(rgShape);
218 if (rgShape.Length > 2)
220 blobPos.Reshape(rgShape);
250 colTop[0].mutable_cpu_data =
convert(data.Item1);
251 if (colTop.
Count > 2)
252 colTop[2].mutable_cpu_data =
convert(data.Item2);
284 public List<int>
Tokenize(
string str,
bool bAddBos,
bool bAddEos)
286 m_bEnableBos = bAddBos;
287 m_bEnableEos = bAddEos;
288 return m_data.
Tokenize(str, bAddBos, bAddEos);
300 public string Detokenize(
float[] rg,
int nStartIdx,
int nCount,
bool bIgnoreBos =
true,
bool bIgnoreEos =
true)
302 return m_data.
Detokenize(rg, nStartIdx, nCount, bIgnoreBos, bIgnoreEos);
318 string strInput = customInput.
GetProperty(
"InputData");
319 if (
string.IsNullOrEmpty(strInput))
320 throw new Exception(
"Could not find 'InputData' property!");
322 int[] rgShape =
new int[2];
324 rgShape[1] = strInput.Length;
326 blobIdx.Reshape(rgShape);
328 List<int> rgTokens = m_data.
Tokenize(strInput,
false,
false);
329 float[] rgInput =
new float[rgTokens.Count];
331 for (
int i = 0; i < strInput.Length; i++)
333 rgInput[i] = rgTokens[i];
336 blobIdx.mutable_cpu_data =
convert(rgInput);
350 if (nTokIdx.HasValue && m_bEnableEos && nTokIdx.Value == (
int)
SPECIAL_TOKENS.EOS)
353 List<float> rgTok =
convertF(colBottom[0].mutable_cpu_data).ToList();
355 rgTok.Add(nTokIdx.Value);
359 List<int> rgShape =
Utility.Clone<
int>(colBottom[0].shape());
360 rgShape[1] = rgTok.
Count;
361 colBottom[0].Reshape(rgShape);
363 colBottom[0].mutable_cpu_data =
convert(rgTok.ToArray());
383 int nVocabCount = blobLogits.
count(nAxis);
384 float[] rgLogits =
new float[nVocabCount];
385 int nIdxStart = blobLogits.
count() - nVocabCount;
386 Dictionary<int, float> rgTopK =
new Dictionary<int, float>();
388 for (
int i = nIdxStart; i < blobLogits.
count(); i++)
390 float fVal = rgData[i];
391 rgTopK.Add(i - nIdxStart, fVal);
393 if (rgTopK.Count > nK)
395 float fMin =
float.MaxValue;
398 foreach (KeyValuePair<int, float> kv
in rgTopK)
407 rgTopK.Remove(nMinIdx);
411 for (
int i = 0; i < rgLogits.Count(); i++)
413 if (rgTopK.ContainsKey(i))
414 rgLogits[i] = rgTopK[i];
416 rgLogits[i] = -
float.MaxValue;
424 m_blobX.
Reshape(1, 1, nVocabCount, 1);
431 if (m_softmax ==
null)
436 m_softmax.
Setup(colBottom, colTop);
441 softmax.
Forward(colBottom, colTop);
447 str += m_data.
Detokenize(nCharIdx,
true,
true);
449 return new List<Tuple<string, int, double>>() {
new Tuple<string, int, double>(str, nCharIdx, 0) };
452 private int argmax(
float[] rgData)
455 float fMax = rgData[0];
457 for (
int i = 1; i < rgData.Length; i++)
459 if (rgData[i] > fMax)
469 private int sample(
float[] rgData)
472 float fRand = (float)m_random.NextDouble();
474 for (
int i = 0; i < rgData.Length; i++)
482 return rgData.Length - 1;
504 string m_strDebugIndexFile;
505 List<int> m_rgDebugIdx =
null;
507 float[] m_rgData =
null;
508 float[] m_rgTgt =
null;
522 string strProgData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
526 m_strData = File.ReadAllText(strSrc);
528 if (File.Exists(strDebugIndexFile))
530 m_strDebugIndexFile =
Utility.
ReplaceMacro(strDebugIndexFile,
"$ProgramData$", strProgData);
531 m_rgDebugIdx =
new List<int>();
532 string[] rgLines = File.ReadAllLines(strDebugIndexFile);
533 foreach (
string strLine
in rgLines)
535 if (strLine.StartsWith(
"idx = "))
537 string strIdx = strLine.Substring(6).Trim(
' ',
'\t',
'\n',
'\r');
538 m_rgDebugIdx.Add(
int.Parse(strIdx));
558 return new List<string>() { m_strData };
575 get {
return (uint)m_vocab.
Count; }
599 public override Tuple<float[], float[]>
GetData(
int nBatchSize,
int nBlockSize,
InputData trgData, out
int[] rgnIdx)
601 int nSize = nBatchSize * nBlockSize;
603 rgnIdx =
new int[nBatchSize];
605 if (m_rgData ==
null || m_rgData.Length != nSize)
606 m_rgData =
new float[nSize];
608 if (m_rgTgt ==
null || m_rgTgt.Length != nSize)
609 m_rgTgt =
new float[nSize];
611 for (
int i = 0; i < nBatchSize; i++)
613 int nMax = m_strData.Count() - (nBlockSize + 1);
615 int nDstIdx = i * nBlockSize;
617 rgnIdx[i] = nDataIdx;
619 if (m_rgDebugIdx !=
null)
621 nDataIdx = m_rgDebugIdx[m_nDebugIdx];
624 if (m_nDebugIdx >= m_rgDebugIdx.Count)
628 List<int> rgTokens =
new List<int>();
629 List<int> rgLastTokens;
632 while (rgTokens.Count < nBlockSize + 1)
634 rgLastTokens = m_vocab.
Tokenize(m_strData[nDataIdx + nIdx].ToString());
635 if (rgLastTokens.Count > 0)
636 rgTokens.AddRange(rgLastTokens);
641 Array.Copy(rgTokens.ToArray(), 0, m_rgData, nDstIdx, nBlockSize);
642 rgTokens.RemoveAt(0);
643 Array.Copy(rgTokens.ToArray(), 0, m_rgTgt, nDstIdx, nBlockSize);
646 return new Tuple<float[], float[]>(m_rgData, m_rgTgt);
656 public override Tuple<float[], float[]>
GetDataAt(
int nBatchSize,
int nBlockSize,
int[] rgnIdx)
658 throw new NotImplementedException();
668 public override List<int>
Tokenize(
string str,
bool bAddBos,
bool bAddEos)
670 return m_vocab.
Tokenize(str, bAddBos, bAddEos).ToList();
680 public override string Detokenize(
int nTokIdx,
bool bIgnoreBos,
bool bIgnoreEos)
682 return m_vocab.
Detokenize(nTokIdx, bIgnoreBos, bIgnoreEos);
694 public override string Detokenize(
float[] rgfTokIdx,
int nStartIdx,
int nCount,
bool bIgnoreBos,
bool bIgnoreEos)
697 for (
int i = nStartIdx; i < nStartIdx + nCount; i++)
699 string strItem = m_vocab.
Detokenize((
int)rgfTokIdx[i], bIgnoreBos, bIgnoreEos);
700 if (
string.IsNullOrEmpty(strItem))
714 get {
return m_vocab.
BOS; }
722 get {
return m_vocab.
EOS; }
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 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.
int Count
Returns the number of items in the collection.
void Reshape(int[] rgShape)
Reshapes all blobs in the collection to the given shape.
void CopyFrom(BlobCollection< T > bSrc, bool bCopyDiff=false)
Copy the data or diff from another BlobCollection into this one.
The Blob is the main holder of data that moves through the Layers of the Net.
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
void SetParameter(string strName, double dfVal)
Set a blob parameter.
int count()
Returns the total number of items in the Blob.
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.
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.
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 TextInputData manages character data read in from a text file. Data is tokenized into indexes tha...
override bool GetDataAvailabilityAt(int nIdx, bool bIncludeSrc, bool bIncludeTrg)
Returns true if data is available at the given index.
override string Detokenize(float[] rgfTokIdx, int nStartIdx, int nCount, bool bIgnoreBos, bool bIgnoreEos)
Detokenize an array into a string.
TextInputData(string strSrc, TokenizedDataParameter.VOCABULARY_TYPE vocabType=TokenizedDataParameter.VOCABULARY_TYPE.CHARACTER, int? nRandomSeed=null, string strDebugIndexFile=null, Phase phase=Phase.NONE)
The constructor.
override List< string > RawData
Return the raw data.
override uint TokenSize
The text data token size is a single character.
override uint VocabularySize
Returns the number of unique characters in the data.
override char EOS
Return the special end of sequence character.
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 BOS
Return the special begin of sequence character.
override string Detokenize(int nTokIdx, bool bIgnoreBos, bool bIgnoreEos)
Detokenize a single token.
override Tuple< float[], float[]> GetDataAt(int nBatchSize, int nBlockSize, int[] rgnIdx)
Specifies the GetDataAt method - Not used.
override List< int > Tokenize(string str, bool bAddBos, bool bAddEos)
Tokenize an input string using the internal vocabulary.
The TokenizedDataLayer loads and tokenizes data for a transformer model where data is loaded in the f...
override void Reshape(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Reshape the top based on the parameter batch and block size.
TokenizedDataLayer(CudaDnn< T > cuda, Log log, LayerParameter p, IXDatabaseBase db, CancelEvent evtCancel)
The TokenizedDataLayer constructor.
string Detokenize(float[] rg, int nStartIdx, int nCount, bool bIgnoreBos=true, bool bIgnoreEos=true)
Detokenize a set of tokens from the data specified.
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 LayerSetUp(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Setup the layer.
override int? MaxBottomBlobs
No bottom blobs for the data layer, except when running.
override int? ExactNumTopBlobs
Returns the maximum number of required top (output) Blobs: data, pos, target
override bool PreProcessInput(string str, int? nTokIdx, BlobCollection< T > colBottom=null)
Preproces the input and return as a set of bottom blobs.
override int MinBottomBlobs
Returns the minimum number of bottom blobs.
override void backward(BlobCollection< T > colTop, List< bool > rgbPropagateDown, BlobCollection< T > colBottom)
Not implemented - data Layers do not perform backward..
override void dispose()
Release all internal blobs.
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 SupportsPostProcessingLogits
Specifies that this layer supports post processing the logits.
override bool SupportsPreProcessing
Specifies that this layer supports preprocessing.
override void forward(BlobCollection< T > colBottom, BlobCollection< T > colTop)
Run the Forward computation, which fills the data into the top (output) Blobs.
List< int > Tokenize(string str, bool bAddBos, bool bAddEos)
Tokenize an input string using the internal vocabulary.
The VocabularyCharacters class manages the data vocabulary of characters.
The VocabularyWords class manages the data vocabulary of words.
Specifies the base parameter for all layers.
TokenizedDataParameter tokenized_data_param
Returns the parameter set when initialized with LayerType.TOKENIZED_DATA
SoftmaxParameter softmax_param
Returns the parameter set when initialized with LayerType.SOFTMAX
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....
Specifies the parameters for the TokenizedDataLayer.
string debug_index_file
Specifies an optional data index file used for debugging only.
SAMPLE_METHOD sample_method
Specifies the sampling method used when post processing logits (default = ARGMAX).
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.
SAMPLE_METHOD
Defines the sampling method 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.
char EOS
Returns the special EOS character.
int[] Tokenize(string str, bool bAddBos, bool bAddEos)
Tokenize a string of data.
int BuildFromString(string strData)
Build the vocabulary from a string.
char BOS
Returns the special BOS character.
int Count
Returns the size of the vocabulary.
string Detokenize(float[] rgf, bool bIgnoreBos, bool bIgnoreEos)
Detokenize an array into a string.
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-...