// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include #include "orttraining/training_ops/cuda/math/batch_scale.h" #include "orttraining/training_ops/cuda/math/batch_scale_impl.h" namespace onnxruntime { namespace cuda { ONNX_OPERATOR_KERNEL_EX( BatchScale, kMSDomain, 1, kCudaExecutionProvider, (*KernelDefBuilder::Create()) .TypeConstraint("T", BuildKernelDefConstraints()), BatchScale); // Put implementation in the anonymous namespace to avoid name collision in the global namespace. namespace { template struct BatchScaleFunctor { void operator()(cudaStream_t stream, int64_t input_element_count, const Tensor* input_tensor, const std::vector& scales, const std::vector& output_tensors) const { typedef typename ToCudaType::MappedType CudaT; std::vector output_data_ptrs; output_data_ptrs.reserve(output_tensors.size()); for (Tensor* output_tensor : output_tensors) { output_data_ptrs.push_back(reinterpret_cast(output_tensor->MutableData())); } BatchScaleImpl(stream, input_element_count, reinterpret_cast(input_tensor->Data()), scales, output_data_ptrs); } }; } // namespace Status BatchScale::ComputeInternal(OpKernelContext* context) const { const Tensor* input_tensor = context->Input(0); size_t output_count = scale2_.has_value() ? 3 : 2; const auto& input_tensor_shape = input_tensor->Shape(); std::vector output_tensors; output_tensors.reserve(output_count); for (size_t i = 0; i < output_count; ++i) { output_tensors.push_back(context->Output(static_cast(i), input_tensor_shape)); } std::vector scales{scale0_, scale1_}; if (output_count == 3) { scales.push_back(scale2_.value()); } utils::MLTypeCallDispatcher t_disp(input_tensor->GetElementType()); t_disp.Invoke(Stream(context), input_tensor_shape.Size(), input_tensor, scales, output_tensors); return Status::OK(); } } // namespace cuda } // namespace onnxruntime