Skip to content

Commit

Permalink
rocAL - Parameter changes (#109)
Browse files Browse the repository at this point in the history
* Adding parameterVX changes

* Resolving review comments

* Adding API comments

* ResolvePR comments

* Resolve PR Comments

* Resolving review comments

* Fix name changes of the variable

* Resolving review comments

---------

Co-authored-by: Swetha B S <[email protected]>
  • Loading branch information
SundarRajan98 and swetha097 authored Mar 18, 2024
1 parent 7ad1a28 commit 92fc82c
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 17 deletions.
6 changes: 6 additions & 0 deletions rocAL/include/parameters/parameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class Parameter {
/// used to internally renew state of the parameter if needed (for random parameters)
virtual void renew(){};

/// allocates memory for the array with specified size
virtual void create_array(unsigned size){};

/// used to fetch the updated param values
virtual std::vector<T> get_array() { return {}; };

virtual ~Parameter() {}
///
/// \return returns if this parameter takes a single value (vs a range of values or many values)
Expand Down
5 changes: 5 additions & 0 deletions rocAL/include/parameters/parameter_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ THE SOFTWARE.
#include "parameter_random.h"
#include "parameter_simple.h"

const int MAX_SEEDS = 1024;

enum class RocalParameterType {
DETERMINISTIC = 0,
RANDOM_UNIFORM,
Expand Down Expand Up @@ -72,6 +74,7 @@ class ParameterFactory {
void set_seed(unsigned seed);
unsigned get_seed();
void generate_seed();
int64_t get_seed_from_seedsequence();

template <typename T>
Parameter<T>* create_uniform_rand_param(T start, T end) {
Expand Down Expand Up @@ -104,4 +107,6 @@ class ParameterFactory {
static ParameterFactory* _instance;
static std::mutex _mutex;
ParameterFactory();
std::vector<int64_t> _seed_vector;
int _seed_sequence_idx = 0;
};
63 changes: 61 additions & 2 deletions rocAL/include/parameters/parameter_random.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ class UniformRand : public Parameter<T> {
T get() override {
return _updated_val;
};
void renew() override {

std::vector<T> get_array() override {
return _param_values;
}

void renew_value() {
std::unique_lock<std::mutex> lock(_lock);
auto val = _generator();

Expand All @@ -64,6 +69,21 @@ class UniformRand : public Parameter<T> {
((double)val / (double)_generator.max()) * ((double)_end - (double)_start) + (double)_start);
}
}

void renew_array() {
for (uint i = 0; i < _size; i++) {
renew_value();
_param_values[i] = _updated_val;
}
}

void renew() override {
if (_param_values.size()) {
renew_array();
} else {
renew_value();
}
}
int update(T start, T end) {
std::unique_lock<std::mutex> lock(_lock);
if (end < start)
Expand All @@ -73,6 +93,14 @@ class UniformRand : public Parameter<T> {
_end = end;
return 0;
}

void create_array(unsigned vector_size) override {
if (_param_values.size() == 0) {
_param_values.resize(vector_size);
_size = vector_size;
}
}

bool single_value() const override {
return (_start == _end);
}
Expand All @@ -81,8 +109,10 @@ class UniformRand : public Parameter<T> {
T _start;
T _end;
T _updated_val;
std::vector<T> _param_values;
std::mt19937 _generator;
std::mutex _lock;
unsigned _size;
};

template <typename T>
Expand Down Expand Up @@ -142,7 +172,8 @@ struct CustomRand : public Parameter<T> {
T default_value() const override {
return static_cast<T>(_mean);
}
void renew() override {

void renew_value() {
std::unique_lock<std::mutex> lock(_lock);
if (single_value()) {
// If there is only a single value possible for the random variable
Expand All @@ -161,10 +192,36 @@ struct CustomRand : public Parameter<T> {
_updated_val = _values[idx];
}
}

void renew_array() {
for (uint i = 0; i < _size; i++) {
renew_value();
_param_values[i] = _updated_val;
}
}

void renew() override {
if (_param_values.size()) {
renew_array();
} else {
renew_value();
}
}
T get() override {
return _updated_val;
};

std::vector<T> get_array() override {
return _param_values;
}

void create_array(unsigned vector_size) override {
if (_param_values.size() == 0) {
_param_values.resize(vector_size);
_size = vector_size;
}
}

bool single_value() const override {
return (_values.size() == 1);
}
Expand All @@ -175,6 +232,8 @@ struct CustomRand : public Parameter<T> {
std::vector<double> _comltv_dist; //!< commulative probabilities
double _mean;
T _updated_val;
std::vector<T> _param_values; //!< The values will be used in parameter_vx.h file after renewing
std::mt19937 _generator;
std::mutex _lock;
unsigned _size;
};
23 changes: 22 additions & 1 deletion rocAL/include/parameters/parameter_simple.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,31 @@ class SimpleParameter : public Parameter<T> {
T get() override {
return _val;
}
int update(T new_val) {

std::vector<T> get_array() override {
return param_values;
}

void update_single_value(T new_val) {
_val = new_val;
}

void update_array(T new_val) {
std::fill(param_values.begin(), param_values.end(), _val);
}

int update(T new_val) {
if (param_values.size())
update_array(new_val);
else
update_single_value(new_val);
return 0;
}

void create_array(unsigned array_size) override {
if (param_values.size() == 0) param_values.resize(array_size);
}

~SimpleParameter() = default;

bool single_value() const override {
Expand All @@ -48,6 +68,7 @@ class SimpleParameter : public Parameter<T> {

private:
T _val;
std::vector<T> param_values; //!< The updated values will be used in parameter_vx.h file
};
using pIntParam = std::shared_ptr<SimpleParameter<int>>;
using pFloatParam = std::shared_ptr<SimpleParameter<float>>;
Expand Down
20 changes: 10 additions & 10 deletions rocAL/include/parameters/parameter_vx.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ class ParameterVX {
THROW("Reading vx scalar failed" + TOSTR(status));
}
void create_array(std::shared_ptr<Graph> graph, vx_enum data_type, unsigned batch_size) {
// _arrVal = (T*)malloc(sizeof(T) * _batch_size);
_batch_size = batch_size;
_arrVal.resize(_batch_size);
_param->create_array(_batch_size);
_array = vxCreateArray(vxGetContext((vx_reference)graph->get()), data_type, _batch_size);
vxAddArrayItems(_array, _batch_size, _arrVal.data(), sizeof(T));
auto status = vxAddArrayItems(_array, _batch_size, get_array().data(), sizeof(T));
if (status != 0)
THROW(" vxAddArrayItems failed in create_array (ParameterVX): " + TOSTR(status))
update_array();
}
void set_param(Parameter<T>* param) {
Expand Down Expand Up @@ -96,11 +97,7 @@ class ParameterVX {
}
void update_array() {
vx_status status;
for (uint i = 0; i < _batch_size; i++) {
_arrVal[i] = renew();
// INFO("update_array: " + TOSTR(i) + "," + TOSTR(_arrVal[i]));
}
status = vxCopyArrayRange((vx_array)_array, 0, _batch_size, sizeof(T), _arrVal.data(), VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
status = vxCopyArrayRange((vx_array)_array, 0, _batch_size, sizeof(T), get_array().data(), VX_WRITE_ONLY, VX_MEMORY_TYPE_HOST);
if (status != 0)
THROW(" vxCopyArrayRange failed in update_array (ParameterVX): " + TOSTR(status))
}
Expand All @@ -109,12 +106,15 @@ class ParameterVX {
return _param->get();
}

std::vector<T> get_array() {
return _param->get_array();
}

private:
vx_scalar _scalar;
vx_array _array;
vx_array _array = nullptr;
Parameter<T>* _param;
T _val;
std::vector<T> _arrVal;
unsigned _batch_size;
unsigned OVX_PARAM_IDX;
const T _DEFAULT_RANGE_START;
Expand Down
18 changes: 14 additions & 4 deletions rocAL/source/parameters/parameter_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,33 +104,43 @@ void ParameterFactory::generate_seed() {
_seed = rd();
}

int64_t
ParameterFactory::get_seed_from_seedsequence() {
auto seed = _seed_vector[_seed_sequence_idx];
_seed_sequence_idx = (_seed_sequence_idx + 1) % MAX_SEEDS;
return seed;
}

void ParameterFactory::set_seed(unsigned seed) {
_seed = seed;
_seed_vector.resize(MAX_SEEDS);
std::seed_seq ss{seed};
ss.generate(_seed_vector.begin(), _seed_vector.end());
}

IntParam* ParameterFactory::create_uniform_int_rand_param(int start, int end) {
auto gen = new UniformRand<int>(start, end, _seed);
auto gen = new UniformRand<int>(start, end, get_seed_from_seedsequence());
auto ret = new IntParam(gen, RocalParameterType::RANDOM_UNIFORM);
_parameters.insert(gen);
return ret;
}

FloatParam* ParameterFactory::create_uniform_float_rand_param(float start, float end) {
auto gen = new UniformRand<float>(start, end, _seed);
auto gen = new UniformRand<float>(start, end, get_seed_from_seedsequence());
auto ret = new FloatParam(gen, RocalParameterType::RANDOM_UNIFORM);
_parameters.insert(gen);
return ret;
}

IntParam* ParameterFactory::create_custom_int_rand_param(const int* value, const double* frequencies, size_t size) {
auto gen = new CustomRand<int>(value, frequencies, size, _seed);
auto gen = new CustomRand<int>(value, frequencies, size, get_seed_from_seedsequence());
auto ret = new IntParam(gen, RocalParameterType::RANDOM_CUSTOM);
_parameters.insert(gen);
return ret;
}

FloatParam* ParameterFactory::create_custom_float_rand_param(const float* value, const double* frequencies, size_t size) {
auto gen = new CustomRand<float>(value, frequencies, size, _seed);
auto gen = new CustomRand<float>(value, frequencies, size, get_seed_from_seedsequence());
auto ret = new FloatParam(gen, RocalParameterType::RANDOM_CUSTOM);
_parameters.insert(gen);
return ret;
Expand Down

0 comments on commit 92fc82c

Please sign in to comment.