fixed ambigous type cast problems in ConfigValue;
added some more VS CRT mappings to basetypes.h (string compare functions)
This commit is contained in:
Родитель
f3cc47bc20
Коммит
92fe5377d5
|
@ -86,6 +86,7 @@ OACR_WARNING_DISABLE(POTENTIAL_ARGUMENT_TYPE_MISMATCH, "Not level1 or level2_sec
|
|||
#include <windows.h> // for CRITICAL_SECTION and Unicode conversion functions --TODO: is there a portable alternative?
|
||||
#endif
|
||||
#if __unix__
|
||||
#include <strings.h>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#endif
|
||||
|
@ -143,9 +144,12 @@ msra::strfun::cstring charpath (const std::wstring & p)
|
|||
return msra::strfun::cstring (&buf[0]);
|
||||
#endif
|
||||
}
|
||||
static inline FILE* _wfopen(const wchar_t * path, const wchar_t * mode) { return fopen(charpath(path), charpath(mode)); }
|
||||
static inline FILE* _wfopen (const wchar_t * path, const wchar_t * mode) { return fopen(charpath(path), charpath(mode)); }
|
||||
// --- basic string functions
|
||||
static inline wchar_t* wcstok_s(wchar_t* s, const wchar_t* delim, wchar_t** ptr) { return ::wcstok(s, delim, ptr); }
|
||||
static inline wchar_t* wcstok_s (wchar_t* s, const wchar_t* delim, wchar_t** ptr) { return ::wcstok(s, delim, ptr); }
|
||||
static inline int _stricmp (const char * a, const char * b) { return ::strcasecmp (a, b); }
|
||||
static inline int _strnicmp (const char * a, const char * b, wchar_t n) { return ::strncasecmp (a, b, n); }
|
||||
static inline int _wcsicmp (const wchar_t * a, const wchar_t * b) { return ::wcscasecmp (a, b); }
|
||||
// -- other
|
||||
static inline void Sleep (size_t ms) { std::this_thread::sleep_for (std::chrono::milliseconds (ms)); }
|
||||
#endif
|
||||
|
|
|
@ -108,25 +108,29 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
return value;
|
||||
}
|
||||
operator float () const { return (float) (double) *this; }
|
||||
operator long () const
|
||||
private:
|
||||
long tolong() const
|
||||
{
|
||||
char * ep; // will be set to point to first character that failed parsing
|
||||
long value = strtol (c_str(), &ep, 10);
|
||||
long value = strtol(c_str(), &ep, 10);
|
||||
if (empty() || *ep != 0)
|
||||
throw std::runtime_error ("ConfigValue (long): invalid input string");
|
||||
throw std::runtime_error("ConfigValue (long): invalid input string");
|
||||
return value;
|
||||
}
|
||||
operator unsigned long () const
|
||||
unsigned long toulong() const
|
||||
{
|
||||
char * ep; // will be set to point to first character that failed parsing
|
||||
unsigned long value = strtoul (c_str(), &ep, 10);
|
||||
unsigned long value = strtoul(c_str(), &ep, 10);
|
||||
if (empty() || *ep != 0)
|
||||
throw std::runtime_error ("ConfigValue (unsigned long): invalid input string");
|
||||
throw std::runtime_error("ConfigValue (unsigned long): invalid input string");
|
||||
return value;
|
||||
}
|
||||
public:
|
||||
operator long() const { return tolong(); }
|
||||
operator unsigned long() const { return toulong(); }
|
||||
operator short () const
|
||||
{
|
||||
long val = (long) *this;
|
||||
long val = tolong();
|
||||
short ival = (short) val;
|
||||
if (val != ival)
|
||||
throw std::runtime_error ("ConfigValue (short): integer argument expected");
|
||||
|
@ -134,7 +138,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
operator unsigned short () const
|
||||
{
|
||||
unsigned long val = (unsigned long) *this;
|
||||
unsigned long val = toulong();
|
||||
unsigned short ival = (unsigned short) val;
|
||||
if (val != ival)
|
||||
throw std::runtime_error ("ConfigValue (unsigned short): integer argument expected");
|
||||
|
@ -142,7 +146,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
operator int () const
|
||||
{
|
||||
long val = (long) *this;
|
||||
long val = tolong();
|
||||
int ival = (int) val;
|
||||
if (val != ival)
|
||||
throw std::runtime_error ("ConfigValue (int): integer argument expected");
|
||||
|
@ -150,7 +154,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
}
|
||||
operator unsigned int () const
|
||||
{
|
||||
unsigned long val = (unsigned long) *this;
|
||||
unsigned long val = toulong();
|
||||
unsigned int ival = (unsigned int) val;
|
||||
if (val != ival)
|
||||
throw std::runtime_error ("ConfigValue (unsigned int): integer argument expected");
|
||||
|
@ -159,7 +163,7 @@ namespace Microsoft { namespace MSR { namespace CNTK {
|
|||
operator int64_t () const
|
||||
{
|
||||
char * ep; // will be set to point to first character that failed parsing
|
||||
int64_t value = _strtoui64 (c_str(), &ep, 10);
|
||||
int64_t value = _strtoi64 (c_str(), &ep, 10);
|
||||
if (empty() || *ep != 0)
|
||||
throw std::runtime_error ("ConfigValue (int64_t): invalid input string");
|
||||
return value;
|
||||
|
|
|
@ -1,118 +1,118 @@
|
|||
//
|
||||
// <copyright file="GPUMatrixUnitTests.cpp" company="Microsoft">
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// </copyright>
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CPUMatrix.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef MATH_EXPORTS
|
||||
#define MATH_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATH_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif /* Linux - already defined in CPUMatrix.h */
|
||||
|
||||
namespace Microsoft { namespace MSR { namespace CNTK {
|
||||
|
||||
template<class ElemType>
|
||||
class MATH_API CPUSparseMatrix : public BaseMatrix<ElemType>
|
||||
{
|
||||
//
|
||||
// <copyright file="GPUMatrixUnitTests.cpp" company="Microsoft">
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// </copyright>
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include "CPUMatrix.h"
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
|
||||
#ifdef _WIN32
|
||||
#ifdef MATH_EXPORTS
|
||||
#define MATH_API __declspec(dllexport)
|
||||
#else
|
||||
#define MATH_API __declspec(dllimport)
|
||||
#endif
|
||||
#endif /* Linux - already defined in CPUMatrix.h */
|
||||
|
||||
namespace Microsoft { namespace MSR { namespace CNTK {
|
||||
|
||||
template<class ElemType>
|
||||
class MATH_API CPUSparseMatrix : public BaseMatrix<ElemType>
|
||||
{
|
||||
typedef BaseMatrix<ElemType> B; using B::m_elemSizeAllocated; using B::m_computeDevice; using B::m_externalBuffer; using B::m_format; using B::m_matrixName;
|
||||
using B::m_numCols; using B::m_numRows; using B::m_nz; using B::m_pArray; // without this, base members would require to use thi-> in GCC
|
||||
|
||||
private:
|
||||
void ZeroInit();
|
||||
void CheckInit(const MatrixFormat format);
|
||||
|
||||
public:
|
||||
CPUSparseMatrix(const MatrixFormat format);
|
||||
CPUSparseMatrix(const MatrixFormat format, const size_t numRows, const size_t numCols, const size_t size);
|
||||
|
||||
~CPUSparseMatrix();
|
||||
|
||||
public:
|
||||
using B::GetNumCols; using B::GetNumRows;
|
||||
|
||||
void SetValue(const size_t rIdx, const size_t cIdx, ElemType val);
|
||||
void SetValue(const CPUSparseMatrix& /*val*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
void ShiftBy(int /*numShift*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
size_t BufferSize() const {return m_elemSizeAllocated*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const;
|
||||
|
||||
void SetGaussianRandomValue(const ElemType /*mean*/, const ElemType /*sigma*/, unsigned long /*seed*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
static void ClassEntropy(const CPUMatrix<ElemType>& a, const CPUMatrix<ElemType>& weight,
|
||||
const CPUSparseMatrix<ElemType> & label, const CPUMatrix<ElemType>& cls,
|
||||
const CPUMatrix<ElemType>& idx2cls, CPUSparseMatrix<ElemType>& etp, CPUMatrix<ElemType>& entropyScore);
|
||||
|
||||
static void ClassEntropyError(CPUSparseMatrix<ElemType>& a);
|
||||
|
||||
static void ClassEntropyGradientOfInput(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& weight,
|
||||
CPUMatrix<ElemType>& grd);
|
||||
|
||||
static void ClassEntropyGradientOfWeight(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& input,
|
||||
const CPUSparseMatrix<ElemType> & label,
|
||||
const CPUMatrix<ElemType>& cls,
|
||||
const CPUMatrix<ElemType>& idx2cls,
|
||||
CPUSparseMatrix<ElemType>& grd);
|
||||
|
||||
static void MultiplyAndWeightedAdd(ElemType alpha, const CPUMatrix<ElemType>& lhs, const bool transposeA,
|
||||
const CPUSparseMatrix<ElemType>& rhs, const bool transposeB, ElemType beta, CPUMatrix<ElemType>& c);
|
||||
|
||||
static void MultiplyAndAdd(ElemType alpha, const CPUMatrix<ElemType>& lhs, const bool transposeA,
|
||||
const CPUSparseMatrix<ElemType>& rhs, const bool transposeB, CPUSparseMatrix<ElemType>& c);
|
||||
|
||||
static void ScaleAndAdd(const ElemType alpha, const CPUSparseMatrix<ElemType>& lhs, CPUMatrix<ElemType>& c);
|
||||
|
||||
/// sum(vec(a).*vec(b))
|
||||
static ElemType InnerProductOfMatrices(const CPUSparseMatrix<ElemType>& /*a*/, const CPUMatrix<ElemType>& /*b*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
static void AddScaledDifference(const ElemType /*alpha*/, const CPUSparseMatrix<ElemType>& /*a*/, const CPUMatrix<ElemType>& /*b*/, CPUMatrix<ElemType>& /*c*/,
|
||||
bool /*bDefaultZero*/ ) { NOT_IMPLEMENTED; }
|
||||
static void AddScaledDifference(const ElemType /*alpha*/, const CPUMatrix<ElemType>& /*a*/, const CPUSparseMatrix<ElemType>& /*b*/, CPUMatrix<ElemType>& /*c*/,
|
||||
bool /*bDefaultZero*/ ) { NOT_IMPLEMENTED; }
|
||||
|
||||
int GetComputeDeviceId() const {return -1;}
|
||||
|
||||
void Resize(const size_t numRows, const size_t numCols, size_t size = 0);
|
||||
void Reset();
|
||||
|
||||
public:
|
||||
void NormalGrad(CPUMatrix<ElemType>& c, const ElemType momentum);
|
||||
void Adagrad(CPUMatrix<ElemType>& c);
|
||||
|
||||
public:
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncateTop (const ElemType /*threshold*/) { NOT_IMPLEMENTED; }
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncateBottom (const ElemType /*threshold*/) { NOT_IMPLEMENTED; }
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncate (const ElemType /*threshold*/);
|
||||
|
||||
public:
|
||||
void Print(const char* /*matrixName*/) const { NOT_IMPLEMENTED; }
|
||||
|
||||
int m_colIdx; //used to SetValue()
|
||||
ElemType *m_val; // values
|
||||
size_t *m_row; //row/col ids in CSC/CSR format
|
||||
size_t *m_pb; //begin ids of col/row in CSC/CSR format
|
||||
|
||||
size_t m_blockSize; //block size
|
||||
ElemType *m_blockVal; //block values
|
||||
size_t *m_blockIds; //block ids
|
||||
};
|
||||
|
||||
typedef CPUSparseMatrix<float> CPUSingleSparseMatrix;
|
||||
typedef CPUSparseMatrix<double> CPUDoubleSparseMatrix;
|
||||
|
||||
}}}
|
||||
|
||||
|
||||
private:
|
||||
void ZeroInit();
|
||||
void CheckInit(const MatrixFormat format);
|
||||
|
||||
public:
|
||||
CPUSparseMatrix(const MatrixFormat format);
|
||||
CPUSparseMatrix(const MatrixFormat format, const size_t numRows, const size_t numCols, const size_t size);
|
||||
|
||||
~CPUSparseMatrix();
|
||||
|
||||
public:
|
||||
using B::GetNumCols; using B::GetNumRows;
|
||||
|
||||
void SetValue(const size_t rIdx, const size_t cIdx, ElemType val);
|
||||
void SetValue(const CPUSparseMatrix& /*val*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
void ShiftBy(int /*numShift*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
size_t BufferSize() const {return m_elemSizeAllocated*sizeof(ElemType);}
|
||||
ElemType* BufferPointer() const;
|
||||
|
||||
void SetGaussianRandomValue(const ElemType /*mean*/, const ElemType /*sigma*/, unsigned long /*seed*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
static void ClassEntropy(const CPUMatrix<ElemType>& a, const CPUMatrix<ElemType>& weight,
|
||||
const CPUSparseMatrix<ElemType> & label, const CPUMatrix<ElemType>& cls,
|
||||
const CPUMatrix<ElemType>& idx2cls, CPUSparseMatrix<ElemType>& etp, CPUMatrix<ElemType>& entropyScore);
|
||||
|
||||
static void ClassEntropyError(CPUSparseMatrix<ElemType>& a);
|
||||
|
||||
static void ClassEntropyGradientOfInput(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& weight,
|
||||
CPUMatrix<ElemType>& grd);
|
||||
|
||||
static void ClassEntropyGradientOfWeight(
|
||||
const CPUSparseMatrix<ElemType>& error,
|
||||
const CPUMatrix<ElemType>& input,
|
||||
const CPUSparseMatrix<ElemType> & label,
|
||||
const CPUMatrix<ElemType>& cls,
|
||||
const CPUMatrix<ElemType>& idx2cls,
|
||||
CPUSparseMatrix<ElemType>& grd);
|
||||
|
||||
static void MultiplyAndWeightedAdd(ElemType alpha, const CPUMatrix<ElemType>& lhs, const bool transposeA,
|
||||
const CPUSparseMatrix<ElemType>& rhs, const bool transposeB, ElemType beta, CPUMatrix<ElemType>& c);
|
||||
|
||||
static void MultiplyAndAdd(ElemType alpha, const CPUMatrix<ElemType>& lhs, const bool transposeA,
|
||||
const CPUSparseMatrix<ElemType>& rhs, const bool transposeB, CPUSparseMatrix<ElemType>& c);
|
||||
|
||||
static void ScaleAndAdd(const ElemType alpha, const CPUSparseMatrix<ElemType>& lhs, CPUMatrix<ElemType>& c);
|
||||
|
||||
/// sum(vec(a).*vec(b))
|
||||
static ElemType InnerProductOfMatrices(const CPUSparseMatrix<ElemType>& /*a*/, const CPUMatrix<ElemType>& /*b*/) { NOT_IMPLEMENTED; }
|
||||
|
||||
static void AddScaledDifference(const ElemType /*alpha*/, const CPUSparseMatrix<ElemType>& /*a*/, const CPUMatrix<ElemType>& /*b*/, CPUMatrix<ElemType>& /*c*/,
|
||||
bool /*bDefaultZero*/ ) { NOT_IMPLEMENTED; }
|
||||
static void AddScaledDifference(const ElemType /*alpha*/, const CPUMatrix<ElemType>& /*a*/, const CPUSparseMatrix<ElemType>& /*b*/, CPUMatrix<ElemType>& /*c*/,
|
||||
bool /*bDefaultZero*/ ) { NOT_IMPLEMENTED; }
|
||||
|
||||
int GetComputeDeviceId() const {return -1;}
|
||||
|
||||
void Resize(const size_t numRows, const size_t numCols, size_t size = 0);
|
||||
void Reset();
|
||||
|
||||
public:
|
||||
void NormalGrad(CPUMatrix<ElemType>& c, const ElemType momentum);
|
||||
void Adagrad(CPUMatrix<ElemType>& c);
|
||||
|
||||
public:
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncateTop (const ElemType /*threshold*/) { NOT_IMPLEMENTED; }
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncateBottom (const ElemType /*threshold*/) { NOT_IMPLEMENTED; }
|
||||
CPUSparseMatrix<ElemType>& InplaceTruncate (const ElemType /*threshold*/);
|
||||
|
||||
public:
|
||||
void Print(const char* /*matrixName*/) const { NOT_IMPLEMENTED; }
|
||||
|
||||
int m_colIdx; //used to SetValue()
|
||||
ElemType *m_val; // values
|
||||
size_t *m_row; //row/col ids in CSC/CSR format
|
||||
size_t *m_pb; //begin ids of col/row in CSC/CSR format
|
||||
|
||||
size_t m_blockSize; //block size
|
||||
ElemType *m_blockVal; //block values
|
||||
size_t *m_blockIds; //block ids
|
||||
};
|
||||
|
||||
typedef CPUSparseMatrix<float> CPUSingleSparseMatrix;
|
||||
typedef CPUSparseMatrix<double> CPUDoubleSparseMatrix;
|
||||
|
||||
}}}
|
||||
|
||||
|
|
Загрузка…
Ссылка в новой задаче