Empty when a tensor can be converted to a type, otherwise the reason it cannot.
Whole-byte integers (8 to 64 bits, signed and unsigned), bool, IEEE half, bfloat16, float and double, and complex of float and double are supported. Complex and real types do not convert to each other. Other types (float8, float6, float4, sub-byte integers and multi-lane elements) are refused.
Quantization is only held by integer storage of at most 32 bits. A source with quantization is dequantized; quantization asks for the result to be quantized and needs an integer target.
- Parameters
-
| source | Layout of the tensor to convert. |
| target | Element type of the result. |
| quantization | Quantization the result should carry, if any. |
Definition at line 398 of file TensorConvert.cpp.
400{
401 if (auto problem = source.diagnose(); !problem.empty())
402 return problem;
403
404 const std::string label = source.name.empty() ? std::string("tensor") : "tensor '" + source.name + "'";
405 if (!source.device.is_host_accessible())
406 return label + ": memory is not readable by the host";
407
408 std::string why;
409 const auto from = layout_of(source.dtype, why);
410 if (!from)
411 return label + ": source " + why;
412 const auto to = layout_of(target, why);
413 if (!to)
414 return label + ": target " + why;
415
416 if ((from->family == Family::COMPLEX) != (to->family == Family::COMPLEX))
417 return label + ": complex and real element types do not convert to each other";
418
419 if (source.quantization && !holds_quantization(*from))
420 return label + ": quantization on " + source.dtype.name() + " storage is not supported";
421
422 if (quantization) {
423 if (!holds_quantization(*to))
424 return label +
": " + target.
name() +
" cannot hold quantization, use an integer type of at most 32 bits";
425
427 probe.
dtype = target;
431 if (
auto problem = probe.
diagnose(); !problem.empty())
432 return problem;
433
434 const bool usable = std::ranges::all_of(quantization->scale,
435 [](double scale) { return std::isfinite(scale) && scale != 0.0; });
436 if (!usable)
437 return label + ": quantization scales must be finite and non-zero";
438 }
439
440 return {};
441}
std::string name() const
Short readable name such as f32, bf16, i64, u8.
std::string diagnose() const
Empty when the spec is self-consistent, otherwise the reason it is not.
std::optional< TensorQuantization > quantization
std::vector< int64_t > strides
Complete description of a tensor: type, shape, layout, meaning.
References MayaFlux::Kakshya::TensorSpec::byte_offset, MayaFlux::Kakshya::TensorSpec::diagnose(), MayaFlux::Kakshya::TensorSpec::dtype, MayaFlux::Kakshya::TensorDType::name(), MayaFlux::Kakshya::TensorSpec::quantization, and MayaFlux::Kakshya::TensorSpec::strides.
Referenced by convert_tensor(), and MayaFlux::Yantra::TensorConverter::validate_input().