|
| 1 | +/************************************************************************* |
| 2 | + * Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * |
| 4 | + * See LICENSE for license information. |
| 5 | + ************************************************************************/ |
| 6 | +#include <cuda_runtime.h> |
| 7 | + |
| 8 | +#include <fstream> |
| 9 | +#include <iostream> |
| 10 | + |
| 11 | +#include "../extensions.h" |
| 12 | +#include "xla/ffi/api/c_api.h" |
| 13 | + |
| 14 | +namespace transformer_engine { |
| 15 | +namespace jax { |
| 16 | + |
| 17 | +Error_Type InspectFFI(cudaStream_t stream, Buffer_Type input_buf, Buffer_Type min_buf, |
| 18 | + Buffer_Type max_buf, Buffer_Type mean_buf, Buffer_Type std_buf, |
| 19 | + Result_Type output_buf) { |
| 20 | + NVTE_CHECK(input_buf.untyped_data() != nullptr, "Input must be provided for inspect operation"); |
| 21 | + NVTE_CHECK(output_buf->untyped_data() != nullptr, |
| 22 | + "Output must be provided for inspect operation"); |
| 23 | + NVTE_CHECK(input_buf.untyped_data() == output_buf->untyped_data(), |
| 24 | + "Input and output must point to the same buffer for inspect operation"); |
| 25 | + |
| 26 | + std::vector<uint8_t> input_data(input_buf.size_bytes()); |
| 27 | + NVTE_CHECK_CUDA(cudaMemcpyAsync(input_data.data(), input_buf.untyped_data(), |
| 28 | + input_buf.size_bytes(), cudaMemcpyDeviceToHost, stream)); |
| 29 | + |
| 30 | + float min_val{}, max_val{}, mean_val{}, std_val{}; |
| 31 | + NVTE_CHECK_CUDA(cudaMemcpyAsync(&min_val, min_buf.untyped_data(), sizeof(float), |
| 32 | + cudaMemcpyDeviceToHost, stream)); |
| 33 | + NVTE_CHECK_CUDA(cudaMemcpyAsync(&max_val, max_buf.untyped_data(), sizeof(float), |
| 34 | + cudaMemcpyDeviceToHost, stream)); |
| 35 | + NVTE_CHECK_CUDA(cudaMemcpyAsync(&mean_val, mean_buf.untyped_data(), sizeof(float), |
| 36 | + cudaMemcpyDeviceToHost, stream)); |
| 37 | + NVTE_CHECK_CUDA(cudaMemcpyAsync(&std_val, std_buf.untyped_data(), sizeof(float), |
| 38 | + cudaMemcpyDeviceToHost, stream)); |
| 39 | + |
| 40 | + NVTE_CHECK_CUDA(cudaStreamSynchronize(stream)); |
| 41 | + |
| 42 | + int device; |
| 43 | + NVTE_CHECK_CUDA(cudaGetDevice(&device)); |
| 44 | + |
| 45 | + // Write the tensor data to a file as a binary blob |
| 46 | + std::string filename = "my_tensor_gpu" + std::to_string(device) + ".bin"; |
| 47 | + std::ofstream file(filename, std::ios::binary); |
| 48 | + NVTE_CHECK(file.is_open(), "Failed to create file: ", filename); |
| 49 | + file.write(reinterpret_cast<const char *>(input_data.data()), input_data.size()); |
| 50 | + file.close(); |
| 51 | + |
| 52 | + // Write out a metadata file |
| 53 | + std::string meta_filename = "my_tensor_gpu" + std::to_string(device) + "_meta.json"; |
| 54 | + std::ofstream meta_file(meta_filename); |
| 55 | + NVTE_CHECK(meta_file.is_open(), "Failed to create file: ", meta_filename); |
| 56 | + meta_file << "{"; |
| 57 | + meta_file << "\"shape\": ["; |
| 58 | + for (size_t i = 0; i < input_buf.dimensions().size(); ++i) { |
| 59 | + meta_file << input_buf.dimensions()[i]; |
| 60 | + if (i < input_buf.dimensions().size() - 1) { |
| 61 | + meta_file << ", "; |
| 62 | + } |
| 63 | + } |
| 64 | + meta_file << "], "; |
| 65 | + meta_file << "\"dtype\": " << static_cast<int>(input_buf.element_type()); |
| 66 | + meta_file << ", \"min\": " << min_val; |
| 67 | + meta_file << ", \"max\": " << max_val; |
| 68 | + meta_file << ", \"mean\": " << mean_val; |
| 69 | + meta_file << ", \"std\": " << std_val; |
| 70 | + meta_file << "}"; |
| 71 | + meta_file.close(); |
| 72 | + |
| 73 | + // Log the tensor metadata to the console |
| 74 | + printf("[gpu%d]: Tensor data written to %s (shape: [", device, filename.c_str()); |
| 75 | + for (size_t i = 0; i < input_buf.dimensions().size(); ++i) { |
| 76 | + printf("%zu", static_cast<size_t>(input_buf.dimensions()[i])); |
| 77 | + if (i < input_buf.dimensions().size() - 1) { |
| 78 | + printf(", "); |
| 79 | + } |
| 80 | + } |
| 81 | + printf("], dtype: %d", static_cast<int>(input_buf.element_type())); |
| 82 | + printf(", min: %f, max: %f, mean: %f, std: %f)\n", min_val, max_val, mean_val, std_val); |
| 83 | + |
| 84 | + return ffi_with_cuda_error_check(); |
| 85 | +} |
| 86 | + |
| 87 | +XLA_FFI_DEFINE_HANDLER_SYMBOL(InspectHandler, InspectFFI, |
| 88 | + FFI::Bind() |
| 89 | + .Ctx<FFI_Stream_Type>() // stream |
| 90 | + .Arg<Buffer_Type>() // input |
| 91 | + .Arg<Buffer_Type>() // min |
| 92 | + .Arg<Buffer_Type>() // max |
| 93 | + .Arg<Buffer_Type>() // mean |
| 94 | + .Arg<Buffer_Type>() // std |
| 95 | + .Ret<Buffer_Type>() // output |
| 96 | +); |
| 97 | + |
| 98 | +} // namespace jax |
| 99 | +} // namespace transformer_engine |
0 commit comments