|
| 1 | +/************************************************************************* |
| 2 | + * Copyright (c) 2022-2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | + * |
| 4 | + * See LICENSE for license information. |
| 5 | + ************************************************************************/ |
| 6 | + |
| 7 | +#include <cuda.h> |
| 8 | +#include <cudaTypedefs.h> |
| 9 | +#include <cuda_runtime.h> |
| 10 | +#include <transformer_engine/cast.h> |
| 11 | +#include <transformer_engine/multi_stream.h> |
| 12 | + |
| 13 | +#include "../common.h" |
| 14 | +#include "../transpose/cast_transpose.h" |
| 15 | +#include "../util/multi_stream.h" |
| 16 | +#include "../utils.cuh" |
| 17 | +#include "dispatch/dequantize.cuh" |
| 18 | +#include "dispatch/quantize.cuh" |
| 19 | +#include "transformer_engine/transpose.h" |
| 20 | + |
| 21 | +void nvte_quantize(const NVTETensor input, NVTETensor output, cudaStream_t stream) { |
| 22 | + NVTE_API_CALL(nvte_quantize); |
| 23 | + using namespace transformer_engine; |
| 24 | + |
| 25 | + constexpr bool IS_ACT = false; |
| 26 | + dispatch::quantize_fwd_helper<IS_ACT, Empty, nullptr>(input, output, nullptr, stream); |
| 27 | +} |
| 28 | + |
| 29 | +void nvte_quantize_noop(const NVTETensor input, NVTETensor output, NVTETensor noop, |
| 30 | + cudaStream_t stream) { |
| 31 | + NVTE_API_CALL(nvte_quantize_noop); |
| 32 | + using namespace transformer_engine; |
| 33 | + |
| 34 | + // Create config with noop tensor |
| 35 | + QuantizationConfig quant_config; |
| 36 | + quant_config.noop_tensor = noop; |
| 37 | + |
| 38 | + nvte_quantize_v2(input, output, reinterpret_cast<NVTEQuantizationConfig>(&quant_config), stream); |
| 39 | +} |
| 40 | + |
| 41 | +void nvte_quantize_v2(const NVTETensor input, NVTETensor output, |
| 42 | + const NVTEQuantizationConfig quant_config, cudaStream_t stream) { |
| 43 | + NVTE_API_CALL(nvte_quantize_v2); |
| 44 | + using namespace transformer_engine; |
| 45 | + |
| 46 | + constexpr bool IS_ACT = false; |
| 47 | + dispatch::quantize_fwd_helper<IS_ACT, Empty, nullptr>(input, output, quant_config, stream); |
| 48 | +} |
| 49 | + |
| 50 | +void nvte_quantize_dbias(const NVTETensor input, NVTETensor output, NVTETensor dbias, |
| 51 | + NVTETensor workspace, cudaStream_t stream) { |
| 52 | + NVTE_API_CALL(nvte_quantize_dbias); |
| 53 | + using namespace transformer_engine; |
| 54 | + |
| 55 | + constexpr bool IS_DBIAS = true; |
| 56 | + constexpr bool IS_DACT = false; |
| 57 | + constexpr const NVTETensor activation_input = nullptr; |
| 58 | + |
| 59 | + dispatch::quantize_bwd_helper<IS_DBIAS, IS_DACT, Empty, nullptr>( |
| 60 | + input, activation_input, output, dbias, workspace, nullptr, stream); |
| 61 | +} |
| 62 | + |
| 63 | +void nvte_dequantize(const NVTETensor input, NVTETensor output, cudaStream_t stream) { |
| 64 | + NVTE_API_CALL(nvte_dequantize); |
| 65 | + using namespace transformer_engine; |
| 66 | + dispatch::dequantize_helper(*convertNVTETensorCheck(input), convertNVTETensorCheck(output), |
| 67 | + stream); |
| 68 | +} |
| 69 | + |
| 70 | +void nvte_multi_tensor_quantize(const NVTETensor *inputs, NVTETensor *outputs, |
| 71 | + const NVTEQuantizationConfig quant_configs, |
| 72 | + const size_t num_tensors, cudaStream_t stream) { |
| 73 | + NVTE_API_CALL(nvte_multi_tensor_quantize); |
| 74 | + using namespace transformer_engine; |
| 75 | + |
| 76 | + constexpr bool IS_ACT = false; |
| 77 | + |
| 78 | + const size_t num_streams = nvte_get_num_compute_streams(); |
| 79 | + |
| 80 | + int num_stream_used = std::min(num_streams, num_tensors); |
| 81 | + // wait for current stream to finish |
| 82 | + NVTE_CHECK_CUDA(cudaEventRecord(detail::get_compute_stream_event(0), stream)); |
| 83 | + for (int s = 0; s < num_stream_used; s++) { |
| 84 | + NVTE_CHECK_CUDA( |
| 85 | + cudaStreamWaitEvent(detail::get_compute_stream(s), detail::get_compute_stream_event(0))); |
| 86 | + } |
| 87 | + |
| 88 | + for (int i = 0; i < num_tensors; i++) { |
| 89 | + dispatch::quantize_fwd_helper<IS_ACT, Empty, nullptr>( |
| 90 | + inputs[i], outputs[i], quant_configs, detail::get_compute_stream(i % num_streams)); |
| 91 | + } |
| 92 | + |
| 93 | + // record events on compute streams |
| 94 | + for (int s = 0; s < num_stream_used; s++) { |
| 95 | + NVTE_CHECK_CUDA( |
| 96 | + cudaEventRecord(detail::get_compute_stream_event(s), detail::get_compute_stream(s))); |
| 97 | + } |
| 98 | + // wait for all compute streams to finish |
| 99 | + for (int s = 0; s < num_stream_used; s++) { |
| 100 | + NVTE_CHECK_CUDA(cudaStreamWaitEvent(stream, detail::get_compute_stream_event(s))); |
| 101 | + } |
| 102 | +} |
0 commit comments