Commit 10432892 authored by Brian Pickrell's avatar Brian Pickrell
Browse files

changed rand_uniform op. to random_uniform, and other requested changes

parent d88ca899
......@@ -183,7 +183,7 @@ register_migraphx_ops(
quant_convolution
quant_dot
quantizelinear
rand_uniform
random_uniform
random_seed
recip
reduce_max
......
......@@ -43,7 +43,7 @@ namespace op {
*/
struct random_seed
{
shape::type_t dtype = shape::type_t::uint32_type;
shape::type_t dtype = shape::type_t::uint64_type;
template <class Self, class F>
static auto reflect(Self& self, F f)
......@@ -54,19 +54,16 @@ struct random_seed
std::string name() const { return "random_seed"; }
shape compute_shape(const std::vector<shape>& inputs) const
{
(void)inputs;
return migraphx::shape(dtype, {1});
check_shapes{inputs, *this}.has(0);
return shape{dtype};
}
argument compute(const shape& output_shape, const std::vector<argument>& args) const
argument compute(const shape& output_shape, const std::vector<argument>&) const
{
(void)args;
argument result(output_shape);
result.visit([&](auto output) {
std::generate(output.begin(), output.end(), [&]() {
return uint32_t(std::chrono::system_clock::now().time_since_epoch().count());
});
output.front() = std::random_device{}();
});
return result;
}
......
......@@ -24,12 +24,12 @@
/**
* Random Uniform distribution operator. Given a shape, populate it with random
* values. Calls to rand_uniform using the same randomization seed will
* values. Calls to random_uniform using the same randomization seed will
* always generate the same pseudo-random sequence. Seed can
* be given as a runtime argument containing a single value, or a compile-time
* attribute.
*
* Inputs: (1) randomization seed (uint32)
* Inputs: (1) randomization seed (uint64)
* (2) the shape of the set to be populated.
*
*
......@@ -38,8 +38,8 @@
* Output: Same shape.
*
*/
#ifndef MIGRAPHX_GUARD_OPERATORS_RAND_UNIFORM_HPP
#define MIGRAPHX_GUARD_OPERATORS_RAND_UNIFORM_HPP
#ifndef MIGRAPHX_GUARD_OPERATORS_RANDOM_UNIFORM_HPP
#define MIGRAPHX_GUARD_OPERATORS_RANDOM_UNIFORM_HPP
#include <migraphx/check_shapes.hpp>
#include <migraphx/argument.hpp>
......@@ -51,53 +51,48 @@ namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace op {
struct rand_uniform
struct random_uniform
{
// The rand_uniform operation does not contain a random number generator seed
// The random_uniform operation does not contain a random number generator seed
// as a member, and expects it to be passed as a runtime input.
// todo: not currently settable
float range_min = 0.0f;
float range_max = 1.0f;
// todo: integer data type(s) not yet supported
shape::type_t dtype = shape::type_t::float_type;
template <class Self, class F>
static auto reflect(Self& self, F f)
{
return pack(f(self.dtype, "dtype"));
return pack(f(self.range_min, "range_min"), f(self.range_max, "range_max"));
}
/**
* Input 1: seed
* Input 2: output shape
*/
std::string name() const { return "rand_uniform"; }
std::string name() const { return "random_uniform"; }
shape compute_shape(std::vector<shape> inputs) const
{
check_shapes{inputs, *this, true}.has(2);
if(inputs.front().type() != shape::type_t::uint32_type)
MIGRAPHX_THROW("RAND_UNIFORM: Input 2 (seed) must have type unsigned int");
if(inputs.front().type() != shape::type_t::uint64_type)
MIGRAPHX_THROW("RANDOM_UNIFORM: Input 1 (seed) must have type long unsigned int");
auto s = inputs.at(1);
if(s.dynamic())
{
return s.with_type(dtype);
return s;
}
else
{
return s.with_lens(s.lens()).with_type(dtype);
return s.with_lens(s.lens());
}
}
argument compute(const shape& output, std::vector<argument> args) const
argument compute(const shape&, std::vector<argument> args) const
{
// Output goes into the passed buffer, not the shape output
(void)output;
argument result{args[1].get_shape()};
// Output goes into the passed buffer, not the shape output.
argument result = args[1];
uint32_t local_seed = args[0].at<uint32_t>(0);
uint64_t local_seed = args[0].at<uint64_t>(0);
std::mt19937 gen(local_seed);
std::uniform_real_distribution<> dis(range_min, range_max);
......
......@@ -2216,17 +2216,17 @@ TEST_CASE(prefix_scan_sum_dyn_2d)
}
}
TEST_CASE(rand_uniform)
TEST_CASE(random_uniform)
{
std::vector<migraphx::shape::dynamic_dimension> dd{{5, 8}, {3, 7}};
migraphx::shape s0{migraphx::shape::uint32_type, {1}};
migraphx::shape s0{migraphx::shape::uint64_type, {1}};
migraphx::shape s1{migraphx::shape::float_type, dd};
expect_shape(s1, migraphx::make_op("rand_uniform"), s0, s1);
expect_shape(s1, migraphx::make_op("random_uniform"), s0, s1);
}
TEST_CASE(random_seed)
{
migraphx::shape s{migraphx::shape::uint32_type, {1}};
migraphx::shape s{migraphx::shape::uint64_type, {1}};
expect_shape(s, migraphx::make_op("random_seed"));
}
......
......@@ -6458,11 +6458,11 @@ TEST_CASE(quantizelinear)
}
}
TEST_CASE(rand_uniform_test)
TEST_CASE(random_uniform_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
uint32_t seed(0);
uint64_t seed(0);
size_t sample_size(200);
// Shape of the random data
......@@ -6473,11 +6473,11 @@ TEST_CASE(rand_uniform_test)
auto input = mm->add_literal(migraphx::literal(rs, data));
// Runtime randomization seed
migraphx::shape seed_shape{migraphx::shape::uint32_type, {1}};
std::vector<uint32_t> seed_data{seed};
migraphx::shape seed_shape{migraphx::shape::uint64_type, {1}};
std::vector<uint64_t> seed_data{seed};
auto seed_input = mm->add_literal(migraphx::literal(seed_shape, seed_data));
mm->add_instruction(migraphx::make_op("rand_uniform"), seed_input, input);
mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, input);
p.compile(migraphx::make_target("ref"));
migraphx::parameter_map params0;
......@@ -6493,11 +6493,11 @@ TEST_CASE(rand_uniform_test)
EXPECT(migraphx::verify::verify_range(result_vec, rand_samples, 100000));
}
TEST_CASE(rand_uniform_dyn_test)
TEST_CASE(random_uniform_dyn_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
uint32_t seed(17);
uint64_t seed(17);
size_t sample_size(200);
// Shape of the random data
......@@ -6505,10 +6505,10 @@ TEST_CASE(rand_uniform_dyn_test)
auto input = mm->add_parameter("Input_1", rs);
// Runtime randomization seed
migraphx::shape seed_shape{migraphx::shape::uint32_type, {1}};
migraphx::shape seed_shape{migraphx::shape::uint64_type, {1}};
auto seed_input = mm->add_parameter("Seed", seed_shape);
mm->add_instruction(migraphx::make_op("rand_uniform", {}), seed_input, input);
mm->add_instruction(migraphx::make_op("random_uniform", {}), seed_input, input);
p.compile(migraphx::make_target("ref"));
// Create a dummy input to hold the random data
......@@ -6516,9 +6516,9 @@ TEST_CASE(rand_uniform_dyn_test)
migraphx::parameter_map params0;
params0["Input_1"] = migraphx::argument(input_fixed_shape1);
migraphx::shape seed_fixed_shape{migraphx::shape::uint32_type, {1}};
std::vector<uint32_t> seed_data = {seed};
params0["Seed"] = migraphx::argument(seed_fixed_shape, seed_data.data());
// migraphx::shape seed_fixed_shape{migraphx::shape::uint64_type, {1}};
std::vector<uint64_t> seed_data = {seed};
params0["Seed"] = migraphx::argument(seed_shape, seed_data.data());
auto result = p.eval(params0).back();
std::vector<float> result_vec(sample_size);
......@@ -6532,7 +6532,7 @@ TEST_CASE(rand_uniform_dyn_test)
EXPECT(migraphx::verify::verify_range(result_vec, rand_samples, 100000));
}
TEST_CASE(rand_uniform_and_seed_test)
TEST_CASE(random_uniform_and_seed_test)
{
migraphx::program p;
auto* mm = p.get_main_module();
......@@ -6546,7 +6546,7 @@ TEST_CASE(rand_uniform_and_seed_test)
// Runtime randomization seed
auto seed_input = mm->add_instruction(migraphx::make_op("random_seed"));
mm->add_instruction(migraphx::make_op("rand_uniform"), seed_input, input);
mm->add_instruction(migraphx::make_op("random_uniform"), seed_input, input);
p.compile(migraphx::make_target("ref"));
// Create a dummy input to hold the random data
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment