Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions extension/tensor/tensor_ptr_maker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

#include <executorch/extension/tensor/tensor_ptr_maker.h>

#include <random>

namespace executorch {
namespace extension {
namespace {

template <
typename INT_T,
typename std::enable_if<
Expand Down Expand Up @@ -72,6 +75,25 @@ bool extract_scalar(exec_aten::Scalar scalar, BOOL_T* out_val) {
extract_scalar(scalar, &out_val), \
#scalar " could not be extracted: wrong type or out of range");

template <typename Distribution>
TensorPtr random_strided(
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type,
exec_aten::TensorShapeDynamism dynamism,
Distribution&& distribution) {
auto tensor =
empty_strided(std::move(sizes), std::move(strides), type, dynamism);
std::default_random_engine gen{std::random_device{}()};

ET_SWITCH_REALB_TYPES(type, nullptr, "random_strided", CTYPE, [&] {
std::generate_n(tensor->mutable_data_ptr<CTYPE>(), tensor->numel(), [&]() {
return static_cast<CTYPE>(distribution(gen));
});
});
return tensor;
}

} // namespace

TensorPtr empty_strided(
Expand Down Expand Up @@ -110,5 +132,46 @@ TensorPtr full_strided(
return tensor;
}

TensorPtr rand_strided(
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type,
exec_aten::TensorShapeDynamism dynamism) {
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::uniform_real_distribution<float>(0.0f, 1.0f));
}

TensorPtr randn_strided(
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type,
exec_aten::TensorShapeDynamism dynamism) {
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::normal_distribution<float>(0.0f, 1.0f));
}

TensorPtr randint_strided(
int64_t low,
int64_t high,
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type,
exec_aten::TensorShapeDynamism dynamism) {
return random_strided(
std::move(sizes),
std::move(strides),
type,
dynamism,
std::uniform_int_distribution<int64_t>(low, high - 1));
}

} // namespace extension
} // namespace executorch
185 changes: 185 additions & 0 deletions extension/tensor/tensor_ptr_maker.h
Original file line number Diff line number Diff line change
Expand Up @@ -497,5 +497,190 @@ inline TensorPtr zeros(
return full(std::move(sizes), 0, type, dynamism);
}

/**
* Creates a TensorPtr filled with random values between 0 and 1.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
**/
TensorPtr rand_strided(
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND);

/**
* Creates a TensorPtr filled with random values between 0 and 1.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr rand_like(
const TensorPtr& other,
exec_aten::ScalarType type = exec_aten::ScalarType::Undefined,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == exec_aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return rand_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}

/**
* Creates a TensorPtr filled with random values between 0 and 1.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr rand(
std::vector<exec_aten::SizesType> sizes,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return rand_strided(std::move(sizes), {}, type, dynamism);
}

/**
* Creates a TensorPtr filled with random values from a normal distribution.
*
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
TensorPtr randn_strided(
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND);

/**
* Creates a TensorPtr filled with random values from a normal distribution.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randn_like(
const TensorPtr& other,
exec_aten::ScalarType type = exec_aten::ScalarType::Undefined,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == exec_aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return randn_strided(
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}

/**
* Creates a TensorPtr filled with random values from a normal distribution.
*
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randn(
std::vector<exec_aten::SizesType> sizes,
exec_aten::ScalarType type = exec_aten::ScalarType::Float,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return randn_strided(std::move(sizes), {}, type, dynamism);
}

/**
* Creates a TensorPtr filled with random integer values in the given range.
*
* @param low The lower bound (inclusive) of the random values.
* @param high The upper bound (exclusive) of the random values.
* @param sizes A vector specifying the size of each dimension.
* @param strides A vector specifying the stride for each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
TensorPtr randint_strided(
int64_t low,
int64_t high,
std::vector<exec_aten::SizesType> sizes,
std::vector<exec_aten::StridesType> strides,
exec_aten::ScalarType type = exec_aten::ScalarType::Int,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND);

/**
* Creates a TensorPtr filled with random integer values in the given range.
*
* @param other A reference to another tensor, whose size and properties will be
* used.
* @param low The lower bound (inclusive) of the random values.
* @param high The upper bound (exclusive) of the random values.
* @param type The scalar type of the tensor elements. If not specified, the
* scalar type of the other tensor is used.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randint_like(
const TensorPtr& other,
int64_t low,
int64_t high,
exec_aten::ScalarType type = exec_aten::ScalarType::Undefined,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
if (type == exec_aten::ScalarType::Undefined) {
type = other->scalar_type();
}
return randint_strided(
low,
high,
{other->sizes().begin(), other->sizes().end()},
{other->strides().begin(), other->strides().end()},
type,
dynamism);
}

/**
* Creates a TensorPtr filled with random integer values in the given range.
*
* @param low The lower bound (inclusive) of the random values.
* @param high The upper bound (exclusive) of the random values.
* @param sizes A vector specifying the size of each dimension.
* @param type The scalar type of the tensor elements.
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
* @return A TensorPtr instance managing the newly created Tensor.
*/
inline TensorPtr randint(
int64_t low,
int64_t high,
std::vector<exec_aten::SizesType> sizes,
exec_aten::ScalarType type = exec_aten::ScalarType::Int,
exec_aten::TensorShapeDynamism dynamism =
exec_aten::TensorShapeDynamism::DYNAMIC_BOUND) {
return randint_strided(low, high, std::move(sizes), {}, type, dynamism);
}

} // namespace extension
} // namespace executorch
Loading
Loading