| 405 | } |
| 406 | |
| 407 | bool test_gpu_float8(nvrhi::IDevice* device, bool e5m2) |
| 408 | { |
| 409 | nvrhi::CommandListHandle commandList = device->createCommandList(); |
| 410 | |
| 411 | constexpr size_t count = 65536; |
| 412 | std::vector<float> f32(count); |
| 413 | std::vector<uint8_t> f8(count); |
| 414 | |
| 415 | std::mt19937 rng(1); |
| 416 | // These are base-2 logarithms for the input numbers |
| 417 | double minLog = e5m2 ? -18.0 : -11.0; |
| 418 | double maxLog = e5m2 ? 17.0 : 10.0; |
| 419 | std::uniform_real_distribution dist(minLog, maxLog); |
| 420 | std::uniform_int_distribution signDist(0, 1); |
| 421 | for (size_t i = 0; i < count; ++i) |
| 422 | { |
| 423 | double rnd = dist(rng); |
| 424 | int signRnd = signDist(rng); |
| 425 | |
| 426 | double value = pow(2.0, rnd) * (signRnd ? -1.0 : 1.0); |
| 427 | f32[i] = float(value); |
| 428 | |
| 429 | if (e5m2) |
| 430 | f8[i] = Float32ToFloat8E5M2(f32[i]).bits; |
| 431 | else |
| 432 | f8[i] = Float32ToFloat8E4M3(f32[i]).bits; |
| 433 | } |
| 434 | |
| 435 | auto bufferDesc = nvrhi::BufferDesc() |
| 436 | .setByteSize(count * sizeof(float)) |
| 437 | .setDebugName("Input Buffer") |
| 438 | .setCanHaveRawViews(true) |
| 439 | .enableAutomaticStateTracking(nvrhi::ResourceStates::CopyDest); |
| 440 | nvrhi::BufferHandle f32buf = device->createBuffer(bufferDesc); |
| 441 | |
| 442 | bufferDesc |
| 443 | .setByteSize(count * sizeof(uint8_t)) |
| 444 | .setDebugName("Output Buffer") |
| 445 | .setCanHaveUAVs(true); |
| 446 | nvrhi::BufferHandle f8buf = device->createBuffer(bufferDesc); |
| 447 | |
| 448 | bufferDesc |
| 449 | .setDebugName("Readback Buffer") |
| 450 | .setCanHaveUAVs(false) |
| 451 | .setCpuAccess(nvrhi::CpuAccessMode::Read); |
| 452 | nvrhi::BufferHandle readbackBuf = device->createBuffer(bufferDesc); |
| 453 | |
| 454 | commandList->open(); |
| 455 | commandList->writeBuffer(f32buf, f32.data(), count * sizeof(float)); |
| 456 | |
| 457 | nvrhi::coopvec::ConvertMatrixLayoutDesc convertDesc{}; |
| 458 | convertDesc.numRows = 1; |
| 459 | convertDesc.numColumns = count; |
| 460 | convertDesc.src.buffer = f32buf; |
| 461 | convertDesc.src.layout = nvrhi::coopvec::MatrixLayout::RowMajor; |
| 462 | convertDesc.src.stride = count * sizeof(float); |
| 463 | convertDesc.src.size = convertDesc.src.stride; |
| 464 | convertDesc.src.type = nvrhi::coopvec::DataType::Float32; |
no test coverage detected