Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tic
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenyuanbo
tic
Commits
d0f40112
Commit
d0f40112
authored
Apr 18, 2018
by
nhynes
Committed by
Tianqi Chen
Apr 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add SGX random engine (#1113)
parent
f2244b09
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
169 additions
and
55 deletions
+169
-55
make/contrib/random.mk
+1
-1
src/contrib/random/mt_random_engine.cc
+86
-0
src/contrib/random/random.cc
+5
-53
src/contrib/random/sgx_random_engine.cc
+75
-0
src/runtime/sgx/common.h
+2
-0
src/runtime/sgx/trusted/runtime.h
+0
-1
No files found.
make/contrib/random.mk
View file @
d0f40112
RANDOM_CONTRIB_SRC = $(wildcard src/contrib/random/
*
.cc)
RANDOM_CONTRIB_SRC = $(wildcard src/contrib/random/
random
.cc)
RANDOM_CONTRIB_OBJ = $(patsubst src/%.cc, build/%.o, $(RANDOM_CONTRIB_SRC))
ifeq ($(USE_RANDOM), 1)
...
...
src/contrib/random/mt_random_engine.cc
0 → 100644
View file @
d0f40112
/*!
* Copyright (c) 2018 by Contributors
* \file random/mt_random_engine.cc
* \brief mt19937 random engine
*/
#include <dmlc/logging.h>
#include <algorithm>
#include <ctime>
#include <random>
namespace
tvm
{
namespace
contrib
{
/*!
* \brief An interface for generating [tensors of] random numbers.
*/
class
RandomEngine
{
public
:
/*!
* \brief Creates a RandomEngine using a default seed.
*/
RandomEngine
()
{
this
->
Seed
(
time
(
0
));
}
/*!
* \brief Creates a RandomEngine, suggesting the use of a provided seed.
*/
explicit
RandomEngine
(
unsigned
seed
)
{
this
->
Seed
(
seed
);
}
/*!
* \brief Seeds the underlying RNG, if possible.
*/
inline
void
Seed
(
unsigned
seed
)
{
rnd_engine_
.
seed
(
seed
);
this
->
rseed_
=
static_cast
<
unsigned
>
(
seed
);
}
/*!
* \return the seed associated with the underlying RNG.
*/
inline
unsigned
GetSeed
()
const
{
return
rseed_
;
}
/*!
* \return a random integer sampled from the RNG.
*/
inline
unsigned
GetRandInt
()
{
return
rnd_engine_
();
}
/*!
* \brief Fills a tensor with values drawn from Unif(low, high)
*/
void
SampleUniform
(
DLTensor
*
data
,
float
low
,
float
high
)
{
CHECK_GT
(
high
,
low
)
<<
"high must be bigger than low"
;
CHECK
(
data
->
strides
==
nullptr
);
DLDataType
dtype
=
data
->
dtype
;
int64_t
size
=
1
;
for
(
int
i
=
0
;
i
<
data
->
ndim
;
++
i
)
{
size
*=
data
->
shape
[
i
];
}
CHECK
(
dtype
.
code
==
kDLFloat
&&
dtype
.
bits
==
32
&&
dtype
.
lanes
==
1
);
if
(
data
->
ctx
.
device_type
==
kDLCPU
)
{
std
::
uniform_real_distribution
<
float
>
uniform_dist
(
low
,
high
);
std
::
generate_n
(
static_cast
<
float
*>
(
data
->
data
),
size
,
[
&
]
()
{
return
uniform_dist
(
rnd_engine_
);
});
}
else
{
LOG
(
FATAL
)
<<
"Do not support random.randint on this device yet"
;
}
}
private
:
std
::
mt19937
rnd_engine_
;
unsigned
rseed_
;
};
}
// namespace contrib
}
// namespace tvm
src/contrib/random/random.cc
View file @
d0f40112
...
...
@@ -7,8 +7,11 @@
#include <dmlc/logging.h>
#include <dmlc/thread_local.h>
#include <algorithm>
#include <random>
#include <ctime>
#ifndef _LIBCPP_SGX_CONFIG
#include "./mt_random_engine.cc"
#else
#include "./sgx_random_engine.cc"
#endif
#define DLPACK_INTEGER_TYPE_SWITCH(type, DType, ...) \
if (type.code == kDLInt && type.bits == 32) { \
...
...
@@ -38,57 +41,6 @@ namespace contrib {
using
namespace
runtime
;
class
RandomEngine
{
public
:
RandomEngine
()
{
this
->
Seed
(
time
(
0
));
}
explicit
RandomEngine
(
int
seed
)
{
this
->
Seed
(
seed
);
}
~
RandomEngine
()
{}
inline
void
Seed
(
int
seed
)
{
rnd_engine_
.
seed
(
seed
);
this
->
rseed_
=
static_cast
<
unsigned
>
(
seed
);
}
inline
unsigned
GetSeed
()
const
{
return
rseed_
;
}
inline
unsigned
GetRandInt
()
{
return
rnd_engine_
();
}
void
SampleUniform
(
DLTensor
*
data
,
float
low
,
float
high
)
{
CHECK_GT
(
high
,
low
)
<<
"high must be bigger than low"
;
CHECK
(
data
->
strides
==
nullptr
);
DLDataType
dtype
=
data
->
dtype
;
int64_t
size
=
1
;
for
(
int
i
=
0
;
i
<
data
->
ndim
;
++
i
)
{
size
*=
data
->
shape
[
i
];
}
CHECK
(
dtype
.
code
==
kDLFloat
&&
dtype
.
bits
==
32
&&
dtype
.
lanes
==
1
);
if
(
data
->
ctx
.
device_type
==
kDLCPU
)
{
std
::
uniform_real_distribution
<
float
>
uniform_dist
(
low
,
high
);
std
::
generate_n
(
static_cast
<
float
*>
(
data
->
data
),
size
,
[
&
]
()
{
return
uniform_dist
(
rnd_engine_
);
});
}
else
{
LOG
(
FATAL
)
<<
"Do not support random.randint on this device yet"
;
}
}
private
:
std
::
mt19937
rnd_engine_
;
unsigned
rseed_
;
};
struct
RandomThreadLocalEntry
{
RandomEngine
random_engine
;
static
RandomThreadLocalEntry
*
ThreadLocal
();
...
...
src/contrib/random/sgx_random_engine.cc
0 → 100644
View file @
d0f40112
/*!
* Copyright (c) 2018 by Contributors
* \file random/sgx_random_engine.h
* \brief SGX trusted random engine
*/
#include <dmlc/logging.h>
#include <sgx_trts.h>
#include <algorithm>
#include "../../runtime/sgx/common.h"
namespace
tvm
{
namespace
contrib
{
/*!
* \brief An interface for generating [tensors of] random numbers.
*/
class
RandomEngine
{
public
:
/*!
* \brief Creates a RandomEngine, suggesting the use of a provided seed.
*/
explicit
RandomEngine
(
unsigned
seed
)
{
LOG
(
WARNING
)
<<
"SGX RandomEngine does not support seeding."
;
}
/*!
* \brief Seeds the underlying RNG, if possible.
*/
inline
void
Seed
(
unsigned
seed
)
{
LOG
(
WARNING
)
<<
"SGX RandomEngine does not support seeding."
;
}
/*!
* \return the seed associated with the underlying RNG.
*/
inline
unsigned
GetSeed
()
const
{
LOG
(
WARNING
)
<<
"SGX RandomEngine does not support seeding."
;
return
0
;
}
/*!
* \return a random integer sampled from the RNG.
*/
inline
unsigned
GetRandInt
()
{
int
rand_int
;
TVM_SGX_CHECKED_CALL
(
sgx_read_rand
(
reinterpret_cast
<
unsigned
char
*>
(
&
rand_int
),
sizeof
(
int
)));
return
rand_int
;
}
/*!
* \brief Fills a tensor with values drawn from Unif(low, high)
*/
void
SampleUniform
(
DLTensor
*
data
,
float
low
,
float
high
)
{
CHECK_GT
(
high
,
low
)
<<
"high must be bigger than low"
;
CHECK
(
data
->
strides
==
nullptr
);
DLDataType
dtype
=
data
->
dtype
;
int64_t
size
=
1
;
for
(
int
i
=
0
;
i
<
data
->
ndim
;
++
i
)
{
size
*=
data
->
shape
[
i
];
}
CHECK
(
dtype
.
code
==
kDLFloat
&&
dtype
.
bits
==
32
&&
dtype
.
lanes
==
1
);
std
::
generate_n
(
static_cast
<
float
*>
(
data
->
data
),
size
,
[
&
]
()
{
float
max_int
=
static_cast
<
float
>
(
std
::
numeric_limits
<
unsigned
>::
max
());
float
unif01
=
GetRandInt
()
/
max_int
;
return
low
+
unif01
*
(
high
-
low
);
});
}
};
}
// namespace contrib
}
// namespace tvm
src/runtime/sgx/common.h
View file @
d0f40112
...
...
@@ -6,6 +6,8 @@
#ifndef TVM_RUNTIME_SGX_COMMON_H_
#define TVM_RUNTIME_SGX_COMMON_H_
#include <sgx_error.h>
namespace
tvm
{
namespace
runtime
{
namespace
sgx
{
...
...
src/runtime/sgx/trusted/runtime.h
View file @
d0f40112
...
...
@@ -6,7 +6,6 @@
#ifndef TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_
#define TVM_RUNTIME_SGX_TRUSTED_RUNTIME_H_
#include <sgx_edger8r.h>
#include <tvm/runtime/packed_func.h>
#include <string>
#include "../common.h"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment