MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ convert_tensor()

MAYAFLUX_API std::expected< TensorData, std::string > MayaFlux::Kakshya::convert_tensor ( const TensorAccess & source,
TensorDType target,
const std::optional< TensorQuantization > & quantization = std::nullopt )
nodiscard

Convert the elements of a tensor to another element type.

The result is a dense C-order tensor with the source's name and axes. A strided source is read through its strides.

Integer to integer conversion is exact and saturates. A float becomes an integer by rounding to nearest, ties to even, with NaN as zero and saturation at the range. A float narrows by rounding to nearest, ties to even, and overflows to infinity. Anything non-zero becomes true in a bool.

Values are real numbers when a quantization is involved: a source with quantization is read as scale * (stored - zero_point), and a requested quantization stores clamp(round(value / scale) + zero_point), with NaN stored as the zero point. A source quantization is not carried to the result unless it is requested again.

Usage:

auto weights = convert_tensor(*bf16_weights.access(), TensorDType::of<float>());
auto packed = convert_tensor(*activations.access(), TensorDType::of<int8_t>(),
TensorQuantization { .scale = { 0.05 }, .zero_point = { 3 }, .axis = std::nullopt });
std::expected< TensorData, std::string > convert_tensor(const TensorAccess &source, TensorDType target, const std::optional< TensorQuantization > &quantization)
Convert the elements of a tensor to another element type.
static constexpr TensorDType of() noexcept
Type for a C++ scalar or complex element.
std::vector< int64_t > zero_point
Affine quantization: real = scale * (stored - zero_point).
Parameters
sourceTensor to read.
targetElement type of the result.
quantizationQuantization the result should carry, if any.
Returns
The converted tensor, or the reason it could not be converted.

Definition at line 443 of file TensorConvert.cpp.

445{
446 const TensorSpec& from_spec = source.spec();
447 if (auto problem = diagnose_conversion(from_spec, target, quantization); !problem.empty())
448 return std::unexpected(std::move(problem));
449
450 if (from_spec.dtype == target && same_quantization(from_spec.quantization, quantization)) {
451 auto copy = TensorData::copy_of(source);
452 if (!copy)
453 return std::unexpected("tensor '" + from_spec.name + "': the copy could not be made");
454 return std::move(*copy);
455 }
456
457 TensorSpec spec = from_spec;
458 spec.dtype = target;
459 spec.strides.clear();
460 spec.byte_offset = 0;
461 spec.device = {};
462 spec.quantization = quantization;
463
464 auto result = TensorData::allocate(std::move(spec));
465 if (!result)
466 return std::unexpected("tensor '" + from_spec.name + "': the result could not be allocated");
467 if (source.element_count() == 0)
468 return std::move(*result);
469
470 std::string unused;
471 const Layout from = *layout_of(from_spec.dtype, unused);
472 const Layout to = *layout_of(target, unused);
473 std::byte* out = result->storage().data();
474
475 if (from.family == Family::COMPLEX) {
476 convert_complex(source, from, to, out);
477 } else {
478 convert_real(source, from, to, quantization, out);
479 }
480 return std::move(*result);
481}
static std::optional< TensorData > copy_of(const TensorAccess &view)
Dense C-ordered copy of any tensor view.
Definition Tensor.cpp:126
static std::optional< TensorData > allocate(TensorSpec spec)
Zero-filled tensor for a spec.
Definition Tensor.cpp:72
std::string diagnose_conversion(const TensorSpec &source, TensorDType target, const std::optional< TensorQuantization > &quantization)
Empty when a tensor can be converted to a type, otherwise the reason it cannot.
void convert_complex(std::span< From > source, std::span< To > destination, ComplexConversionStrategy strategy)
Legacy interface - redirects to convert_data.
std::optional< TensorQuantization > quantization
Complete description of a tensor: type, shape, layout, meaning.

References MayaFlux::Kakshya::TensorData::allocate(), MayaFlux::Kakshya::TensorSpec::byte_offset, convert_complex(), MayaFlux::Kakshya::TensorData::copy_of(), MayaFlux::Kakshya::TensorSpec::device, diagnose_conversion(), MayaFlux::Kakshya::TensorSpec::dtype, MayaFlux::Kakshya::TensorSpec::name, MayaFlux::Kakshya::TensorSpec::quantization, and MayaFlux::Kakshya::TensorSpec::strides.

Referenced by MayaFlux::Yantra::TensorConverter::operation_function(), and MayaFlux::Yantra::TensorKernels::pack_weights().

Here is the call graph for this function:
Here is the caller graph for this function: